From b7877d136db8e5a15af30a34c21b0a45c083bf49 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 4 Feb 2020 00:02:44 +0100 Subject: [PATCH] Test nodeInfo and encodeTx in RestServer --- packages/sdk/src/restclient.spec.ts | 31 +++++++++++++++++ packages/sdk/src/restclient.ts | 2 +- packages/sdk/src/testdata/cosmoshub.json | 44 ++++++++++++++++++++++++ packages/sdk/types/restclient.d.ts | 1 + 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 packages/sdk/src/testdata/cosmoshub.json diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index e44812ee..55293bad 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -1,10 +1,41 @@ +/* eslint-disable @typescript-eslint/camelcase */ +import { Encoding } from "@iov/encoding"; + import { RestClient } from "./restclient"; +import data from "./testdata/cosmoshub.json"; +import { StdTx } from "./types"; + +const { fromBase64 } = Encoding; const httpUrl = "http://localhost:1317"; +const defaultNetworkId = "testing"; +function pendingWithoutCosmos(): void { + if (!process.env.COSMOS_ENABLED) { + return pending("Set COSMOS_ENABLED to enable Cosmos node-based tests"); + } +} describe("RestClient", () => { it("can be constructed", () => { const client = new RestClient(httpUrl); expect(client).toBeTruthy(); }); + + describe("nodeInfo", () => { + it("works", async () => { + pendingWithoutCosmos(); + const client = new RestClient(httpUrl); + const info = await client.nodeInfo(); + expect(info.node_info.network).toEqual(defaultNetworkId); + }); + }); + + describe("encodeTx", () => { + it("works for cosmoshub example", async () => { + pendingWithoutCosmos(); + const tx: StdTx = data.tx.value; + const client = new RestClient(httpUrl); + expect(await client.encodeTx(tx)).toEqual(fromBase64(data.tx_data)); + }); + }); }); diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index c7d17e94..b210c873 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -139,7 +139,7 @@ export class RestClient { return responseData as BlocksResponse; } - // encodeTx returns the amino-encoding of the transaction + /** returns the amino-encoding of the transaction performed by the server */ public async encodeTx(stdTx: StdTx): Promise { const tx = { type: "cosmos-sdk/StdTx", value: stdTx }; const responseData = await this.post("/txs/encode", tx); diff --git a/packages/sdk/src/testdata/cosmoshub.json b/packages/sdk/src/testdata/cosmoshub.json new file mode 100644 index 00000000..cb33539c --- /dev/null +++ b/packages/sdk/src/testdata/cosmoshub.json @@ -0,0 +1,44 @@ +{ + "//source": "https://hubble.figment.network/cosmos/chains/cosmoshub-3/blocks/415777/transactions/2BD600EA6090FC75FD844CA73542CC90A828770F4C01C5B483C3C1C43CCB65F4?format=json", + "tx": { + "type": "cosmos-sdk/StdTx", + "value": { + "msg": [ + { + "type": "cosmos-sdk/MsgSend", + "value": { + "from_address": "cosmos1txqfn5jmcts0x0q7krdxj8tgf98tj0965vqlmq", + "to_address": "cosmos1nynns8ex9fq6sjjfj8k79ymkdz4sqth06xexae", + "amount": [ + { + "denom": "uatom", + "amount": "35997500" + } + ] + } + } + ], + "fee": { + "amount": [ + { + "denom": "uatom", + "amount": "2500" + } + ], + "gas": "100000" + }, + "signatures": [ + { + "pub_key": { + "type": "tendermint/PubKeySecp256k1", + "value": "A5qFcJBJvEK/fOmEAY0DHNWwSRZ9TEfNZyH8VoVvDtAq" + }, + "signature": "NK1Oy4EUGAsoC03c1wi9GG03JC/39LEdautC5Jk643oIbEPqeXHMwaqbdvO/Jws0X/NAXaN8SAy2KNY5Qml+5Q==" + } + ], + "memo": "" + } + }, + "tx_data": "ygEoKBapCkOoo2GaChRZgJnSW8Lg8zwesNppHWhJTrk8uhIUmSc4HyYqQahKSZHt4pN2aKsALu8aEQoFdWF0b20SCDM1OTk3NTAwEhMKDQoFdWF0b20SBDI1MDAQoI0GGmoKJuta6YchA5qFcJBJvEK/fOmEAY0DHNWwSRZ9TEfNZyH8VoVvDtAqEkA0rU7LgRQYCygLTdzXCL0YbTckL/f0sR1q60LkmTrjeghsQ+p5cczBqpt2878nCzRf80Bdo3xIDLYo1jlCaX7l", + "id": "2BD600EA6090FC75FD844CA73542CC90A828770F4C01C5B483C3C1C43CCB65F4" +} diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index b818be1e..08f16827 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -71,6 +71,7 @@ export declare class RestClient { nodeInfo(): Promise; blocksLatest(): Promise; blocks(height: number): Promise; + /** returns the amino-encoding of the transaction performed by the server */ encodeTx(stdTx: StdTx): Promise; authAccounts(address: string, height?: string): Promise; txs(query: string): Promise;