xiongzhu il y a 2 ans
Parent
commit
47be70db7b

+ 1 - 0
src/accounts/accounts.service.ts

@@ -46,6 +46,7 @@ export class AccountsService {
     @Cron('0/30 * * * * *')
     async handleCron() {
         for (let account of await this.findAll()) {
+            Logger.log(`fetching balances for ${account.address}`)
             const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
             const ethProvider = new ethers.providers.InfuraProvider(
                 web3Config[account.network].ethereumNetwork,

+ 12 - 0
src/accounts/entities/account.entity.ts

@@ -35,4 +35,16 @@ export class Account {
 
     @Column()
     zkUsdcBalance: string
+
+    @Column()
+    swapNum: number
+
+    @Column()
+    bridgeNum: number
+
+    @Column()
+    liuidityNum: number
+
+    @Column()
+    mintNum: number
 }

+ 4 - 0
src/app.module.ts

@@ -18,6 +18,8 @@ import { GalleryModule } from './gallery/gallery.module'
 import { AccountsModule } from './accounts/accounts.module'
 import { Web3Module } from './web3/web3.module'
 import { ScheduleModule } from '@nestjs/schedule'
+import { TaskModule } from './task/task.module';
+import { LogsModule } from './logs/logs.module';
 @Module({
     imports: [
         DevtoolsModule.register({
@@ -74,6 +76,8 @@ import { ScheduleModule } from '@nestjs/schedule'
         GalleryModule,
         AccountsModule,
         Web3Module,
+        TaskModule,
+        LogsModule,
     ],
     controllers: [],
     providers: [

+ 25 - 0
src/task/entities/task.entity.ts

@@ -0,0 +1,25 @@
+import { Column, CreateDateColumn, PrimaryGeneratedColumn } from 'typeorm'
+import { TaskStatus } from '../enums/task-status.enum'
+
+export class Task {
+    @PrimaryGeneratedColumn()
+    id: number
+
+    @Column()
+    userId: number
+
+    @Column()
+    type: string
+
+    @Column({ type: 'enum', enum: TaskStatus })
+    status: TaskStatus
+
+    @CreateDateColumn()
+    createdAt: Date
+
+    @Column({ type: 'text' })
+    accounts: number[]
+
+    @Column()
+    progress: number
+}

+ 5 - 0
src/task/enums/task-status.enum.ts

@@ -0,0 +1,5 @@
+export enum TaskStatus {
+    Pending = 'pending',
+    InProgress = 'in_progress',
+    Done = 'done'
+}

+ 4 - 0
src/task/task.controller.ts

@@ -0,0 +1,4 @@
+import { Controller } from '@nestjs/common';
+
+@Controller('task')
+export class TaskController {}

+ 11 - 0
src/task/task.module.ts

@@ -0,0 +1,11 @@
+import { Module } from '@nestjs/common'
+import { TaskService } from './task.service'
+import { TaskController } from './task.controller'
+import { Web3Module } from 'src/web3/web3.module'
+
+@Module({
+    imports: [Web3Module],
+    providers: [TaskService],
+    controllers: [TaskController]
+})
+export class TaskModule {}

+ 9 - 0
src/task/task.service.ts

@@ -0,0 +1,9 @@
+import { Injectable } from '@nestjs/common'
+import { Web3Service } from 'src/web3/web3.service'
+
+@Injectable()
+export class TaskService {
+    constructor(web3Service: Web3Service) {}
+
+    
+}

+ 2 - 1
src/web3/web3.module.ts

@@ -6,6 +6,7 @@ import { AccountsModule } from '../accounts/accounts.module'
 @Module({
     imports: [AccountsModule],
     providers: [Web3Service],
-    controllers: [Web3Controller]
+    controllers: [Web3Controller],
+    exports: [Web3Service]
 })
 export class Web3Module {}

+ 55 - 55
src/web3/web3.service.ts

@@ -47,12 +47,12 @@ import { Cron } from '@nestjs/schedule'
 export class Web3Service {
     constructor(private readonly accountService: AccountsService) {}
 
-    async zkDeposit(accountId, amount, network: Network) {
+    async zkDeposit(accountId, amount) {
         const account = await this.accountService.findById(accountId)
-        const provider = new Provider(web3Config[network].zksyncRpcUrl)
+        const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
         const ethProvider = new ethers.providers.InfuraProvider(
-            web3Config[network].ethereumNetwork,
-            web3Config[network].infuraApiKey
+            web3Config[account.network].ethereumNetwork,
+            web3Config[account.network].infuraApiKey
         )
         const wallet = new Wallet(account.privateKey, provider, ethProvider)
         const deposit = await wallet.deposit({
@@ -78,19 +78,19 @@ export class Web3Service {
         // Logger.log('finalizedEthBalance', ethers.utils.formatEther(finalizedEthBalance))
     }
 
-    async zkWithdraw(accountId, amount, network: Network) {
+    async zkWithdraw(accountId, amount) {
         const account = await this.accountService.findById(accountId)
-        const provider = new Provider(web3Config[network].zksyncRpcUrl)
+        const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
         const ethProvider = new ethers.providers.InfuraProvider(
-            web3Config[network].ethereumNetwork,
-            web3Config[network].infuraApiKey
+            web3Config[account.network].ethereumNetwork,
+            web3Config[account.network].infuraApiKey
         )
         const wallet = new Wallet(account.privateKey, provider, ethProvider)
         const withdrawL2 = await wallet.withdraw({
             token: utils.ETH_ADDRESS,
             amount: ethers.utils.parseEther(amount)
         })
-        Logger.log(`Widthdraw sent. ${web3Config[network].zksyncExplorer}/tx/${withdrawL2.hash}`)
+        Logger.log(`Widthdraw sent. ${web3Config[account.network].zksyncExplorer}/tx/${withdrawL2.hash}`)
         // // Await processing of the deposit on L1
         // const ethereumTxReceipt = await deposit.waitL1Commit()
 
@@ -109,17 +109,17 @@ export class Web3Service {
         // Logger.log('finalizedEthBalance', ethers.utils.formatEther(finalizedEthBalance))
     }
 
-    async orbiterDeposit(accountId, amount, network: Network) {
+    async orbiterDeposit(accountId, amount) {
         const account = await this.accountService.findById(accountId)
         const provider = new ethers.providers.InfuraProvider(
-            web3Config[network].ethereumNetwork,
-            web3Config[network].infuraApiKey
+            web3Config[account.network].ethereumNetwork,
+            web3Config[account.network].infuraApiKey
         )
         const wallet = new ethers.Wallet(account.privateKey, provider)
         const tx = await wallet.sendTransaction({
             from: wallet.address,
-            to: web3Config[network].orbiterEthAddress,
-            value: ethers.utils.parseEther(amount).add(web3Config[network].orbiterIdCodeZk)
+            to: web3Config[account.network].orbiterEthAddress,
+            value: ethers.utils.parseEther(amount).add(web3Config[account.network].orbiterIdCodeZk)
         })
         console.log('Mining transaction...')
         // Waiting for the transaction to be mined
@@ -127,31 +127,31 @@ export class Web3Service {
         console.log('Transaction mined!', receipt)
     }
 
-    async orbiterWithdraw(accountId, amount, network: Network) {
+    async orbiterWithdraw(accountId, amount) {
         throw new Error('Method not implemented.')
     }
 
-    async addLiquidity(accountId, amount, network: Network) {
-        const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
+    async addLiquidity(accountId, amount) {
         const account = await this.accountService.findById(accountId)
-        const provider = new Provider(web3Config[network].zksyncRpcUrl)
+        const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
+        const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
         const zkWallet = new Wallet(account.privateKey).connect(provider)
         const chain = initialChainTable[chainId]
-        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
+        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
         console.log('address: ', zkWallet.address)
 
         const liquidityManagerContract = getLiquidityManagerContract(
-            web3Config[network].liquidityManagerAddress,
+            web3Config[account.network].liquidityManagerAddress,
             web3 as any
         )
-        console.log('liquidity manager address: ', web3Config[network].liquidityManagerAddress)
+        console.log('liquidity manager address: ', web3Config[account.network].liquidityManagerAddress)
 
         const tokenA = getGasToken(chainId)
         console.log('tokenA: ', tokenA)
-        const tokenB = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
+        const tokenB = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
         console.log('tokenB: ', tokenB)
 
-        await this.approve(tokenB.address, web3Config[network].liquidityManagerAddress, zkWallet, network)
+        await this.approve(tokenB.address, web3Config[account.network].liquidityManagerAddress, zkWallet, account.network)
 
         const fee = 2000 // 2000 means 0.2%
         const poolAddress = await getPoolAddress(liquidityManagerContract, tokenA, tokenB, fee)
@@ -216,29 +216,29 @@ export class Web3Service {
         const tx = await zkWallet.sendTransaction({
             from: options.from,
             value: Web3.utils.numberToHex(options.value),
-            to: web3Config[network].liquidityManagerAddress,
+            to: web3Config[account.network].liquidityManagerAddress,
             data: calling.encodeABI(),
             gasLimit
         })
         console.log('tx: ', tx)
     }
 
-    async removeLiquidity(accountId, network: Network) {
-        const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
+    async removeLiquidity(accountId) {
         const account = await this.accountService.findById(accountId)
-        const provider = new Provider(web3Config[network].zksyncRpcUrl)
+        const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
+        const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
         const wallet = new Wallet(account.privateKey).connect(provider)
         const chain = initialChainTable[chainId]
-        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
+        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
 
         const liquidityManagerContract = getLiquidityManagerContract(
-            web3Config[network].liquidityManagerAddress,
+            web3Config[account.network].liquidityManagerAddress,
             web3 as any
         )
-        Logger.log('liquidity manager address: ', web3Config[network].liquidityManagerAddress)
+        Logger.log('liquidity manager address: ', web3Config[account.network].liquidityManagerAddress)
 
         const tokenA = getGasToken(chainId)
-        const tokenB = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
+        const tokenB = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
 
         Logger.log(`tokenA: ${tokenA.symbol} tokenB: ${tokenB.symbol}`, 'FetchLiquidities')
         const liquidities = await fetchLiquiditiesOfAccount(
@@ -283,7 +283,7 @@ export class Web3Service {
         console.log('gas limit: ', gasLimit)
         const tx0 = await wallet.sendTransaction({
             from: wallet.address,
-            to: web3Config[network].liquidityManagerAddress,
+            to: web3Config[account.network].liquidityManagerAddress,
             data: decLiquidityCalling.encodeABI(),
             gasLimit
         })
@@ -321,13 +321,13 @@ export class Web3Service {
         console.log('approve tx: ', JSON.stringify(tx0, null, 4))
     }
 
-    async swapWithExactOutput(accountId, amount, network: Network) {
+    async swapWithExactOutput(accountId, amount) {
         const account = await this.accountService.findById(accountId)
-        const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
+        const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
         const chain = initialChainTable[chainId]
-        const provider = new Provider(web3Config[network].zksyncRpcUrl)
+        const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
         const wallet = new Wallet(account.privateKey).connect(provider)
-        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
+        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
         console.log('address: ', wallet.address)
 
         const balance = await web3.eth.getBalance(wallet.address)
@@ -335,7 +335,7 @@ export class Web3Service {
 
         const fromToken = getGasToken(chainId)
         console.log('fromToken: ', fromToken)
-        const toToken = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
+        const toToken = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
         console.log('toToken: ', toToken)
         const fee = 2000 // 2000 means 0.2%
 
@@ -344,7 +344,7 @@ export class Web3Service {
         const toTokenBalance = await toTokenContract.methods.balanceOf(wallet.address).call()
         console.log(toToken.symbol + ' balance: ' + amount2Decimal(new BigNumber(toTokenBalance), toToken))
 
-        const quoterContract = getQuoterContract(web3Config[network].quoterAddress, web3 as any)
+        const quoterContract = getQuoterContract(web3Config[account.network].quoterAddress, web3 as any)
 
         const receiveAmount = new BigNumber(amount).times(10 ** toToken.decimal)
 
@@ -363,7 +363,7 @@ export class Web3Service {
         console.log(toToken.symbol + ' to desired: ', amount)
         console.log(fromToken.symbol + ' to pay: ', payAmountDecimal)
 
-        const swapContract = getSwapContract(web3Config[network].swapAddress, web3 as any)
+        const swapContract = getSwapContract(web3Config[account.network].swapAddress, web3 as any)
 
         const swapParams = {
             ...params,
@@ -387,29 +387,29 @@ export class Web3Service {
         const tx = await wallet.sendTransaction({
             value: Web3.utils.numberToHex(options.value),
             data: swapCalling.encodeABI(),
-            to: web3Config[network].swapAddress,
+            to: web3Config[account.network].swapAddress,
             gasLimit
         })
         console.log(tx)
     }
 
-    async swapWithExactInput(accountId, amount, network: Network) {
+    async swapWithExactInput(accountId, amount) {
         const account = await this.accountService.findById(accountId)
-        const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
+        const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
         const chain = initialChainTable[chainId]
-        const provider = new Provider(web3Config[network].zksyncRpcUrl)
+        const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
         const wallet = new Wallet(account.privateKey).connect(provider)
-        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
+        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
         console.log('address: ', wallet.address)
 
-        const fromToken = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
+        const fromToken = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
         console.log('fromToken: ', fromToken)
         const toToken = getGasToken(chainId)
         console.log('toToken: ', toToken)
 
-        await this.approve(fromToken.address, web3Config[network].swapAddress, wallet, network)
+        await this.approve(fromToken.address, web3Config[account.network].swapAddress, wallet, account.network)
 
-        const quoterContract = getQuoterContract(web3Config[network].quoterAddress, web3 as any)
+        const quoterContract = getQuoterContract(web3Config[account.network].quoterAddress, web3 as any)
 
         const payAmount = new BigNumber(amount).times(10 ** fromToken.decimal)
         const fee = 2000 // 2000 means 0.2%
@@ -428,7 +428,7 @@ export class Web3Service {
         console.log(fromToken.symbol + ' to pay: ', amount)
         console.log(toToken.symbol + ' to acquire: ', receiveAmountDecimal)
 
-        const swapContract = getSwapContract(web3Config[network].swapAddress, web3 as any)
+        const swapContract = getSwapContract(web3Config[account.network].swapAddress, web3 as any)
 
         const swapParams = {
             ...params,
@@ -450,29 +450,29 @@ export class Web3Service {
         const tx = await wallet.sendTransaction({
             value: Web3.utils.numberToHex(options.value),
             data: swapCalling.encodeABI(),
-            to: web3Config[network].swapAddress,
+            to: web3Config[account.network].swapAddress,
             gasLimit
         })
         console.log(JSON.stringify(tx, null, 4))
     }
 
-    async mint(accountId, network: Network) {
+    async mint(accountId) {
         const account = await this.accountService.findById(accountId)
-        const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
+        const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
         const chain = initialChainTable[chainId]
-        const provider = new Provider(web3Config[network].zksyncRpcUrl)
+        const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
         const wallet = new Wallet(account.privateKey).connect(provider)
-        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
+        const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
         console.log('address: ', wallet.address)
 
-        const contract = new web3.eth.Contract(mintSquareAbi, web3Config[network].mintAddress, web3 as any)
+        const contract = new web3.eth.Contract(mintSquareAbi, web3Config[account.network].mintAddress, web3 as any)
         // @ts-ignore
         const mintCall = await contract.methods.mint('ipfs://QmPxcRPvjMDrv7d4WjQK5KNXkuXudGhKjPTDnzuFkRQ6Fs')
         const gasLimit = await mintCall.estimateGas({ from: wallet.address })
         console.log('gas limit: ', gasLimit)
         const tx = await wallet.sendTransaction({
             from: wallet.address,
-            to: web3Config[network].mintAddress,
+            to: web3Config[account.network].mintAddress,
             data: mintCall.encodeABI(),
             gasLimit
         })