diff --git a/CHANGELOG.md b/CHANGELOG.md index 15f2b40350..21a519fcd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ and this project adheres to - @cosmjs/stargate: Add Amino JSON support for `MsgCreateVestingAccount`. - @cosmjs/stargate and @cosmjs/cosmwasm-stargate: Create and use BroadcastTxError ([#1096]). +- @cosmjs/stargate: Add height parameter to `QueryClient.queryUnverified` + ([#1250]). - @cosmjs/faucet: Allow configuring the cooldown value via `FAUCET_COOLDOWN_TIME` environment variable. @@ -35,6 +37,7 @@ and this project adheres to [#1176]: https://github.com/cosmos/cosmjs/pull/1176 [#1224]: https://github.com/cosmos/cosmjs/pull/1224 [#1225]: https://github.com/cosmos/cosmjs/issues/1225 +[#1250]: https://github.com/cosmos/cosmjs/issues/1250 ### Fixed diff --git a/packages/stargate/src/queryclient/queryclient.spec.ts b/packages/stargate/src/queryclient/queryclient.spec.ts index 0f93b0b993..823fb13a8c 100644 --- a/packages/stargate/src/queryclient/queryclient.spec.ts +++ b/packages/stargate/src/queryclient/queryclient.spec.ts @@ -1,10 +1,27 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { coin } from "@cosmjs/amino"; import { toAscii } from "@cosmjs/encoding"; +import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; +import { assert } from "@cosmjs/utils"; import { Metadata } from "cosmjs-types/cosmos/bank/v1beta1/bank"; -import { QueryAllBalancesRequest, QueryAllBalancesResponse } from "cosmjs-types/cosmos/bank/v1beta1/query"; +import { + QueryAllBalancesRequest, + QueryAllBalancesResponse, + QueryBalanceRequest, + QueryBalanceResponse, +} from "cosmjs-types/cosmos/bank/v1beta1/query"; -import { pendingWithoutSimapp, simapp, simapp44Enabled, unused } from "../testutils.spec"; +import { SigningStargateClient } from "../signingstargateclient"; +import { + defaultSigningClientOptions, + faucet, + makeRandomAddress, + pendingWithoutSimapp, + simapp, + simapp44Enabled, + unused, +} from "../testutils.spec"; import { QueryClient } from "./queryclient"; async function makeClient(rpcUrl: string): Promise<[QueryClient, Tendermint34Client]> { @@ -101,5 +118,52 @@ describe("QueryClient", () => { tmClient.disconnect(); }); + + it("works for height", async () => { + pendingWithoutSimapp(); + const [queryClient, tmClient] = await makeClient(simapp.tendermintUrlHttp); + + const joe = makeRandomAddress(); + const h1 = (await tmClient.status()).syncInfo.latestBlockHeight; + + // Send tokens to `recipient` + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic); + const client = await SigningStargateClient.connectWithSigner( + simapp.tendermintUrlHttp, + wallet, + defaultSigningClientOptions, + ); + const amount = coin(332211, simapp.denomFee); + await client.sendTokens(faucet.address0, joe, [amount], "auto"); + + const h2 = (await tmClient.status()).syncInfo.latestBlockHeight; + assert(h1 < h2); + + // Query with no height + { + const requestData = QueryBalanceRequest.encode({ address: joe, denom: simapp.denomFee }).finish(); + const data = await queryClient.queryUnverified(`/cosmos.bank.v1beta1.Query/Balance`, requestData); + const response = QueryBalanceResponse.decode(data); + expect(response.balance).toEqual(amount); + } + + // Query at h2 (after send) + { + const requestData = QueryBalanceRequest.encode({ address: joe, denom: simapp.denomFee }).finish(); + const data = await queryClient.queryUnverified(`/cosmos.bank.v1beta1.Query/Balance`, requestData, h2); + const response = QueryBalanceResponse.decode(data); + expect(response.balance).toEqual(amount); + } + + // Query at h1 (before send) + { + const requestData = QueryBalanceRequest.encode({ address: joe, denom: simapp.denomFee }).finish(); + const data = await queryClient.queryUnverified(`/cosmos.bank.v1beta1.Query/Balance`, requestData, h1); + const response = QueryBalanceResponse.decode(data); + expect(response.balance).toEqual({ amount: "0", denom: simapp.denomFee }); + } + + tmClient.disconnect(); + }); }); }); diff --git a/packages/stargate/src/queryclient/queryclient.ts b/packages/stargate/src/queryclient/queryclient.ts index 5965cfa677..eceec9929f 100644 --- a/packages/stargate/src/queryclient/queryclient.ts +++ b/packages/stargate/src/queryclient/queryclient.ts @@ -570,11 +570,16 @@ export class QueryClient { }; } - public async queryUnverified(path: string, request: Uint8Array): Promise { + public async queryUnverified( + path: string, + request: Uint8Array, + desiredHeight?: number, + ): Promise { const response = await this.tmClient.abciQuery({ path: path, data: request, prove: false, + height: desiredHeight, }); if (response.code) {