From fd8ceee9673c1015cb2bf83dafb053564cb73f78 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 15 Jul 2020 17:23:30 +0200 Subject: [PATCH] sdk38: Add tests for LCD distribution module --- .../sdk38/src/lcdapi/distribution.spec.ts | 194 ++++++++++++++++++ packages/sdk38/src/testutils.spec.ts | 2 + 2 files changed, 196 insertions(+) create mode 100644 packages/sdk38/src/lcdapi/distribution.spec.ts diff --git a/packages/sdk38/src/lcdapi/distribution.spec.ts b/packages/sdk38/src/lcdapi/distribution.spec.ts new file mode 100644 index 00000000..8fb521e1 --- /dev/null +++ b/packages/sdk38/src/lcdapi/distribution.spec.ts @@ -0,0 +1,194 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { Bech32 } from "@cosmjs/encoding"; +import { sleep } from "@cosmjs/utils"; + +import { coin, coins } from "../coins"; +import { isPostTxFailure } from "../cosmosclient"; +import { makeSignBytes } from "../encoding"; +import { SigningCosmosClient } from "../signingcosmosclient"; +import { + bigDecimalMatcher, + faucet, + nonNegativeIntegerMatcher, + pendingWithoutWasmd, + validatorAddress, + wasmd, + wasmdEnabled, +} from "../testutils.spec"; +import { Secp256k1Wallet } from "../wallet"; +import { DistributionExtension, setupDistributionExtension } from "./distribution"; +import { LcdClient } from "./lcdclient"; + +function makeDistributionClient(apiUrl: string): LcdClient & DistributionExtension { + return LcdClient.withExtensions({ apiUrl }, setupDistributionExtension); +} + +describe("DistributionExtension", () => { + const defaultFee = { + amount: coins(25000, "ucosm"), + gas: "1500000", // 1.5 million + }; + + beforeAll(async () => { + if (wasmdEnabled()) { + const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic); + const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet, {}); + + const chainId = await client.getChainId(); + const msg = { + type: "cosmos-sdk/MsgDelegate", + value: { + delegator_address: faucet.address, + validator_address: validatorAddress, + amount: coin(25000, "ustake"), + }, + }; + const memo = "Test delegation for wasmd"; + const { accountNumber, sequence } = await client.getNonce(); + const signBytes = makeSignBytes([msg], defaultFee, chainId, memo, accountNumber, sequence); + const signature = await wallet.sign(faucet.address, signBytes); + const tx = { + msg: [msg], + fee: defaultFee, + memo: memo, + signatures: [signature], + }; + + const receipt = await client.postTx(tx); + if (isPostTxFailure(receipt)) { + throw new Error("Delegation failed"); + } + + await sleep(75); // wait until transactions are indexed + } + }); + + describe("delegatorRewards", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.delegatorRewards(faucet.address); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: { + rewards: [ + { + validator_address: validatorAddress, + reward: null, + }, + ], + total: null, + }, + }); + }); + }); + + describe("delegatorReward", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.delegatorReward(faucet.address, validatorAddress); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: [], + }); + }); + }); + + describe("withdrawAddress", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.withdrawAddress(faucet.address); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: faucet.address, + }); + }); + }); + + describe("validator", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.validator(validatorAddress); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: { + // TODO: This smells like a bug in the backend to me + operator_address: Bech32.encode("cosmos", Bech32.decode(validatorAddress).data), + self_bond_rewards: [ + { denom: "ucosm", amount: jasmine.stringMatching(bigDecimalMatcher) }, + { denom: "ustake", amount: jasmine.stringMatching(bigDecimalMatcher) }, + ], + val_commission: [ + { denom: "ucosm", amount: jasmine.stringMatching(bigDecimalMatcher) }, + { denom: "ustake", amount: jasmine.stringMatching(bigDecimalMatcher) }, + ], + }, + }); + }); + }); + + describe("validatorRewards", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.validatorRewards(validatorAddress); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: [ + { denom: "ucosm", amount: jasmine.stringMatching(bigDecimalMatcher) }, + { denom: "ustake", amount: jasmine.stringMatching(bigDecimalMatcher) }, + ], + }); + }); + }); + + describe("validatorOutstandingRewards", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.validatorOutstandingRewards(validatorAddress); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: [ + { denom: "ucosm", amount: jasmine.stringMatching(bigDecimalMatcher) }, + { denom: "ustake", amount: jasmine.stringMatching(bigDecimalMatcher) }, + ], + }); + }); + }); + + describe("parameters", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.parameters(); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: { + community_tax: "0.020000000000000000", + base_proposer_reward: "0.010000000000000000", + bonus_proposer_reward: "0.040000000000000000", + withdraw_addr_enabled: true, + }, + }); + }); + }); + + describe("communityPool", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeDistributionClient(wasmd.endpoint); + const response = await client.distribution.communityPool(); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: [ + { denom: "ucosm", amount: jasmine.stringMatching(bigDecimalMatcher) }, + { denom: "ustake", amount: jasmine.stringMatching(bigDecimalMatcher) }, + ], + }); + }); + }); +}); diff --git a/packages/sdk38/src/testutils.spec.ts b/packages/sdk38/src/testutils.spec.ts index b6c80a73..37356640 100644 --- a/packages/sdk38/src/testutils.spec.ts +++ b/packages/sdk38/src/testutils.spec.ts @@ -38,6 +38,8 @@ export const faucet = { address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6", }; +export const validatorAddress = "cosmosvaloper1gjvanqxc774u6ed9thj4gpn9gj5zus5u32enqn"; + /** Unused account */ export const unused = { pubkey: {