Skip to content

Commit

Permalink
Added gateway call to examples (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Oct 3, 2024
1 parent 8421cef commit 2f1b130
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 1,176 deletions.
12 changes: 12 additions & 0 deletions examples/hello/contracts/Hello.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ contract Hello is UniversalContract {
function onRevert(RevertContext calldata revertContext) external override {
emit RevertEvent("Revert on ZetaChain", revertContext);
}

function gatewayCall(
bytes memory receiver,
address zrc20,
bytes calldata message,
uint256 gasLimit,
RevertOptions memory revertOptions
) external {
(, uint256 gasFee) = IZRC20(zrc20).withdrawGasFeeWithGasLimit(gasLimit);
IZRC20(zrc20).approve(address(gateway), gasFee);
gateway.call(receiver, zrc20, message, gasLimit, revertOptions);
}
}
2 changes: 1 addition & 1 deletion examples/hello/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./tasks/deploy";
import "./tasks/deployRevert";
import "./tasks/solana/interact";
import "./tasks/gatewayCall";
import "@zetachain/localnet/tasks";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";
Expand Down
4 changes: 2 additions & 2 deletions examples/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/localnet": "3.0.3",
"@zetachain/localnet": "^3.0.4",
"@zetachain/toolkit": "13.0.0-rc2",
"axios": "^1.3.6",
"chai": "^4.2.0",
Expand Down Expand Up @@ -59,4 +59,4 @@
"@solana/web3.js": "^1.95.2",
"@zetachain/protocol-contracts": "10.0.0-rc10"
}
}
}
126 changes: 126 additions & 0 deletions examples/hello/tasks/gatewayCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import ZRC20ABI from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json";

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

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

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

const functionSignature = ethers.utils.id(args.function).slice(0, 10);

const types = JSON.parse(args.types);

const valuesArray = args.values.map((value: any, index: number) => {
const type = types[index];

if (type === "bool") {
try {
return JSON.parse(value.toLowerCase());
} catch (e) {
throw new Error(`Invalid boolean value: ${value}`);
}
} else if (type.startsWith("uint") || type.startsWith("int")) {
return ethers.BigNumber.from(value);
} else {
return value;
}
});
const encodedParameters = ethers.utils.defaultAbiCoder.encode(
types,
valuesArray
);

const message = ethers.utils.hexlify(
ethers.utils.concat([functionSignature, encodedParameters])
);

const gasLimit = hre.ethers.BigNumber.from(args.gasLimit);
const zrc20 = new ethers.Contract(args.zrc20, ZRC20ABI.abi, signer);
const [, gasFee] = await zrc20.withdrawGasFeeWithGasLimit(gasLimit);
const zrc20TransferTx = await zrc20.transfer(
args.contract,
gasFee,
txOptions
);

await zrc20TransferTx.wait();

const factory = await hre.ethers.getContractFactory("Hello");
const contract = factory.attach(args.contract);

const tx = await contract.gatewayCall(
ethers.utils.hexlify(args.receiver),
args.zrc20,
message,
gasLimit,
revertOptions,
txOptions
);

console.log(`Transaction hash: ${tx.hash}`);
await tx.wait();
console.log("gatewayCall executed successfully");
};

task(
"gateway-call",
"Calls the gatewayCall function on the Hello contract",
main
)
.addParam("contract", "The address of the deployed Hello contract")
.addParam("zrc20", "The address of ZRC-20 to pay fees")
.addOptionalParam(
"gasLimit",
"Gas limit for for a cross-chain call",
7000000,
types.int
)
.addOptionalParam(
"txOptionsGasPrice",
"The gas price for the transaction",
10000000000,
types.int
)
.addOptionalParam(
"txOptionsGasLimit",
"The gas limit for the transaction",
7000000,
types.int
)
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
"Revert address",
"0x0000000000000000000000000000000000000000"
)
.addOptionalParam("revertMessage", "Revert message", "0x")
.addParam(
"receiver",
"The address of the receiver contract on a connected chain"
)
.addOptionalParam(
"onRevertGasLimit",
"The gas limit for the revert transaction",
7000000,
types.int
)
.addParam("function", `Function to call (example: "hello(string)")`)
.addParam("types", `The types of the parameters (example: '["string"]')`)
.addVariadicPositionalParam("values", "The values of the parameters");

module.exports = {};
55 changes: 0 additions & 55 deletions examples/hello/tasks/solana/deposit.ts

This file was deleted.

Loading

0 comments on commit 2f1b130

Please sign in to comment.