Skip to content

Commit

Permalink
Refactor/erc20 reindex (#922)
Browse files Browse the repository at this point in the history
* refactor: erc20 reindex

* fix: logic

* test: test
  • Loading branch information
phamphong9981 authored Oct 10, 2024
1 parent 68c294c commit 4d79ec6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
29 changes: 23 additions & 6 deletions src/services/evm/erc20_reindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -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)),
Expand All @@ -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);
Expand All @@ -83,7 +100,7 @@ export class Erc20Reindexer {
const resultBuildErc20Activities =
await Erc20Handler.buildErc20Activities(
0,
Number(blockHeight),
erc20ActivityBlockCheckpoint,
trx,
this.logger,
[address],
Expand Down Expand Up @@ -115,7 +132,7 @@ export class Erc20Reindexer {
while (true) {
const erc20ActivitiesInDb = await Erc20Handler.getErc20Activities(
0,
Number(blockHeight),
erc20BalanceBlockCheckpoint,
trx,
[address],
{
Expand Down
16 changes: 14 additions & 2 deletions test/unit/services/evm/erc20_reindex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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({
Expand Down

0 comments on commit 4d79ec6

Please sign in to comment.