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 total holder merge dev #921

Merged
merged 3 commits into from
Oct 4, 2024
Merged
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
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
Loading