Skip to content

Commit

Permalink
Fix/total holders (#1207)
Browse files Browse the repository at this point in the history
* change total holder from account balance aggregate --> erc20_contract

* split sync native. erc20 token
  • Loading branch information
duonghb53 authored Oct 7, 2024
1 parent 6f28c40 commit d7e6165
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
94 changes: 83 additions & 11 deletions src/components/queues/token/token.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class TokenProcessor implements OnModuleInit {
QUEUES.TOKEN.JOB_SYNC_ASSET,
{ explorer },
{
repeat: { cron: `${index + 1}0 * * * * *` },
repeat: { cron: `${(index + 1) % 6}0 * * * * *` },
},
);
this.tokenQueue.add(
Expand Down Expand Up @@ -236,10 +236,10 @@ export class TokenProcessor implements OnModuleInit {
@Process(QUEUES.TOKEN.JOB_SYNC_NATIVE_ASSET_HOLDER)
async syncNativeAssetHolder(job: Job): Promise<void> {
try {
const listHolderStatistic = await this.getHolders(job.data.explorer, [
ASSETS_TYPE.IBC,
ASSETS_TYPE.NATIVE,
]);
const listHolderStatistic = await this.getNativeHolders(
job.data.explorer,
[ASSETS_TYPE.IBC, ASSETS_TYPE.NATIVE],
);

await this.upsertTokenHolderStatistic(listHolderStatistic);
} catch (error) {
Expand All @@ -261,7 +261,42 @@ export class TokenProcessor implements OnModuleInit {
}
}

async getHolders(
async getErc20Holders(
explorer: Explorer,
type: string[],
): Promise<TokenHolderStatistic[]> {
const listHolder: TokenHolderStatistic[] = [];
const nativeAsset = await this.assetsRepository.find({
where: {
type: In(type),
explorer: { id: explorer.id },
},
});

const totalHolders = await this.getErc20HolderWithPagination(
nativeAsset,
explorer,
);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const [_index, asset] of nativeAsset.entries()) {
const newTotalHolder =
totalHolders.filter((f) => f.address === asset.denom)[0]
?.total_holder || 0;

const newHolderStatistic = new TokenHolderStatistic();
newHolderStatistic.id = null;
newHolderStatistic.asset = asset;
newHolderStatistic.totalHolder = newTotalHolder;
newHolderStatistic.date = new Date();

listHolder.push(newHolderStatistic);
}

return listHolder;
}

async getNativeHolders(
explorer: Explorer,
type: string[],
): Promise<TokenHolderStatistic[]> {
Expand All @@ -273,7 +308,7 @@ export class TokenProcessor implements OnModuleInit {
},
});

const totalHolders = await this.getHolderWithPagination(
const totalHolders = await this.getNativeHolderWithPagination(
nativeAsset,
explorer,
);
Expand Down Expand Up @@ -361,9 +396,10 @@ export class TokenProcessor implements OnModuleInit {
async syncErc20AssetHolder(job: Job): Promise<void> {
try {
const erc20Asset = await this.getNewErc20Info(job.data.explorer);
const listHolderStatistic = await this.getHolders(job.data.explorer, [
ASSETS_TYPE.ERC20,
]);
const listHolderStatistic = await this.getErc20Holders(
job.data.explorer,
[ASSETS_TYPE.ERC20],
);

await this.assetsRepository.save(erc20Asset);
await this.upsertTokenHolderStatistic(listHolderStatistic);
Expand Down Expand Up @@ -433,7 +469,43 @@ export class TokenProcessor implements OnModuleInit {
return result;
}

async getHolderWithPagination(assets: Asset[], explorer: Explorer) {
async getErc20HolderWithPagination(assets: Asset[], explorer: Explorer) {
const totalHolders = [];
let page = 0;
let index = 0;
const limit = INDEXER_API_V2.MAX_REQUEST;
do {
index = page * limit;
const subQuery = `erc20_contract(limit: $limit, offset: $offset) {
total_holder
address
}`;

const query = util.format(
INDEXER_API_V2.GRAPH_QL.BASE_QUERY,
explorer.chainDb,
subQuery,
);
const graphqlQueryTotalHolder: any = {
query,
operationName: INDEXER_API_V2.OPERATION_NAME.BASE_QUERY,
variables: { limit: limit },
};

graphqlQueryTotalHolder.variables.offset = index;

const holders = (
await this.serviceUtil.fetchDataFromGraphQL(graphqlQueryTotalHolder)
)?.data[explorer.chainDb].erc20_contract;

totalHolders.push(holders);
page++;
} while (index < assets.length);

return totalHolders.flat();
}

async getNativeHolderWithPagination(assets: Asset[], explorer: Explorer) {
let totalHolders = {};
let page = 0;
let index = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export const INDEXER_API_V2 = {
}
}
}`,
BASE_QUERY: `query BaseQuery {
BASE_QUERY: `query BaseQuery($limit: Int = 100, $offset: Int = 0) {
%s { %s } }`,
LIST_VALIDATOR: `query ListValidator($address: [String!] = null) {
%s {
Expand Down

0 comments on commit d7e6165

Please sign in to comment.