-
Notifications
You must be signed in to change notification settings - Fork 3
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 (total holder) #777
base: develop
Are you sure you want to change the base?
Changes from 1 commit
813e744
16bad20
84f2d33
f700378
3dc6694
a511053
70f2335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'); | ||
} |
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', | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,24 @@ import { PublicClient, getContract } from 'viem'; | |
import config from '../../../config.json' assert { type: 'json' }; | ||
import '../../../fetch-polyfill.js'; | ||
import BullableService, { QueueHandler } from '../../base/bullable.service'; | ||
import { SERVICE as COSMOS_SERVICE } from '../../common'; | ||
import { SERVICE as COSMOS_SERVICE, Config } from '../../common'; | ||
import knex from '../../common/utils/db_connection'; | ||
import EtherJsClient from '../../common/utils/etherjs_client'; | ||
import { BlockCheckpoint, EVMSmartContract, EvmEvent } from '../../models'; | ||
import { | ||
Block, | ||
BlockCheckpoint, | ||
EVMSmartContract, | ||
Erc20Statistic, | ||
EvmEvent, | ||
} from '../../models'; | ||
import { AccountBalance } from '../../models/account_balance'; | ||
import { Erc20Activity } from '../../models/erc20_activity'; | ||
import { Erc20Contract } from '../../models/erc20_contract'; | ||
import { BULL_JOB_NAME, SERVICE as EVM_SERVICE, SERVICE } from './constant'; | ||
import { ERC20_EVENT_TOPIC0, Erc20Handler } from './erc20_handler'; | ||
import { convertEthAddressToBech32Address } from './utils'; | ||
|
||
const { NODE_ENV } = Config; | ||
@Service({ | ||
name: EVM_SERVICE.V1.Erc20.key, | ||
version: 1, | ||
|
@@ -154,6 +161,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 this.getErc20Activities(startBlock, endBlock); | ||
// get missing Account | ||
|
@@ -370,50 +378,96 @@ export default class Erc20Service extends BullableService { | |
})); | ||
} | ||
|
||
async handleStatistic(startBlock: number) { | ||
const systemDate = ( | ||
await Block.query() | ||
.where('height', startBlock + 1) | ||
.first() | ||
.throwIfNotFound() | ||
).time; | ||
const lastUpdatedDate = (await Erc20Statistic.query().max('date').first()) | ||
?.max; | ||
if (lastUpdatedDate) { | ||
systemDate.setHours(0, 0, 0, 0); | ||
lastUpdatedDate.setHours(0, 0, 0, 0); | ||
if (systemDate > lastUpdatedDate) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gộp 2 câu if điều kiện chạy lại There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Đoạn này em đang check xem là ngày dd/mm/yy (tại thời điểm 0h0p0s) đã thống kê trong DB chưa. Vì mỗi 6s/block nên phải chuyển timestamp về ngày để so sánh đảm bảo 1 ngày 1 thống kê |
||
await this.handleTotalHolderStatistic(systemDate); | ||
} | ||
} else { | ||
await this.handleTotalHolderStatistic(systemDate); | ||
} | ||
} | ||
|
||
async handleTotalHolderStatistic(systemDate: Date) { | ||
const totalHolder = await Erc20Contract.query() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. là array nhưng tên ko có s |
||
.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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. '(holders.amount > 0)::int as count' đc ko? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ko đc anh ạ |
||
) | ||
); | ||
if (totalHolder.length > 0) { | ||
await Erc20Statistic.query().insert( | ||
totalHolder.map((e) => | ||
Erc20Statistic.fromJson({ | ||
erc20_contract_id: e.erc20_contract_id, | ||
total_holder: e.count, | ||
date: systemDate, | ||
peara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
) | ||
); | ||
} | ||
} | ||
|
||
public async _start(): Promise<void> { | ||
this.viemClient = EtherJsClient.getViemClient(); | ||
await this.createJob( | ||
BULL_JOB_NAME.HANDLE_ERC20_CONTRACT, | ||
BULL_JOB_NAME.HANDLE_ERC20_CONTRACT, | ||
{}, | ||
{ | ||
removeOnComplete: true, | ||
removeOnFail: { | ||
count: 3, | ||
}, | ||
repeat: { | ||
every: config.erc20.millisecondRepeatJob, | ||
}, | ||
} | ||
); | ||
await this.createJob( | ||
BULL_JOB_NAME.HANDLE_ERC20_ACTIVITY, | ||
BULL_JOB_NAME.HANDLE_ERC20_ACTIVITY, | ||
{}, | ||
{ | ||
removeOnComplete: true, | ||
removeOnFail: { | ||
count: 3, | ||
}, | ||
repeat: { | ||
every: config.erc20.millisecondRepeatJob, | ||
}, | ||
} | ||
); | ||
await this.createJob( | ||
BULL_JOB_NAME.HANDLE_ERC20_BALANCE, | ||
BULL_JOB_NAME.HANDLE_ERC20_BALANCE, | ||
{}, | ||
{ | ||
removeOnComplete: true, | ||
removeOnFail: { | ||
count: 3, | ||
}, | ||
repeat: { | ||
every: config.erc20.millisecondRepeatJob, | ||
}, | ||
} | ||
); | ||
if (NODE_ENV !== 'test') { | ||
await this.createJob( | ||
BULL_JOB_NAME.HANDLE_ERC20_CONTRACT, | ||
BULL_JOB_NAME.HANDLE_ERC20_CONTRACT, | ||
{}, | ||
{ | ||
removeOnComplete: true, | ||
removeOnFail: { | ||
count: 3, | ||
}, | ||
repeat: { | ||
every: config.erc20.millisecondRepeatJob, | ||
}, | ||
} | ||
); | ||
await this.createJob( | ||
BULL_JOB_NAME.HANDLE_ERC20_ACTIVITY, | ||
BULL_JOB_NAME.HANDLE_ERC20_ACTIVITY, | ||
{}, | ||
{ | ||
removeOnComplete: true, | ||
removeOnFail: { | ||
count: 3, | ||
}, | ||
repeat: { | ||
every: config.erc20.millisecondRepeatJob, | ||
}, | ||
} | ||
); | ||
await this.createJob( | ||
BULL_JOB_NAME.HANDLE_ERC20_BALANCE, | ||
BULL_JOB_NAME.HANDLE_ERC20_BALANCE, | ||
{}, | ||
{ | ||
removeOnComplete: true, | ||
removeOnFail: { | ||
count: 3, | ||
}, | ||
repeat: { | ||
every: config.erc20.millisecondRepeatJob, | ||
}, | ||
} | ||
); | ||
} | ||
return super._start(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ko viết await như này, khó đọc mà cũng ko có xử lý lỗi