瀏覽代碼

optimize gas estimation

Shawn Lu 1 年之前
父節點
當前提交
3efc12c01c
共有 1 個文件被更改,包括 8 次插入6 次删除
  1. 8 6
      src/bridge/StargateClient.ts

+ 8 - 6
src/bridge/StargateClient.ts

@@ -46,20 +46,22 @@ export class StargateClient {
 
 
   async bridge(toChainId: number) {
   async bridge(toChainId: number) {
     const routerEth = RouterETH__factory.connect(chainInfoMap[this.chainId].ethRouterAddress, this.wallet)
     const routerEth = RouterETH__factory.connect(chainInfoMap[this.chainId].ethRouterAddress, this.wallet)
-    const balance = await this.provider.getBalance(this.wallet.address)
     const lzGasCost = await this.getL0GasCost(toChainId)
     const lzGasCost = await this.getL0GasCost(toChainId)
-    // the cost is not precisely calculated, should be sufficient for common L2s
     const { gasCost, gasPrice, gasLimit } = await this.calculateGasCost(toChainId, routerEth, lzGasCost)
     const { gasCost, gasPrice, gasLimit } = await this.calculateGasCost(toChainId, routerEth, lzGasCost)
-    if (balance < gasCost + lzGasCost) throw new Error('Insufficient balance')
+    let cost = gasCost
     return await polly()
     return await polly()
       .retry(5)
       .retry(5)
       .executeForPromise(async info => {
       .executeForPromise(async info => {
         try {
         try {
+          const balance = await this.provider.getBalance(this.wallet.address)
+          // the cost is not precisely calculated, should be sufficient for common L2s
+          if (balance < gasCost + lzGasCost) throw new Error('Insufficient balance')
           if (info.count > 0) {
           if (info.count > 0) {
             StargateClient.logger.info(`${this.wallet.address}: Retry ${info.count} times`)
             StargateClient.logger.info(`${this.wallet.address}: Retry ${info.count} times`)
           }
           }
-          // add 10% gas cost for each retry
-          const sendAmount = balance - lzGasCost - (gasCost * (100n + BigInt(info.count * 20))) / 100n
+          // add 30% gas cost for each retry
+          cost = (cost * 120n) / 100n
+          const sendAmount = balance - lzGasCost - cost
           // const sendAmount = ethers.parseEther('0.01')
           // const sendAmount = ethers.parseEther('0.01')
           const minReceiveAmount = (sendAmount * 995n) / 1000n
           const minReceiveAmount = (sendAmount * 995n) / 1000n
           const res = await routerEth.swapETH(
           const res = await routerEth.swapETH(
@@ -96,7 +98,7 @@ export class StargateClient {
         value: amount + lzGasCost,
         value: amount + lzGasCost,
       },
       },
     )
     )
-    const gasCost = ((this.chainId === 110 ? gasPrice : ethers.parseUnits('0.55', 'gwei')) * gasLimit * 110n) / 100n
+    const gasCost = ((this.chainId === 110 ? gasPrice : ethers.parseUnits('0.6', 'gwei')) * gasLimit * 110n) / 100n
     return { gasCost, gasPrice, gasLimit }
     return { gasCost, gasPrice, gasLimit }
   }
   }
 }
 }