xiongzhu 2 ani în urmă
părinte
comite
a04dee4117
4 a modificat fișierele cu 47 adăugiri și 26 ștergeri
  1. 2 0
      src/task/enums/task-type.enum.ts
  2. 15 0
      src/task/task.service.ts
  3. 5 12
      src/utils/common.ts
  4. 25 14
      src/web3/web3.service.ts

+ 2 - 0
src/task/enums/task-type.enum.ts

@@ -9,5 +9,7 @@ export enum TaskType {
     syncSwapEth = 'syncSwapEth',
     addLiquidity = 'addLiquidity',
     removeLiquidity = 'removeLiquidity',
+    addLiquiditySyncSwap = 'addLiquiditySyncSwap',
+    removeLiquiditySyncSwap = 'removeLiquiditySyncSwap',
     mint = 'mint'
 }

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

@@ -215,6 +215,21 @@ export class TaskService implements OnModuleInit {
                         case TaskType.removeLiquidity:
                             web3Result = await this.web3Service.removeLiquidity(accountId)
                             break
+                        case TaskType.addLiquiditySyncSwap:
+                            web3Result = await this.web3Service.addLiquiditySyncSwap(
+                                accountId,
+                                taskItem.amountType === 'amount'
+                                    ? randomAmount(taskItem.amount.min, taskItem.amount.max)
+                                    : randomBalancePercent(
+                                          await this.accountsService.getZkBalance(account),
+                                          taskItem.percent.min,
+                                          taskItem.percent.max
+                                      )
+                            )
+                            break
+                        case TaskType.removeLiquiditySyncSwap:
+                            web3Result = await this.web3Service.removeLiquiditySyncSwap(accountId)
+                            break
                         case TaskType.mint:
                             web3Result = await this.web3Service.mint(accountId)
                             break

+ 5 - 12
src/utils/common.ts

@@ -1,4 +1,5 @@
 import BigNumber from 'bignumber.js'
+BigNumber.config({ EXPONENTIAL_AT: 1e9 })
 export function hideSensitiveData(str: string) {
     return str.replace(/(?<=^.).*(?=.$)/, '***')
 }
@@ -16,17 +17,9 @@ export function randomAmount(min, max, decimals = 6) {
 export function randomBalancePercent(balance, min, max) {
     let minNum = new BigNumber(min)
     let maxNum = new BigNumber(max)
-    let d = maxNum
-        .minus(minNum)
-        .times(balance)
-        .times(new BigNumber(10 ** 6))
-    let random = new BigNumber(Math.random()).times(d).integerValue()
-    const res = random
-        .dividedBy(new BigNumber(10 ** 6))
-        .plus(minNum)
-        .dividedBy(new BigNumber(100))
-        .times(balance)
-        .precision(6)
-        .toString()
+    let d = maxNum.minus(minNum)
+
+    let random = new BigNumber(Math.random()).times(d).plus(minNum)
+    const res = new BigNumber(balance).times(random).dividedBy(new BigNumber(100)).precision(6).toString()
     return res
 }

+ 25 - 14
src/web3/web3.service.ts

@@ -302,12 +302,13 @@ export class Web3Service {
         Logger.log(`poolAddress: ${poolAddress}`, 'addLiquiditySyncSwap')
 
         const router = new web3.eth.Contract(SyncSwapRouterAbi, config.syncSwapRouterAddress)
+        const value = ethers.utils.parseEther(amount).toHexString()
         const addLiquidity = router.methods.addLiquidity2(
             // @ts-ignore
             poolAddress,
             [
-                [ethers.constants.AddressZero, ethers.utils.parseEther(amount).toString()],
-                [config.zkUsdcAddress, ethers.utils.parseUnits('0', 6).toString()]
+                [ethers.constants.AddressZero, value],
+                [config.zkUsdcAddress, ethers.utils.parseUnits('0', 6).toHexString()]
             ],
             //encode abi
             ethers.utils.defaultAbiCoder.encode(['uint256'], [zkWallet.address]),
@@ -324,10 +325,13 @@ export class Web3Service {
             // from: wallet.address,
             to: config.syncSwapRouterAddress,
             data: addLiquidity.encodeABI(),
-            value: ethers.utils.parseEther(amount).toString(),
+            value: value,
             gasLimit
         })
         Logger.log(`transaction sent: ${tx.hash}`, 'addLiquiditySyncSwap')
+        account.addLiuidityNum = (account.addLiuidityNum || 0) + 1
+        account.lastAddLiuidity = new Date()
+        await this.accountService.save([account])
         return new Web3Result(account.address, tx.hash, config, true)
     }
 
@@ -370,6 +374,9 @@ export class Web3Service {
             gasLimit
         })
         Logger.log(`transaction sent: ${tx.hash}`, 'removeLiquiditySyncSwap')
+        account.removeLiuidityNum = (account.removeLiuidityNum || 0) + 1
+        account.lastRemoveLiuidity = new Date()
+        await this.accountService.save([account])
         return new Web3Result(account.address, tx.hash, config, true)
     }
 
@@ -650,15 +657,11 @@ export class Web3Service {
         const poolFactory = new web3.eth.Contract(SyncSwapPoolFactoryAbi, config.syncSwapPoolFactoryAddress)
         // @ts-ignore
         const poolAddress = await poolFactory.methods.getPool(config.syncSwapWethAddress, config.zkUsdcAddress).call()
-        Logger.log(`poolAddress: ${poolAddress}`, 'addLiquiditySyncSwap')
+        Logger.log(`poolAddress: ${poolAddress}`, 'swapSyncSwap')
 
         const router = new web3.eth.Contract(SyncSwapRouterAbi, config.syncSwapRouterAddress)
-        const value =
-            inputToken === 'eth'
-                ? ethers.utils.parseEther(amount).toString()
-                : ethers.utils.parseUnits(amount, 6).toString()
-        const swap = router.methods.swap(
-            // @ts-ignore
+        const value = inputToken === 'eth' ? ethers.utils.parseEther(amount) : ethers.utils.parseUnits(amount, 6)
+        const params = [
             [
                 [
                     [
@@ -677,12 +680,17 @@ export class Web3Service {
                         ]
                     ],
                     inputToken === 'eth' ? ethers.constants.AddressZero : config.zkUsdcAddress,
-                    value
+                    value.toHexString()
                 ]
             ],
             0,
-            ethers.constants.AddressZero,
-            '0x'
+            Math.floor(new Date().getTime() / 1000 + 60 * 30) + ''
+        ]
+        const swap = router.methods.swap(
+            // @ts-ignore
+            params[0],
+            params[1],
+            params[2]
         )
 
         const gasLimit = await swap.estimateGas({
@@ -692,10 +700,13 @@ export class Web3Service {
             // from: wallet.address,
             to: config.syncSwapRouterAddress,
             data: swap.encodeABI(),
-            value: value,
+            value: inputToken === 'eth' ? value : '0x00',
             gasLimit
         })
         Logger.log(`transaction sent: ${tx.hash}`, 'swapSyncSwap')
+        account.swapNum = (account.swapNum || 0) + 1
+        account.lastSwap = new Date()
+        await this.accountService.save([account])
         return new Web3Result(account.address, tx.hash, config, true)
     }