diff --git a/packages/stargate/src/queries/bank.spec.ts b/packages/stargate/src/queries/bank.spec.ts index 14a0f6c5..323d6635 100644 --- a/packages/stargate/src/queries/bank.spec.ts +++ b/packages/stargate/src/queries/bank.spec.ts @@ -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([]); + }); }); }); }); diff --git a/packages/stargate/src/queries/bank.ts b/packages/stargate/src/queries/bank.ts index 4266f288..3d6b1092 100644 --- a/packages/stargate/src/queries/bank.ts +++ b/packages/stargate/src/queries/bank.ts @@ -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; readonly unverified: { + readonly balance: (address: string, denom: string) => Promise; readonly allBalances: (address: string) => Promise; }; }; @@ -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); diff --git a/packages/stargate/types/queries/bank.d.ts b/packages/stargate/types/queries/bank.d.ts index 6b62b2c4..e9311a92 100644 --- a/packages/stargate/types/queries/bank.d.ts +++ b/packages/stargate/types/queries/bank.d.ts @@ -4,6 +4,7 @@ export interface BankExtension { readonly bank: { readonly balance: (address: string, denom: string) => Promise; readonly unverified: { + readonly balance: (address: string, denom: string) => Promise; readonly allBalances: (address: string) => Promise; }; };