From b0735d6b3520eea0880994b5d56a5e2e35218951 Mon Sep 17 00:00:00 2001 From: Thanh Hao Date: Sat, 18 Mar 2023 16:48:09 +0700 Subject: [PATCH 1/8] [Thanhhao1986][Dev] Fix: Validator load performance (#734) * Refactore performance validator * Commit code * Fix: Validator load performance * Fix: Validator load performance * Fix: Validator load performance * Fix: Validator load performance * Fix: Validator load performance --- .../delegator-reward.repository.ts | 35 ++------------- .../validator/services/validator.service.ts | 44 ++++++++++--------- 2 files changed, 26 insertions(+), 53 deletions(-) diff --git a/src/components/validator/repositories/delegator-reward.repository.ts b/src/components/validator/repositories/delegator-reward.repository.ts index bfc245a4..72b38cf8 100644 --- a/src/components/validator/repositories/delegator-reward.repository.ts +++ b/src/components/validator/repositories/delegator-reward.repository.ts @@ -1,34 +1,5 @@ -import { DelegatorReward } from "../../../shared/entities/delegator-reward.entity"; -import { EntityRepository, Repository } from "typeorm"; +import { DelegatorReward } from '../../../shared/entities/delegator-reward.entity'; +import { EntityRepository, Repository } from 'typeorm'; @EntityRepository(DelegatorReward) -export class DelegatorRewardRepository extends Repository { - - /** - * Get reward by address - * @param delegatorAddress - * @param validatorAdress - */ - async getRewardByAddress(delegatorAddress: string[], validatorAdress: string[]) { - return await this.createQueryBuilder() - .select('validator_address, delegator_address, SUM(amount) amount') - .where(' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)') - .setParameters({delegatorAddress, validatorAdress}) - .groupBy('validator_address, delegator_address') - .getRawMany(); - } - - /** - * Get rew - * @param delegatorAddress - * @returns - */ - async getClaimRewardByDelegatorAddress(delegatorAddress: string) { - return await this.createQueryBuilder() - .select('delegator_address, SUM(amount) amount') - .where('delegator_address =:delegatorAddress') - .setParameter("delegatorAddress", delegatorAddress) - .groupBy('delegator_address') - .getRawOne(); - } -} \ No newline at end of file +export class DelegatorRewardRepository extends Repository {} diff --git a/src/components/validator/services/validator.service.ts b/src/components/validator/services/validator.service.ts index 8fb02af2..8cabc967 100644 --- a/src/components/validator/services/validator.service.ts +++ b/src/components/validator/services/validator.service.ts @@ -2,7 +2,12 @@ import { Injectable } from '@nestjs/common'; import { plainToClass } from 'class-transformer'; import { DelegationRepository } from '../repositories/delegation.repository'; -import { AkcLogger, INDEXER_API, RequestContext } from '../../../shared'; +import { + AkcLogger, + INDEXER_API, + LINK_API, + RequestContext, +} from '../../../shared'; import { DelegationParamsDto } from '../dtos/delegation-params.dto'; import * as util from 'util'; @@ -21,6 +26,7 @@ export class ValidatorService { api: string; private indexerUrl; private indexerChainId; + private coinMinimalDenom: string; constructor( private readonly logger: AkcLogger, @@ -36,6 +42,7 @@ export class ValidatorService { this.api = appParams.node.api; this.indexerUrl = appParams.indexer.url; this.indexerChainId = appParams.indexer.chainId; + this.coinMinimalDenom = appParams.chainInfo.coinMinimalDenom; } async getTotalValidator(): Promise { @@ -195,8 +202,6 @@ export class ValidatorService { ): Promise { this.logger.log(ctx, `${this.getDelegations.name} was called!`); const result: any = {}; - //get available balance - const accountData = await this.serviceUtil.getDataAPI( `${this.indexerUrl}${util.format( INDEXER_API.ACCOUNT_DELEGATIONS, @@ -217,12 +222,14 @@ export class ValidatorService { result.available_balance = Number(data.account_balances[0].amount); } result.claim_reward = 0; - const withdrawReward = - await this.delegatorRewardRepository.getClaimRewardByDelegatorAddress( - delegatorAddress, - ); - if (withdrawReward) { - result.claim_reward = Number(withdrawReward?.amount); + + // Call indexer get data delegations + const rewards = accountData?.data.account_delegate_rewards?.rewards; + const totalReward = accountData?.data.account_delegate_rewards?.total?.find( + (item) => item.denom === this.coinMinimalDenom, + ); + if (totalReward) { + result.claim_reward = Number(totalReward?.amount) || 0; } const delegations: any = []; @@ -230,7 +237,7 @@ export class ValidatorService { const delegatorAddr: string[] = []; if (data?.account_delegations) { const delegationsData = data.account_delegations; - for (let i = 0; i < delegationsData.length; i++) { + for (let i = 0; i < delegationsData?.length; i++) { const delegation: any = {}; const item = delegationsData[i]; delegation.amount_staked = Number(item.balance.amount); @@ -259,17 +266,14 @@ export class ValidatorService { if (delegations.length > 0) { const ranks = await this.validatorRepository.getRanks(validatorAddress); - const delegatorRewards = - await this.delegatorRewardRepository.getRewardByAddress( - delegatorAddr, - validatorAddress, - ); - for (let i = 0; i < delegations.length; i++) { + for (let i = 0; i < rewards?.length; i++) { const item = delegations[i]; // Set Rank for validators const rank = ranks.find( - (f) => f.operator_address === item.validator_address, + (f) => + f.operator_address === item.validator_address && + item.reward?.denom === this.coinMinimalDenom, ); if (rank) { item.validator_name = rank.title; @@ -279,10 +283,8 @@ export class ValidatorService { } // Set reward for validators - const reward = delegatorRewards.find( - (f) => - f.validator_address === item.validator_address && - f.validator_address === item.validator_address, + const reward = rewards.find( + (f) => f.validator_address === item.validator_address, ); if (reward) { item.reward = Number(reward.amount); From 4edf7174b3ed87a71a9287cd015d847299ed481a Mon Sep 17 00:00:00 2001 From: nthduc95 Date: Tue, 21 Mar 2023 10:22:50 +0700 Subject: [PATCH 2/8] Fix balance get by owner Revert source code --- .../cw20-token/services/cw20-token.service.ts | 3 +- .../delegator-reward.repository.ts | 35 +++++++++++++-- .../validator/services/validator.service.ts | 44 +++++++++---------- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/components/cw20-token/services/cw20-token.service.ts b/src/components/cw20-token/services/cw20-token.service.ts index a31faa46..cfcafe76 100644 --- a/src/components/cw20-token/services/cw20-token.service.ts +++ b/src/components/cw20-token/services/cw20-token.service.ts @@ -148,7 +148,8 @@ export class Cw20TokenService { }), ]); - assetDto.balance = totalBalances ? totalBalances.total : 0; + assetDto.balance = + totalBalances && totalBalances?.total ? totalBalances.total : 0; // price of aura if (tokenData) { assetDto.price = tokenData.current_price || 0; diff --git a/src/components/validator/repositories/delegator-reward.repository.ts b/src/components/validator/repositories/delegator-reward.repository.ts index 72b38cf8..bfc245a4 100644 --- a/src/components/validator/repositories/delegator-reward.repository.ts +++ b/src/components/validator/repositories/delegator-reward.repository.ts @@ -1,5 +1,34 @@ -import { DelegatorReward } from '../../../shared/entities/delegator-reward.entity'; -import { EntityRepository, Repository } from 'typeorm'; +import { DelegatorReward } from "../../../shared/entities/delegator-reward.entity"; +import { EntityRepository, Repository } from "typeorm"; @EntityRepository(DelegatorReward) -export class DelegatorRewardRepository extends Repository {} +export class DelegatorRewardRepository extends Repository { + + /** + * Get reward by address + * @param delegatorAddress + * @param validatorAdress + */ + async getRewardByAddress(delegatorAddress: string[], validatorAdress: string[]) { + return await this.createQueryBuilder() + .select('validator_address, delegator_address, SUM(amount) amount') + .where(' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)') + .setParameters({delegatorAddress, validatorAdress}) + .groupBy('validator_address, delegator_address') + .getRawMany(); + } + + /** + * Get rew + * @param delegatorAddress + * @returns + */ + async getClaimRewardByDelegatorAddress(delegatorAddress: string) { + return await this.createQueryBuilder() + .select('delegator_address, SUM(amount) amount') + .where('delegator_address =:delegatorAddress') + .setParameter("delegatorAddress", delegatorAddress) + .groupBy('delegator_address') + .getRawOne(); + } +} \ No newline at end of file diff --git a/src/components/validator/services/validator.service.ts b/src/components/validator/services/validator.service.ts index 8cabc967..8fb02af2 100644 --- a/src/components/validator/services/validator.service.ts +++ b/src/components/validator/services/validator.service.ts @@ -2,12 +2,7 @@ import { Injectable } from '@nestjs/common'; import { plainToClass } from 'class-transformer'; import { DelegationRepository } from '../repositories/delegation.repository'; -import { - AkcLogger, - INDEXER_API, - LINK_API, - RequestContext, -} from '../../../shared'; +import { AkcLogger, INDEXER_API, RequestContext } from '../../../shared'; import { DelegationParamsDto } from '../dtos/delegation-params.dto'; import * as util from 'util'; @@ -26,7 +21,6 @@ export class ValidatorService { api: string; private indexerUrl; private indexerChainId; - private coinMinimalDenom: string; constructor( private readonly logger: AkcLogger, @@ -42,7 +36,6 @@ export class ValidatorService { this.api = appParams.node.api; this.indexerUrl = appParams.indexer.url; this.indexerChainId = appParams.indexer.chainId; - this.coinMinimalDenom = appParams.chainInfo.coinMinimalDenom; } async getTotalValidator(): Promise { @@ -202,6 +195,8 @@ export class ValidatorService { ): Promise { this.logger.log(ctx, `${this.getDelegations.name} was called!`); const result: any = {}; + //get available balance + const accountData = await this.serviceUtil.getDataAPI( `${this.indexerUrl}${util.format( INDEXER_API.ACCOUNT_DELEGATIONS, @@ -222,14 +217,12 @@ export class ValidatorService { result.available_balance = Number(data.account_balances[0].amount); } result.claim_reward = 0; - - // Call indexer get data delegations - const rewards = accountData?.data.account_delegate_rewards?.rewards; - const totalReward = accountData?.data.account_delegate_rewards?.total?.find( - (item) => item.denom === this.coinMinimalDenom, - ); - if (totalReward) { - result.claim_reward = Number(totalReward?.amount) || 0; + const withdrawReward = + await this.delegatorRewardRepository.getClaimRewardByDelegatorAddress( + delegatorAddress, + ); + if (withdrawReward) { + result.claim_reward = Number(withdrawReward?.amount); } const delegations: any = []; @@ -237,7 +230,7 @@ export class ValidatorService { const delegatorAddr: string[] = []; if (data?.account_delegations) { const delegationsData = data.account_delegations; - for (let i = 0; i < delegationsData?.length; i++) { + for (let i = 0; i < delegationsData.length; i++) { const delegation: any = {}; const item = delegationsData[i]; delegation.amount_staked = Number(item.balance.amount); @@ -266,14 +259,17 @@ export class ValidatorService { if (delegations.length > 0) { const ranks = await this.validatorRepository.getRanks(validatorAddress); - for (let i = 0; i < rewards?.length; i++) { + const delegatorRewards = + await this.delegatorRewardRepository.getRewardByAddress( + delegatorAddr, + validatorAddress, + ); + for (let i = 0; i < delegations.length; i++) { const item = delegations[i]; // Set Rank for validators const rank = ranks.find( - (f) => - f.operator_address === item.validator_address && - item.reward?.denom === this.coinMinimalDenom, + (f) => f.operator_address === item.validator_address, ); if (rank) { item.validator_name = rank.title; @@ -283,8 +279,10 @@ export class ValidatorService { } // Set reward for validators - const reward = rewards.find( - (f) => f.validator_address === item.validator_address, + const reward = delegatorRewards.find( + (f) => + f.validator_address === item.validator_address && + f.validator_address === item.validator_address, ); if (reward) { item.reward = Number(reward.amount); From 0c7c476f9f57dfcc99735efc5ae8075a122739bc Mon Sep 17 00:00:00 2001 From: nthduc95 Date: Tue, 21 Mar 2023 11:36:20 +0700 Subject: [PATCH 3/8] format code --- .../delegator-reward.repository.ts | 64 ++++++++++--------- .../validator/services/validator.service.ts | 4 +- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/components/validator/repositories/delegator-reward.repository.ts b/src/components/validator/repositories/delegator-reward.repository.ts index bfc245a4..a4653145 100644 --- a/src/components/validator/repositories/delegator-reward.repository.ts +++ b/src/components/validator/repositories/delegator-reward.repository.ts @@ -1,34 +1,38 @@ -import { DelegatorReward } from "../../../shared/entities/delegator-reward.entity"; -import { EntityRepository, Repository } from "typeorm"; +import { DelegatorReward } from '../../../shared/entities/delegator-reward.entity'; +import { EntityRepository, Repository } from 'typeorm'; @EntityRepository(DelegatorReward) export class DelegatorRewardRepository extends Repository { + /** + * Get reward by address + * @param delegatorAddress + * @param validatorAdress + */ + async getRewardByAddress( + delegatorAddress: string[], + validatorAdress: string[], + ) { + return await this.createQueryBuilder() + .select('validator_address, delegator_address, SUM(amount) amount') + .where( + ' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)', + ) + .setParameters({ delegatorAddress, validatorAdress }) + .groupBy('validator_address, delegator_address') + .getRawMany(); + } - /** - * Get reward by address - * @param delegatorAddress - * @param validatorAdress - */ - async getRewardByAddress(delegatorAddress: string[], validatorAdress: string[]) { - return await this.createQueryBuilder() - .select('validator_address, delegator_address, SUM(amount) amount') - .where(' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)') - .setParameters({delegatorAddress, validatorAdress}) - .groupBy('validator_address, delegator_address') - .getRawMany(); - } - - /** - * Get rew - * @param delegatorAddress - * @returns - */ - async getClaimRewardByDelegatorAddress(delegatorAddress: string) { - return await this.createQueryBuilder() - .select('delegator_address, SUM(amount) amount') - .where('delegator_address =:delegatorAddress') - .setParameter("delegatorAddress", delegatorAddress) - .groupBy('delegator_address') - .getRawOne(); - } -} \ No newline at end of file + /** + * Get rew + * @param delegatorAddress + * @returns + */ + async getClaimRewardByDelegatorAddress(delegatorAddress: string) { + return await this.createQueryBuilder() + .select('delegator_address, SUM(amount) amount') + .where('delegator_address =:delegatorAddress') + .setParameter('delegatorAddress', delegatorAddress) + .groupBy('delegator_address') + .getRawOne(); + } +} diff --git a/src/components/validator/services/validator.service.ts b/src/components/validator/services/validator.service.ts index 8fb02af2..dcdaf479 100644 --- a/src/components/validator/services/validator.service.ts +++ b/src/components/validator/services/validator.service.ts @@ -280,9 +280,7 @@ export class ValidatorService { // Set reward for validators const reward = delegatorRewards.find( - (f) => - f.validator_address === item.validator_address && - f.validator_address === item.validator_address, + (f) => f.validator_address === item.validator_address, ); if (reward) { item.reward = Number(reward.amount); From 6c170e0bec1cee20711dbe5f9cebecbe755c5060 Mon Sep 17 00:00:00 2001 From: Duc Nguyen <119287881+nthduc95@users.noreply.github.com> Date: Tue, 28 Mar 2023 08:48:21 +0700 Subject: [PATCH 4/8] Remove check owner when register contract code (#739) --- .../contract-code/services/contract-code.service.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/components/contract-code/services/contract-code.service.ts b/src/components/contract-code/services/contract-code.service.ts index 19922acc..edf7248a 100644 --- a/src/components/contract-code/services/contract-code.service.ts +++ b/src/components/contract-code/services/contract-code.service.ts @@ -58,13 +58,6 @@ export class ContractCodeService { Message: ERROR_MAP.CONTRACT_CODE_ID_EXIST.Message }; } - //check creator - if (contractCodeDb.creator !== request.account_address) { - return { - Code: ERROR_MAP.NOT_CONTRACT_CREATOR.Code, - Message: ERROR_MAP.NOT_CONTRACT_CREATOR.Message - }; - } //register in indexer const properties = { codeId: request.code_id, From 51160b93f62e1dadd6701e9a0a7f74541699720f Mon Sep 17 00:00:00 2001 From: Duc Nguyen <119287881+nthduc95@users.noreply.github.com> Date: Tue, 28 Mar 2023 10:10:12 +0700 Subject: [PATCH 5/8] [nthduc95][Dev] Refactor performance validator (#738) --- .../delegator-reward.repository.ts | 50 ++++++---------- .../validator/services/validator.service.ts | 60 +++++++++++-------- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/src/components/validator/repositories/delegator-reward.repository.ts b/src/components/validator/repositories/delegator-reward.repository.ts index a4653145..2761913d 100644 --- a/src/components/validator/repositories/delegator-reward.repository.ts +++ b/src/components/validator/repositories/delegator-reward.repository.ts @@ -3,36 +3,22 @@ import { EntityRepository, Repository } from 'typeorm'; @EntityRepository(DelegatorReward) export class DelegatorRewardRepository extends Repository { - /** - * Get reward by address - * @param delegatorAddress - * @param validatorAdress - */ - async getRewardByAddress( - delegatorAddress: string[], - validatorAdress: string[], - ) { - return await this.createQueryBuilder() - .select('validator_address, delegator_address, SUM(amount) amount') - .where( - ' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)', - ) - .setParameters({ delegatorAddress, validatorAdress }) - .groupBy('validator_address, delegator_address') - .getRawMany(); - } - - /** - * Get rew - * @param delegatorAddress - * @returns - */ - async getClaimRewardByDelegatorAddress(delegatorAddress: string) { - return await this.createQueryBuilder() - .select('delegator_address, SUM(amount) amount') - .where('delegator_address =:delegatorAddress') - .setParameter('delegatorAddress', delegatorAddress) - .groupBy('delegator_address') - .getRawOne(); - } + // /** + // * Get reward by address + // * @param delegatorAddress + // * @param validatorAdress + // */ + // async getRewardByAddress( + // delegatorAddress: string[], + // validatorAdress: string[], + // ) { + // return await this.createQueryBuilder() + // .select('validator_address, delegator_address, SUM(amount) amount') + // .where( + // ' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)', + // ) + // .setParameters({ delegatorAddress, validatorAdress }) + // .groupBy('validator_address, delegator_address') + // .getRawMany(); + // } } diff --git a/src/components/validator/services/validator.service.ts b/src/components/validator/services/validator.service.ts index dcdaf479..4c1bb2e1 100644 --- a/src/components/validator/services/validator.service.ts +++ b/src/components/validator/services/validator.service.ts @@ -2,7 +2,12 @@ import { Injectable } from '@nestjs/common'; import { plainToClass } from 'class-transformer'; import { DelegationRepository } from '../repositories/delegation.repository'; -import { AkcLogger, INDEXER_API, RequestContext } from '../../../shared'; +import { + AkcLogger, + INDEXER_API, + LINK_API, + RequestContext, +} from '../../../shared'; import { DelegationParamsDto } from '../dtos/delegation-params.dto'; import * as util from 'util'; @@ -21,6 +26,7 @@ export class ValidatorService { api: string; private indexerUrl; private indexerChainId; + private coinMinimalDenom: string; constructor( private readonly logger: AkcLogger, @@ -36,6 +42,7 @@ export class ValidatorService { this.api = appParams.node.api; this.indexerUrl = appParams.indexer.url; this.indexerChainId = appParams.indexer.chainId; + this.coinMinimalDenom = appParams.chainInfo.coinMinimalDenom; } async getTotalValidator(): Promise { @@ -195,8 +202,6 @@ export class ValidatorService { ): Promise { this.logger.log(ctx, `${this.getDelegations.name} was called!`); const result: any = {}; - //get available balance - const accountData = await this.serviceUtil.getDataAPI( `${this.indexerUrl}${util.format( INDEXER_API.ACCOUNT_DELEGATIONS, @@ -217,12 +222,14 @@ export class ValidatorService { result.available_balance = Number(data.account_balances[0].amount); } result.claim_reward = 0; - const withdrawReward = - await this.delegatorRewardRepository.getClaimRewardByDelegatorAddress( - delegatorAddress, - ); - if (withdrawReward) { - result.claim_reward = Number(withdrawReward?.amount); + + // Call indexer get data delegations + const rewards = accountData?.data.account_delegate_rewards?.rewards; + const totalReward = accountData?.data.account_delegate_rewards?.total?.find( + (item) => item.denom === this.coinMinimalDenom, + ); + if (totalReward) { + result.claim_reward = Number(totalReward?.amount) || 0; } const delegations: any = []; @@ -230,17 +237,15 @@ export class ValidatorService { const delegatorAddr: string[] = []; if (data?.account_delegations) { const delegationsData = data.account_delegations; - for (let i = 0; i < delegationsData.length; i++) { + for (let i = 0; i < delegationsData?.length; i++) { const delegation: any = {}; const item = delegationsData[i]; delegation.amount_staked = Number(item.balance.amount); delegation.validator_address = item.delegation.validator_address; delegation.pending_reward = 0; - if ( - data?.account_delegate_rewards && - data.account_delegate_rewards?.rewards - ) { - const findReward = data.account_delegate_rewards?.rewards.find( + + if (rewards?.length > 0) { + const findReward = rewards.find( (i) => i.validator_address === item.delegation.validator_address, ); if (findReward && findReward.reward.length > 0) { @@ -259,11 +264,11 @@ export class ValidatorService { if (delegations.length > 0) { const ranks = await this.validatorRepository.getRanks(validatorAddress); - const delegatorRewards = - await this.delegatorRewardRepository.getRewardByAddress( - delegatorAddr, - validatorAddress, - ); + // const delegatorRewards = + // await this.delegatorRewardRepository.getRewardByAddress( + // delegatorAddr, + // validatorAddress, + // ); for (let i = 0; i < delegations.length; i++) { const item = delegations[i]; @@ -278,13 +283,16 @@ export class ValidatorService { item.jailed = rank.jailed; } + // TODO: Set temp data total reward = 0 + item.reward = 0; + // Set reward for validators - const reward = delegatorRewards.find( - (f) => f.validator_address === item.validator_address, - ); - if (reward) { - item.reward = Number(reward.amount); - } + // const reward = delegatorRewards.find( + // (f) => f.validator_address === item.validator_address, + // ); + // if (reward) { + // item.reward = Number(reward.amount); + // } } } From dbb896813621b73c29fe54f30dc1178a4c46fb0a Mon Sep 17 00:00:00 2001 From: Duc Nguyen <119287881+nthduc95@users.noreply.github.com> Date: Tue, 28 Mar 2023 10:48:15 +0700 Subject: [PATCH 6/8] Revert "Remove check owner when register contract code (#739)" (#741) --- .../contract-code/services/contract-code.service.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/components/contract-code/services/contract-code.service.ts b/src/components/contract-code/services/contract-code.service.ts index edf7248a..19922acc 100644 --- a/src/components/contract-code/services/contract-code.service.ts +++ b/src/components/contract-code/services/contract-code.service.ts @@ -58,6 +58,13 @@ export class ContractCodeService { Message: ERROR_MAP.CONTRACT_CODE_ID_EXIST.Message }; } + //check creator + if (contractCodeDb.creator !== request.account_address) { + return { + Code: ERROR_MAP.NOT_CONTRACT_CREATOR.Code, + Message: ERROR_MAP.NOT_CONTRACT_CREATOR.Message + }; + } //register in indexer const properties = { codeId: request.code_id, From 3257373d831b8c94471de7ea97ce0e703a1042f4 Mon Sep 17 00:00:00 2001 From: Duc Nguyen <119287881+nthduc95@users.noreply.github.com> Date: Tue, 28 Mar 2023 15:31:50 +0700 Subject: [PATCH 7/8] [nthduc95][Euphoria] Baseline code 20230328 (#744) --- .../cw20-token/services/cw20-token.service.ts | 3 +- .../delegator-reward.repository.ts | 52 +++++++--------- .../validator/services/validator.service.ts | 62 ++++++++++--------- src/shared/constants/common.ts | 2 +- 4 files changed, 58 insertions(+), 61 deletions(-) diff --git a/src/components/cw20-token/services/cw20-token.service.ts b/src/components/cw20-token/services/cw20-token.service.ts index a31faa46..cfcafe76 100644 --- a/src/components/cw20-token/services/cw20-token.service.ts +++ b/src/components/cw20-token/services/cw20-token.service.ts @@ -148,7 +148,8 @@ export class Cw20TokenService { }), ]); - assetDto.balance = totalBalances ? totalBalances.total : 0; + assetDto.balance = + totalBalances && totalBalances?.total ? totalBalances.total : 0; // price of aura if (tokenData) { assetDto.price = tokenData.current_price || 0; diff --git a/src/components/validator/repositories/delegator-reward.repository.ts b/src/components/validator/repositories/delegator-reward.repository.ts index bfc245a4..2761913d 100644 --- a/src/components/validator/repositories/delegator-reward.repository.ts +++ b/src/components/validator/repositories/delegator-reward.repository.ts @@ -1,34 +1,24 @@ -import { DelegatorReward } from "../../../shared/entities/delegator-reward.entity"; -import { EntityRepository, Repository } from "typeorm"; +import { DelegatorReward } from '../../../shared/entities/delegator-reward.entity'; +import { EntityRepository, Repository } from 'typeorm'; @EntityRepository(DelegatorReward) export class DelegatorRewardRepository extends Repository { - - /** - * Get reward by address - * @param delegatorAddress - * @param validatorAdress - */ - async getRewardByAddress(delegatorAddress: string[], validatorAdress: string[]) { - return await this.createQueryBuilder() - .select('validator_address, delegator_address, SUM(amount) amount') - .where(' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)') - .setParameters({delegatorAddress, validatorAdress}) - .groupBy('validator_address, delegator_address') - .getRawMany(); - } - - /** - * Get rew - * @param delegatorAddress - * @returns - */ - async getClaimRewardByDelegatorAddress(delegatorAddress: string) { - return await this.createQueryBuilder() - .select('delegator_address, SUM(amount) amount') - .where('delegator_address =:delegatorAddress') - .setParameter("delegatorAddress", delegatorAddress) - .groupBy('delegator_address') - .getRawOne(); - } -} \ No newline at end of file + // /** + // * Get reward by address + // * @param delegatorAddress + // * @param validatorAdress + // */ + // async getRewardByAddress( + // delegatorAddress: string[], + // validatorAdress: string[], + // ) { + // return await this.createQueryBuilder() + // .select('validator_address, delegator_address, SUM(amount) amount') + // .where( + // ' delegator_address IN (:delegatorAddress) AND validator_address IN(:validatorAdress)', + // ) + // .setParameters({ delegatorAddress, validatorAdress }) + // .groupBy('validator_address, delegator_address') + // .getRawMany(); + // } +} diff --git a/src/components/validator/services/validator.service.ts b/src/components/validator/services/validator.service.ts index 8fb02af2..4c1bb2e1 100644 --- a/src/components/validator/services/validator.service.ts +++ b/src/components/validator/services/validator.service.ts @@ -2,7 +2,12 @@ import { Injectable } from '@nestjs/common'; import { plainToClass } from 'class-transformer'; import { DelegationRepository } from '../repositories/delegation.repository'; -import { AkcLogger, INDEXER_API, RequestContext } from '../../../shared'; +import { + AkcLogger, + INDEXER_API, + LINK_API, + RequestContext, +} from '../../../shared'; import { DelegationParamsDto } from '../dtos/delegation-params.dto'; import * as util from 'util'; @@ -21,6 +26,7 @@ export class ValidatorService { api: string; private indexerUrl; private indexerChainId; + private coinMinimalDenom: string; constructor( private readonly logger: AkcLogger, @@ -36,6 +42,7 @@ export class ValidatorService { this.api = appParams.node.api; this.indexerUrl = appParams.indexer.url; this.indexerChainId = appParams.indexer.chainId; + this.coinMinimalDenom = appParams.chainInfo.coinMinimalDenom; } async getTotalValidator(): Promise { @@ -195,8 +202,6 @@ export class ValidatorService { ): Promise { this.logger.log(ctx, `${this.getDelegations.name} was called!`); const result: any = {}; - //get available balance - const accountData = await this.serviceUtil.getDataAPI( `${this.indexerUrl}${util.format( INDEXER_API.ACCOUNT_DELEGATIONS, @@ -217,12 +222,14 @@ export class ValidatorService { result.available_balance = Number(data.account_balances[0].amount); } result.claim_reward = 0; - const withdrawReward = - await this.delegatorRewardRepository.getClaimRewardByDelegatorAddress( - delegatorAddress, - ); - if (withdrawReward) { - result.claim_reward = Number(withdrawReward?.amount); + + // Call indexer get data delegations + const rewards = accountData?.data.account_delegate_rewards?.rewards; + const totalReward = accountData?.data.account_delegate_rewards?.total?.find( + (item) => item.denom === this.coinMinimalDenom, + ); + if (totalReward) { + result.claim_reward = Number(totalReward?.amount) || 0; } const delegations: any = []; @@ -230,17 +237,15 @@ export class ValidatorService { const delegatorAddr: string[] = []; if (data?.account_delegations) { const delegationsData = data.account_delegations; - for (let i = 0; i < delegationsData.length; i++) { + for (let i = 0; i < delegationsData?.length; i++) { const delegation: any = {}; const item = delegationsData[i]; delegation.amount_staked = Number(item.balance.amount); delegation.validator_address = item.delegation.validator_address; delegation.pending_reward = 0; - if ( - data?.account_delegate_rewards && - data.account_delegate_rewards?.rewards - ) { - const findReward = data.account_delegate_rewards?.rewards.find( + + if (rewards?.length > 0) { + const findReward = rewards.find( (i) => i.validator_address === item.delegation.validator_address, ); if (findReward && findReward.reward.length > 0) { @@ -259,11 +264,11 @@ export class ValidatorService { if (delegations.length > 0) { const ranks = await this.validatorRepository.getRanks(validatorAddress); - const delegatorRewards = - await this.delegatorRewardRepository.getRewardByAddress( - delegatorAddr, - validatorAddress, - ); + // const delegatorRewards = + // await this.delegatorRewardRepository.getRewardByAddress( + // delegatorAddr, + // validatorAddress, + // ); for (let i = 0; i < delegations.length; i++) { const item = delegations[i]; @@ -278,15 +283,16 @@ export class ValidatorService { item.jailed = rank.jailed; } + // TODO: Set temp data total reward = 0 + item.reward = 0; + // Set reward for validators - const reward = delegatorRewards.find( - (f) => - f.validator_address === item.validator_address && - f.validator_address === item.validator_address, - ); - if (reward) { - item.reward = Number(reward.amount); - } + // const reward = delegatorRewards.find( + // (f) => f.validator_address === item.validator_address, + // ); + // if (reward) { + // item.reward = Number(reward.amount); + // } } } diff --git a/src/shared/constants/common.ts b/src/shared/constants/common.ts index e5090baa..bf30bf3d 100644 --- a/src/shared/constants/common.ts +++ b/src/shared/constants/common.ts @@ -184,7 +184,7 @@ export const ERROR_MAP = { }, MINTER_OR_CONTRACT_ADDRESS_INVALID: { Code: 'E007', - Message: `Attestor address or contract address invalid`, + Message: `Creator address or contract address invalid`, }, YOUR_ADDRESS_INVALID: { Code: 'E008', From dee75c23d404fb2413a6cc926efa42ece347749c Mon Sep 17 00:00:00 2001 From: Duc Nguyen <119287881+nthduc95@users.noreply.github.com> Date: Tue, 28 Mar 2023 15:43:41 +0700 Subject: [PATCH 8/8] change message attestor to creator (#745) --- src/shared/constants/common.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/constants/common.ts b/src/shared/constants/common.ts index e5090baa..bf30bf3d 100644 --- a/src/shared/constants/common.ts +++ b/src/shared/constants/common.ts @@ -184,7 +184,7 @@ export const ERROR_MAP = { }, MINTER_OR_CONTRACT_ADDRESS_INVALID: { Code: 'E007', - Message: `Attestor address or contract address invalid`, + Message: `Creator address or contract address invalid`, }, YOUR_ADDRESS_INVALID: { Code: 'E008',