xiongzhu 2 vuotta sitten
vanhempi
commit
6a38cea0ae

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

@@ -23,4 +23,9 @@ export class AccountsController {
     public async delete(@Param('id') id: string) {
         return await this.accountsService.deleteById(parseInt(id))
     }
+
+    @Get('/refreshBalance')
+    public async refreshBalance(@Req() req) {
+        this.accountsService.refreshBalance(req.user.userId)
+    }
 }

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

@@ -21,7 +21,7 @@ export class AccountsService {
         return await this.accountsRepository.find()
     }
 
-    async findByUserId(userId: number, network: Network) {
+    async findByUserId(userId: number, network?: Network) {
         return await this.accountsRepository.findBy({
             userId,
             network
@@ -68,8 +68,8 @@ export class AccountsService {
         }
     }
 
-    @OnEvent('updateBalance')
-    async handleOrderCreatedEvent(accountId) {
+    @OnEvent('refreshBalance')
+    async refreshBalanceByAccountId(accountId) {
         try {
             const account = await this.findById(accountId)
             Logger.log(`fetching balances for ${account.address}`)
@@ -87,7 +87,14 @@ export class AccountsService {
             account.zkUsdcBalance = ethers.utils.formatUnits(zkUsdcBalance, 6)
             await this.save([account])
         } catch (error) {
-            Logger.error(error, 'updateBalance')
+            Logger.error(error, 'refreshBalance')
+        }
+    }
+
+    async refreshBalance(userId: number) {
+        const accounts = await this.findByUserId(userId)
+        for (let account of accounts) {
+            await this.refreshBalanceByAccountId(account.id)
         }
     }
 }

+ 1 - 1
src/task/task.service.ts

@@ -126,7 +126,7 @@ export class TaskService {
             await this.taskLogRepository.save(taskLog)
             task.progress += 1
             await this.taskRepository.save(task)
-            this.eventEmitter.emit('updateBalance', accountId)
+            this.eventEmitter.emit('refreshBalance', accountId)
             await setTimeout(parseInt(randomAmount(task.params.minInterval, task.params.maxInterval, 0)))
         }
         task.status = TaskStatus.Done