Shawn Lu il y a 1 an
Parent
commit
5788d13876

+ 7 - 5
src/bridge/StargateClient.ts

@@ -1,6 +1,6 @@
 import { ethers } from 'ethers'
 import { Router__factory, RouterETH, RouterETH__factory, StargateFeeLibraryV07__factory } from '../contract'
-import { chainInfoMap } from '../config/chain'
+import { ChainId, chainInfoMap } from '../config/chain'
 import polly from 'polly-js'
 import { newLogger } from '../utils/logger'
 
@@ -45,6 +45,7 @@ export class StargateClient {
   }
 
   async bridge(toChainId: number) {
+    if(toChainId === ChainId.ZKSYNC) throw new Error('Unsupported.')
     return await polly()
       .waitAndRetry([1000 * 60, 1000 * 60 * 2, 1000 * 60 * 3])
       .executeForPromise(async info => {
@@ -60,9 +61,10 @@ export class StargateClient {
           if (info.count > 0) {
             StargateClient.logger.info(`${this.wallet.address}: Retry ${info.count} times`)
           }
-          // add 30% gas cost for each retry
-          cost = (cost * 120n) / 100n
-          limit = (limit * 120n) / 100n
+          const rate = BigInt(info.count) * 30n + 100n
+          // add 20% gas cost for each retry
+          cost = (cost * rate) / 100n
+          limit = (limit * rate) / 100n
           const sendAmount = balance - lzGasCost - cost
           // const sendAmount = ethers.parseEther('0.01')
           const minReceiveAmount = (sendAmount * 995n) / 1000n
@@ -101,6 +103,6 @@ export class StargateClient {
       },
     )
     // const gasCost = ((this.chainId === 110 ? gasPrice : ethers.parseUnits('0.6', 'gwei')) * gasLimit * 110n) / 100n
-    return { gasCost: ethers.parseEther('0.0004'), gasPrice, gasLimit }
+    return { gasCost: ethers.parseEther('0.0001'), gasPrice, gasLimit: (gasLimit * 150n) / 100n }
   }
 }

+ 9 - 0
src/config/chain.ts

@@ -19,6 +19,7 @@ export enum ChainId {
   OPTIMISM = 111,
   BASE = 184,
   ZKSYNC = 324,
+  LINEA = 183,
 }
 
 export const chainInfoMap: { [chainId: number]: ChainInfo } = {
@@ -71,4 +72,12 @@ export const chainInfoMap: { [chainId: number]: ChainInfo } = {
       networkCode: 2,
     },
   },
+  [ChainId.LINEA]: {
+    name: 'Linea',
+    chainId: 183,
+    rpcUrl: 'https://1rpc.io/linea',
+    feeAddress: '0x45A01E4e04F14f7A4a6702c74187c5F6222033cd',
+    routerAddress: '0x2F6F07CDcf3588944Bf4C42aC74ff24bF56e7590',
+    ethRouterAddress: '0x8731d54E9D02c286767d56ac03e8037C07e01e98',
+  },
 }

+ 15 - 0
src/jobs/BaseInteractJob.ts

@@ -0,0 +1,15 @@
+import { CronJob } from './CronJob'
+import { PKMapping } from '../singletons'
+
+export class BaseInteractJob extends CronJob {
+  constructor() {
+    super('BaseInteract')
+  }
+
+  protected async run(): Promise<void> {
+    if (!PKMapping.instance.size) {
+      this.logger.warn('private key mapping is empty...')
+      return
+    }
+  }
+}

+ 9 - 9
src/jobs/CreateBridgeTaskJob.ts

@@ -1,5 +1,5 @@
 import { CronJob } from './CronJob'
-import { DBClient } from '../singletons'
+import { DBClient, PKMapping } from '../singletons'
 import { Account, TaskStatus } from '@prisma/client'
 import { ChainId } from '../config/chain'
 
@@ -33,7 +33,7 @@ export class CreateBridgeTaskJob extends CronJob {
         data: {
           Task: {
             create: {
-              scheduleTime: this.getRandomTime(14),
+              scheduleTime: this.getRandomTime(48),
               fromChain: account.currentChainId,
               toChain: this.getRandomDestination(account),
               status: TaskStatus.AWAITING,
@@ -51,13 +51,13 @@ export class CreateBridgeTaskJob extends CronJob {
   }
 
   private getRandomDestination(account: Account) {
-    let toZkSync = false
-    if (account.bridgeCount > 12 && account.bridgeCount < 18) {
-      toZkSync = Math.random() > 0.5
-    } else if (account.bridgeCount >= 18) {
-      toZkSync = true
-    }
-    if (toZkSync) return ChainId.ZKSYNC
+    // let toZkSync = false
+    // if (account.bridgeCount > 12 && account.bridgeCount < 18) {
+    //   toZkSync = Math.random() > 0.5
+    // } else if (account.bridgeCount >= 18) {
+    //   toZkSync = true
+    // }
+    // if (toZkSync) return ChainId.ZKSYNC
     const chainIds = Object.values(ChainId).filter(
       (value): value is number =>
         typeof value === 'number' && value !== account.currentChainId && value !== ChainId.ZKSYNC,

+ 18 - 2
src/main.ts

@@ -1,9 +1,10 @@
 import express from 'express'
-import { PKMapping } from './singletons'
+import { DBClient, PKMapping } from './singletons'
 import { ethers, getIndexedAccountPath } from 'ethers'
 import { CreateBridgeTaskJob } from './jobs/CreateBridgeTaskJob'
 import { BridgeJob } from './jobs/BridgeJob'
 import { ConfirmJob } from './jobs/ConfirmJob'
+import { ChainId } from './config/chain'
 const app = express()
 const port = 9900
 app.use(express.json())
@@ -17,7 +18,22 @@ app.post('/start', (req, res) => {
     const wallet = ethers.HDNodeWallet.fromPhrase(phrases, null, getIndexedAccountPath(i))
     PKMapping.instance.set(wallet.address, wallet.privateKey)
   }
-  res.send('OK')
+  res.status(200).send('OK')
+})
+
+app.post('/add', async (req, res) => {
+  const { pks } = req.body
+  for (const pk of pks) {
+    const wallet = new ethers.Wallet(pk)
+    PKMapping.instance.set(wallet.address, wallet.privateKey)
+    const account = await DBClient.instance.account.findFirst({ where: { address: wallet.address } })
+    if (!account) {
+      await DBClient.instance.account.create({
+        data: { address: wallet.address, bridgeCount: 0, currentChainId: ChainId.LINEA, lastUpdate: new Date() },
+      })
+    }
+  }
+  res.status(200).send(JSON.stringify({ processed: pks.length }))
 })
 
 app.listen(port, () => {