Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
1,update filterUserMiners
2,update task script
  • Loading branch information
john.yang committed Feb 2, 2023
1 parent b4e6e58 commit f13d0c6
Show file tree
Hide file tree
Showing 9 changed files with 1,278 additions and 1,201 deletions.
16 changes: 16 additions & 0 deletions compiler_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

{
"language": "Solidity",
"settings": {
"optimizer": {
"enabled": true,
"runs": 2000
},
"outputSelection": {
"*": {
"": ["ast"],
"*": ["abi", "metadata", "devdoc", "userdoc", "storageLayout", "evm.legacyAssembly", "evm.bytecode", "evm.deployedBytecode", "evm.methodIdentifiers", "evm.gasEstimates", "evm.assembly"]
}
}
}
}
50 changes: 35 additions & 15 deletions contracts/FILL.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity 0.8.17;

import "@zondax/filecoin-solidity/contracts/v0.8/MinerAPI.sol";
import "@zondax/filecoin-solidity/contracts/v0.8/types/MinerTypes.sol";
import "@zondax/filecoin-solidity/contracts/v0.8/cbor/BytesCbor.sol";
import "@zondax/filecoin-solidity/contracts/v0.8/cbor/BigIntCbor.sol";
import "@zondax/filecoin-solidity/contracts/v0.8/SendAPI.sol";

Expand Down Expand Up @@ -170,9 +169,9 @@ interface FILLInterface {

library OperateLib {
function filterBorrows(
FILLInterface.BorrowInfo[] memory borrows,
FILLInterface.BorrowInfo[] storage borrows,
address account
) internal pure returns (FILLInterface.BorrowInfo[] memory) {
) internal view returns (FILLInterface.BorrowInfo[] memory) {
uint256 resultCount;
for (uint256 i = 0; i < borrows.length; i++) {
if (borrows[i].account == account) {
Expand All @@ -197,7 +196,13 @@ library OperateLib {
bytes[] memory bindKeys,
address account
) internal view returns (bytes[] memory) {
bytes[] memory ret = new bytes[](bindKeys.length);
uint256 resultCount;
for (uint256 i = 0; i < bindKeys.length; i++) {
if (minerBindsMap[bindKeys[i]] == account) {
resultCount++;
}
}
bytes[] memory ret = new bytes[](resultCount);
uint256 j = 0;
for (uint256 i = 0; i < bindKeys.length; i++) {
if (minerBindsMap[bindKeys[i]] == account) {
Expand All @@ -210,10 +215,8 @@ library OperateLib {
}

contract FILL is Context, FILLInterface {
using BytesCBOR for bytes;

mapping(bytes => address) private minerBindsMap;
bytes[] bindKeys;
bytes[] private bindKeys;
mapping(bytes => MinerStackInfo) private minerStacks;
BorrowInfo[] private borrows;

Expand Down Expand Up @@ -279,7 +282,7 @@ contract FILL is Context, FILLInterface {
) external returns (uint256) {
haveStaking(minerAddr);
isBindMiner(_msgSender(), minerAddr);
checkinterestRate(interest_rate, slippage);
checkInterestRate(interest_rate, slippage);
require(
(amount + minerStacks[minerAddr].borrowCount) <
minerStacks[minerAddr].quota,
Expand Down Expand Up @@ -344,13 +347,12 @@ contract FILL is Context, FILLInterface {

function bindMiner(
bytes memory minerAddr,
address blindAccount,
address bindAccount,
string memory message,
bytes memory signature
) external onlyOwner returns (bool) {
// todo : not ready
if (minerBindsMap[minerAddr] == address(0)) {
minerBindsMap[minerAddr] = blindAccount;
minerBindsMap[minerAddr] = bindAccount;
bindKeys.push(minerAddr);
}
return true;
Expand Down Expand Up @@ -384,6 +386,7 @@ contract FILL is Context, FILLInterface {
.getBeneficiary(minerAddr);

// todo : check new_beneficiary

// new_quota check
uint256 quota = uint256(
bytes32(
Expand Down Expand Up @@ -473,6 +476,14 @@ contract FILL is Context, FILLInterface {
return OperateLib.filterBorrows(borrows, account);
}

function getStackMinerInfo(bytes memory minerAddr)
external
view
returns (MinerStackInfo memory)
{
return minerStacks[minerAddr];
}

function allBorrows() external view returns (BorrowInfo[] memory) {
return borrows;
}
Expand All @@ -484,7 +495,8 @@ contract FILL is Context, FILLInterface {
_accumulatedInterestFIL -
(_accumulatedBorrowFIL - _accumulatedPaybackFIL)),
totalDeposited: _accumulatedDepositFIL,
utilizedLiquidity: 0,
utilizedLiquidity: (_accumulatedBorrowFIL -
_accumulatedPaybackFIL),
accumulatedInterest: _accumulatedInterestFIL,
accumulatedPayback: _accumulatedPaybackFIL,
accumulatedRedeem: _accumulatedRedeemFIL,
Expand Down Expand Up @@ -603,14 +615,22 @@ contract FILL is Context, FILLInterface {
private
view
{
// todo : add exchangeRate check here
// require(
// (exchRate - slippage) <= _exchangeRate &&
// _exchangeRate <= (exchRate + slippage),
// "check exchange rate failed"
// );
}

function checkinterestRate(uint256 interest_rate, uint256 slippage)
function checkInterestRate(uint256 interest_rate, uint256 slippage)
private
view
{
// todo : add exchangeRate check here
// require(
// (interest_rate - slippage) <= _interestRate &&
// _interestRate <= (interest_rate + slippage),
// "check interest rate failed"
// );
}

function isBindMiner(address account, bytes memory minerAddr) private view {
Expand Down
10 changes: 9 additions & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ require("dotenv").config();
const PRIVATE_KEY = process.env.PRIVATE_KEY;
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.17",
solidity: {
version: "0.8.17",
settings: {
optimizer: {
enabled: true,
runs: 2000,
},
},
},
defaultNetwork: "hyperspace",
networks: {
wallaby: {
Expand Down
23 changes: 23 additions & 0 deletions tasks/FILL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fa = require("@glif/filecoin-address");
const { utils } = require("ethers");
const { task } = require("hardhat/config");
const { callRpc, hexToBytes } = require("./common");

task("fill-fillInfo", "fillInfo")
.addParam("contractaddress", "The FILLContract address")
.setAction(async (taskArgs) => {
const contractAddr = taskArgs.contractaddress;
const FILLContract = await ethers.getContractFactory("FILL");
//Get signer information
const accounts = await ethers.getSigners();
const signer = accounts[0];

const FILLContractContract = new ethers.Contract(
contractAddr,
FILLContract.interface,
signer
);
const fillInfo = await FILLContractContract.fillInfo();
console.log("fillInfo:", fillInfo);
});
module.exports = {};
26 changes: 5 additions & 21 deletions tasks/Miner/miner-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
//
// DRAFT!! THIS CODE HAS NOT BEEN AUDITED - USE ONLY FOR PROTOTYPING
//
const util = require("util");
const fa = require("@glif/filecoin-address");
const request = util.promisify(require("request"));
const { callRpc } = require("../common");

task("miner-info", "")
.addParam("contractaddress", "The MinerOp address")
Expand All @@ -27,25 +26,10 @@ task("miner-info", "")
const { beneficiary, quota, expiration } = taskArgs;
const networkId = network.name;

const priorityFee = await callRpc("eth_maxPriorityFeePerGas");

async function callRpc(method, params) {
var options = {
method: "POST",
url: network.config.url,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
method: method,
params: params,
id: 1,
}),
};
const res = await request(options);
return JSON.parse(res.body).result;
}
const priorityFee = await callRpc(
network.config.url,
"eth_maxPriorityFeePerGas"
);

console.log("Calling getBeneficiary method");
const MinerOp = await ethers.getContractFactory("MinerOp");
Expand Down
31 changes: 31 additions & 0 deletions tasks/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const util = require("util");
const request = util.promisify(require("request"));

async function callRpc(url, method, params) {
var options = {
method: "POST",
url: url,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
method: method,
params: params,
id: 1,
}),
};
const res = await request(options);
return JSON.parse(res.body).result;
}

function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return new Uint8Array(bytes);
}

module.exports = {
callRpc,
hexToBytes,
};
55 changes: 25 additions & 30 deletions tasks/get-address.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,52 @@
const fa = require("@glif/filecoin-address");
const util = require("util");
const request = util.promisify(require("request"));
const { utils } = require("ethers");
const { callRpc, hexToBytes } = require("./common");

task(
"get-address",
"Gets Filecoin f4 address and corresponding Ethereum address."
).setAction(async (taskArgs) => {
const DEPLOYER_PRIVATE_KEY = network.config.accounts[0];

function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return new Uint8Array(bytes);
}

async function callRpc(method, params) {
var options = {
method: "POST",
url: network.config.url,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
method: method,
params: params,
id: 1,
}),
};
const res = await request(options);
return JSON.parse(res.body).result;
}

const deployer = new ethers.Wallet(DEPLOYER_PRIVATE_KEY);

const pubKey = hexToBytes(deployer.publicKey.slice(2));

const priorityFee = await callRpc("eth_maxPriorityFeePerGas");
const priorityFee = await callRpc(
network.config.url,
"eth_maxPriorityFeePerGas"
);
console.log(priorityFee);

const minerId = fa.newFromString("t01000");
console.log(
"minerId :",
minerId.toString(),
",bytes: ",
minerId.bytes,
",payload: ",
minerId.payload(),
"hex addr:",
Buffer.from(minerId.bytes).toString("hex"),
utils.hexlify(minerId.bytes)
);

const addrHex = "0x00e807";
const newAddr = fa.newAddress(
0,
utils.arrayify(utils.hexStripZeros(addrHex))
);
console.log(newAddr.toString());
// console.log(fa.idFromAddress(newAddr));
const filBalance = await callRpc(
network.config.url,
"Filecoin.WalletBalance",
[newAddr.toString()]
);
console.log("filBalance:", filBalance);

const f4Address = fa.newDelegatedEthAddress(deployer.address).toString();
const nonce = await callRpc("Filecoin.MpoolGetNonce", [f4Address]);
const nonce = await callRpc(network.config.url, "Filecoin.MpoolGetNonce", [
f4Address,
]);
console.log(
"Ethereum address (this addresss should work for most tools):",
deployer.address
Expand Down
2 changes: 1 addition & 1 deletion tasks/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
exports.getAddress = require("./get-address");

exports.sendCoin = require("./FILL/send-coin");
exports.fill = require("./fill");

exports.minerInfo = require("./Miner/miner-info");
Loading

0 comments on commit f13d0c6

Please sign in to comment.