Skip to content

Commit

Permalink
Merge pull request #927 from aura-nw/baseline/main_20230914
Browse files Browse the repository at this point in the history
baseline/main_20230914
  • Loading branch information
nhphuc2411 authored Sep 14, 2023
2 parents 73483b0 + aa28470 commit 39e89ff
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/components/cw20-token/services/cw20-token.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { In, Not } from 'typeorm';
import { IsNull, Not } from 'typeorm';
import * as util from 'util';
import { AccountService } from '../../../components/account/services/account.service';
import {
Expand All @@ -23,7 +23,6 @@ export class Cw20TokenService {
private minimalDenom;
private decimals;
private precisionDiv;
private configUrl;
private chainDB;

constructor(
Expand All @@ -38,7 +37,6 @@ export class Cw20TokenService {
this.minimalDenom = this.appParams.chainInfo.coinMinimalDenom;
this.decimals = this.appParams.chainInfo.coinDecimals;
this.precisionDiv = this.appParams.chainInfo.precisionDiv;
this.configUrl = this.appParams.configUrl;
this.chainDB = this.appParams.indexerV2.chainDB;
}

Expand Down Expand Up @@ -117,7 +115,9 @@ export class Cw20TokenService {

let tokens = [];
if (asset?.length > 0) {
const listTokenMarketsInfo = await this.tokenMarketsRepository.find();
const listTokenMarketsInfo = await this.tokenMarketsRepository.find({
where: { contract_address: Not(IsNull()) },
});
tokens = asset.map((item) => {
const tokenMarketsInfo = listTokenMarketsInfo.find(
(f) => f.contract_address === item.smart_contract.address,
Expand Down Expand Up @@ -174,7 +174,10 @@ export class Cw20TokenService {
this.logger.log(ctx, `${this.getPriceById.name} was called!`);
if (query.contractAddress) {
return await this.tokenMarketsRepository.find({
where: { contract_address: query.contractAddress },
where: [
{ contract_address: query.contractAddress },
{ denom: query.contractAddress },
],
});
} else {
return await this.tokenMarketsRepository.find();
Expand Down Expand Up @@ -296,13 +299,10 @@ export class Cw20TokenService {
(str) => str?.minimal_denom || str?.denom,
);
if (ibcBalances?.length > 0) {
//get coin info from config
const configData = await this.serviceUtil.getDataAPI(
this.configUrl,
'',
ctx,
);
const coins = configData?.coins;
//get coin info from DB
const listIbcTokenMarketsInfo = await this.tokenMarketsRepository.find({
where: { denom: Not(IsNull()) },
});
for (let i = 0; i < ibcBalances.length; i++) {
const item = ibcBalances[i];
const asset = new AssetDto();
Expand All @@ -312,13 +312,20 @@ export class Cw20TokenService {
);
//get ibc info
const denom = item.minimal_denom || item.denom;
const findCoin = coins?.find((f) => f.denom === denom);
if (findCoin) {
asset.name = findCoin.name;
asset.symbol = findCoin.display;
asset.image = findCoin.logo;
asset.denom = findCoin.denom;
asset.decimals = Number(findCoin.decimal) || 0;
const tokenMarketsInfo = listIbcTokenMarketsInfo.find(
(f) => f.denom === denom,
);
if (tokenMarketsInfo) {
asset.name = tokenMarketsInfo.name;
asset.symbol = tokenMarketsInfo.symbol;
asset.image = tokenMarketsInfo.image;
asset.denom = tokenMarketsInfo.denom;
asset.verify_status = tokenMarketsInfo?.verify_status || '';
asset.verify_text = tokenMarketsInfo?.verify_text || '';
asset.price = tokenMarketsInfo?.current_price || null;
asset.price_change_percentage_24h =
tokenMarketsInfo?.price_change_percentage_24h || 0;
asset.decimals = Number(tokenMarketsInfo.decimal) || 0;
result.push(asset);
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/migrations/1694571809657-add-denom-column-token-market.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class addDenomColumnTokenMarket1694571809657
implements MigrationInterface
{
name = 'addDenomColumnTokenMarket1694571809657';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`token_markets\` ADD \`denom\` varchar(255) NULL AFTER \`contract_address\``,
);
await queryRunner.query(
`ALTER TABLE \`token_markets\` ADD \`decimal\` int NOT NULL DEFAULT '0'`,
);
await queryRunner.query(
`ALTER TABLE \`token_markets\` CHANGE \`code_id\` \`code_id\` int NOT NULL DEFAULT '0'`,
);
await queryRunner.query(
`ALTER TABLE \`token_markets\` CHANGE \`coin_id\` \`coin_id\` varchar(255) NOT NULL DEFAULT ''`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`token_markets\` DROP COLUMN \`denom\``,
);
await queryRunner.query(
`ALTER TABLE \`token_markets\` DROP COLUMN \`decimal\``,
);
}
}
6 changes: 6 additions & 0 deletions src/shared/entities/token-markets.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ export class TokenMarkets extends BaseEntityIncrementId {

@Column({ name: 'verify_text', nullable: true })
verify_text: string;

@Column({ name: 'denom', nullable: true })
denom: string;

@Column({ name: 'decimal', default: 0 })
decimal: number;
}

0 comments on commit 39e89ff

Please sign in to comment.