Răsfoiți Sursa

optimize gas estimation

Shawn Lu 1 an în urmă
părinte
comite
3efc12c01c
1 a modificat fișierele cu 8 adăugiri și 6 ștergeri
  1. 8 6
      src/bridge/StargateClient.ts

+ 8 - 6
src/bridge/StargateClient.ts

@@ -46,20 +46,22 @@ export class StargateClient {
 
   async bridge(toChainId: number) {
     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)
-    // the cost is not precisely calculated, should be sufficient for common L2s
     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()
       .retry(5)
       .executeForPromise(async info => {
         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) {
             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 minReceiveAmount = (sendAmount * 995n) / 1000n
           const res = await routerEth.swapETH(
@@ -96,7 +98,7 @@ export class StargateClient {
         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 }
   }
 }