Skip to content

Commit

Permalink
Merge pull request #747 from aura-nw/baseline/main_20230328
Browse files Browse the repository at this point in the history
Baseline/main 20230328
  • Loading branch information
nhphuc2411 authored Mar 28, 2023
2 parents 3d783fd + fc1ebb6 commit 060063d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 61 deletions.
3 changes: 2 additions & 1 deletion src/components/cw20-token/services/cw20-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DelegatorReward> {

/**
* 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();
// }
}
62 changes: 34 additions & 28 deletions src/components/validator/services/validator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,6 +26,7 @@ export class ValidatorService {
api: string;
private indexerUrl;
private indexerChainId;
private coinMinimalDenom: string;

constructor(
private readonly logger: AkcLogger,
Expand All @@ -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<number> {
Expand Down Expand Up @@ -195,8 +202,6 @@ export class ValidatorService {
): Promise<any> {
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,
Expand All @@ -217,30 +222,30 @@ 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 = [];
const validatorAddress: string[] = [];
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) {
Expand All @@ -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];

Expand All @@ -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);
// }
}
}

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 @@ -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',
Expand Down

0 comments on commit 060063d

Please sign in to comment.