Skip to content

Commit

Permalink
refactor: evm update asset job (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
phamphong9981 authored Oct 1, 2024
1 parent d511a54 commit f9fbdea
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 3 additions & 1 deletion ci/config.json.ci
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@
},
"jobUpdateAssets": {
"millisecondRepeatJob": 10000,
"lcdRecordGet": 5
"lcdRecordGet": 5,
"key": "jobUpdateAssets",
"blocksPerCall": 1000
},
"jobCreateConstraintInTransactionMessagePartition": {
"jobRepeatCheckNeedCreateConstraint": {
Expand Down
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@
},
"jobUpdateAssets": {
"millisecondRepeatJob": 10000,
"lcdRecordGet": 5
"lcdRecordGet": 5,
"key": "jobUpdateAssets",
"blocksPerCall": 1000
},
"uploadBlockRawLogToS3": {
"key": "uploadBlockRawLogToS3",
Expand Down
1 change: 1 addition & 0 deletions src/services/evm/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export const BULL_JOB_NAME = {
REINDEX_ERC20: 'reindex:erc20',
REFRESH_ERC721_HOLDER_STATISTIC: 'refresh:erc721-holder-statistic',
CRAWL_EVM_ACCOUNT_PUBKEY: 'crawl:evm-account-pubkey',
UPDATE_EVM_ASSETS: 'update:evm-assets',
};

export const MSG_TYPE = {
Expand Down
30 changes: 26 additions & 4 deletions src/services/evm/update_assets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import BullableService, { QueueHandler } from '../../base/bullable.service';
import { BULL_JOB_NAME, SERVICE } from './constant';
import { Asset } from '../../models/asset';
import { Erc20Contract } from '../../models/erc20_contract';
import { BlockCheckpoint } from '../../models';
import knex from '../../common/utils/db_connection';

@Service({
name: SERVICE.V1.JobService.UpdateEvmAssets.key,
Expand All @@ -20,7 +22,15 @@ export default class UpdateEvmAssetsJob extends BullableService {
jobName: BULL_JOB_NAME.JOB_UPDATE_EVM_ASSETS,
})
async jobUpdateEvmAssets() {
const erc20Assets = await Erc20Contract.query();
const [startBlock, endBlock, updateBlockCheckpoint] =
await BlockCheckpoint.getCheckpoint(
BULL_JOB_NAME.UPDATE_EVM_ASSETS,
[BULL_JOB_NAME.HANDLE_ERC20_BALANCE],
config.jobUpdateAssets.key
);
const erc20Assets = await Erc20Contract.query()
.where('last_updated_height', '>', startBlock)
.andWhere('last_updated_height', '<=', endBlock);
const assets: Asset[] = [];
assets.push(
...erc20Assets.map((erc20Asset) =>
Expand All @@ -35,9 +45,21 @@ export default class UpdateEvmAssetsJob extends BullableService {
})
)
);
if (assets.length > 0) {
await Asset.query().insert(assets).onConflict('denom').merge();
}
await knex.transaction(async (trx) => {
if (assets.length > 0) {
await Asset.query()
.insert(assets)
.onConflict('denom')
.merge()
.transacting(trx);
}
updateBlockCheckpoint.height = endBlock;
await BlockCheckpoint.query()
.insert(updateBlockCheckpoint)
.onConflict('job_name')
.merge()
.transacting(trx);
});
}

public async _start(): Promise<void> {
Expand Down

0 comments on commit f9fbdea

Please sign in to comment.