Skip to content

Commit

Permalink
fil-library init (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
lordshashank authored Jul 23, 2024
1 parent 367b156 commit f745bd0
Show file tree
Hide file tree
Showing 8 changed files with 1,240 additions and 1,427 deletions.
655 changes: 320 additions & 335 deletions contracts/basic-deal-client/DealClient.sol

Large diffs are not rendered by default.

64 changes: 28 additions & 36 deletions contracts/basic-solidity-examples/SimpleCoin.sol
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
pragma solidity 0.8.23;

import "hardhat/console.sol";

error SimpleCoin__NotEnoughBalance();

contract SimpleCoin {
mapping (address => uint) public balances;
uint256 private i_tokensToBeMinted;

event tokensMinted(
uint256 indexed numberTokensMinted,
address owner
);

constructor(uint256 tokensToBeMinted) {
balances[tx.origin] = tokensToBeMinted;
i_tokensToBeMinted= tokensToBeMinted;
emit tokensMinted(tokensToBeMinted, msg.sender);
}
mapping(address => uint) public balances;
uint256 private i_tokensToBeMinted;

function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] < amount) {
// return false;
revert SimpleCoin__NotEnoughBalance();
}

balances[msg.sender] -= amount;
balances[receiver] += amount;
console.log(
"Transferring from %s to %s %s tokens",
msg.sender,
receiver,
amount
);
return true;
}
event tokensMinted(uint256 indexed numberTokensMinted, address owner);

function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
constructor(uint256 tokensToBeMinted) {
balances[tx.origin] = tokensToBeMinted;
i_tokensToBeMinted = tokensToBeMinted;
emit tokensMinted(tokensToBeMinted, msg.sender);
}

function getMintedTokenBalance() public view returns(uint256){
return i_tokensToBeMinted;
function sendCoin(address receiver, uint amount) public returns (bool sufficient) {
if (balances[msg.sender] < amount) {
// return false;
revert SimpleCoin__NotEnoughBalance();
}
}

balances[msg.sender] -= amount;
balances[receiver] += amount;
console.log("Transferring from %s to %s %s tokens", msg.sender, receiver, amount);
return true;
}

function getBalance(address addr) public view returns (uint) {
return balances[addr];
}

function getMintedTokenBalance() public view returns (uint256) {
return i_tokensToBeMinted;
}
}
67 changes: 45 additions & 22 deletions contracts/filecoin-api-examples/DealRewarder.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
pragma solidity ^0.8.23;

import { MarketAPI } from "@zondax/filecoin-solidity/contracts/v0.8/MarketAPI.sol";
import { CommonTypes } from "@zondax/filecoin-solidity/contracts/v0.8/types/CommonTypes.sol";
import { MarketTypes } from "@zondax/filecoin-solidity/contracts/v0.8/types/MarketTypes.sol";
import { Actor } from "@zondax/filecoin-solidity/contracts/v0.8/utils/Actor.sol";
import { Misc } from "@zondax/filecoin-solidity/contracts/v0.8/utils/Misc.sol";
import {MarketAPI} from "filecoin-solidity-api/contracts/v0.8/MarketAPI.sol";
import {MarketTypes} from "filecoin-solidity-api/contracts/v0.8/types/MarketTypes.sol";
import {CommonTypes} from "filecoin-solidity-api/contracts/v0.8/types/CommonTypes.sol";
import {Actor} from "filecoin-solidity-api/contracts/v0.8/utils/Actor.sol";
import {Misc} from "filecoin-solidity-api/contracts/v0.8/utils/Misc.sol";

/*
Contract Usage
Expand Down Expand Up @@ -34,9 +34,9 @@ contract DealRewarder {
function fund(uint64 unused) public payable {}

function addCID(bytes calldata cidraw, uint size) public {
require(msg.sender == owner);
cidSet[cidraw] = true;
cidSizes[cidraw] = size;
require(msg.sender == owner);
cidSet[cidraw] = true;
cidSizes[cidraw] = size;
}

function policyOK(bytes memory cidraw, uint64 provider) internal view returns (bool) {
Expand All @@ -47,31 +47,48 @@ contract DealRewarder {
function authorizeData(bytes memory cidraw, uint64 provider, uint size) public {
require(cidSet[cidraw], "cid must be added before authorizing");
require(cidSizes[cidraw] == size, "data size must match expected");
require(policyOK(cidraw, provider), "deal failed policy check: has provider already claimed this cid?");
require(
policyOK(cidraw, provider),
"deal failed policy check: has provider already claimed this cid?"
);

cidProviders[cidraw][provider] = true;
}

type FilActorId is uint64;

function claim_bounty(uint64 deal_id) public {
MarketTypes.GetDealDataCommitmentReturn memory commitmentRet = MarketAPI.getDealDataCommitment(deal_id);
uint64 providerRet = MarketAPI.getDealProvider(deal_id);
(, MarketTypes.GetDealDataCommitmentReturn memory commitmentRet) = MarketAPI
.getDealDataCommitment(deal_id);
(, uint64 providerRet) = MarketAPI.getDealProvider(deal_id);

authorizeData(commitmentRet.data, providerRet, commitmentRet.size);

// get dealer (bounty hunter client)
uint64 clientRet = MarketAPI.getDealClient(deal_id);
(, uint64 clientRet) = MarketAPI.getDealClient(deal_id);

// send reward to client
// send reward to client
send(clientRet);

// send reward to client
// send reward to client
send(clientRet);

}

function call_actor_id(uint64 method, uint256 value, uint64 flags, uint64 codec, bytes memory params, uint64 id) public returns (bool, int256, uint64, bytes memory) {
(bool success, bytes memory data) = address(CALL_ACTOR_ID).delegatecall(abi.encode(method, value, flags, codec, params, id));
(int256 exit, uint64 return_codec, bytes memory return_value) = abi.decode(data, (int256, uint64, bytes));
function call_actor_id(
uint64 method,
uint256 value,
uint64 flags,
uint64 codec,
bytes memory params,
uint64 id
) public returns (bool, int256, uint64, bytes memory) {
(bool success, bytes memory data) = address(CALL_ACTOR_ID).delegatecall(
abi.encode(method, value, flags, codec, params, id)
);
(int256 exit, uint64 return_codec, bytes memory return_value) = abi.decode(
data,
(int256, uint64, bytes)
);
return (success, exit, return_codec, return_value);
}

Expand All @@ -81,7 +98,13 @@ contract DealRewarder {
delete emptyParams;

uint oneFIL = 1000000000000000000;
Actor.callByID(CommonTypes.FilActorId.wrap(actorID), METHOD_SEND, Misc.NONE_CODEC, emptyParams, oneFIL, false);
Actor.callByID(
CommonTypes.FilActorId.wrap(actorID),
METHOD_SEND,
Misc.NONE_CODEC,
emptyParams,
oneFIL,
false
);
}
}

36 changes: 18 additions & 18 deletions contracts/filecoin-api-examples/FilecoinMarketConsumer.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma solidity ^0.8.23;

import {MarketAPI} from "@zondax/filecoin-solidity/contracts/v0.8/MarketAPI.sol";
import {MarketTypes} from "@zondax/filecoin-solidity/contracts/v0.8/types/MarketTypes.sol";
import {CommonTypes} from "@zondax/filecoin-solidity/contracts/v0.8/types/CommonTypes.sol";
import {MarketAPI} from "filecoin-solidity-api/contracts/v0.8/MarketAPI.sol";
import {MarketTypes} from "filecoin-solidity-api/contracts/v0.8/types/MarketTypes.sol";
import {CommonTypes} from "filecoin-solidity-api/contracts/v0.8/types/CommonTypes.sol";

contract FilecoinMarketConsumer {
CommonTypes.DealLabel public dealLabel;
Expand All @@ -13,7 +13,7 @@ contract FilecoinMarketConsumer {
MarketTypes.GetDealDataCommitmentReturn public dealCommitment;
MarketTypes.GetDealTermReturn public dealTerm;
CommonTypes.BigInt public dealPricePerEpoch;
CommonTypes.BigInt public clientCollateral;
CommonTypes.BigInt public clientCollateral;
CommonTypes.BigInt public providerCollateral;
MarketTypes.GetDealActivationReturn public activationStatus;

Expand All @@ -28,45 +28,45 @@ contract FilecoinMarketConsumer {
storeProviderCollateral(dealId);
storeDealVerificaton(dealId);
storeDealActivationStatus(dealId);
}
}

function storeDealLabel(uint64 dealId) public {
dealLabel = MarketAPI.getDealLabel(dealId);
function storeDealLabel(uint64 dealId) public {
(, dealLabel) = MarketAPI.getDealLabel(dealId);
}

function storeDealClient(uint64 dealId) public {
dealClientActorId = MarketAPI.getDealClient(dealId);
(, dealClientActorId) = MarketAPI.getDealClient(dealId);
}

function storeDealClientProvider(uint64 dealId) public {
dealProviderActorId = MarketAPI.getDealProvider(dealId);
(, dealProviderActorId) = MarketAPI.getDealProvider(dealId);
}

function storeDealCommitment(uint64 dealId) public {
dealCommitment = MarketAPI.getDealDataCommitment(dealId);
(, dealCommitment) = MarketAPI.getDealDataCommitment(dealId);
}

function storeDealTerm(uint64 dealId) public {
dealTerm = MarketAPI.getDealTerm(dealId);
(, dealTerm) = MarketAPI.getDealTerm(dealId);
}

function storeDealTotalPrice(uint64 dealId) public {
dealPricePerEpoch = MarketAPI.getDealTotalPrice(dealId);
(, dealPricePerEpoch) = MarketAPI.getDealTotalPrice(dealId);
}

function storeClientCollateral(uint64 dealId) public {
clientCollateral = MarketAPI.getDealClientCollateral(dealId);
(, clientCollateral) = MarketAPI.getDealClientCollateral(dealId);
}

function storeProviderCollateral(uint64 dealId) public {
providerCollateral = MarketAPI.getDealProviderCollateral(dealId);
(, providerCollateral) = MarketAPI.getDealProviderCollateral(dealId);
}

function storeDealVerificaton(uint64 dealId) public {
isDealActivated = MarketAPI.getDealVerified(dealId);
(, isDealActivated) = MarketAPI.getDealVerified(dealId);
}

function storeDealActivationStatus(uint64 dealId) public {
activationStatus = MarketAPI.getDealActivation(dealId);
(, activationStatus) = MarketAPI.getDealActivation(dealId);
}
}
14 changes: 7 additions & 7 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const PRIVATE_KEY = process.env.PRIVATE_KEY
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: {
version: "0.8.17",
version: "0.8.23",
settings: {
optimizer: {
enabled: true,
runs: 1000,
details: { yul: false },
},
optimizer: {
enabled: true,
runs: 1000,
details: { yul: false },
},
},
},
},
defaultNetwork: "calibrationnet",
networks: {
localnet: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": ">=16.0.0",
"@zondax/filecoin-solidity": "^4.0.3",
"chai": "^4.2.0",
"cids": "^1.1.9",
"dotenv": "^16.4.5",
"ethers": "^6.1.0",
"filecoin-solidity-api": "^1.1.3",
"hardhat": "^2.19.1",
"hardhat-deploy": "^0.11.44",
"hardhat-deploy-ethers": "^0.4.1",
Expand Down
File renamed without changes.
Loading

0 comments on commit f745bd0

Please sign in to comment.