Skip to content

Commit

Permalink
feat: erc20 total holder (#907)
Browse files Browse the repository at this point in the history
* feat: erc20 total holder

* feat: erc20 total holder
  • Loading branch information
phamphong9981 authored Sep 23, 2024
1 parent b61bddb commit 9e6012b
Show file tree
Hide file tree
Showing 5 changed files with 965 additions and 157 deletions.
29 changes: 29 additions & 0 deletions migrations/evm/20240920024049_erc20_contract_add_total_holder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Knex } from 'knex';
import { AccountBalance } from '../../src/models';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('erc20_contract', (table) => {
table.integer('total_holder').defaultTo(0).index();
});
await knex.raw(`set statement_timeout to 0`);
const totalHolders = await AccountBalance.query(knex)
.select('account_balance.denom')
.where('account_balance.type', AccountBalance.TYPE.ERC20_TOKEN)
.andWhere('account_balance.amount', '>', 0)
.count()
.groupBy('account_balance.denom');
if (totalHolders.length > 0) {
const stringListUpdates = totalHolders
.map((totalHolder) => `('${totalHolder.denom}', ${totalHolder.count})`)
.join(',');
await knex.raw(
`UPDATE erc20_contract SET total_holder = temp.total_holder from (VALUES ${stringListUpdates}) as temp(address, total_holder) where temp.address = erc20_contract.address`
);
}
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('erc20_contract', (table) => {
table.dropColumn('total_holder');
});
}
2 changes: 2 additions & 0 deletions src/models/account_balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import BaseModel from './base';
import { Account } from './account';

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

static softDelete = false;

account!: Account;
Expand Down
2 changes: 2 additions & 0 deletions src/models/erc20_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class Erc20Contract extends BaseModel {

track!: boolean;

total_holder!: number;

last_updated_height!: number;

static get tableName() {
Expand Down
13 changes: 10 additions & 3 deletions src/services/evm/erc20_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export class Erc20Handler {
const amount = (
BigInt(fromAccountBalance?.amount || 0) - BigInt(erc20Activity.amount)
).toString();
if (BigInt(amount) === BigInt(0)) {
erc20Contract.total_holder -= 1;
}
// update object accountBalance
this.accountBalances[key] = AccountBalance.fromJson({
denom: erc20Activity.erc20_contract_address,
Expand Down Expand Up @@ -144,10 +147,14 @@ export class Erc20Handler {
`Process erc20 balance: toAccountBalance ${erc20Activity.to} was updated`
);
}
const initAmount = toAccountBalance?.amount || 0;
// calculate new balance: increase balance of to account
const amount = (
BigInt(toAccountBalance?.amount || 0) + BigInt(erc20Activity.amount)
BigInt(initAmount) + BigInt(erc20Activity.amount)
).toString();
if (BigInt(amount) > BigInt(0) && BigInt(initAmount) === BigInt(0)) {
erc20Contract.total_holder += 1;
}
// update object accountBalance
this.accountBalances[key] = AccountBalance.fromJson({
denom: erc20Activity.erc20_contract_address,
Expand Down Expand Up @@ -545,7 +552,7 @@ export class Erc20Handler {
) as [string, bigint];
return Erc20Activity.fromJson({
evm_event_id: e.id,
sender: e.sender,
sender: bytesToHex(e.sender),
action: ERC20_ACTION.DEPOSIT,
erc20_contract_address: e.address,
amount: amount.toString(),
Expand Down Expand Up @@ -573,7 +580,7 @@ export class Erc20Handler {
) as [string, bigint];
return Erc20Activity.fromJson({
evm_event_id: e.id,
sender: e.sender,
sender: bytesToHex(e.sender),
action: ERC20_ACTION.WITHDRAWAL,
erc20_contract_address: e.address,
amount: amount.toString(),
Expand Down
Loading

0 comments on commit 9e6012b

Please sign in to comment.