Skip to content

Commit

Permalink
feat(nibijs): distribution queries (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Feb 21, 2023
1 parent 0a99491 commit d5c3789
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
118 changes: 118 additions & 0 deletions packages/nibijs/src/query/distribution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* eslint-disable @typescript-eslint/naming-convention */
import * as pb from "@nibiruchain/protojs/dist/cosmos/distribution/v1beta1/query"
import Long from "long"

import {
createPagination,
createProtobufRpcClient,
QueryClient,
} from "@cosmjs/stargate"

export interface DistributionExtension {
/** distrbution: query extension for the distribution module. The distribution
* module manages the distribution mechanism for passively distributing
* staking rewards to validators and delegators.
*/
readonly distribution: {
communityPool: () => Promise<pb.QueryCommunityPoolResponse>
delegationRewards: (
delegatorAddress: string,
validatorAddress: string,
) => Promise<pb.QueryDelegationRewardsResponse>

/** Returns the rewards (Coin[]) accrued from all delegations for the given
* delegator address. */
delegationTotalRewards: (
delegatorAddress: string,
) => Promise<pb.QueryDelegationTotalRewardsResponse>
delegatorValidators: (
delegatorAddress: string,
) => Promise<pb.QueryDelegatorValidatorsResponse>
delegatorWithdrawAddress: (
delegatorAddress: string,
) => Promise<pb.QueryDelegatorWithdrawAddressResponse>
/** Returns the module parameters for the distribution module. */
params: () => Promise<pb.QueryParamsResponse>
validatorCommission: (
validatorAddress: string,
) => Promise<pb.QueryValidatorCommissionResponse>
validatorOutstandingRewards: (
validatorAddress: string,
) => Promise<pb.QueryValidatorOutstandingRewardsResponse>
validatorSlashes: (
validatorAddress: string,
startingHeight: number,
endingHeight: number,
paginationKey?: Uint8Array,
) => Promise<pb.QueryValidatorSlashesResponse>
}
}

export function setupDistributionExtension(base: QueryClient): DistributionExtension {
const rpc = createProtobufRpcClient(base)
const queryService = new pb.QueryClientImpl(rpc)

return {
distribution: {
communityPool: async () => {
const response = await queryService.CommunityPool({})
return response
},
delegationRewards: async (delegatorAddress: string, validatorAddress: string) => {
const response = await queryService.DelegationRewards({
delegatorAddress,
validatorAddress,
})
return response
},
delegationTotalRewards: async (delegatorAddress: string) => {
const response = await queryService.DelegationTotalRewards({
delegatorAddress,
})
return response
},
delegatorValidators: async (delegatorAddress: string) => {
const response = await queryService.DelegatorValidators({
delegatorAddress,
})
return response
},
delegatorWithdrawAddress: async (delegatorAddress: string) => {
const response = await queryService.DelegatorWithdrawAddress({
delegatorAddress,
})
return response
},
params: async () => {
const response = await queryService.Params({})
return response
},
validatorCommission: async (validatorAddress: string) => {
const response = await queryService.ValidatorCommission({
validatorAddress,
})
return response
},
validatorOutstandingRewards: async (validatorAddress: string) => {
const response = await queryService.ValidatorOutstandingRewards({
validatorAddress,
})
return response
},
validatorSlashes: async (
validatorAddress: string,
startingHeight: number,
endingHeight: number,
paginationKey?: Uint8Array,
) => {
const response = await queryService.ValidatorSlashes({
validatorAddress,
startingHeight: Long.fromNumber(startingHeight, true),
endingHeight: Long.fromNumber(endingHeight, true),
pagination: createPagination(paginationKey),
})
return response
},
},
}
}
5 changes: 4 additions & 1 deletion packages/nibijs/src/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { setupPerpExtension, PerpExtension } from "./perp"
import { OracleExtension, setupOracleExtension } from "./oracle"
import { setupStakingExtension, StakingExtension } from "./staking"
import { setupVpoolExtension, VpoolExtension } from "./vpool"
import { DistributionExtension, setupDistributionExtension } from "./distribution"

export type ExtendedQueryClient = BankExtension &
QueryClient &
Expand All @@ -22,7 +23,8 @@ export type ExtendedQueryClient = BankExtension &
VpoolExtension &
OracleExtension &
EpochsExtension &
StakingExtension
StakingExtension &
DistributionExtension

export interface IQueryCmd {
/**
Expand Down Expand Up @@ -87,6 +89,7 @@ export class QueryCmd implements IQueryCmd {
setupOracleExtension,
setupEpochsExtension,
setupStakingExtension,
setupDistributionExtension,
)
}

Expand Down
18 changes: 18 additions & 0 deletions packages/nibijs/src/test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,21 @@ describe("staking module queries", () => {
expect(infoResp.validators.length).toBeGreaterThan(0)
})
})

describe("distribution module queries", () => {
test("distribution params", async () => {
const { client: query } = await newQueryCmd(chain)
const resp = await query.distribution.params()
const { params } = resp
expect(params).toBeDefined()
const properties: string[] = [
"communityTax",
"baseProposerReward",
"bonusProposerReward",
"withdrawAddrEnabled",
]
properties.forEach((prop) => {
expect(params).toHaveProperty(prop)
})
})
})

0 comments on commit d5c3789

Please sign in to comment.