From 8f1f8d93bee73b4359fe959f95d477f006bf068d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 21 Feb 2020 12:00:10 +0100 Subject: [PATCH] Sort methods in RestClient --- packages/sdk/src/restclient.spec.ts | 110 +++++++++++++++------------- packages/sdk/src/restclient.ts | 44 +++++++---- packages/sdk/types/restclient.d.ts | 11 ++- 3 files changed, 99 insertions(+), 66 deletions(-) diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index d66c34de..2b4700e3 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -169,15 +169,61 @@ describe("RestClient", () => { expect(client).toBeTruthy(); }); - describe("nodeInfo", () => { - it("works", async () => { + // The /auth endpoints + + describe("authAccounts", () => { + it("works for unused account without pubkey", async () => { pendingWithoutWasmd(); const client = new RestClient(httpUrl); - const info = await client.nodeInfo(); - expect(info.node_info.network).toEqual(defaultNetworkId); + const { result } = await client.authAccounts(unusedAccount.address); + expect(result).toEqual({ + type: "cosmos-sdk/Account", + value: { + address: unusedAccount.address, + public_key: "", // not known to the chain + coins: [ + { + amount: "1000000000", + denom: "ucosm", + }, + { + amount: "1000000000", + denom: "ustake", + }, + ], + account_number: 5, + sequence: 0, + }, + }); + }); + + // This fails in the first test run if you forget to run `./scripts/wasmd/init.sh` + it("has correct pubkey for faucet", async () => { + pendingWithoutWasmd(); + const client = new RestClient(httpUrl); + const { result } = await client.authAccounts(faucet.address); + expect(result.value).toEqual( + jasmine.objectContaining({ + public_key: encodeBech32Pubkey(faucet.pubkey, "cosmospub"), + }), + ); + }); + + // This property is used by CosmWasmClient.getAccount + it("returns empty address for non-existent account", async () => { + pendingWithoutWasmd(); + const client = new RestClient(httpUrl); + const nonExistentAccount = makeRandomAddress(); + const { result } = await client.authAccounts(nonExistentAccount); + expect(result).toEqual({ + type: "cosmos-sdk/Account", + value: jasmine.objectContaining({ address: "" }), + }); }); }); + // The /blocks endpoints + describe("blocksLatest", () => { it("works", async () => { pendingWithoutWasmd(); @@ -245,57 +291,19 @@ describe("RestClient", () => { }); }); - describe("authAccounts", () => { - it("works for unused account without pubkey", async () => { - pendingWithoutWasmd(); - const client = new RestClient(httpUrl); - const { result } = await client.authAccounts(unusedAccount.address); - expect(result).toEqual({ - type: "cosmos-sdk/Account", - value: { - address: unusedAccount.address, - public_key: "", // not known to the chain - coins: [ - { - amount: "1000000000", - denom: "ucosm", - }, - { - amount: "1000000000", - denom: "ustake", - }, - ], - account_number: 5, - sequence: 0, - }, - }); - }); + // The /node_info endpoint - // This fails in the first test run if you forget to run `./scripts/wasmd/init.sh` - it("has correct pubkey for faucet", async () => { + describe("nodeInfo", () => { + it("works", async () => { pendingWithoutWasmd(); const client = new RestClient(httpUrl); - const { result } = await client.authAccounts(faucet.address); - expect(result.value).toEqual( - jasmine.objectContaining({ - public_key: encodeBech32Pubkey(faucet.pubkey, "cosmospub"), - }), - ); - }); - - // This property is used by CosmWasmClient.getAccount - it("returns empty address for non-existent account", async () => { - pendingWithoutWasmd(); - const client = new RestClient(httpUrl); - const nonExistentAccount = makeRandomAddress(); - const { result } = await client.authAccounts(nonExistentAccount); - expect(result).toEqual({ - type: "cosmos-sdk/Account", - value: jasmine.objectContaining({ address: "" }), - }); + const info = await client.nodeInfo(); + expect(info.node_info.network).toEqual(defaultNetworkId); }); }); + // The /txs endpoints + describe("encodeTx", () => { it("works for cosmoshub example", async () => { pendingWithoutWasmd(); @@ -304,7 +312,7 @@ describe("RestClient", () => { }); }); - describe("post", () => { + describe("postTx", () => { it("can send tokens", async () => { pendingWithoutWasmd(); const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); @@ -410,6 +418,8 @@ describe("RestClient", () => { }); }); + // The /wasm endpoints + describe("query", () => { it("can list upload code", async () => { pendingWithoutWasmd(); diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index 476dc85a..ea58248b 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -235,14 +235,19 @@ export class RestClient { return data; } - public async nodeInfo(): Promise { - const responseData = await this.get("/node_info"); - if (!(responseData as any).node_info) { + // The /auth endpoints + + public async authAccounts(address: string): Promise { + const path = `/auth/accounts/${address}`; + const responseData = await this.get(path); + if ((responseData as any).result.type !== "cosmos-sdk/Account") { throw new Error("Unexpected response data format"); } - return responseData as NodeInfoResponse; + return responseData as AuthAccountsResponse; } + // The /blocks endpoints + public async blocksLatest(): Promise { const responseData = await this.get("/blocks/latest"); if (!(responseData as any).block) { @@ -259,6 +264,18 @@ export class RestClient { return responseData as BlockResponse; } + // The /node_info endpoint + + public async nodeInfo(): Promise { + const responseData = await this.get("/node_info"); + if (!(responseData as any).node_info) { + throw new Error("Unexpected response data format"); + } + return responseData as NodeInfoResponse; + } + + // The /txs endpoints + /** returns the amino-encoding of the transaction performed by the server */ public async encodeTx(tx: CosmosSdkTx): Promise { const responseData = await this.post("/txs/encode", tx); @@ -268,15 +285,6 @@ export class RestClient { return Encoding.fromBase64((responseData as EncodeTxResponse).tx); } - public async authAccounts(address: string): Promise { - const path = `/auth/accounts/${address}`; - const responseData = await this.get(path); - if ((responseData as any).result.type !== "cosmos-sdk/Account") { - throw new Error("Unexpected response data format"); - } - return responseData as AuthAccountsResponse; - } - public async txs(query: string): Promise { const responseData = await this.get(`/txs?${query}`); if (!(responseData as any).txs) { @@ -293,7 +301,13 @@ export class RestClient { return responseData as TxsResponse; } - // tx must be JSON encoded StdTx (no wrapper) + /** + * Broadcasts a signed transaction to into the transaction pool. + * Depending on the RestClient's broadcast mode, this might or might + * wait for checkTx or deliverTx to be executed before returning. + * + * @param tx must be JSON encoded StdTx (no wrapper) + */ public async postTx(tx: Uint8Array): Promise { // TODO: check this is StdTx const decoded = JSON.parse(fromUtf8(tx)); @@ -311,6 +325,8 @@ export class RestClient { return responseData as PostTxsResponse; } + // The /wasm endpoints + // wasm rest queries are listed here: https://github.com/cosmwasm/wasmd/blob/master/x/wasm/client/rest/query.go#L19-L27 public async listCodeInfo(): Promise { const path = `/wasm/code`; diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index d629c7bc..430f2965 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -124,14 +124,21 @@ export declare class RestClient { constructor(url: string, mode?: BroadcastMode); get(path: string): Promise; post(path: string, params: PostTxsParams): Promise; - nodeInfo(): Promise; + authAccounts(address: string): Promise; blocksLatest(): Promise; blocks(height: number): Promise; + nodeInfo(): Promise; /** returns the amino-encoding of the transaction performed by the server */ encodeTx(tx: CosmosSdkTx): Promise; - authAccounts(address: string): Promise; txs(query: string): Promise; txsById(id: string): Promise; + /** + * Broadcasts a signed transaction to into the transaction pool. + * Depending on the RestClient's broadcast mode, this might or might + * wait for checkTx or deliverTx to be executed before returning. + * + * @param tx must be JSON encoded StdTx (no wrapper) + */ postTx(tx: Uint8Array): Promise; listCodeInfo(): Promise; getCode(id: number): Promise;