Skip to content

Commit

Permalink
Add decodeCosmosSdkDecFromProto and MintExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Jan 3, 2022
1 parent 57a56cf commit f79a6f3
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to
([#932]).
- @cosmjs/stargate: Merge `DeliverTxFailure` and `DeliverTxSuccess` into a
single `DeliverTxResponse` ([#878], [#949]). Add `assertIsDeliverTxFailure`.
- @cosmjs/stargate: Created initial `MintExtension`.
- @cosmjs/stargate: Created `types.Dec` decoder function
`decodeCosmosSdkDecFromProto`.
- @cosmjs/amino: Added `StdTx`, `isStdTx` and `makeStdTx` and removed them from
@cosmjs/launchpad. They are re-exported in @cosmjs/launchpad for backwards
compatibility.
Expand Down
3 changes: 3 additions & 0 deletions packages/stargate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ export {
BankExtension,
createPagination,
createProtobufRpcClient,
decodeCosmosSdkDecFromProto,
DistributionExtension,
GovExtension,
GovParamsType,
GovProposalId,
IbcExtension,
MintExtension,
ProtobufRpcClient,
QueryClient,
setupAuthExtension,
setupBankExtension,
setupDistributionExtension,
setupGovExtension,
setupIbcExtension,
setupMintExtension,
setupStakingExtension,
setupTxExtension,
StakingExtension,
Expand Down
8 changes: 7 additions & 1 deletion packages/stargate/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export { BankExtension, setupBankExtension } from "./bank";
export { DistributionExtension, setupDistributionExtension } from "./distribution";
export { GovExtension, GovParamsType, GovProposalId, setupGovExtension } from "./gov";
export { IbcExtension, setupIbcExtension } from "./ibc";
export { MintExtension, setupMintExtension } from "./mint";
export { setupStakingExtension, StakingExtension } from "./staking";
export { setupTxExtension, TxExtension } from "./tx";
export { createPagination, createProtobufRpcClient, ProtobufRpcClient } from "./utils";
export {
createPagination,
createProtobufRpcClient,
decodeCosmosSdkDecFromProto,
ProtobufRpcClient,
} from "./utils";
30 changes: 30 additions & 0 deletions packages/stargate/src/queries/mint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { assert } from "@cosmjs/utils";

import { QueryClient } from "../";
import { pendingWithoutSimapp, simapp } from "../testutils.spec";
import { MintExtension, setupMintExtension } from "./mint";
import { decodeCosmosSdkDecFromProto } from "./utils";

async function makeClientWithMint(
rpcUrl: string,
): Promise<[QueryClient & MintExtension, Tendermint34Client]> {
const tmClient = await Tendermint34Client.connect(rpcUrl);
return [QueryClient.withExtensions(tmClient, setupMintExtension), tmClient];
}

describe("MintExtension", () => {
describe("inflation", () => {
it("works", async () => {
pendingWithoutSimapp();
const [client, tmClient] = await makeClientWithMint(simapp.tendermintUrl);

const response = await client.mint.inflation();
assert(response.inflation);
expect(decodeCosmosSdkDecFromProto(response.inflation).toFloatApproximation()).toBeGreaterThan(0.13);
expect(decodeCosmosSdkDecFromProto(response.inflation).toFloatApproximation()).toBeLessThan(0.1301);

tmClient.disconnect();
});
});
});
26 changes: 26 additions & 0 deletions packages/stargate/src/queries/mint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { QueryClientImpl, QueryInflationResponse } from "cosmjs-types/cosmos/mint/v1beta1/query";

import { createProtobufRpcClient } from "../";
import { QueryClient } from "./queryclient";

export interface MintExtension {
readonly mint: {
inflation: () => Promise<QueryInflationResponse>;
};
}

export function setupMintExtension(base: QueryClient): MintExtension {
const rpc = createProtobufRpcClient(base);
// Use this service to get easy typed access to query methods
// This cannot be used for proof verification
const queryService = new QueryClientImpl(rpc);

return {
mint: {
inflation: async () => {
const response = await queryService.Inflation({});
return response;
},
},
};
}
21 changes: 21 additions & 0 deletions packages/stargate/src/queries/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { fromHex } from "@cosmjs/encoding";

import { decodeCosmosSdkDecFromProto } from "./utils";

describe("utils", () => {
describe("decodeCosmosSdkDecFromProto", () => {
it("works for string inputs", () => {
expect(decodeCosmosSdkDecFromProto("0").toString()).toEqual("0");
expect(decodeCosmosSdkDecFromProto("1").toString()).toEqual("0.000000000000000001");
expect(decodeCosmosSdkDecFromProto("3000000").toString()).toEqual("0.000000000003");
expect(decodeCosmosSdkDecFromProto("123456789123456789").toString()).toEqual("0.123456789123456789");
expect(decodeCosmosSdkDecFromProto("1234567891234567890").toString()).toEqual("1.23456789123456789");
});

it("works for byte inputs", () => {
expect(decodeCosmosSdkDecFromProto(fromHex("313330303033343138373830313631333938")).toString()).toEqual(
"0.130003418780161398",
);
});
});
});
15 changes: 13 additions & 2 deletions packages/stargate/src/queries/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bech32 } from "@cosmjs/encoding";
import { Uint64 } from "@cosmjs/math";
import { Bech32, fromAscii } from "@cosmjs/encoding";
import { Decimal, Uint64 } from "@cosmjs/math";
import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination";
import Long from "long";

Expand Down Expand Up @@ -53,3 +53,14 @@ export function longify(value: string | number | Long | Uint64): Long {
const checkedValue = Uint64.fromString(value.toString());
return Long.fromBytesBE([...checkedValue.toBytesBigEndian()], true);
}

/**
* Takes a string or binary encoded `github.com/cosmos/cosmos-sdk/types.Dec` from the
* protobuf API and converts it into a `Decimal` with 18 fractional digits.
*
* See https://github.com/cosmos/cosmos-sdk/issues/10863 for more context why this is needed.
*/
export function decodeCosmosSdkDecFromProto(input: string | Uint8Array): Decimal {
const asString = typeof input === "string" ? input : fromAscii(input);
return Decimal.fromAtomics(asString, 18);
}

0 comments on commit f79a6f3

Please sign in to comment.