Skip to content
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

fix: Check layerzero destination gas limit before sending tokens #151

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .release/.changeset/modern-months-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bnb-chain/canonical-bridge-widget": patch
"@bnb-chain/canonical-bridge-sdk": patch
---

Check Layerzero destination gas limit
26 changes: 22 additions & 4 deletions packages/canonical-bridge-sdk/src/layerZero/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,25 @@ export class LayerZero {
}: ISendCakeTokenInput): Promise<Hash> {
try {
const address32Bytes = pad(userAddress, { size: 32 });
// check destination chain gas limit
const dstGasLimit = await publicClient.readContract({
address: bridgeAddress,
abi: CAKE_PROXY_OFT_ABI,
functionName: 'minDstGasLookup',
args: [dstEndpoint, 0],
});
const gasLimit =
dstGasLimit !== 0n && !!dstGasLimit ? dstGasLimit : gasAmount;
/* version 1 - send token
* version 2 - send token and air drop native gas on destination chain
* https://docs.layerzero.network/v1/developers/evm/evm-guides/advanced/relayer-adapter-parameters#airdrop
*/
const adapterParams =
version === 1
? encodePacked(['uint16', 'uint256'], [version, gasAmount])
? encodePacked(['uint16', 'uint256'], [version, gasLimit])
: encodePacked(
['uint16', 'uint', 'uint', 'address'],
[2, gasAmount, airDropGas, dstAddress]
[2, gasLimit, airDropGas, dstAddress]
);
const fees = await publicClient.readContract({
address: bridgeAddress,
Expand Down Expand Up @@ -122,13 +131,22 @@ export class LayerZero {
dstAddress = '0x',
}: IGetEstimateFeeInput) {
try {
// check destination chain gas limit
const dstGasLimit = await publicClient.readContract({
address: bridgeAddress,
abi: CAKE_PROXY_OFT_ABI,
functionName: 'minDstGasLookup',
args: [dstEndpoint, 0],
});
const gasLimit =
dstGasLimit !== 0n && !!dstGasLimit ? dstGasLimit : gasAmount;
const address32Bytes = pad(userAddress, { size: 32 });
const adapterParams =
version === 1
? encodePacked(['uint16', 'uint256'], [version, gasAmount])
? encodePacked(['uint16', 'uint256'], [version, gasLimit])
: encodePacked(
['uint16', 'uint', 'uint', 'address'],
[2, gasAmount, airDropGas, dstAddress]
[2, gasLimit, airDropGas, dstAddress]
);
const fees = await publicClient.readContract({
address: bridgeAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ export const useGetLayerZeroFees = () => {
);

const address32Bytes = pad(address || DEFAULT_ADDRESS, { size: 32 });
const adapterParams = encodePacked(['uint16', 'uint256'], [1, 200000n]);
const dstGasLimit = await publicClient.readContract({
address: bridgeAddress,
abi: CAKE_PROXY_OFT_ABI,
functionName: 'minDstGasLookup',
args: [toTokenInfo?.layerZero?.raw?.endpointID, 0],
});
const gasLimit = dstGasLimit !== 0n && !!dstGasLimit ? dstGasLimit : 200000n;
const adapterParams = encodePacked(['uint16', 'uint256'], [1, gasLimit]);
const callParams = [
address,
'0x0000000000000000000000000000000000000000', // zroPaymentAddress
Expand Down
Loading