|
@@ -18,6 +18,7 @@ import { setTimeout } from 'timers/promises'
|
|
|
import schedule from 'node-schedule'
|
|
import schedule from 'node-schedule'
|
|
|
import { SchedulerRegistry } from '@nestjs/schedule'
|
|
import { SchedulerRegistry } from '@nestjs/schedule'
|
|
|
import { CronJob } from 'cron'
|
|
import { CronJob } from 'cron'
|
|
|
|
|
+import { TaskItem } from './model/task-item.model'
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class TaskService implements OnModuleInit {
|
|
export class TaskService implements OnModuleInit {
|
|
|
constructor(
|
|
constructor(
|
|
@@ -81,6 +82,20 @@ export class TaskService implements OnModuleInit {
|
|
|
return task
|
|
return task
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ async getAmount(account, taskItem: TaskItem, token: 'eth' | 'zk' | 'usdc' = 'eth') {
|
|
|
|
|
+ return taskItem.amountType === 'amount'
|
|
|
|
|
+ ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
+ : randomBalancePercent(
|
|
|
|
|
+ token === 'eth'
|
|
|
|
|
+ ? await this.accountsService.getEthBalance(account)
|
|
|
|
|
+ : token === 'zk'
|
|
|
|
|
+ ? await this.accountsService.getZkBalance(account)
|
|
|
|
|
+ : await this.accountsService.getUsdcBalance(account),
|
|
|
|
|
+ taskItem.percent.min,
|
|
|
|
|
+ taskItem.percent.max
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async runTask(task: Task) {
|
|
async runTask(task: Task) {
|
|
|
if (task.status == TaskStatus.Done) {
|
|
if (task.status == TaskStatus.Done) {
|
|
|
return
|
|
return
|
|
@@ -94,6 +109,7 @@ export class TaskService implements OnModuleInit {
|
|
|
taskLog.accountId = accountId
|
|
taskLog.accountId = accountId
|
|
|
taskLog.startTime = new Date()
|
|
taskLog.startTime = new Date()
|
|
|
taskLog.type = taskItem.type
|
|
taskLog.type = taskItem.type
|
|
|
|
|
+ let amount: string
|
|
|
try {
|
|
try {
|
|
|
const account = await this.accountsService.findById(accountId)
|
|
const account = await this.accountsService.findById(accountId)
|
|
|
taskLog.address = account.address
|
|
taskLog.address = account.address
|
|
@@ -101,213 +117,77 @@ export class TaskService implements OnModuleInit {
|
|
|
let web3Result: Web3Result
|
|
let web3Result: Web3Result
|
|
|
switch (taskItem.type) {
|
|
switch (taskItem.type) {
|
|
|
case TaskType.bridge2zk:
|
|
case TaskType.bridge2zk:
|
|
|
- web3Result = await this.web3Service.zkDeposit(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getEthBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'eth')
|
|
|
|
|
+ web3Result = await this.web3Service.zkDeposit(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.bridge2eth:
|
|
case TaskType.bridge2eth:
|
|
|
- web3Result = await this.web3Service.zkWithdraw(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.zkWithdraw(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.orbiter2zk:
|
|
case TaskType.orbiter2zk:
|
|
|
- web3Result = await this.web3Service.orbiterDeposit(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getEthBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'eth')
|
|
|
|
|
+ web3Result = await this.web3Service.orbiterDeposit(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.orbiter2eth:
|
|
case TaskType.orbiter2eth:
|
|
|
- web3Result = await this.web3Service.orbiterWithdraw(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.orbiterWithdraw(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.swapUsdc:
|
|
case TaskType.swapUsdc:
|
|
|
- web3Result = await this.web3Service.swapWithExactInput(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'eth'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.swapWithExactInput(accountId, amount, 'eth')
|
|
|
break
|
|
break
|
|
|
case TaskType.swapEth:
|
|
case TaskType.swapEth:
|
|
|
- web3Result = await this.web3Service.swapWithExactInput(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getUsdcBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'usdc'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'usdc')
|
|
|
|
|
+ web3Result = await this.web3Service.swapWithExactInput(accountId, amount, 'usdc')
|
|
|
break
|
|
break
|
|
|
case TaskType.syncSwapUsdc:
|
|
case TaskType.syncSwapUsdc:
|
|
|
- web3Result = await this.web3Service.swapSyncSwap(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'eth'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.swapSyncSwap(accountId, amount, 'eth')
|
|
|
break
|
|
break
|
|
|
case TaskType.syncSwapEth:
|
|
case TaskType.syncSwapEth:
|
|
|
- web3Result = await this.web3Service.swapSyncSwap(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getUsdcBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'usdc'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'usdc')
|
|
|
|
|
+ web3Result = await this.web3Service.swapSyncSwap(accountId, amount, 'usdc')
|
|
|
break
|
|
break
|
|
|
case TaskType.muteSwapUsdc:
|
|
case TaskType.muteSwapUsdc:
|
|
|
- web3Result = await this.web3Service.muteSwap(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'eth'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.muteSwap(accountId, amount, 'eth')
|
|
|
break
|
|
break
|
|
|
case TaskType.muteSwapEth:
|
|
case TaskType.muteSwapEth:
|
|
|
- web3Result = await this.web3Service.muteSwap(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getUsdcBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'usdc'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'usdc')
|
|
|
|
|
+ web3Result = await this.web3Service.muteSwap(accountId, amount, 'usdc')
|
|
|
break
|
|
break
|
|
|
case TaskType.spacefiSwapUsdc:
|
|
case TaskType.spacefiSwapUsdc:
|
|
|
- web3Result = await this.web3Service.spacefiSwap(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'eth'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.spacefiSwap(accountId, amount, 'eth')
|
|
|
break
|
|
break
|
|
|
case TaskType.spacefiSwapEth:
|
|
case TaskType.spacefiSwapEth:
|
|
|
- web3Result = await this.web3Service.spacefiSwap(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getUsdcBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- ),
|
|
|
|
|
- 'usdc'
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'usdc')
|
|
|
|
|
+ web3Result = await this.web3Service.spacefiSwap(accountId, amount, 'usdc')
|
|
|
break
|
|
break
|
|
|
case TaskType.addLiquidity:
|
|
case TaskType.addLiquidity:
|
|
|
- web3Result = await this.web3Service.addLiquidity(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.addLiquidity(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.removeLiquidity:
|
|
case TaskType.removeLiquidity:
|
|
|
web3Result = await this.web3Service.removeLiquidity(accountId)
|
|
web3Result = await this.web3Service.removeLiquidity(accountId)
|
|
|
break
|
|
break
|
|
|
case TaskType.addLiquiditySyncSwap:
|
|
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
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.addLiquiditySyncSwap(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.removeLiquiditySyncSwap:
|
|
case TaskType.removeLiquiditySyncSwap:
|
|
|
web3Result = await this.web3Service.removeLiquiditySyncSwap(accountId)
|
|
web3Result = await this.web3Service.removeLiquiditySyncSwap(accountId)
|
|
|
break
|
|
break
|
|
|
case TaskType.addLiquidityMute:
|
|
case TaskType.addLiquidityMute:
|
|
|
- web3Result = await this.web3Service.addLiquidityMute(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.addLiquidityMute(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.removeLiquidityMute:
|
|
case TaskType.removeLiquidityMute:
|
|
|
web3Result = await this.web3Service.removeLiquidityMute(accountId)
|
|
web3Result = await this.web3Service.removeLiquidityMute(accountId)
|
|
|
break
|
|
break
|
|
|
case TaskType.addLiquiditySpacefi:
|
|
case TaskType.addLiquiditySpacefi:
|
|
|
- web3Result = await this.web3Service.addLiquiditySpacefi(
|
|
|
|
|
- accountId,
|
|
|
|
|
- taskItem.amountType === 'amount'
|
|
|
|
|
- ? randomAmount(taskItem.amount.min, taskItem.amount.max)
|
|
|
|
|
- : randomBalancePercent(
|
|
|
|
|
- await this.accountsService.getZkBalance(account),
|
|
|
|
|
- taskItem.percent.min,
|
|
|
|
|
- taskItem.percent.max
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ amount = await this.getAmount(account, taskItem, 'zk')
|
|
|
|
|
+ web3Result = await this.web3Service.addLiquiditySpacefi(accountId, amount)
|
|
|
break
|
|
break
|
|
|
case TaskType.removeLiquiditySpacefi:
|
|
case TaskType.removeLiquiditySpacefi:
|
|
|
web3Result = await this.web3Service.removeLiquiditySpacefi(accountId)
|
|
web3Result = await this.web3Service.removeLiquiditySpacefi(accountId)
|
|
@@ -325,6 +205,7 @@ export class TaskService implements OnModuleInit {
|
|
|
taskLog.status = LogStatus.Failed
|
|
taskLog.status = LogStatus.Failed
|
|
|
taskLog.error = error.message
|
|
taskLog.error = error.message
|
|
|
}
|
|
}
|
|
|
|
|
+ taskLog.amount = amount
|
|
|
taskLog.finishTime = new Date()
|
|
taskLog.finishTime = new Date()
|
|
|
await this.taskLogRepository.save(taskLog)
|
|
await this.taskLogRepository.save(taskLog)
|
|
|
task.progress += 1
|
|
task.progress += 1
|