Skip to content

Commit

Permalink
Feat/erc20 total holder merge dev (#921)
Browse files Browse the repository at this point in the history
* feat: erc20 total holder

* feat: erc20 total holder

* feat: update hasura metadata

---------

Co-authored-by: phamphong9981 <[email protected]>
  • Loading branch information
fibonacci998 and phamphong9981 authored Oct 4, 2024
1 parent 20e4df6 commit 5fbad45
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ select_permissions:
- decimal
- name
- track
- total_holder
- last_updated_height
filter: {}
limit: 100
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
9 changes: 8 additions & 1 deletion src/services/evm/erc20_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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 @@ -143,10 +146,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

0 comments on commit 5fbad45

Please sign in to comment.