Skip to content

Commit

Permalink
refactor(solidity): bridge call task (#514)
Browse files Browse the repository at this point in the history
Co-authored-by: nulnut <151493716+nulnut@users.noreply.github.com>
  • Loading branch information
zakir-code and nulnut authored May 24, 2024
1 parent 61cba6e commit bbc55fb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
26 changes: 21 additions & 5 deletions solidity/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ const config: HardhatUserConfig = {
hardhat: {
chainId: process.env.CHAIN_ID ? parseInt(process.env.CHAIN_ID) : 1337,
},
mainnet: {
url: `${process.env.MAINNET_URL || "https://mainnet.infura.io/v3/" + process.env.INFURA_KEY}`,
ethereum: {
url: `${process.env.ETHEREUM_URL || "https://1rpc.io/eth"}`,
chainId: 1,
},
sepolia: {
url: `${process.env.SEPOLIA_URL || "https://sepolia.infura.io/v3/" + process.env.INFURA_KEY}`,
url: `${process.env.SEPOLIA_URL || "https://rpc.sepolia.org"}`,
chainId: 11155111,
},
arbitrumSepolia: {
Expand All @@ -35,6 +35,14 @@ const config: HardhatUserConfig = {
url: `${process.env.BASE_URL || "https://sepolia.base.org"}`,
chainId: 84532,
},
fxcore: {
url: `${process.env.FXCORE_URL || "https://fx-json-web3.functionx.io:8545"}`,
chainId: 530,
},
dhobyghaut: {
url: `${process.env.DHOBYGHAUT_URL || "https://testnet-fx-json-web3.functionx.io:8545"}`,
chainId: 90001,
},
localhost: {
url: `${process.env.LOCAL_URL || "http://127.0.0.1:8545"}`,
},
Expand Down Expand Up @@ -72,21 +80,29 @@ const config: HardhatUserConfig = {
},
etherscan: {
apiKey: {
mainnet: `${process.env.ETHERSCAN_API_KEY}`,
ethereum: `${process.env.ETHERSCAN_API_KEY}`,
sepolia: `${process.env.ETHERSCAN_API_KEY}`,
arbitrumSepolia: `${process.env.ETHERSCAN_API_KEY}`,
optimisticSepolia: `${process.env.ETHERSCAN_API_KEY}`,
baseSepolia: `${process.env.ETHERSCAN_API_KEY}`
},
customChains: [
{
network: "ethereum",
chainId: 1,
urls: {
apiURL: "https://api.etherscan.io/api",
browserURL: "https://etherscan.io"
}
},
{
network: "optimisticSepolia",
chainId: 11155420,
urls: {
apiURL: "https://api-sepolia-optimistic.etherscan.io/api",
browserURL: "https://sepolia-optimism.etherscan.io/"
}
}
},
]
},
dependencyCompiler: {
Expand Down
80 changes: 46 additions & 34 deletions solidity/tasks/bridge_tasks.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {task} from "hardhat/config";
import {boolean, string} from "hardhat/internal/core/params/argumentTypes";
import { task } from "hardhat/config";
import { boolean, string } from "hardhat/internal/core/params/argumentTypes";
import {
AddTxParam,
GetGravityId,
GetOracleSet,
SUB_CHECK_PRIVATE_KEY,
SUB_CONFIRM_TRANSACTION,
SUB_CREATE_ASSET_DATA,
SUB_CREATE_TRANSACTION,
TransactionToJson,
vote_power
} from "./subtasks";
import {bech32} from "bech32";
import { bech32 } from "bech32";
import { ZeroAddress } from "ethers";

const sendToFx = task("send-to-fx", "call bridge contract sendToFx()")
.addParam("bridgeContract", "bridge token address", undefined, string, false)
Expand All @@ -20,7 +20,7 @@ const sendToFx = task("send-to-fx", "call bridge contract sendToFx()")
.addParam("destination", "destination address", undefined, string, false)
.addParam("targetIbc", "target ibc address", "", string, true)
.setAction(async (taskArgs, hre) => {
const {wallet} = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);
const { wallet } = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);

const bridgeTokenContract = await hre.ethers.getContractAt("ERC20TokenTest", taskArgs.bridgeToken, wallet);
const from = await wallet.getAddress();
Expand All @@ -43,7 +43,7 @@ const sendToFx = task("send-to-fx", "call bridge contract sendToFx()")
gasLimit: taskArgs.gasLimit,
});

const {answer} = await hre.run(SUB_CONFIRM_TRANSACTION, {
const { answer } = await hre.run(SUB_CONFIRM_TRANSACTION, {
message: `\n${TransactionToJson(tx)}\n`,
disableConfirm: taskArgs.disableConfirm,
});
Expand Down Expand Up @@ -79,7 +79,7 @@ const sendToFx = task("send-to-fx", "call bridge contract sendToFx()")
gasLimit: taskArgs.gasLimit,
});

const {answer} = await hre.run(SUB_CONFIRM_TRANSACTION, {
const { answer } = await hre.run(SUB_CONFIRM_TRANSACTION, {
message: `\n${TransactionToJson(tx)}\n`,
disableConfirm: taskArgs.disableConfirm,
});
Expand All @@ -100,8 +100,8 @@ const initBridge = task("init-bridge", "init bridge contract")
.addParam("restUrl", "fx node rest rpc url", undefined, string, false)
.addParam("chainName", "init cross chain name", undefined, string, false)
.setAction(async (taskArgs, hre) => {
const {bridgeLogic, bridgeContract, restUrl, chainName} = taskArgs;
const {wallet} = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);
const { bridgeLogic, bridgeContract, restUrl, chainName } = taskArgs;
const { wallet } = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);
const from = await wallet.getAddress();

const bridge_logic_factory = await hre.ethers.getContractFactory("FxBridgeLogic")
Expand Down Expand Up @@ -144,7 +144,7 @@ const initBridge = task("init-bridge", "init bridge contract")
gasLimit: taskArgs.gasLimit,
});

const {answer} = await hre.run(SUB_CONFIRM_TRANSACTION, {
const { answer } = await hre.run(SUB_CONFIRM_TRANSACTION, {
message: `\n${TransactionToJson(tx)}\n`,
disableConfirm: taskArgs.disableConfirm,
});
Expand All @@ -165,8 +165,8 @@ const addBridgeToken = task("add-bridge-token", "add bridge token into bridge co
.addParam("isOriginal", "bridge token target ibc for bridge token", false, boolean, true)
.addParam("targetIbc", "bridge token target ibc for bridge token", "", string, true)
.setAction(async (taskArgs, hre) => {
const {bridgeContract, tokenContract, isOriginal, targetIbc} = taskArgs;
const {wallet} = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);
const { bridgeContract, tokenContract, isOriginal, targetIbc } = taskArgs;
const { wallet } = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);
const from = await wallet.getAddress();

const bridge_factory = await hre.ethers.getContractFactory("FxBridgeLogic")
Expand All @@ -182,7 +182,7 @@ const addBridgeToken = task("add-bridge-token", "add bridge token into bridge co
gasLimit: taskArgs.gasLimit,
});

const {answer} = await hre.run(SUB_CONFIRM_TRANSACTION, {
const { answer } = await hre.run(SUB_CONFIRM_TRANSACTION, {
message: `\n${TransactionToJson(tx)}\n`,
disableConfirm: taskArgs.disableConfirm,
});
Expand All @@ -197,52 +197,64 @@ const addBridgeToken = task("add-bridge-token", "add bridge token into bridge co
}
});

const bridgeERC20Call = task("bridge-erc20-call", "bridge erc20 and call contract function")
const bridgeCall = task("bridge-call", "bridge call function")
.addParam("bridgeContract", "bridge contract address", undefined, string, false)
.addParam("dstChainId", "destination chain id", undefined, string, false)
.addParam("callGasLimit", "call gas limit", "0", string, true)
.addParam("receiver", "call receiver", undefined, string, false)
.addParam("tokens", "bridge token address list", undefined, string, false)
.addParam("amounts", "bridge token amount list", undefined, string, false)
.addParam("to", "call to", undefined, string, false)
.addParam("message", "call message", undefined, string, false)
.addParam("data", "call data", undefined, string, false)
.addParam("callValue", "call value", "0", string, true)
.addParam("bridgeTokens", "bridge token address list", undefined, string, false)
.addParam("bridgeAmounts", "bridge token amount list", undefined, string, false)
.addParam("memo", "call memo", undefined, string, false)

.setAction(async (taskArgs, hre) => {
const {
let {
bridgeContract,
dstChainId,
callGasLimit,
receiver,
tokens,
amounts,
to,
message,
data,
callValue,
bridgeTokens,
bridgeAmounts
memo
} = taskArgs;
const {wallet} = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);
const { wallet } = await hre.run(SUB_CHECK_PRIVATE_KEY, taskArgs);
const from = await wallet.getAddress();

const asset = await hre.run(SUB_CREATE_ASSET_DATA, {
bridgeTokens: bridgeTokens,
bridgeAmounts: bridgeAmounts,
assetType: "ERC20"
});
let tokenArr, amountArr;
if (tokens === "") {
tokenArr = []
amountArr = []
} else {
tokenArr = tokens.split(",")
amountArr = amounts.split(",")
}
if (tokenArr.length !== amountArr.length) {
console.log(`tokens and amounts do not match`)
return
}

if (to == "") {
to = ZeroAddress;
}

const bridge_logic_factory = await hre.ethers.getContractFactory("FxBridgeLogic")
const data = bridge_logic_factory.interface.encodeFunctionData('bridgeCall', [
dstChainId, callGasLimit, receiver, to, message, callValue, asset
const bridgeCallData = bridge_logic_factory.interface.encodeFunctionData('bridgeCall', [
dstChainId, receiver, tokenArr, amountArr, to, data, callValue, memo
])

const tx = await hre.run(SUB_CREATE_TRANSACTION, {
from: from, to: bridgeContract, data: data, value: taskArgs.value,
from: from, to: bridgeContract, data: bridgeCallData, value: taskArgs.value,
gasPrice: taskArgs.gasPrice,
maxFeePerGas: taskArgs.maxFeePerGas,
maxPriorityFeePerGas: taskArgs.maxPriorityFeePerGas,
nonce: taskArgs.nonce,
gasLimit: taskArgs.gasLimit,
});

const {answer} = await hre.run(SUB_CONFIRM_TRANSACTION, {
const { answer } = await hre.run(SUB_CONFIRM_TRANSACTION, {
message: `\n${TransactionToJson(tx)}\n`,
disableConfirm: taskArgs.disableConfirm,
});
Expand All @@ -257,4 +269,4 @@ const bridgeERC20Call = task("bridge-erc20-call", "bridge erc20 and call contrac
}
});

AddTxParam([sendToFx, initBridge, addBridgeToken, bridgeERC20Call])
AddTxParam([sendToFx, initBridge, addBridgeToken, bridgeCall])

0 comments on commit bbc55fb

Please sign in to comment.