Skip to content

Commit

Permalink
fix: refactor using job instead of action when proxy trigger erc20, e…
Browse files Browse the repository at this point in the history
…rc721
  • Loading branch information
fibonacci998 committed Nov 8, 2024
1 parent 917306a commit eeda9c2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 53 deletions.
2 changes: 2 additions & 0 deletions src/services/evm/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
32 changes: 26 additions & 6 deletions src/services/evm/crawl_evm_proxy_history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand All @@ -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() {
Expand Down
21 changes: 7 additions & 14 deletions src/services/evm/erc20.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 10 additions & 14 deletions src/services/evm/erc721.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -548,6 +541,9 @@ export default class Erc721Service extends BullableService {
{},
{
removeOnComplete: true,
removeOnFail: {
count: 3,
},
repeat: {
every: config.erc721.millisecondRepeatJob,
},
Expand Down
47 changes: 28 additions & 19 deletions test/unit/services/evm/crawl_evm_proxy_history.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
);
}
}

0 comments on commit eeda9c2

Please sign in to comment.