瀏覽代碼

merkly task added

hel 1 年之前
父節點
當前提交
edf4839446
共有 2 個文件被更改,包括 69 次插入3 次删除
  1. 51 0
      src/bridge/MerklyClient.ts
  2. 18 3
      src/jobs/BridgeJob.ts

+ 51 - 0
src/bridge/MerklyClient.ts

@@ -0,0 +1,51 @@
+import { Contract, ethers } from 'ethers'
+import { ChainId, chainInfoMap } from '../config/chain'
+import { newLogger } from '../utils/logger'
+
+export class MerklyClient {
+  wallet: ethers.Wallet
+  chainId: number
+  provider: ethers.JsonRpcProvider
+  router: ethers.Contract
+  static logger = newLogger('MerklyClient')
+
+  constructor(privateKey: string, chainId: number) {
+    this.chainId = chainId
+    this.provider = new ethers.JsonRpcProvider(chainInfoMap[chainId].rpcUrl)
+    this.wallet = new ethers.Wallet(privateKey, this.provider)
+    this.router = new Contract(
+      chainInfoMap[chainId].merklyInfo.routerAddress,
+      [
+        'function quoteBridge(uint32 _destination, uint amount) external view returns (uint fee)',
+        'function bridgeETH(uint32 _destination, uint amount) public payable returns (bytes32 messageId)',
+        'function bridgeWETH(uint32 _destination, uint amount) public payable returns (bytes32 messageId)',
+      ],
+      this.wallet,
+    )
+  }
+
+  async bridge(toChainId: number = ChainId.ARBITRUM) {
+    const toInfo = chainInfoMap[toChainId].merklyInfo
+    if (!toInfo) {
+      throw new Error('Unsupported chain')
+    }
+    const balance = await this.provider.getBalance(this.wallet.address)
+    const quote = await this.router.quoteBridge(toInfo.merklyChainId, balance)
+    console.log('quote', ethers.formatEther(quote))
+    console.log('balance', ethers.formatEther(balance))
+    if (balance > quote) {
+      const feeData = await this.provider.getFeeData()
+      const estimatedGas = await this.router.bridgeETH.estimateGas(toInfo.merklyChainId, balance - quote, {
+        value: balance,
+      })
+      const gasCost = (feeData.gasPrice * estimatedGas * 108n) / 100n
+      console.log('gasCost', ethers.formatEther(gasCost))
+      const tx = await this.router.bridgeETH(toInfo.merklyChainId, balance - quote - gasCost, {
+        value: balance - gasCost,
+        gasLimit: (estimatedGas * 105n) / 100n,
+        gasPrice: feeData.gasPrice,
+      })
+      return tx.hash
+    }
+  }
+}

+ 18 - 3
src/jobs/BridgeJob.ts

@@ -5,6 +5,7 @@ import { ChainId } from '../config/chain'
 import { StargateClient } from '../bridge/StargateClient'
 import { OwltoClient } from '../bridge/OwltoClient'
 import { forEachAsync } from '../utils'
+import { MerklyClient } from '../bridge/MerklyClient'
 
 export class BridgeJob extends CronJob {
   constructor() {
@@ -66,11 +67,25 @@ export class BridgeJob extends CronJob {
     }
   }
 
+  merklyTask(task: Task) {
+    return (
+      task.fromChain !== ChainId.LINEA &&
+      task.toChain !== ChainId.LINEA &&
+      task.fromChain !== ChainId.ZKSYNC &&
+      task.toChain !== ChainId.ZKSYNC
+    )
+  }
+
   private async bridge(privateKey: string, task: Task) {
     if (task.toChain !== ChainId.ZKSYNC) {
-      // use stargate to bridge via LayerZero
-      const client = new StargateClient(privateKey, task.fromChain)
-      return client.bridge(task.toChain)
+      if (this.merklyTask(task) && Math.random() < 0.5) {
+        const client = new MerklyClient(privateKey, task.fromChain)
+        return client.bridge(task.toChain)
+      } else {
+        // use stargate to bridge via LayerZero
+        const client = new StargateClient(privateKey, task.fromChain)
+        return client.bridge(task.toChain)
+      }
     } else {
       // use Owlto to bridge to ZKSync
       const client = new OwltoClient(privateKey, task.fromChain)