-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decodeCosmosSdkDecFromProto and MintExtension
- Loading branch information
1 parent
57a56cf
commit f79a6f3
Showing
7 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters