Add unverified balance query

This commit is contained in:
Simon Warta 2020-08-13 11:43:53 +02:00
parent b222f49cab
commit 71b0728042
3 changed files with 66 additions and 16 deletions

View File

@ -45,30 +45,72 @@ describe("BankExtension", () => {
});
});
describe("allBalances", () => {
it("returns all balances for unused account", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
describe("unverified", () => {
describe("balance", () => {
it("works for different existing balances", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const balances = await client.bank.unverified.allBalances(unused.address);
expect(balances).toEqual([
{
const response1 = await client.bank.unverified.balance(unused.address, simapp.denomFee);
expect(response1).toEqual({
amount: unused.balanceFee,
denom: simapp.denomFee,
},
{
});
const response2 = await client.bank.unverified.balance(unused.address, simapp.denomStaking);
expect(response2).toEqual({
amount: unused.balanceStaking,
denom: simapp.denomStaking,
},
]);
});
});
it("returns zero for non-existent balance", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.unverified.balance(unused.address, "gintonic");
expect(response).toEqual({
amount: "0",
denom: "gintonic",
});
});
it("returns zero for non-existent address", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const response = await client.bank.unverified.balance(nonExistentAddress, simapp.denomFee);
expect(response).toEqual({
amount: "0",
denom: simapp.denomFee,
});
});
});
it("returns an empty list for non-existent account", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
describe("allBalances", () => {
it("returns all balances for unused account", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const balances = await client.bank.unverified.allBalances(nonExistentAddress);
expect(balances).toEqual([]);
const balances = await client.bank.unverified.allBalances(unused.address);
expect(balances).toEqual([
{
amount: unused.balanceFee,
denom: simapp.denomFee,
},
{
amount: unused.balanceStaking,
denom: simapp.denomStaking,
},
]);
});
it("returns an empty list for non-existent account", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
const balances = await client.bank.unverified.allBalances(nonExistentAddress);
expect(balances).toEqual([]);
});
});
});
});

View File

@ -1,4 +1,5 @@
import { toAscii } from "@cosmjs/encoding";
import { assert } from "@cosmjs/utils";
import { cosmos } from "../generated/codecimpl";
import { QueryClient } from "../queryclient";
@ -8,6 +9,7 @@ export interface BankExtension {
readonly bank: {
readonly balance: (address: string, denom: string) => Promise<cosmos.ICoin | null>;
readonly unverified: {
readonly balance: (address: string, denom: string) => Promise<cosmos.ICoin>;
readonly allBalances: (address: string) => Promise<cosmos.ICoin[]>;
};
};
@ -39,6 +41,11 @@ export function setupBankExtension(base: QueryClient): BankExtension {
return responseData.length ? toObject(cosmos.Coin.decode(responseData)) : null;
},
unverified: {
balance: async (address: string, denom: string) => {
const { balance } = await queryService.balance({ address: toAccAddress(address), denom: denom });
assert(balance);
return toObject(balance);
},
allBalances: async (address: string) => {
const { balances } = await queryService.allBalances({ address: toAccAddress(address) });
return balances.map(toObject);

View File

@ -4,6 +4,7 @@ export interface BankExtension {
readonly bank: {
readonly balance: (address: string, denom: string) => Promise<cosmos.ICoin | null>;
readonly unverified: {
readonly balance: (address: string, denom: string) => Promise<cosmos.ICoin>;
readonly allBalances: (address: string) => Promise<cosmos.ICoin[]>;
};
};