From 0f20f474559f39f4425442c34fc3c8d51eacd783 Mon Sep 17 00:00:00 2001 From: phamphong9981 Date: Mon, 9 Sep 2024 15:04:52 +0700 Subject: [PATCH 1/2] refactor: detect proxy api then handle erc721/20 types --- src/services/evm/constant.ts | 4 ++ .../evm/crawl_evm_proxy_history.service.ts | 39 ++++++++++++++++++- src/services/evm/evm_proxy.service.ts | 6 +++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/services/evm/constant.ts b/src/services/evm/constant.ts index bdf370d9d..7a1aa0f68 100644 --- a/src/services/evm/constant.ts +++ b/src/services/evm/constant.ts @@ -149,6 +149,10 @@ export const SERVICE = { CrawlEvmProxyHistory: { key: 'CrawlEvmProxyHistory', path: 'v1.CrawlEvmProxyHistory', + handleTypeProxyContracts: { + key: 'handleTypeProxyContracts', + path: 'v1.CrawlEvmProxyHistory.handleTypeProxyContracts', + }, }, JobService: { CrawlEvmEvent: { diff --git a/src/services/evm/crawl_evm_proxy_history.service.ts b/src/services/evm/crawl_evm_proxy_history.service.ts index 77b38da3c..29d771889 100644 --- a/src/services/evm/crawl_evm_proxy_history.service.ts +++ b/src/services/evm/crawl_evm_proxy_history.service.ts @@ -1,9 +1,12 @@ /* eslint-disable @typescript-eslint/return-await */ /* eslint-disable no-await-in-loop */ /* eslint-disable no-restricted-syntax */ -import { Service } from '@ourparentcenter/moleculer-decorators-extended'; +import { + Action, + Service, +} from '@ourparentcenter/moleculer-decorators-extended'; import _, { Dictionary } from 'lodash'; -import { ServiceBroker } from 'moleculer'; +import { Context, ServiceBroker } from 'moleculer'; import { PublicClient, decodeAbiParameters, @@ -234,6 +237,38 @@ export default class CrawlProxyContractEVMService extends BullableService { await this.handleTypeProxyContracts(newProxyContracts); } + @Action({ + name: SERVICE.V1.CrawlEvmProxyHistory.handleTypeProxyContracts.key, + params: { + proxyHistoryIds: { + type: 'array', + items: 'number', + required: true, + }, + }, + }) + public async actionHandleTypeProxyContracts( + ctx: Context<{ proxyHistoryIds: number[] }> + ) { + const ids = ctx.params.proxyHistoryIds; + // handle erc20 proxies + await this.handleErc20ProxyContracts( + ids.map((id) => + EvmProxyHistory.fromJson({ + id, + }) + ) + ); + // handle erc721 proxies + await this.handleErc721ProxyContracts( + ids.map((id) => + EvmProxyHistory.fromJson({ + id, + }) + ) + ); + } + async handleTypeProxyContracts(proxyContracts: EvmProxyHistory[]) { // handle erc20 proxies await this.handleErc20ProxyContracts(proxyContracts); diff --git a/src/services/evm/evm_proxy.service.ts b/src/services/evm/evm_proxy.service.ts index 5cf1be74e..3aa6475a0 100644 --- a/src/services/evm/evm_proxy.service.ts +++ b/src/services/evm/evm_proxy.service.ts @@ -78,6 +78,12 @@ export default class EVMProxy extends BaseService { }) .returning('*'); } + await this.broker.call( + SERVICE.V1.CrawlEvmProxyHistory.handleTypeProxyContracts.path, + { + proxyHistoryIds: [evmProxyHistory.id], + } + ); } return evmProxyHistory; From aa59d78acdd30fd056248501df21f13868fd74e3 Mon Sep 17 00:00:00 2001 From: phamphong9981 Date: Mon, 9 Sep 2024 16:32:15 +0700 Subject: [PATCH 2/2] fix: test --- test/unit/services/evm/evm_proxy.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/services/evm/evm_proxy.spec.ts b/test/unit/services/evm/evm_proxy.spec.ts index dbed7c726..50da6fb16 100644 --- a/test/unit/services/evm/evm_proxy.spec.ts +++ b/test/unit/services/evm/evm_proxy.spec.ts @@ -51,7 +51,7 @@ export default class EvmProxyServiceTest { jest .spyOn(ContractHelper.prototype, 'isContractProxy') .mockResolvedValueOnce(proxyContractRPC); - + jest.spyOn(this.evmProxyService.broker, 'call').mockImplementation(); const insertedSmartContract = await EVMSmartContract.query() .insert({ address: ctx.params.contractAddress }) .returning('*'); @@ -82,7 +82,7 @@ export default class EvmProxyServiceTest { jest .spyOn(this.evmProxyService.viemClient, 'getBlockNumber') .mockResolvedValueOnce(Promise.resolve(BigInt(lastBlockHeight))); - + jest.spyOn(this.evmProxyService.broker, 'call').mockImplementation(); await EVMSmartContract.query() .insert({ address: ctx.params.contractAddress,