|
|
@@ -12,7 +12,8 @@ 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 { EventEmitter2 } from '@nestjs/event-emitter'
|
|
|
@Injectable()
|
|
|
export class TaskService {
|
|
|
constructor(
|
|
|
@@ -21,7 +22,8 @@ export class TaskService {
|
|
|
@InjectRepository(Task)
|
|
|
private readonly taskRepository: Repository<Task>,
|
|
|
@InjectRepository(TaskLog)
|
|
|
- private readonly taskLogRepository: Repository<TaskLog>
|
|
|
+ private readonly taskLogRepository: Repository<TaskLog>,
|
|
|
+ private eventEmitter: EventEmitter2
|
|
|
) {}
|
|
|
|
|
|
async findAll(req: PageRequest<Task>): Promise<Pagination<Task>> {
|
|
|
@@ -37,7 +39,7 @@ export class TaskService {
|
|
|
userId,
|
|
|
...createTask
|
|
|
})
|
|
|
- if (!createTask.startTime) {
|
|
|
+ if (!createTask.startTime || createTask.startTime <= new Date()) {
|
|
|
this.runTask(task)
|
|
|
}
|
|
|
return task
|
|
|
@@ -54,31 +56,54 @@ export class TaskService {
|
|
|
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, task.params.amount)
|
|
|
+ web3Result = await this.web3Service.zkDeposit(
|
|
|
+ accountId,
|
|
|
+ randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
+ )
|
|
|
break
|
|
|
case TaskType.bridge2eth:
|
|
|
- web3Result = await this.web3Service.zkWithdraw(accountId, task.params.amount)
|
|
|
+ web3Result = await this.web3Service.zkWithdraw(
|
|
|
+ accountId,
|
|
|
+ randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
+ )
|
|
|
break
|
|
|
case TaskType.orbiter2zk:
|
|
|
- web3Result = await this.web3Service.orbiterDeposit(accountId, task.params.amount)
|
|
|
+ web3Result = await this.web3Service.orbiterDeposit(
|
|
|
+ accountId,
|
|
|
+ randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
+ )
|
|
|
break
|
|
|
case TaskType.orbiter2eth:
|
|
|
- web3Result = await this.web3Service.orbiterWithdraw(accountId, task.params.amount)
|
|
|
+ web3Result = await this.web3Service.orbiterWithdraw(
|
|
|
+ accountId,
|
|
|
+ randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
+ )
|
|
|
break
|
|
|
case TaskType.swapUsdc:
|
|
|
- web3Result = await this.web3Service.swapWithExactInput(accountId, task.params.amount)
|
|
|
+ web3Result = await this.web3Service.swapWithExactInput(
|
|
|
+ accountId,
|
|
|
+ randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
+ )
|
|
|
break
|
|
|
case TaskType.swapEth:
|
|
|
- web3Result = await this.web3Service.swapWithExactOutput(accountId, task.params.amount)
|
|
|
+ web3Result = await this.web3Service.swapWithExactOutput(
|
|
|
+ accountId,
|
|
|
+ randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
+ )
|
|
|
break
|
|
|
case TaskType.addLiquidity:
|
|
|
- web3Result = await this.web3Service.addLiquidity(accountId, task.params.amount)
|
|
|
+ web3Result = await this.web3Service.addLiquidity(
|
|
|
+ accountId,
|
|
|
+ randomAmount(task.params.minAmount, task.params.maxAmount)
|
|
|
+ )
|
|
|
break
|
|
|
case TaskType.removeLiquidity:
|
|
|
web3Result = await this.web3Service.removeLiquidity(accountId)
|
|
|
@@ -98,6 +123,7 @@ export class TaskService {
|
|
|
await this.taskLogRepository.save(taskLog)
|
|
|
task.progress += 1
|
|
|
await this.taskRepository.save(task)
|
|
|
+ this.eventEmitter.emit('updateBalance', accountId)
|
|
|
}
|
|
|
task.status = TaskStatus.Done
|
|
|
await this.taskRepository.save(task)
|