Skip to content

Commit

Permalink
fix: code
Browse files Browse the repository at this point in the history
  • Loading branch information
phamphong9981 committed Jul 24, 2024
1 parent af50ce9 commit 14b9ba9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
33 changes: 1 addition & 32 deletions src/common/utils/db_connection.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import Knex, { Knex as IKnex } from 'knex';
import Knex from 'knex';
import knexConfig from '../../../knexfile';

const environment = process.env.NODE_ENV || 'development';
const cfg = knexConfig[environment];
const knex = Knex(cfg);

export default knex;

export async function batchUpdate(
trx: IKnex.Transaction,
tableName: string,
records: any,
fields: string[]
) {
if (records.length === 0) return;
const stringListUpdates = records
.map((record: any) => {
const values = fields.map((field) => {
if (record[field]?.type === 'timestamp') {
return `to_timestamp(${record[field].value})`;
}

if (record[field] !== undefined) {
return `'${record[field]}'`;
}
return 'NULL';
});
return `(${record.id}, ${values.join(', ')})`;
})
.join(',');
const query = `
UPDATE ${tableName}
SET ${fields.map((field) => `${field} = temp.${field}`).join(', ')}
FROM (VALUES ${stringListUpdates}) AS temp(id, ${fields.join(', ')})
WHERE temp.id = ${tableName}.id
`;
await trx.raw(query);
}
21 changes: 12 additions & 9 deletions src/services/evm/erc20_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _, { Dictionary } from 'lodash';
import Moleculer from 'moleculer';
import { decodeAbiParameters, keccak256, toHex } from 'viem';
import config from '../../../config.json' assert { type: 'json' };
import knex, { batchUpdate } from '../../common/utils/db_connection';
import knex from '../../common/utils/db_connection';
import {
Erc20Activity,
Erc20Contract,
Expand Down Expand Up @@ -130,11 +130,12 @@ export class Erc20Handler {
this.erc20Contracts[erc20Activity.erc20_contract_address];
if (
erc20Contract &&
erc20Contract.last_updated_height <= erc20Activity.height
erc20Contract.last_updated_height <= erc20Activity.height &&
erc20Contract.total_supply !== null
) {
// update total supply
erc20Contract.total_supply = (
BigInt(erc20Contract.total_supply || 0) + BigInt(erc20Activity.amount)
BigInt(erc20Contract.total_supply) + BigInt(erc20Activity.amount)
).toString();
// update last updated height
erc20Contract.last_updated_height = erc20Activity.height;
Expand Down Expand Up @@ -168,11 +169,12 @@ export class Erc20Handler {
this.erc20Contracts[erc20Activity.erc20_contract_address];
if (
erc20Contract &&
erc20Contract.last_updated_height <= erc20Activity.height
erc20Contract.last_updated_height <= erc20Activity.height &&
erc20Contract.total_supply !== null
) {
// update total supply
erc20Contract.total_supply = (
BigInt(erc20Contract.total_supply || 0) - BigInt(erc20Activity.amount)
BigInt(erc20Contract.total_supply) - BigInt(erc20Activity.amount)
).toString();
// update last updated height
erc20Contract.last_updated_height = erc20Activity.height;
Expand Down Expand Up @@ -342,10 +344,11 @@ export class Erc20Handler {
erc20Handler.process();
const updatedErc20Contracts = Object.values(erc20Handler.erc20Contracts);
if (updatedErc20Contracts.length > 0) {
await batchUpdate(trx, Erc20Contract.tableName, updatedErc20Contracts, [
'last_updated_height',
'total_supply',
]);
await Erc20Contract.query()
.transacting(trx)
.insert(updatedErc20Contracts)
.onConflict(['id'])
.merge();
}
const updatedAccountBalances = Object.values(
erc20Handler.accountBalances
Expand Down
3 changes: 2 additions & 1 deletion test/unit/services/evm/erc20_reindex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ export default class Erc20ReindexTest {
@Test('test reindex')
async testReindex() {
const viemClient = getViemClient();
Erc20Handler.erc20ModuleAccount = '0x0000000000000dfd';
Erc20Handler.erc20ModuleAccount =
'aura16st9fmex8xjdahwzwxldhjm2ssu6utw8rnxlkj';
jest.spyOn(viemClient, 'getBlockNumber').mockResolvedValue(BigInt(123456));
// Instantiate Erc20Reindexer with the mock
const reindexer = new Erc20Reindexer(viemClient, this.broker.logger);
Expand Down

0 comments on commit 14b9ba9

Please sign in to comment.