-
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
Open
phamphong9981
wants to merge
7
commits into
develop
Choose a base branch
from
feat/erc20-statistic
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
813e744
feat: erc20 statistic
phamphong9981 16bad20
Merge branch 'develop' of github.com:aura-nw/horoscope-v2 into feat/e…
phamphong9981 84f2d33
refactor: review
phamphong9981 f700378
Merge branch 'develop' of github.com:aura-nw/horoscope-v2 into feat/e…
phamphong9981 3dc6694
refactor: review
phamphong9981 a511053
Merge branch 'develop' of github.com:aura-nw/horoscope-v2 into feat/e…
phamphong9981 70f2335
refactor: review
phamphong9981 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,17 +8,24 @@ import { Context, ServiceBroker } from 'moleculer'; | |
import { PublicClient, getContract } from 'viem'; | ||
import config from '../../../config.json' assert { type: 'json' }; | ||
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 { getViemClient } 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, | ||
|
@@ -156,6 +163,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 | ||
|
@@ -372,50 +380,96 @@ 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' | ||
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 (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, | ||
peara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
) | ||
); | ||
} | ||
} | ||
|
||
public async _start(): Promise<void> { | ||
this.viemClient = 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
gộp 2 câu if điều kiện chạy lại
if (!lastUpdatedDate || systemDate > lastUpdatedDate)
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.
Đ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ê