sdk38: Add tests for LCD distribution module

This commit is contained in:
willclarktech 2020-07-15 17:23:30 +02:00
parent 509a7e9164
commit fd8ceee967
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
2 changed files with 196 additions and 0 deletions

View File

@ -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) },
],
});
});
});
});

View File

@ -38,6 +38,8 @@ export const faucet = {
address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
};
export const validatorAddress = "cosmosvaloper1gjvanqxc774u6ed9thj4gpn9gj5zus5u32enqn";
/** Unused account */
export const unused = {
pubkey: {