|
|
@@ -12,7 +12,7 @@ import { TaskType } from './enums/task-type.enum'
|
|
|
import { AccountsService } from 'src/accounts/accounts.service'
|
|
|
import { LogStatus } from './enums/log-status.enum'
|
|
|
import { Web3Result } from 'src/web3/model/web3-result'
|
|
|
-import { randomAmount } from 'src/utils/common'
|
|
|
+import { randomAmount, randomBalancePercent } from 'src/utils/common'
|
|
|
import { EventEmitter2 } from '@nestjs/event-emitter'
|
|
|
import { setTimeout } from 'timers/promises'
|
|
|
import schedule from 'node-schedule'
|
|
|
@@ -88,81 +88,127 @@ export class TaskService implements OnModuleInit {
|
|
|
task.status = TaskStatus.InProgress
|
|
|
await this.taskRepository.save(task)
|
|
|
for (let accountId of task.accounts) {
|
|
|
- const taskLog = new TaskLog()
|
|
|
- taskLog.taskId = task.id
|
|
|
- taskLog.accountId = accountId
|
|
|
- taskLog.startTime = new Date()
|
|
|
- taskLog.type = task.type
|
|
|
- try {
|
|
|
- const account = await this.accountsService.findById(accountId)
|
|
|
- taskLog.address = account.address
|
|
|
- taskLog.accountName = account.name
|
|
|
- let web3Result: Web3Result
|
|
|
- switch (task.type) {
|
|
|
- case TaskType.bridge2zk:
|
|
|
- web3Result = await this.web3Service.zkDeposit(
|
|
|
- accountId,
|
|
|
- randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
- )
|
|
|
- break
|
|
|
- case TaskType.bridge2eth:
|
|
|
- web3Result = await this.web3Service.zkWithdraw(
|
|
|
- accountId,
|
|
|
- randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
- )
|
|
|
- break
|
|
|
- case TaskType.orbiter2zk:
|
|
|
- web3Result = await this.web3Service.orbiterDeposit(
|
|
|
- accountId,
|
|
|
- randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
- )
|
|
|
- break
|
|
|
- case TaskType.orbiter2eth:
|
|
|
- web3Result = await this.web3Service.orbiterWithdraw(
|
|
|
- accountId,
|
|
|
- randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
- )
|
|
|
- break
|
|
|
- case TaskType.swapUsdc:
|
|
|
- web3Result = await this.web3Service.swapWithExactOutput(
|
|
|
- accountId,
|
|
|
- randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
- )
|
|
|
- break
|
|
|
- case TaskType.swapEth:
|
|
|
- web3Result = await this.web3Service.swapWithExactInput(
|
|
|
- accountId,
|
|
|
- randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
- )
|
|
|
- break
|
|
|
- case TaskType.addLiquidity:
|
|
|
- web3Result = await this.web3Service.addLiquidity(
|
|
|
- accountId,
|
|
|
- randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
- )
|
|
|
- break
|
|
|
- case TaskType.removeLiquidity:
|
|
|
- web3Result = await this.web3Service.removeLiquidity(accountId)
|
|
|
- break
|
|
|
- case TaskType.mint:
|
|
|
- web3Result = await this.web3Service.mint(accountId)
|
|
|
- break
|
|
|
+ for (let taskItem of task.items) {
|
|
|
+ const taskLog = new TaskLog()
|
|
|
+ taskLog.taskId = task.id
|
|
|
+ taskLog.accountId = accountId
|
|
|
+ taskLog.startTime = new Date()
|
|
|
+ taskLog.type = taskItem.type
|
|
|
+ try {
|
|
|
+ const account = await this.accountsService.findById(accountId)
|
|
|
+ taskLog.address = account.address
|
|
|
+ taskLog.accountName = account.name
|
|
|
+ let web3Result: Web3Result
|
|
|
+ switch (taskItem.type) {
|
|
|
+ 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
|
|
|
+ )
|
|
|
+ )
|
|
|
+ break
|
|
|
+ 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
|
|
|
+ )
|
|
|
+ )
|
|
|
+ break
|
|
|
+ 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
|
|
|
+ )
|
|
|
+ )
|
|
|
+ break
|
|
|
+ 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
|
|
|
+ )
|
|
|
+ )
|
|
|
+ break
|
|
|
+ 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'
|
|
|
+ )
|
|
|
+ break
|
|
|
+ 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'
|
|
|
+ )
|
|
|
+ break
|
|
|
+ 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
|
|
|
+ )
|
|
|
+ )
|
|
|
+ break
|
|
|
+ case TaskType.removeLiquidity:
|
|
|
+ web3Result = await this.web3Service.removeLiquidity(accountId)
|
|
|
+ break
|
|
|
+ case TaskType.mint:
|
|
|
+ web3Result = await this.web3Service.mint(accountId)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ taskLog.status = LogStatus.Success
|
|
|
+ taskLog.txHash = web3Result.hash
|
|
|
+ taskLog.url = web3Result.url
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error(error, 'TaskService.runTask')
|
|
|
+ Logger.error(error.stack, 'TaskService.runTask')
|
|
|
+ taskLog.status = LogStatus.Failed
|
|
|
+ taskLog.error = error.message
|
|
|
}
|
|
|
- taskLog.status = LogStatus.Success
|
|
|
- taskLog.txHash = web3Result.hash
|
|
|
- taskLog.url = web3Result.url
|
|
|
- } catch (error) {
|
|
|
- Logger.error(error, 'TaskService.runTask')
|
|
|
- Logger.error(error.stack, 'TaskService.runTask')
|
|
|
- taskLog.status = LogStatus.Failed
|
|
|
- taskLog.error = error.message
|
|
|
+ taskLog.finishTime = new Date()
|
|
|
+ await this.taskLogRepository.save(taskLog)
|
|
|
+ task.progress += 1
|
|
|
+ await this.taskRepository.save(task)
|
|
|
+ this.eventEmitter.emit('refreshBalance', accountId)
|
|
|
+ await setTimeout(parseInt(randomAmount(task.delay.min, task.delay.max, 0)))
|
|
|
}
|
|
|
- taskLog.finishTime = new Date()
|
|
|
- await this.taskLogRepository.save(taskLog)
|
|
|
- task.progress += 1
|
|
|
- await this.taskRepository.save(task)
|
|
|
- this.eventEmitter.emit('refreshBalance', accountId)
|
|
|
- await setTimeout(parseInt(randomAmount(task.params.minInterval, task.params.maxInterval, 0)))
|
|
|
}
|
|
|
task.status = TaskStatus.Done
|
|
|
await this.taskRepository.save(task)
|