From eeda9c2c87d4699fc4db2ad2abccf28b3bdbc434 Mon Sep 17 00:00:00 2001 From: fibonacci998 Date: Fri, 8 Nov 2024 15:23:47 +0700 Subject: [PATCH] fix: refactor using job instead of action when proxy trigger erc20, erc721 --- src/services/evm/constant.ts | 2 + .../evm/crawl_evm_proxy_history.service.ts | 32 ++++++++++--- src/services/evm/erc20.service.ts | 21 +++------ src/services/evm/erc721.service.ts | 24 ++++------ .../evm/crawl_evm_proxy_history.spec.ts | 47 +++++++++++-------- 5 files changed, 73 insertions(+), 53 deletions(-) diff --git a/src/services/evm/constant.ts b/src/services/evm/constant.ts index c96ab6e2f..a8cf569d0 100644 --- a/src/services/evm/constant.ts +++ b/src/services/evm/constant.ts @@ -302,6 +302,8 @@ export const BULL_JOB_NAME = { UPDATE_EVM_ASSETS: 'update:evm-assets', CRAWL_EVM_ACCOUNT_BALANCE_NONCE: 'crawl:evm-account-balance-nonce', UPDATE_ACCOUNT_ID_IN_EVM_TX: 'update:account-id-in-evm-tx', + INSERT_ERC20_CONTRACT: 'job:insert-erc20-contract', + INSERT_ERC721_CONTRACT: 'job:insert-erc721-contract', }; export const MSG_TYPE = { diff --git a/src/services/evm/crawl_evm_proxy_history.service.ts b/src/services/evm/crawl_evm_proxy_history.service.ts index d9e258794..76b0c9435 100644 --- a/src/services/evm/crawl_evm_proxy_history.service.ts +++ b/src/services/evm/crawl_evm_proxy_history.service.ts @@ -260,9 +260,19 @@ export default class CrawlProxyContractEVMService extends BullableService { proxyContracts.map((e) => e.id) ) .select('evm_proxy_history.proxy_contract as address', 'proxy.id as id'); - await this.broker.call(SERVICE.V1.Erc20.insertNewErc20Contracts.path, { - evmSmartContracts: erc20ProxyContracts, - }); + await this.createJob( + BULL_JOB_NAME.INSERT_ERC20_CONTRACT, + BULL_JOB_NAME.INSERT_ERC20_CONTRACT, + { + evmSmartContracts: erc20ProxyContracts, + }, + { + removeOnComplete: true, + removeOnFail: false, + attempts: config.jobRetryAttempt, + backoff: config.jobRetryBackoff, + } + ); } async handleErc721ProxyContracts(proxyContracts: EvmProxyHistory[]) { @@ -283,9 +293,19 @@ export default class CrawlProxyContractEVMService extends BullableService { proxyContracts.map((e) => e.id) ) .select('evm_proxy_history.proxy_contract as address', 'proxy.id as id'); - await this.broker.call(SERVICE.V1.Erc721.insertNewErc721Contracts.path, { - evmSmartContracts: erc721ProxyContracts, - }); + await this.createJob( + BULL_JOB_NAME.INSERT_ERC721_CONTRACT, + BULL_JOB_NAME.INSERT_ERC721_CONTRACT, + { + evmSmartContracts: erc721ProxyContracts, + }, + { + removeOnComplete: true, + removeOnFail: false, + attempts: config.jobRetryAttempt, + backoff: config.jobRetryBackoff, + } + ); } public async _start() { diff --git a/src/services/evm/erc20.service.ts b/src/services/evm/erc20.service.ts index 1f4d76f05..6939b3c4a 100644 --- a/src/services/evm/erc20.service.ts +++ b/src/services/evm/erc20.service.ts @@ -196,21 +196,14 @@ export default class Erc20Service extends BullableService { } } - @Action({ - name: SERVICE.V1.Erc20.insertNewErc20Contracts.key, - params: { - evmSmartContracts: 'any[]', - }, + @QueueHandler({ + queueName: BULL_JOB_NAME.INSERT_ERC20_CONTRACT, + jobName: BULL_JOB_NAME.INSERT_ERC20_CONTRACT, }) - async insertNewErc20Contracts( - ctx: Context<{ - evmSmartContracts: { - id: number; - address: string; - }[]; - }> - ) { - const { evmSmartContracts } = ctx.params; + async insertNewErc20Contracts(_payload: { + evmSmartContracts: { id: number; address: string }[]; + }) { + const { evmSmartContracts } = _payload; if (evmSmartContracts.length > 0) { const currentHeight = await this.viemClient.getBlockNumber(); const erc20Instances = await this.getErc20Instances( diff --git a/src/services/evm/erc721.service.ts b/src/services/evm/erc721.service.ts index 7f72adc43..cd5e82459 100644 --- a/src/services/evm/erc721.service.ts +++ b/src/services/evm/erc721.service.ts @@ -312,21 +312,14 @@ export default class Erc721Service extends BullableService { this.logger.info(`Reindex erc721 contract ${address} done.`); } - @Action({ - name: SERVICE.V1.Erc721.insertNewErc721Contracts.key, - params: { - evmSmartContracts: 'any[]', - }, + @QueueHandler({ + queueName: BULL_JOB_NAME.INSERT_ERC721_CONTRACT, + jobName: BULL_JOB_NAME.INSERT_ERC721_CONTRACT, }) - async insertNewErc721Contracts( - ctx: Context<{ - evmSmartContracts: { - id: number; - address: string; - }[]; - }> - ) { - const { evmSmartContracts } = ctx.params; + async insertNewErc721Contracts(_payload: { + evmSmartContracts: { id: number; address: string }[]; + }) { + const { evmSmartContracts } = _payload; if (evmSmartContracts.length > 0) { const currentHeight = await this.viemClient.getBlockNumber(); const erc721Instances = await this.getErc721Instances( @@ -548,6 +541,9 @@ export default class Erc721Service extends BullableService { {}, { removeOnComplete: true, + removeOnFail: { + count: 3, + }, repeat: { every: config.erc721.millisecondRepeatJob, }, diff --git a/test/unit/services/evm/crawl_evm_proxy_history.spec.ts b/test/unit/services/evm/crawl_evm_proxy_history.spec.ts index f0ac34c29..88a9b71f4 100644 --- a/test/unit/services/evm/crawl_evm_proxy_history.spec.ts +++ b/test/unit/services/evm/crawl_evm_proxy_history.spec.ts @@ -3,6 +3,7 @@ import { ServiceBroker } from 'moleculer'; import knex from '../../../../src/common/utils/db_connection'; import { EVMSmartContract, EvmProxyHistory } from '../../../../src/models'; import CrawlProxyContractEVMService from '../../../../src/services/evm/crawl_evm_proxy_history.service'; +import { BULL_JOB_NAME } from '../../../../src/services/evm/constant'; @Describe('Test EVMProxy') export default class EvmProxyServiceTest { @@ -90,29 +91,37 @@ export default class EvmProxyServiceTest { .onConflict(['proxy_contract', 'block_height']) .merge() .returning('id'); - const result = jest - .spyOn(this.evmProxyHistoryService.broker, 'call') - .mockResolvedValue([]); + const result = jest.spyOn(this.evmProxyHistoryService, 'createJob'); await this.evmProxyHistoryService.handleTypeProxyContracts( // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore newProxyContracts ); - expect(result).toHaveBeenCalledWith('v1.Erc20.insertNewErc20Contracts', { - evmSmartContracts: [ - { - address: evmSmartContracts[0].address, - id: evmSmartContracts[0].id, - }, - ], - }); - expect(result).toHaveBeenCalledWith('v1.Erc721.insertNewErc721Contracts', { - evmSmartContracts: [ - { - address: evmSmartContracts[2].address, - id: evmSmartContracts[2].id, - }, - ], - }); + expect(result).toHaveBeenCalledWith( + BULL_JOB_NAME.INSERT_ERC20_CONTRACT, + BULL_JOB_NAME.INSERT_ERC20_CONTRACT, + { + evmSmartContracts: [ + { + id: evmSmartContracts[0].id, + address: evmSmartContracts[0].address, + }, + ], + }, + expect.any(Object) + ); + expect(result).toHaveBeenCalledWith( + BULL_JOB_NAME.INSERT_ERC721_CONTRACT, + BULL_JOB_NAME.INSERT_ERC721_CONTRACT, + { + evmSmartContracts: [ + { + id: evmSmartContracts[2].id, + address: evmSmartContracts[2].address, + }, + ], + }, + expect.any(Object) + ); } }