From 054acb7c8001e72b637c55704d770e07bd53d6e1 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 20 Oct 2021 15:15:14 +0200 Subject: [PATCH] Use query queryVerified key that works for 0.42 and 0.44 --- .../stargate/src/queries/queryclient.spec.ts | 38 ++++++++++++------- .../template/.simapp/config/genesis.json | 20 +++++++++- .../template/.simapp/config/genesis.json | 20 +++++++++- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/packages/stargate/src/queries/queryclient.spec.ts b/packages/stargate/src/queries/queryclient.spec.ts index 160758af7a..db3f830a6c 100644 --- a/packages/stargate/src/queries/queryclient.spec.ts +++ b/packages/stargate/src/queries/queryclient.spec.ts @@ -1,33 +1,42 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { toAscii } from "@cosmjs/encoding"; import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; +import { Metadata } from "cosmjs-types/cosmos/bank/v1beta1/bank"; import { QueryAllBalancesRequest, QueryAllBalancesResponse } from "cosmjs-types/cosmos/bank/v1beta1/query"; -import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin"; -import { nonNegativeIntegerMatcher, pendingWithoutSimapp, simapp, unused } from "../testutils.spec"; +import { pendingWithoutSimapp, simapp, unused } from "../testutils.spec"; import { QueryClient } from "./queryclient"; -import { toAccAddress } from "./utils"; async function makeClient(rpcUrl: string): Promise<[QueryClient, Tendermint34Client]> { const tmClient = await Tendermint34Client.connect(rpcUrl); return [QueryClient.withExtensions(tmClient), tmClient]; } +/** + * See + * - https://github.com/cosmos/cosmos-sdk/blob/v0.42.10/x/bank/types/key.go#L27 + * - https://github.com/cosmos/cosmos-sdk/blob/v0.44.2/x/bank/types/key.go#L28 + */ +const denomMetadataPrefix = new Uint8Array([0x01]); + describe("QueryClient", () => { describe("queryVerified", () => { it("works via WebSockets", async () => { pendingWithoutSimapp(); const [client, tmClient] = await makeClient(simapp.tendermintUrlWs); + // "keys before 0.45 had denom two times in the key" + // https://github.com/cosmos/cosmos-sdk/blob/10ad61a4dd/x/bank/migrations/v045/store_test.go#L91 const key = Uint8Array.from([ - ...toAscii("balances"), - ...toAccAddress(unused.address), + ...denomMetadataPrefix, + ...toAscii(simapp.denomFee), ...toAscii(simapp.denomFee), ]); const data = await client.queryVerified("bank", key); - const response = Coin.decode(data); - expect(response.amount).toMatch(nonNegativeIntegerMatcher); - expect(response.denom).toEqual(simapp.denomFee); + + const response = Metadata.decode(data); + expect(response.base).toEqual(simapp.denomFee); + expect(response.description).toEqual("The fee token of this test chain"); tmClient.disconnect(); }); @@ -36,15 +45,18 @@ describe("QueryClient", () => { pendingWithoutSimapp(); const [client, tmClient] = await makeClient(simapp.tendermintUrlHttp); + // "keys before 0.45 had denom two times in the key" + // https://github.com/cosmos/cosmos-sdk/blob/10ad61a4dd/x/bank/migrations/v045/store_test.go#L91 const key = Uint8Array.from([ - ...toAscii("balances"), - ...toAccAddress(unused.address), + ...denomMetadataPrefix, + ...toAscii(simapp.denomFee), ...toAscii(simapp.denomFee), ]); const data = await client.queryVerified("bank", key); - const response = Coin.decode(data); - expect(response.amount).toMatch(nonNegativeIntegerMatcher); - expect(response.denom).toEqual(simapp.denomFee); + + const response = Metadata.decode(data); + expect(response.base).toEqual(simapp.denomFee); + expect(response.description).toEqual("The fee token of this test chain"); tmClient.disconnect(); }); diff --git a/scripts/simapp/template/.simapp/config/genesis.json b/scripts/simapp/template/.simapp/config/genesis.json index e9c0fb4f17..b5cd768f31 100644 --- a/scripts/simapp/template/.simapp/config/genesis.json +++ b/scripts/simapp/template/.simapp/config/genesis.json @@ -435,7 +435,25 @@ ] } ], - "denom_metadata": [], + "denom_metadata": [ + { + "description": "The fee token of this test chain", + "denom_units": [ + { + "denom": "ucosm", + "exponent": 0, + "aliases": [] + }, + { + "denom": "COSM", + "exponent": 6, + "aliases": [] + } + ], + "base": "ucosm", + "display": "COSM" + } + ], "params": { "default_send_enabled": true, "send_enabled": [] diff --git a/scripts/simapp44/template/.simapp/config/genesis.json b/scripts/simapp44/template/.simapp/config/genesis.json index e9c0fb4f17..b5cd768f31 100644 --- a/scripts/simapp44/template/.simapp/config/genesis.json +++ b/scripts/simapp44/template/.simapp/config/genesis.json @@ -435,7 +435,25 @@ ] } ], - "denom_metadata": [], + "denom_metadata": [ + { + "description": "The fee token of this test chain", + "denom_units": [ + { + "denom": "ucosm", + "exponent": 0, + "aliases": [] + }, + { + "denom": "COSM", + "exponent": 6, + "aliases": [] + } + ], + "base": "ucosm", + "display": "COSM" + } + ], "params": { "default_send_enabled": true, "send_enabled": []