Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/erc20 statistic evm #904

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions migrations/evm/20240502080101_erc20_statistic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable('erc20_statistic', (table) => {
table.increments();
table.integer('erc20_contract_id').notNullable();
table.foreign('erc20_contract_id').references('erc20_contract.id');
table.integer('total_holder');
table.date('date').index().notNullable();
table.unique(['erc20_contract_id', 'date']);
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTableIfExists('erc20_statistic');
}
11 changes: 11 additions & 0 deletions src/models/erc20_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import BaseModel from './base';
import { EVMSmartContract } from './evm_smart_contract';
// eslint-disable-next-line import/no-cycle
import { Erc20Activity } from './erc20_activity';
import { AccountBalance } from './account_balance';

export class Erc20Contract extends BaseModel {
[relation: string]: any;

static softDelete = false;

id!: number;
Expand Down Expand Up @@ -61,6 +64,14 @@ export class Erc20Contract extends BaseModel {
from: 'erc20_contract.address',
},
},
holders: {
relation: Model.HasManyRelation,
modelClass: AccountBalance,
join: {
to: 'account_balance.denom',
from: 'erc20_contract.address',
},
},
};
}

Expand Down
44 changes: 44 additions & 0 deletions src/models/erc20_statistic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Model } from 'objection';
import BaseModel from './base';
import { Erc20Contract } from './erc20_contract';

export class Erc20Statistic extends BaseModel {
static softDelete = false;

[relation: string]: any;

date!: Date;

erc20_contract_id!: number;

total_holder!: number;

static get tableName() {
return 'erc20_statistic';
}

static get jsonSchema() {
return {
type: 'object',
required: ['erc20_contract_id', 'total_holder', 'date'],
properties: {
erc20_contract_id: { type: 'number' },
total_holder: { type: 'number' },
date: { type: 'object' },
},
};
}

static get relationMappings() {
return {
erc20_contract: {
relation: Model.BelongsToOneRelation,
modelClass: Erc20Contract,
join: {
from: 'erc20_statistic.erc20_contract_id',
to: 'erc20_contract.id',
},
},
};
}
}
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ export * from './erc721_stats';
export * from './evm_block';
export * from './optimism_deposit';
export * from './optimism_withdrawal';
export * from './erc20_statistic';
52 changes: 51 additions & 1 deletion src/services/evm/erc20.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import BullableService, { QueueHandler } from '../../base/bullable.service';
import { SERVICE as COSMOS_SERVICE, Config } from '../../common';
import knex from '../../common/utils/db_connection';
import { getViemClient } from '../../common/utils/etherjs_client';
import { BlockCheckpoint, EVMSmartContract } from '../../models';
import {
Block,
BlockCheckpoint,
EVMSmartContract,
Erc20Statistic,
} from '../../models';
import { Erc20Activity } from '../../models/erc20_activity';
import { Erc20Contract } from '../../models/erc20_contract';
import { BULL_JOB_NAME, SERVICE as EVM_SERVICE, SERVICE } from './constant';
Expand Down Expand Up @@ -129,6 +134,7 @@ export default class Erc20Service extends BullableService {
[BULL_JOB_NAME.HANDLE_ERC20_ACTIVITY],
config.erc20.key
);
await this.handleStatistic(startBlock);
// get Erc20 activities
let erc20Activities = await Erc20Handler.getErc20Activities(
startBlock,
Expand Down Expand Up @@ -358,6 +364,50 @@ export default class Erc20Service extends BullableService {
}
}

async handleStatistic(startBlock: number) {
const systemDate = (
await Block.query()
.where('height', startBlock + 1)
.first()
.throwIfNotFound()
).time;
const lastUpdatedRecord = await Erc20Statistic.query().max('date').first();
const lastUpdatedDate = lastUpdatedRecord?.max;
if (lastUpdatedDate) {
systemDate.setHours(0, 0, 0, 0);
lastUpdatedDate.setHours(0, 0, 0, 0);
if (systemDate > lastUpdatedDate) {
await this.handleTotalHolderStatistic(systemDate);
}
} else {
await this.handleTotalHolderStatistic(systemDate);
}
}

async handleTotalHolderStatistic(systemDate: Date) {
const totalHolders = await Erc20Contract.query()
.joinRelated('holders')
.where('erc20_contract.track', true)
.groupBy('erc20_contract.id')
.select(
'erc20_contract.id as erc20_contract_id',
knex.raw(
'count(CASE when holders.amount > 0 THEN 1 ELSE null END) as count'
)
);
if (totalHolders.length > 0) {
await Erc20Statistic.query().insert(
totalHolders.map((e) =>
Erc20Statistic.fromJson({
erc20_contract_id: e.erc20_contract_id,
total_holder: e.count,
date: systemDate,
})
)
);
}
}

public async _start(): Promise<void> {
this.viemClient = getViemClient();
if (NODE_ENV !== 'test') {
Expand Down
Loading