Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility function to get query responses to Decimal. #965

Closed
puneet2019 opened this issue Dec 16, 2021 · 8 comments · Fixed by #969
Closed

Add a utility function to get query responses to Decimal. #965

puneet2019 opened this issue Dec 16, 2021 · 8 comments · Fixed by #969

Comments

@puneet2019
Copy link

puneet2019 commented Dec 16, 2021

import {Tendermint34Client} from "@cosmjs/tendermint-rpc";
import {createProtobufRpcClient, QueryClient} from "@cosmjs/stargate";
import {QueryClientImpl as MintQueryClientImpl} from 'cosmjs-types/cosmos/mint/v1beta1/query';
import { Decimal } from "@cosmjs/math";

       const tendermintClient = await Tendermint34Client.connect("rpc.cosmos....");
       const queryClient = new QueryClient(tendermintClient);
       const rpcClient = createProtobufRpcClient(queryClient);
       const queryService = new MintQueryClientImpl(rpcClient);
       const response = await queryService.Inflation({});
       console.log(response.inflation)

Here response.infaltion returns a uint8array.

        let decoder = new TextDecoder('utf8');
        let decimalString = Decimal.fromAtomics(decoder.decode(response.inflation),18).toString()

Is there another way to do this?

@webmaster128
Copy link
Member

webmaster128 commented Dec 16, 2021

Yeah, the proto encoding of Cosmos SDK decimals (github.com/cosmos/cosmos-sdk/types.Dec) is annoying.

Your code looks correct. decoder.decode(response.inflation) can be replaced with fromUtf8(response.inflation).

I was looking for an example how we do it in CosmJS but it seems like all the queries that return decimals are only implemented for Launchpad, not Stargate. I'd very much appreciate a port of packages/launchpad/src/lcdapi/mint.ts to packages/stargate/src/queries/.

@vishrutsingh
Copy link

@webmaster128 I'd like to take up the task. Thanks

@webmaster128
Copy link
Member

What's really strange is the binary encoding in

// QueryInflationResponse is the response type for the Query/Inflation RPC
// method.
message QueryInflationResponse {
  // inflation is the current minting inflation value.
  bytes inflation = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

vs the string encoding in

// CommissionRates defines the initial commission rates to be used for creating
// a validator.
message CommissionRates {
  option (gogoproto.equal)            = true;
  option (gogoproto.goproto_stringer) = false;

  // rate is the commission rate charged to delegators, as a fraction.
  string rate = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
  // max_rate defines the maximum commission rate which validator can ever charge, as a fraction.
  string max_rate = 2 [
    (gogoproto.moretags)   = "yaml:\"max_rate\"",
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable)   = false
  ];
  // max_change_rate defines the maximum daily increase of the validator commission, as a fraction.
  string max_change_rate = 3 [
    (gogoproto.moretags)   = "yaml:\"max_change_rate\"",
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable)   = false
  ];
}

@webmaster128
Copy link
Member

Could you have a look at #969 (new decodeCosmosSdkDecFromProto function) and see if that serves your needs?

@webmaster128
Copy link
Member

#969 now conntains a full MintExtension for queries. Here I took the approch to wrap the whole protobuf API in custom types and do the decimal parsing internally. So it should be easy to use. It is somewhat inconsistent to many other query extensions that expose the original proto type. Happy for some 👀 on the PR.

@puneet2019
Copy link
Author

Will check it out with @blackpanther1881

@webmaster128
Copy link
Member

Please also note cosmos/cosmos-sdk#10863 if you care about the future of Decimals in Cosmos SDK.

@blackpanther1881
Copy link
Contributor

@webmaster128 both decodeCosmosSdkDecFromProto and MintExtension from #969 working fine. decodeCosmosSdkDecFromProto response.toString() giving required result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants