-
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 all commits
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
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,48 @@ | ||
import { ethers } from "ethers"; | ||
|
||
import GatewayABI from "./abi/GatewayEVM.sol/GatewayEVM.json"; | ||
import { ZetaChainClient } from "./client"; | ||
|
||
export const evmCall = async function ( | ||
this: ZetaChainClient, | ||
args: { | ||
callOnRevert: boolean; | ||
gasLimit: number; | ||
gasPrice: ethers.BigNumber; | ||
gatewayEvm: string; | ||
onRevertGasLimit: number; | ||
receiver: string; | ||
revertAddress: string; | ||
revertMessage: string; | ||
types: string; | ||
values: any[]; | ||
} | ||
) { | ||
const signer = this.signer; | ||
const { utils } = ethers; | ||
const gateway = new ethers.Contract(args.gatewayEvm, GatewayABI.abi, signer); | ||
|
||
const encodedParameters = utils.defaultAbiCoder.encode( | ||
JSON.parse(args.types), | ||
args.values | ||
); | ||
const tx = await gateway[ | ||
"call(address,bytes,(address,bool,address,bytes,uint256))" | ||
]( | ||
args.receiver, | ||
encodedParameters, | ||
{ | ||
abortAddress: "0x0000000000000000000000000000000000000000", | ||
callOnRevert: args.callOnRevert, | ||
onRevertGasLimit: args.onRevertGasLimit, | ||
revertAddress: args.revertAddress, | ||
revertMessage: utils.hexlify(utils.toUtf8Bytes(args.revertMessage)), | ||
}, | ||
{ | ||
gasLimit: args.gasLimit, | ||
gasPrice: args.gasPrice, | ||
} | ||
); | ||
|
||
return tx; | ||
}; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import ERC20_ABI from "@openzeppelin/contracts/build/contracts/ERC20.json"; | ||
import { ethers } from "ethers"; | ||
|
||
import GatewayABI from "./abi/GatewayEVM.sol/GatewayEVM.json"; | ||
import { ZetaChainClient } from "./client"; | ||
|
||
export const evmDeposit = async function ( | ||
this: ZetaChainClient, | ||
args: { | ||
amount: string; | ||
callOnRevert: boolean; | ||
erc20: string; | ||
gasLimit: number; | ||
gasPrice: ethers.BigNumber; | ||
gatewayEvm: string; | ||
onRevertGasLimit: number; | ||
receiver: string; | ||
revertAddress: string; | ||
revertMessage: string; | ||
} | ||
) { | ||
const signer = this.signer; | ||
const { utils } = ethers; | ||
const gateway = new ethers.Contract(args.gatewayEvm, 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, | ||
}; | ||
let tx; | ||
if (args.erc20) { | ||
const erc20Contract = new 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, | ||
}); | ||
} | ||
|
||
return tx; | ||
}; | ||
Comment on lines
+7
to
+68
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 of
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import ERC20_ABI from "@openzeppelin/contracts/build/contracts/ERC20.json"; | ||
import { ethers } from "ethers"; | ||
|
||
import GatewayABI from "./abi/GatewayEVM.sol/GatewayEVM.json"; | ||
import { ZetaChainClient } from "./client"; | ||
|
||
export const evmDepositAndCall = async function ( | ||
this: ZetaChainClient, | ||
args: { | ||
amount: string; | ||
callOnRevert: boolean; | ||
erc20: string; | ||
gasLimit: number; | ||
gasPrice: ethers.BigNumber; | ||
gatewayEvm: string; | ||
onRevertGasLimit: number; | ||
receiver: string; | ||
revertAddress: string; | ||
revertMessage: string; | ||
types: string; | ||
values: any[]; | ||
} | ||
) { | ||
const signer = this.signer; | ||
const { utils } = ethers; | ||
const gateway = new ethers.Contract(args.gatewayEvm, 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 encodedParameters = utils.defaultAbiCoder.encode( | ||
JSON.parse(args.types), | ||
args.values | ||
); | ||
let tx; | ||
if (args.erc20) { | ||
const erc20Contract = new 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, | ||
} | ||
); | ||
} | ||
|
||
return tx; | ||
}; | ||
Comment on lines
+7
to
+82
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 of
Enhance validation of token addresses and amounts. Optimize transaction options for cost-efficiency. Improve error handling to manage exceptions and reverts more effectively. |
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.
Review of
evmCall
function: Potential security and maintainability concerns.Security Concern: JSON Parsing in Transaction Call
Direct use of
JSON.parse(args.types)
in the transaction setup could lead to security vulnerabilities if the input is not properly validated. Consider validating or sanitizing the input before parsing to ensure it does not introduce code injection risks.Hardcoded Address Usage
The
abortAddress
is hardcoded, which might limit flexibility. If this address might need to change based on the network or other conditions, consider making it a configurable parameter.Dynamic Method Access
Accessing the contract method using a string key (
gateway["call(address,bytes,(address,bool,address,bytes,uint256))"]
) assumes the method name is stable. This could lead to maintenance issues if the ABI changes. Consider defining these method names as constants or ensuring they are updated when the ABI changes.Error Handling
The function does not explicitly handle errors that could occur during the transaction call. Consider adding try-catch blocks around the transaction call to handle possible rejections or failures gracefully.