Skip to content

Commit

Permalink
Gateway methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 6, 2024
1 parent 748d377 commit 68320d6
Show file tree
Hide file tree
Showing 8 changed files with 532 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/tasks/src/abi/GatewayEVM.sol/GatewayEVM.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/tasks/src/abi/GatewayZEVM.sol/GatewayZEVM.json

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions packages/tasks/src/evmCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { task, types } from "hardhat/config";

Check failure on line 1 in packages/tasks/src/evmCall.ts

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import GatewayABI from "./abi/GatewayEVM.sol/GatewayEVM.json";

export const evmCall = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();
const { utils } = hre.ethers;

const gateway = new hre.ethers.Contract(
args.gatewayEvm,
GatewayABI.abi,
signer
);

const encodedParameters = utils.defaultAbiCoder.encode(
JSON.parse(args.types),
args.values
);

try {
const tx = await gateway[
"call(address,bytes,(address,bool,address,bytes,uint256))"
](
args.receiver,
encodedParameters,
{
revertAddress: args.revertAddress,
callOnRevert: args.callOnRevert,

Check failure on line 28 in packages/tasks/src/evmCall.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'callOnRevert' should be before 'revertAddress'
abortAddress: "0x0000000000000000000000000000000000000000", // not used

Check failure on line 29 in packages/tasks/src/evmCall.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'abortAddress' should be before 'callOnRevert'
revertMessage: utils.hexlify(utils.toUtf8Bytes(args.revertMessage)),
onRevertGasLimit: args.onRevertGasLimit,

Check failure on line 31 in packages/tasks/src/evmCall.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'onRevertGasLimit' should be before 'revertMessage'
},
{
gasPrice: args.gasPrice,
gasLimit: args.gasLimit,

Check failure on line 35 in packages/tasks/src/evmCall.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'gasLimit' should be before 'gasPrice'
}
);
const receipt = await tx.wait();
console.log("Transaction hash:", receipt.transactionHash);
} catch (e) {
console.error("Transaction error:", e);
}
};

task("evm-call", "Call a universal app", evmCall)
.addParam("receiver", "Receiver address on ZetaChain")
.addOptionalParam(
"gatewayEvm",
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
)
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
"Revert address",
"0x0000000000000000000000000000000000000000"
)
.addOptionalParam(
"gasPrice",
"The gas price for the transaction",
10000000000,
types.int
)
.addOptionalParam(
"gasLimit",
"The gas limit for the transaction",
7000000,
types.int
)
.addOptionalParam(
"onRevertGasLimit",
"The gas limit for the revert transaction",
7000000,
types.int
)
.addOptionalParam("revertMessage", "Revert message", "0x")
.addParam("types", "The types of the parameters (example: ['string'])")
.addVariadicPositionalParam("values", "The values of the parameters");
100 changes: 100 additions & 0 deletions packages/tasks/src/evmDeposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { task, types } from "hardhat/config";

Check failure on line 1 in packages/tasks/src/evmDeposit.ts

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import GatewayABI from "./abi/GatewayEVM.sol/GatewayEVM.json";
import ERC20_ABI from "@openzeppelin/contracts/build/contracts/ERC20.json";

export const evmDeposit = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();
const { utils } = hre.ethers;

const gateway = new hre.ethers.Contract(
args.gatewayEvm,
GatewayABI.abi,
signer
);

const revertOptions = {
revertAddress: args.revertAddress,
callOnRevert: args.callOnRevert,

Check failure on line 18 in packages/tasks/src/evmDeposit.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'callOnRevert' should be before 'revertAddress'
abortAddress: "0x0000000000000000000000000000000000000000", // not used

Check failure on line 19 in packages/tasks/src/evmDeposit.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'abortAddress' should be before 'callOnRevert'
revertMessage: utils.hexlify(utils.toUtf8Bytes(args.revertMessage)),
onRevertGasLimit: args.onRevertGasLimit,

Check failure on line 21 in packages/tasks/src/evmDeposit.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'onRevertGasLimit' should be before 'revertMessage'
};

const txOptions = {
gasPrice: args.gasPrice,
gasLimit: args.gasLimit,

Check failure on line 26 in packages/tasks/src/evmDeposit.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'gasLimit' should be before 'gasPrice'
};

try {
let tx;
if (args.erc20) {
const erc20Contract = new hre.ethers.Contract(
args.erc20,
ERC20_ABI.abi,
signer
);
const decimals = await erc20Contract.decimals();
const value = utils.parseUnits(args.amount, decimals);
await erc20Contract.connect(signer).approve(args.gatewayEvm, value);
const method =
"deposit(address,uint256,address,(address,bool,address,bytes,uint256))";
tx = await gateway[method](
args.receiver,
value,
args.erc20,
revertOptions,
txOptions
);
} else {
const value = utils.parseEther(args.amount);
const method = "deposit(address,(address,bool,address,bytes,uint256))";
tx = await gateway[method](args.receiver, revertOptions, {
...txOptions,
value,
});
}
if (tx) {
const receipt = await tx.wait();
console.log("Transaction hash:", receipt.transactionHash);
}
} catch (e) {
console.error("Transaction error:", e);
}
};

task("evm-deposit", "Deposit tokens", evmDeposit)
.addParam("receiver", "Receiver address on ZetaChain")
.addOptionalParam(
"gatewayEvm",
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
)
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
"Revert address",
"0x0000000000000000000000000000000000000000",
types.string
)
.addOptionalParam(
"gasPrice",
"The gas price for the transaction",
50000000000,
types.int
)
.addOptionalParam(
"gasLimit",
"The gas limit for the transaction",
7000000,
types.int
)
.addOptionalParam(
"onRevertGasLimit",
"The gas limit for the revert transaction",
7000000,
types.int
)
.addOptionalParam("revertMessage", "Revert message", "0x")
.addParam("amount", "amount of ETH to send with the transaction")
.addOptionalParam("erc20", "ERC-20 token address");
117 changes: 117 additions & 0 deletions packages/tasks/src/evmDepositAndCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import GatewayABI from "./abi/GatewayEVM.sol/GatewayEVM.json";
import ERC20_ABI from "@openzeppelin/contracts/build/contracts/ERC20.json";

export const evmDepositAndCall = async (
args: any,
hre: HardhatRuntimeEnvironment
) => {
const [signer] = await hre.ethers.getSigners();
const { utils } = hre.ethers;

const gateway = new hre.ethers.Contract(
args.gatewayEvm,
GatewayABI.abi,
signer
);

const revertOptions = {
revertAddress: args.revertAddress,
callOnRevert: args.callOnRevert,
abortAddress: "0x0000000000000000000000000000000000000000", // not used
revertMessage: utils.hexlify(utils.toUtf8Bytes(args.revertMessage)),
onRevertGasLimit: args.onRevertGasLimit,
};

const txOptions = {
gasPrice: args.gasPrice,
gasLimit: args.gasLimit,
};

const encodedParameters = utils.defaultAbiCoder.encode(
JSON.parse(args.types),
args.values
);

try {
let tx;
if (args.erc20) {
const erc20Contract = new hre.ethers.Contract(
args.erc20,
ERC20_ABI.abi,
signer
);
const decimals = await erc20Contract.decimals();
const value = utils.parseUnits(args.amount, decimals);
await erc20Contract.connect(signer).approve(args.gatewayEvm, value);
const method =
"depositAndCall(address,uint256,address,bytes,(address,bool,address,bytes,uint256))";
tx = await gateway[method](
args.receiver,
value,
args.erc20,
encodedParameters,
revertOptions,
txOptions
);
} else {
const value = utils.parseEther(args.amount);
const method =
"depositAndCall(address,bytes,(address,bool,address,bytes,uint256))";
tx = await gateway[method](
args.receiver,
encodedParameters,
revertOptions,
{
...txOptions,
value,
}
);
}
if (tx) {
const receipt = await tx.wait();
console.log("Transaction hash:", receipt.transactionHash);
}
} catch (e) {
console.error("Transaction error:", e);
}
};

task("evm-deposit-and-call", "Deposit tokens", evmDepositAndCall)
.addParam("receiver", "Receiver address on ZetaChain")
.addOptionalParam(
"gatewayEvm",
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
)
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
"Revert address",
"0x0000000000000000000000000000000000000000",
types.string
)
.addOptionalParam(
"gasPrice",
"The gas price for the transaction",
50000000000,
types.int
)
.addOptionalParam(
"gasLimit",
"The gas limit for the transaction",
7000000,
types.int
)
.addOptionalParam(
"onRevertGasLimit",
"The gas limit for the revert transaction",
7000000,
types.int
)
.addOptionalParam("revertMessage", "Revert message", "0x")
.addParam("amount", "amount of ETH to send with the transaction")
.addOptionalParam("erc20", "ERC-20 token address")
.addParam("types", "The types of the parameters (example: ['string'])")
.addVariadicPositionalParam("values", "The values of the parameters");
5 changes: 5 additions & 0 deletions packages/tasks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ export { sendZETATask } from "./sendZETA";
// export { verifyTask } from "./verify";
export { tokensTask } from "./tokens";
export { withdrawTask } from "./withdraw";
export { evmCall } from "./evmCall";
export { evmDeposit } from "./evmDeposit";
export { evmDepositAndCall } from "./evmDepositAndCall";
export { zetachainCall } from "./zetachainCall";
export { zetachainWithdraw } from "./zetachainWithdraw";
Loading

0 comments on commit 68320d6

Please sign in to comment.