Create proper supply module

This commit is contained in:
Simon Warta 2020-07-07 09:15:18 +02:00
parent 6d767cf5b1
commit 467b29e451
8 changed files with 92 additions and 2 deletions

View File

@ -0,0 +1,3 @@
// See tracking issue https://github.com/CosmWasm/cosmjs/issues/276 for module support
export { SupplyModule, TotalSupplyAllReponse, TotalSupplyReponse } from "./supply";

View File

@ -0,0 +1,40 @@
import { LcdClient } from "../lcdclient";
import { pendingWithoutWasmd, wasmd } from "../testutils.spec";
import { setupSupplyModule } from "./supply";
describe("supply", () => {
describe("totalSupplyAll", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = LcdClient.withModules({ apiUrl: wasmd.endpoint }, setupSupplyModule);
const supply = await client.totalSupplyAll();
expect(supply).toEqual({
height: jasmine.stringMatching(/^[0-9]+$/),
result: [
{
amount: jasmine.stringMatching(/^[0-9]+$/),
denom: "ucosm",
},
{
amount: jasmine.stringMatching(/^[0-9]+$/),
denom: "ustake",
},
],
});
});
});
describe("totalSupply", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = LcdClient.withModules({ apiUrl: wasmd.endpoint }, setupSupplyModule);
const supply = await client.totalSupply("ucosm");
expect(supply).toEqual({
height: jasmine.stringMatching(/^[0-9]+$/),
result: jasmine.stringMatching(/^[0-9]+$/),
});
});
});
});

View File

@ -0,0 +1,29 @@
import { Coin } from "../coins";
import { LcdApiArray, LcdClient, LcdModule } from "../lcdclient";
export interface TotalSupplyAllReponse {
readonly height: string;
readonly result: LcdApiArray<Coin>;
}
export interface TotalSupplyReponse {
readonly height: string;
/** The amount */
readonly result: string;
}
export interface SupplyModule extends LcdModule {
readonly totalSupplyAll: () => Promise<TotalSupplyAllReponse>;
readonly totalSupply: (denom: string) => Promise<TotalSupplyReponse>;
}
export function setupSupplyModule(base: LcdClient): SupplyModule {
return {
totalSupplyAll: async () => {
return base.get(`/supply/total`);
},
totalSupply: async (denom: string) => {
return base.get(`/supply/total/${denom}`);
},
};
}

View File

@ -21,7 +21,7 @@ export function normalizeArray<T>(backend: LcdApiArray<T>): ReadonlyArray<T> {
return backend || [];
}
type LcdModule = Record<string, () => any>;
export type LcdModule = Record<string, (...args: any[]) => any>;
type LcdModuleSetup<M> = (base: LcdClient) => M;

View File

@ -0,0 +1 @@
export { SupplyModule, TotalSupplyAllReponse, TotalSupplyReponse } from "./supply";

16
packages/sdk38/types/lcdapi/supply.d.ts vendored Normal file
View File

@ -0,0 +1,16 @@
import { Coin } from "../coins";
import { LcdApiArray, LcdClient, LcdModule } from "../lcdclient";
export interface TotalSupplyAllReponse {
readonly height: string;
readonly result: LcdApiArray<Coin>;
}
export interface TotalSupplyReponse {
readonly height: string;
/** The amount */
readonly result: string;
}
export interface SupplyModule extends LcdModule {
readonly totalSupplyAll: () => Promise<TotalSupplyAllReponse>;
readonly totalSupply: (denom: string) => Promise<TotalSupplyReponse>;
}
export declare function setupSupplyModule(base: LcdClient): SupplyModule;

View File

@ -0,0 +1 @@
export {};

View File

@ -12,7 +12,7 @@ import { CosmosSdkTx, StdTx } from "./types";
/** Unfortunately, Cosmos SDK encodes empty arrays as null */
export declare type LcdApiArray<T> = ReadonlyArray<T> | null;
export declare function normalizeArray<T>(backend: LcdApiArray<T>): ReadonlyArray<T>;
declare type LcdModule = Record<string, () => any>;
export declare type LcdModule = Record<string, (...args: any[]) => any>;
declare type LcdModuleSetup<M> = (base: LcdClient) => M;
export interface LcdClientBaseOptions {
readonly apiUrl: string;