Test BankExtension.allBalances

This commit is contained in:
Simon Warta 2020-08-13 00:12:53 +02:00
parent 735617aa18
commit 59687d0fd6
4 changed files with 35 additions and 7 deletions

View File

@ -44,4 +44,31 @@ describe("BankExtension", () => {
expect(response).toBeNull();
});
});
describe("allBalances", () => {
it("returns all balances for unused account", async () => {
pendingWithoutSimapp();
const client = await makeBankClient(simapp.tendermintUrl);
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

@ -7,7 +7,8 @@ import { QueryClient } from "../queryclient";
* Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin)
* in a ways that makes Jasmine's toEqual happy.
*/
function toObject<I extends O, O>(thing: I): O {
// eslint-disable-next-line @typescript-eslint/ban-types
function toObject<I extends object>(thing: I): Omit<I, never> {
return { ...thing };
}
@ -15,7 +16,7 @@ export interface BankExtension {
readonly bank: {
readonly balance: (address: string, denom: string) => Promise<cosmos.ICoin | null>;
readonly unverified: {
readonly balances: (address: string) => Promise<cosmos.ICoin[]>;
readonly allBalances: (address: string) => Promise<cosmos.ICoin[]>;
};
};
}
@ -47,9 +48,9 @@ export function setupBankExtension(base: QueryClient): BankExtension {
return responseData.length ? toObject(cosmos.Coin.decode(responseData)) : null;
},
unverified: {
balances: async (address: string) => {
const response = await queryService.allBalances({ address: Bech32.decode(address).data });
return response.balances;
allBalances: async (address: string) => {
const { balances } = await queryService.allBalances({ address: Bech32.decode(address).data });
return balances.map(toObject);
},
},
},

View File

@ -204,7 +204,7 @@ export class StargateClient {
* proofs from such a method.
*/
public async getAllBalancesUnverified(address: string): Promise<readonly Coin[]> {
const balances = await this.queryClient.bank.unverified.balances(address);
const balances = await this.queryClient.bank.unverified.allBalances(address);
return balances.map(coinFromProto);
}

View File

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