xiongzhu преди 2 години
родител
ревизия
d748c524ac

+ 6 - 5
src/accounts/accounts.controller.ts

@@ -1,20 +1,21 @@
 import { AccountsService } from './accounts.service'
-import { Body, Controller, Delete, Get, Param, Put, Req } from '@nestjs/common'
+import { Body, Controller, Delete, Get, Param, Put, Query, Req } from '@nestjs/common'
 import { Account } from './entities/account.entity'
+import { Network } from '../web3/network.enum'
 
 @Controller('accounts')
 export class AccountsController {
     constructor(private readonly accountsService: AccountsService) {}
 
     @Get('/my')
-    public async my(@Req() req) {
-        return await this.accountsService.findByUserId(req.user.userId)
+    public async my(@Req() req, @Query('network') network: Network) {
+        return await this.accountsService.findByUserId(req.user.userId, network)
     }
 
     @Put()
     public async save(@Body() accounts: Account | Account[], @Req() req) {
-        if(!Array.isArray(accounts)) accounts = [accounts]
-        accounts.forEach(a => a.userId = req.user.userId)
+        if (!Array.isArray(accounts)) accounts = [accounts]
+        accounts.forEach((a) => (a.userId = req.user.userId))
         return await this.accountsService.save(accounts)
     }
 

+ 4 - 2
src/accounts/accounts.service.ts

@@ -2,6 +2,7 @@ import { Injectable, InternalServerErrorException } from '@nestjs/common'
 import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'
 import { Account } from './entities/account.entity'
 import { Repository } from 'typeorm'
+import { Network } from '../web3/network.enum'
 
 @Injectable()
 export class AccountsService {
@@ -11,9 +12,10 @@ export class AccountsService {
         return await this.accountsRepository.save(account)
     }
 
-    async findByUserId(userId: number) {
+    async findByUserId(userId: number, network: Network) {
         return await this.accountsRepository.findBy({
-            userId
+            userId,
+            network
         })
     }
 

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

@@ -1,3 +1,4 @@
+import { Network } from '../../web3/network.enum'
 import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
 
 @Entity()
@@ -16,4 +17,7 @@ export class Account {
 
     @Column()
     userId: number
+
+    @Column({ type: 'enum', enum: Network, default: Network.Testnet })
+    network: Network
 }

+ 4 - 0
src/web3/network.enum.ts

@@ -0,0 +1,4 @@
+export enum Network {
+    Mainnet = 'mainnet',
+    Testnet = 'testnet'
+}

+ 7 - 6
src/web3/web3.controller.ts

@@ -1,5 +1,6 @@
 import { Body, Controller, Post } from '@nestjs/common'
 import { Web3Service } from './web3.service'
+import { Network } from './network.enum';
 
 @Controller('web3')
 export class Web3Controller {
@@ -7,39 +8,39 @@ export class Web3Controller {
 
     @Post('/zk-deposit')
     public async zkDeposit(
-        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: NetworkType }
+        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: Network }
     ) {
         return await this.web3Service.zkDeposit(accountId, amount, network)
     }
 
     @Post('/zk-withdraw')
     public async zkWidthdraw(
-        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: NetworkType }
+        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: Network }
     ) {
         return await this.web3Service.zkWidthdraw(accountId, amount, network)
     }
 
     @Post('/add-liquidity')
     public async addLiquidity(
-        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: NetworkType }
+        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: Network }
     ) {
         return await this.web3Service.addLiquidity(accountId, amount, network)
     }
 
     @Post('/remove-liquidity')
-    public async removeLiquidity(@Body() { accountId, network }: { accountId: string; network: NetworkType }) {
+    public async removeLiquidity(@Body() { accountId, network }: { accountId: string; network: Network }) {
         return await this.web3Service.removeLiquidity(accountId, network)
     }
 
     @Post('/swap-exact-out')
     public async swap(
-        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: NetworkType }
+        @Body() { accountId, amount, network }: { accountId: string; amount: string; network: Network }
     ) {
         return await this.web3Service.swapWithExactOutput(accountId, amount, network)
     }
 
     @Post('/mint')
-    public async mint(@Body() { accountId, network }: { accountId: string; network: NetworkType }) {
+    public async mint(@Body() { accountId, network }: { accountId: string; network: Network }) {
         return await this.web3Service.mint(accountId, network)
     }
 }

+ 9 - 8
src/web3/web3.service.ts

@@ -35,6 +35,7 @@ import { getPoolContract, getPoolState, getPointDelta } from 'iziswap-sdk/lib/po
 import { getQuoterContract, quoterSwapChainWithExactOutput } from 'iziswap-sdk/lib/quoter'
 import { getSwapContract, getSwapChainWithExactOutputCall } from 'iziswap-sdk/lib/swap'
 import mintSquareAbi from './mintSquareAbi.json'
+import { Network } from './network.enum'
 
 @Injectable()
 export class Web3Service {
@@ -71,7 +72,7 @@ export class Web3Service {
         private readonly accountService: AccountsService
     ) {}
 
-    async zkDeposit(accountId, amount, network: NetworkType) {
+    async zkDeposit(accountId, amount, network: Network) {
         const account = await this.accountService.findById(accountId)
         const provider = new Provider(this.config[network].zksyncRpcUrl)
         const ethProvider = new ethers.providers.InfuraProvider(
@@ -102,7 +103,7 @@ export class Web3Service {
         // Logger.log('finalizedEthBalance', ethers.utils.formatEther(finalizedEthBalance))
     }
 
-    async zkWidthdraw(accountId, amount, network: NetworkType) {
+    async zkWidthdraw(accountId, amount, network: Network) {
         const account = await this.accountService.findById(accountId)
         const provider = new Provider(this.config[network].zksyncRpcUrl)
         const ethProvider = new ethers.providers.InfuraProvider(
@@ -133,7 +134,7 @@ export class Web3Service {
         // Logger.log('finalizedEthBalance', ethers.utils.formatEther(finalizedEthBalance))
     }
 
-    async addLiquidity(accountId, amount, network: NetworkType) {
+    async addLiquidity(accountId, amount, network: Network) {
         const chainId = this.config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
         const account = await this.accountService.findById(accountId)
         const provider = new Provider(this.config[network].zksyncRpcUrl)
@@ -225,7 +226,7 @@ export class Web3Service {
         console.log('tx: ', tx)
     }
 
-    async removeLiquidity(accountId, network: NetworkType) {
+    async removeLiquidity(accountId, network: Network) {
         const chainId = this.config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
         const account = await this.accountService.findById(accountId)
         const provider = new Provider(this.config[network].zksyncRpcUrl)
@@ -292,7 +293,7 @@ export class Web3Service {
         console.log('decLiquidityCalling tx: ', JSON.stringify(tx0, null, 4))
     }
 
-    async allowance(tokenAddress, spender, wallet: Wallet, network: NetworkType): Promise<number> {
+    async allowance(tokenAddress, spender, wallet: Wallet, network: Network): Promise<number> {
         const web3 = new Web3(new Web3.providers.HttpProvider(this.config[network].zksyncRpcUrl))
         const tokenAContract = new web3.eth.Contract(erc20, tokenAddress)
         // @ts-ignore
@@ -302,7 +303,7 @@ export class Web3Service {
         return out as unknown as number
     }
 
-    async approve(tokenAddress, spender, wallet: Wallet, network: NetworkType) {
+    async approve(tokenAddress, spender, wallet: Wallet, network: Network) {
         const allowance = await this.allowance(tokenAddress, spender, wallet, network)
         if (allowance > 0) {
             return
@@ -323,7 +324,7 @@ export class Web3Service {
         console.log('approve tx: ', JSON.stringify(tx0, null, 4))
     }
 
-    async swapWithExactOutput(accountId, amount, network: NetworkType) {
+    async swapWithExactOutput(accountId, amount, network: Network) {
         const account = await this.accountService.findById(accountId)
         const chainId = this.config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
         const chain = initialChainTable[chainId]
@@ -395,7 +396,7 @@ export class Web3Service {
         console.log(tx)
     }
 
-    async mint(accountId, network: NetworkType) {
+    async mint(accountId, network: Network) {
         const account = await this.accountService.findById(accountId)
         const chainId = this.config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
         const chain = initialChainTable[chainId]