Skip to content

Commit

Permalink
Merge pull request #151 from bnb-chain/cake/gasLimit1129
Browse files Browse the repository at this point in the history
fix: Check layerzero destination gas limit before sending tokens
  • Loading branch information
Halibao-Lala authored Dec 3, 2024
2 parents 2723b11 + 792cae1 commit c891340
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
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

0 comments on commit c891340

Please sign in to comment.