-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Gateway methods and Solana deposit #179
Changes from 1 commit
68320d6
2699208
03ade9d
39bce32
34adb75
fc1d8c1
7a44446
39ea712
4783164
8acb4bc
8fc9586
b10bd01
bc06386
a759ecf
2aaeb2f
ce2aed4
b80fc27
585aedb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { ethers } from "ethers"; | ||
|
||
import GatewayABI from "./abi/GatewayZEVM.sol/GatewayZEVM.json"; | ||
import ZRC20ABI from "./abi/ZRC20.sol/ZRC20.json"; | ||
import { ZetaChainClient } from "./client"; | ||
|
||
export const zetachainWithdrawAndCall = async function ( | ||
this: ZetaChainClient, | ||
args: { | ||
amount: string; | ||
zrc20: string; | ||
receiver: string; | ||
function: string; | ||
types: string; | ||
values: any[]; | ||
gasLimit: number; | ||
gasPrice: ethers.BigNumber; | ||
gatewayZetaChain: string; | ||
callOnRevert: boolean; | ||
onRevertGasLimit: number; | ||
revertAddress: string; | ||
revertMessage: string; | ||
} | ||
) { | ||
const signer = this.signer; | ||
const { utils } = ethers; | ||
|
||
const gateway = new ethers.Contract( | ||
args.gatewayZetaChain, | ||
GatewayABI.abi, | ||
signer | ||
); | ||
|
||
const revertOptions = { | ||
abortAddress: "0x0000000000000000000000000000000000000000", | ||
callOnRevert: args.callOnRevert, | ||
onRevertGasLimit: args.onRevertGasLimit, | ||
revertAddress: args.revertAddress, | ||
// not used | ||
revertMessage: utils.hexlify(utils.toUtf8Bytes(args.revertMessage)), | ||
}; | ||
|
||
const txOptions = { | ||
gasLimit: args.gasLimit, | ||
gasPrice: args.gasPrice, | ||
}; | ||
|
||
const functionSignature = utils.id(args.function).slice(0, 10); | ||
const encodedParameters = utils.defaultAbiCoder.encode( | ||
JSON.parse(args.types), | ||
args.values | ||
); | ||
|
||
const message = utils.hexlify( | ||
utils.concat([functionSignature, encodedParameters]) | ||
); | ||
Comment on lines
+47
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize encoding of function calls. The encoding of function calls using |
||
const zrc20 = new ethers.Contract(args.zrc20, ZRC20ABI.abi, signer); | ||
const decimals = await zrc20.decimals(); | ||
const value = utils.parseUnits(args.amount, decimals); | ||
const [gasZRC20, gasFee] = await zrc20.withdrawGasFeeWithGasLimit( | ||
args.gasLimit | ||
); | ||
if (args.zrc20 === gasZRC20) { | ||
const approveGasAndWithdraw = await zrc20.approve( | ||
args.gatewayZetaChain, | ||
value.add(gasFee), | ||
txOptions | ||
); | ||
await approveGasAndWithdraw.wait(); | ||
} else { | ||
const gasZRC20Contract = new ethers.Contract( | ||
gasZRC20, | ||
ZRC20ABI.abi, | ||
signer | ||
); | ||
const approveGas = await gasZRC20Contract.approve( | ||
args.gatewayZetaChain, | ||
gasFee, | ||
txOptions | ||
); | ||
await approveGas.wait(); | ||
const approveWithdraw = await zrc20.approve( | ||
args.gatewayZetaChain, | ||
value, | ||
txOptions | ||
); | ||
await approveWithdraw.wait(); | ||
} | ||
const method = | ||
"withdrawAndCall(bytes,uint256,address,bytes,uint256,(address,bool,address,bytes,uint256))"; | ||
const tx = await gateway[method]( | ||
utils.hexlify(args.receiver), | ||
value, | ||
args.zrc20, | ||
message, | ||
args.gasLimit, | ||
revertOptions, | ||
txOptions | ||
); | ||
return tx; | ||
Comment on lines
+57
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review and optimize token handling and transaction execution logic. The logic for handling token approvals and executing the transaction is complex and involves multiple conditional branches. This could be simplified or at least better documented to improve maintainability. Additionally, ensure that all asynchronous operations are properly awaited and errors are handled appropriately. + try {
const decimals = await zrc20.decimals();
const value = utils.parseUnits(args.amount, decimals);
const [gasZRC20, gasFee] = await zrc20.withdrawGasFeeWithGasLimit(args.gasLimit);
if (args.zrc20 === gasZRC20) {
const approveGasAndWithdraw = await zrc20.approve(
args.gatewayZetaChain,
value.add(gasFee),
txOptions
);
await approveGasAndWithdraw.wait();
} else {
// Handle other cases similarly...
}
const tx = await gateway['withdrawAndCall'](utils.hexlify(args.receiver), value, args.zrc20, message, args.gasLimit, revertOptions, txOptions);
return tx;
+ } catch (error) {
+ console.error('Transaction execution failed:', error);
+ throw error;
+ }
|
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper error handling for contract instantiation.
Instantiating a new contract with
ethers.Contract
should include error handling to manage cases where the contract address or ABI might be incorrect, or the network connection fails. Consider wrapping this in a try-catch block.