From 05b7aa1be52558002fdda79298ea815c9b9745aa Mon Sep 17 00:00:00 2001 From: fly33499 Date: Wed, 13 Oct 2021 20:11:53 +0900 Subject: [PATCH] feat: update distribution service & testcase --- sdk/FirmaDistributionService.ts | 27 +++++++++ .../distribution/DistributionQueryClient.ts | 16 ++++++ test/15.distribution_query.test.ts | 57 ++++++++++++++++--- 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/sdk/FirmaDistributionService.ts b/sdk/FirmaDistributionService.ts index b0cb3b3..85a0b05 100644 --- a/sdk/FirmaDistributionService.ts +++ b/sdk/FirmaDistributionService.ts @@ -5,6 +5,7 @@ import { FirmaWalletService } from "./FirmaWalletService"; import { FirmaConfig } from "./FirmaConfig"; import { FirmaUtil } from "./FirmaUtil"; import { BroadcastTxResponse } from './firmachain/common/stargateclient'; +import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin'; export class FirmaDistributionService { @@ -155,6 +156,32 @@ export class FirmaDistributionService { } } + public async getValidatorOutStandingReward(validatorAddress: string): Promise { + try { + let queryClient = new DistributionQueryClient(this._config.restApiAddress); + let result = await queryClient.queryGetValidatorOutStandingReward(validatorAddress); + + return result; + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } + + public async getValidatorCommission(validatorAddress: string): Promise { + try { + let queryClient = new DistributionQueryClient(this._config.restApiAddress); + let result = await queryClient.queryGetValidatorCommission(validatorAddress); + + return result; + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } + public async getTotalRewardInfo(address: string): Promise { try { let queryClient = new DistributionQueryClient(this._config.restApiAddress); diff --git a/sdk/firmachain/distribution/DistributionQueryClient.ts b/sdk/firmachain/distribution/DistributionQueryClient.ts index e0946be..e3665eb 100644 --- a/sdk/firmachain/distribution/DistributionQueryClient.ts +++ b/sdk/firmachain/distribution/DistributionQueryClient.ts @@ -1,4 +1,5 @@ import Axios, { AxiosInstance } from 'axios'; +import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin"; interface TotalRewardInfoInternal { rewards: { validator_address: string, reward: { denom: string, amount: string }[] }[]; @@ -42,6 +43,21 @@ export class DistributionQueryClient { return result.data.rewards[0].amount; } + + public async queryGetValidatorOutStandingReward(address: string): Promise { + let path = "/cosmos/distribution/v1beta1/validators" + "/" + address + "/outstanding_rewards"; + + var result = await this._axios.get(path); + return result.data.rewards.rewards; + } + + public async queryGetValidatorCommission(address: string): Promise { + let path = "/cosmos/distribution/v1beta1/validators" + "/" + address + "/commission"; + + var result = await this._axios.get(path); + return result.data.commission.commission; + } + public async queryGetTotalRewardInfo(address: string): Promise { let path = "/cosmos/distribution/v1beta1/delegators" + "/" + address + "/rewards"; diff --git a/test/15.distribution_query.test.ts b/test/15.distribution_query.test.ts index 8331a44..d39b196 100644 --- a/test/15.distribution_query.test.ts +++ b/test/15.distribution_query.test.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import { FirmaUtil } from '..'; import { FirmaSDK } from "../sdk/FirmaSDK" import { aliceMnemonic, bobMnemonic, TestChainConfig, validatorMnemonic } from './config_test'; @@ -9,7 +10,50 @@ describe('[15. Distribution Query Test]', () => { it('get communityPool', async () => { var poolAmount = await firma.Distribution.getCommunityPool(); - //console.log("communityPool :" + poolAmount); + console.log("communityPool :" + poolAmount); + }) + + it('get validator commission', async () => { + + let validatorList = await firma.Staking.getValidatorList(); + let validatorAddress = validatorList[0].operator_address; + + var result = await firma.Distribution.getValidatorCommission(validatorAddress); + //console.log(result[0]); + }) + + it('get validator outstanding_rewards', async () => { + + let validatorList = await firma.Staking.getValidatorList(); + let validatorAddress = validatorList[0].operator_address; + + var result = await firma.Distribution.getValidatorOutStandingReward(validatorAddress); + //console.log(result[0]); + }) + + it('get validator self delegator\'s reward', async () => { + + let validatorList = await firma.Staking.getValidatorList(); + let validatorAddress = validatorList[0].operator_address; + + let address = FirmaUtil.getAccAddressFromValOperAddress(validatorAddress); + let result = await firma.Distribution.getTotalRewardInfo(address); + + //console.log("validatorAddress: " + validatorAddress); + //console.log("address: " + address); + //console.log(result); + }) + + it('get commission from self delegator', async () => { + + let validatorList = await firma.Staking.getValidatorList(); + let validatorAddress = validatorList[0].operator_address; + + let address = FirmaUtil.getAccAddressFromValOperAddress(validatorAddress); + let newValidatorAddress = FirmaUtil.getValOperAddressFromAccAddress(address); + + let commission = await firma.Distribution.getValidatorCommission(newValidatorAddress); + //console.log(commission[0]); }) // Total Reward based user side @@ -19,7 +63,6 @@ describe('[15. Distribution Query Test]', () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); // INFO: All the validator information and interests information that the user needs to receive can be viewed here. - var result = await firma.Distribution.getTotalRewardInfo(await wallet.getAddress()); for (var i = 0; i < result.rewards.length; i++) { @@ -38,12 +81,10 @@ describe('[15. Distribution Query Test]', () => { var totalReward = await firma.Distribution.getTotalRewardInfo(await wallet.getAddress()); - for (var i = 0; i < totalReward.rewards.length; i++) { - const validatorAddress = totalReward.rewards[i].validator_address; - var amount = await firma.Distribution.getRewardInfo(await wallet.getAddress(), validatorAddress); + const validatorAddress = totalReward.rewards[0].validator_address; + var amount = await firma.Distribution.getRewardInfo(await wallet.getAddress(), validatorAddress); - //console.log("validator_address: " + validatorAddress); - //console.log("reward: " + amount); - } + //console.log("validator_address: " + validatorAddress); + //console.log("reward: " + amount); }) }); \ No newline at end of file