diff --git a/src/services/evm/erc20_reindex.ts b/src/services/evm/erc20_reindex.ts index 545ec09d1..09faa279c 100644 --- a/src/services/evm/erc20_reindex.ts +++ b/src/services/evm/erc20_reindex.ts @@ -3,9 +3,15 @@ import Moleculer from 'moleculer'; import { getContract, PublicClient } from 'viem'; import config from '../../../config.json' assert { type: 'json' }; import knex from '../../common/utils/db_connection'; -import { AccountBalance, Erc20Activity, Erc20Contract } from '../../models'; +import { + AccountBalance, + BlockCheckpoint, + Erc20Activity, + Erc20Contract, +} from '../../models'; import { Erc20Handler } from './erc20_handler'; import { convertEthAddressToBech32Address } from './utils'; +import { BULL_JOB_NAME } from './constant'; export class Erc20Reindexer { viemClient: PublicClient; @@ -29,6 +35,18 @@ export class Erc20Reindexer { await Erc20Contract.query() .patch({ track: false }) .where('address', address); + const erc20ActivityBlockCheckpoint = ( + await BlockCheckpoint.query() + .where('job_name', BULL_JOB_NAME.HANDLE_ERC20_ACTIVITY) + .first() + .throwIfNotFound() + ).height; + const erc20BalanceBlockCheckpoint = ( + await BlockCheckpoint.query() + .where('job_name', BULL_JOB_NAME.HANDLE_ERC20_BALANCE) + .first() + .throwIfNotFound() + ).height; // reindex await knex.transaction(async (trx) => { const erc20Contract = await Erc20Contract.query() @@ -55,8 +73,7 @@ export class Erc20Reindexer { abi: Erc20Contract.ABI, client: this.viemClient, }); - const [blockHeight, ...contractInfo] = await Promise.all([ - this.viemClient.getBlockNumber(), + const contractInfo = await Promise.all([ contract.read.name().catch(() => Promise.resolve(undefined)), contract.read.symbol().catch(() => Promise.resolve(undefined)), contract.read.decimals().catch(() => Promise.resolve(undefined)), @@ -71,7 +88,7 @@ export class Erc20Reindexer { total_supply: '0', decimal: contractInfo[2], track: true, - last_updated_height: Number(blockHeight), + last_updated_height: erc20BalanceBlockCheckpoint, }) ) .transacting(trx); @@ -83,7 +100,7 @@ export class Erc20Reindexer { const resultBuildErc20Activities = await Erc20Handler.buildErc20Activities( 0, - Number(blockHeight), + erc20ActivityBlockCheckpoint, trx, this.logger, [address], @@ -115,7 +132,7 @@ export class Erc20Reindexer { while (true) { const erc20ActivitiesInDb = await Erc20Handler.getErc20Activities( 0, - Number(blockHeight), + erc20BalanceBlockCheckpoint, trx, [address], { diff --git a/test/unit/services/evm/erc20_reindex.spec.ts b/test/unit/services/evm/erc20_reindex.spec.ts index 9b28e5f93..06b804873 100644 --- a/test/unit/services/evm/erc20_reindex.spec.ts +++ b/test/unit/services/evm/erc20_reindex.spec.ts @@ -7,12 +7,14 @@ import { getViemClient } from '../../../../src/common/utils/etherjs_client'; import { Account, AccountBalance, + BlockCheckpoint, Erc20Activity, Erc20Contract, EvmEvent, EVMSmartContract, EVMTransaction, } from '../../../../src/models'; +import { BULL_JOB_NAME } from '../../../../src/services/evm/constant'; import { ABI_TRANSFER_PARAMS } from '../../../../src/services/evm/erc20_handler'; import { Erc20Reindexer } from '../../../../src/services/evm/erc20_reindex'; @@ -121,6 +123,16 @@ const evmTransaction = EVMTransaction.fromJson({ index: 0, from: hexToBytes('0x51aeade652867f342ddc012e15c27d0cd6220398'), }); +const blockCheckpoints = [ + BlockCheckpoint.fromJson({ + job_name: BULL_JOB_NAME.HANDLE_ERC20_BALANCE, + height: 123456, + }), + BlockCheckpoint.fromJson({ + job_name: BULL_JOB_NAME.HANDLE_ERC20_ACTIVITY, + height: 123457, + }), +]; @Describe('Test erc20 reindex') export default class Erc20ReindexTest { @@ -130,12 +142,13 @@ export default class Erc20ReindexTest { async initSuite() { await this.broker.start(); await knex.raw( - 'TRUNCATE TABLE evm_transaction, evm_smart_contract, erc20_contract, account, erc20_activity, account_balance, evm_event RESTART IDENTITY CASCADE' + 'TRUNCATE TABLE evm_transaction, evm_smart_contract, erc20_contract, account, erc20_activity, account_balance, evm_event, block_checkpoint RESTART IDENTITY CASCADE' ); await EVMTransaction.query().insert(evmTransaction); await EVMSmartContract.query().insert(evmSmartContract); await Erc20Contract.query().insertGraph(erc20Contract); await Account.query().insertGraph(accounts); + await BlockCheckpoint.query().insert(blockCheckpoints); } @AfterAll() @@ -148,7 +161,6 @@ export default class Erc20ReindexTest { @Test('test reindex') async testReindex() { const viemClient = getViemClient(); - jest.spyOn(viemClient, 'getBlockNumber').mockResolvedValue(BigInt(123456)); // Instantiate Erc20Reindexer with the mock const reindexer = new Erc20Reindexer(viemClient, this.broker.logger); const event = EvmEvent.fromJson({