From cbcf854a98bae5ba044a09de57e73f458fbf2a88 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 11 Mar 2020 10:32:11 +0100 Subject: [PATCH 1/3] Rename to CosmWasmClient.getChainId --- packages/bcp/src/cosmwasmconnection.ts | 2 +- packages/sdk/src/cosmwasmclient.spec.ts | 10 +++++----- packages/sdk/src/cosmwasmclient.ts | 2 +- packages/sdk/src/signingcosmwasmclient.ts | 8 ++++---- packages/sdk/types/cosmwasmclient.d.ts | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index e872c9dc..841143db 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -94,7 +94,7 @@ export class CosmWasmConnection implements BlockchainConnection { } private static async initialize(cosmWasmClient: CosmWasmClient): Promise { - const rawChainId = await cosmWasmClient.chainId(); + const rawChainId = await cosmWasmClient.getChainId(); return Caip5.encode(rawChainId); } diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index 22e68a60..521335df 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -48,11 +48,11 @@ describe("CosmWasmClient", () => { }); }); - describe("chainId", () => { + describe("getChainId", () => { it("works", async () => { pendingWithoutWasmd(); const client = new CosmWasmClient(wasmdEndpoint); - expect(await client.chainId()).toEqual("testing"); + expect(await client.getChainId()).toEqual("testing"); }); }); @@ -152,7 +152,7 @@ describe("CosmWasmClient", () => { // header expect(response.header.height).toBeGreaterThanOrEqual(1); - expect(response.header.chainId).toEqual(await client.chainId()); + expect(response.header.chainId).toEqual(await client.getChainId()); expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now()); expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual( ReadonlyDate.now() - 5_000, @@ -173,7 +173,7 @@ describe("CosmWasmClient", () => { // header expect(response.header.height).toEqual(height - 1); - expect(response.header.chainId).toEqual(await client.chainId()); + expect(response.header.chainId).toEqual(await client.getChainId()); expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now()); expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual( ReadonlyDate.now() - 5_000, @@ -223,7 +223,7 @@ describe("CosmWasmClient", () => { gas: "890000", }; - const chainId = await client.chainId(); + const chainId = await client.getChainId(); const { accountNumber, sequence } = await client.getNonce(faucet.address); const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence); const signature = await pen.sign(signBytes); diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index 623ddd15..50d8aefa 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -153,7 +153,7 @@ export class CosmWasmClient { this.restClient = new RestClient(url, broadcastMode); } - public async chainId(): Promise { + public async getChainId(): Promise { const response = await this.restClient.nodeInfo(); return response.node_info.network; } diff --git a/packages/sdk/src/signingcosmwasmclient.ts b/packages/sdk/src/signingcosmwasmclient.ts index 2592c04a..44a80c73 100644 --- a/packages/sdk/src/signingcosmwasmclient.ts +++ b/packages/sdk/src/signingcosmwasmclient.ts @@ -144,7 +144,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const fee = this.fees.upload; const { accountNumber, sequence } = await this.getNonce(); - const chainId = await this.chainId(); + const chainId = await this.getChainId(); const signBytes = makeSignBytes([storeCodeMsg], fee, chainId, memo, accountNumber, sequence); const signature = await this.signCallback(signBytes); const signedTx = { @@ -189,7 +189,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const fee = this.fees.init; const { accountNumber, sequence } = await this.getNonce(); - const chainId = await this.chainId(); + const chainId = await this.getChainId(); const signBytes = makeSignBytes([instantiateMsg], fee, chainId, memo, accountNumber, sequence); const signature = await this.signCallback(signBytes); @@ -227,7 +227,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const fee = this.fees.exec; const { accountNumber, sequence } = await this.getNonce(); - const chainId = await this.chainId(); + const chainId = await this.getChainId(); const signBytes = makeSignBytes([executeMsg], fee, chainId, memo, accountNumber, sequence); const signature = await this.signCallback(signBytes); const signedTx = { @@ -261,7 +261,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { }; const fee = this.fees.send; const { accountNumber, sequence } = await this.getNonce(); - const chainId = await this.chainId(); + const chainId = await this.getChainId(); const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence); const signature = await this.signCallback(signBytes); const signedTx = { diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index 8864fd7d..f3ed5ac2 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -113,7 +113,7 @@ export declare class CosmWasmClient { protected anyValidAddress: string | undefined; private readonly codesCache; constructor(url: string, broadcastMode?: BroadcastMode); - chainId(): Promise; + getChainId(): Promise; getHeight(): Promise; /** * Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID) From a50369e43e716a8925e4c5e7751babc5a1e7325d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 11 Mar 2020 10:39:37 +0100 Subject: [PATCH 2/3] Cache chain ID --- packages/sdk/src/cosmwasmclient.spec.ts | 12 ++++++++++++ packages/sdk/src/cosmwasmclient.ts | 11 +++++++++-- packages/sdk/types/cosmwasmclient.d.ts | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index 521335df..6f9ff4c4 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -54,6 +54,18 @@ describe("CosmWasmClient", () => { const client = new CosmWasmClient(wasmdEndpoint); expect(await client.getChainId()).toEqual("testing"); }); + + it("caches chain ID", async () => { + pendingWithoutWasmd(); + const client = new CosmWasmClient(wasmdEndpoint); + const openedClient = (client as unknown) as PrivateCosmWasmClient; + const getCodeSpy = spyOn(openedClient.restClient, "nodeInfo").and.callThrough(); + + expect(await client.getChainId()).toEqual("testing"); // from network + expect(await client.getChainId()).toEqual("testing"); // from cache + + expect(getCodeSpy).toHaveBeenCalledTimes(1); + }); }); describe("getHeight", () => { diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index 50d8aefa..1413b11c 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -148,14 +148,21 @@ export class CosmWasmClient { protected anyValidAddress: string | undefined; private readonly codesCache = new Map(); + private chainId: string | undefined; public constructor(url: string, broadcastMode = BroadcastMode.Block) { this.restClient = new RestClient(url, broadcastMode); } public async getChainId(): Promise { - const response = await this.restClient.nodeInfo(); - return response.node_info.network; + if (!this.chainId) { + const response = await this.restClient.nodeInfo(); + const chainId = response.node_info.network; + if (!chainId) throw new Error("Chain ID must not be empty"); + this.chainId = chainId; + } + + return this.chainId; } public async getHeight(): Promise { diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index f3ed5ac2..b83ff2f5 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -112,6 +112,7 @@ export declare class CosmWasmClient { /** Any address the chain considers valid (valid bech32 with proper prefix) */ protected anyValidAddress: string | undefined; private readonly codesCache; + private chainId; constructor(url: string, broadcastMode?: BroadcastMode); getChainId(): Promise; getHeight(): Promise; From 4f5b5b8aca6ee3486f7c16168d8e8075343bbe52 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 11 Mar 2020 10:44:00 +0100 Subject: [PATCH 3/3] Pull out wasmd.expectedChainId --- .../sdk/src/cosmwasmclient.searchtx.spec.ts | 30 +++++----- packages/sdk/src/cosmwasmclient.spec.ts | 60 +++++++++---------- packages/sdk/src/restclient.spec.ts | 50 ++++++++-------- packages/sdk/src/testutils.spec.ts | 5 +- 4 files changed, 74 insertions(+), 71 deletions(-) diff --git a/packages/sdk/src/cosmwasmclient.searchtx.spec.ts b/packages/sdk/src/cosmwasmclient.searchtx.spec.ts index 799684da..be9cd8b3 100644 --- a/packages/sdk/src/cosmwasmclient.searchtx.spec.ts +++ b/packages/sdk/src/cosmwasmclient.searchtx.spec.ts @@ -11,8 +11,8 @@ import { fromOneElementArray, makeRandomAddress, pendingWithoutWasmd, + wasmd, wasmdEnabled, - wasmdEndpoint, } from "./testutils.spec"; import { Coin, CosmosSdkTx, isMsgExecuteContract, isMsgInstantiateContract, isMsgSend } from "./types"; @@ -39,7 +39,7 @@ describe("CosmWasmClient.searchTx", () => { beforeAll(async () => { if (wasmdEnabled()) { const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes => + const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes => pen.sign(signBytes), ); @@ -51,7 +51,7 @@ describe("CosmWasmClient.searchTx", () => { }; const result = await client.sendTokens(recipient, [transferAmount]); await sleep(50); // wait until tx is indexed - const txDetails = await new RestClient(wasmdEndpoint).txsById(result.transactionHash); + const txDetails = await new RestClient(wasmd.endpoint).txsById(result.transactionHash); postedSend = { sender: faucet.address, recipient: recipient, @@ -71,7 +71,7 @@ describe("CosmWasmClient.searchTx", () => { }; const result = await client.execute(hashInstance, msg); await sleep(50); // wait until tx is indexed - const txDetails = await new RestClient(wasmdEndpoint).txsById(result.transactionHash); + const txDetails = await new RestClient(wasmd.endpoint).txsById(result.transactionHash); postedExecute = { sender: faucet.address, contract: hashInstance, @@ -87,7 +87,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by ID", async () => { pendingWithoutWasmd(); assert(postedSend, "value must be set in beforeAll()"); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const result = await client.searchTx({ id: postedSend.hash }); expect(result.length).toEqual(1); expect(result[0]).toEqual( @@ -101,7 +101,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by ID (non existent)", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const nonExistentId = "0000000000000000000000000000000000000000000000000000000000000000"; const result = await client.searchTx({ id: nonExistentId }); expect(result.length).toEqual(0); @@ -110,7 +110,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by ID and filter by minHeight", async () => { pendingWithoutWasmd(); assert(postedSend); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const query = { id: postedSend.hash }; { @@ -139,7 +139,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by height", async () => { pendingWithoutWasmd(); assert(postedSend, "value must be set in beforeAll()"); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const result = await client.searchTx({ height: postedSend.height }); expect(result.length).toEqual(1); expect(result[0]).toEqual( @@ -156,7 +156,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by sender", async () => { pendingWithoutWasmd(); assert(postedSend, "value must be set in beforeAll()"); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const results = await client.searchTx({ sentFromOrTo: postedSend.sender }); expect(results.length).toBeGreaterThanOrEqual(1); @@ -182,7 +182,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by recipient", async () => { pendingWithoutWasmd(); assert(postedSend, "value must be set in beforeAll()"); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const results = await client.searchTx({ sentFromOrTo: postedSend.recipient }); expect(results.length).toBeGreaterThanOrEqual(1); @@ -208,7 +208,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by recipient and filter by minHeight", async () => { pendingWithoutWasmd(); assert(postedSend); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const query = { sentFromOrTo: postedSend.recipient }; { @@ -235,7 +235,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by recipient and filter by maxHeight", async () => { pendingWithoutWasmd(); assert(postedSend); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const query = { sentFromOrTo: postedSend.recipient }; { @@ -264,7 +264,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by transfer.recipient", async () => { pendingWithoutWasmd(); assert(postedSend, "value must be set in beforeAll()"); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const results = await client.searchTx({ tags: [{ key: "transfer.recipient", value: postedSend.recipient }], }); @@ -290,7 +290,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by message.contract_address", async () => { pendingWithoutWasmd(); assert(postedExecute, "value must be set in beforeAll()"); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const results = await client.searchTx({ tags: [{ key: "message.contract_address", value: postedExecute.contract }], }); @@ -332,7 +332,7 @@ describe("CosmWasmClient.searchTx", () => { it("can search by message.contract_address + message.action", async () => { pendingWithoutWasmd(); assert(postedExecute, "value must be set in beforeAll()"); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const results = await client.searchTx({ tags: [ { key: "message.contract_address", value: postedExecute.contract }, diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index 6f9ff4c4..6050d99c 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -17,8 +17,8 @@ import { makeRandomAddress, pendingWithoutWasmd, tendermintIdMatcher, + wasmd, wasmdEnabled, - wasmdEndpoint, } from "./testutils.spec"; import { MsgSend, StdFee } from "./types"; @@ -43,7 +43,7 @@ interface HackatomInstance { describe("CosmWasmClient", () => { describe("makeReadOnly", () => { it("can be constructed", () => { - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); expect(client).toBeTruthy(); }); }); @@ -51,18 +51,18 @@ describe("CosmWasmClient", () => { describe("getChainId", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); - expect(await client.getChainId()).toEqual("testing"); + const client = new CosmWasmClient(wasmd.endpoint); + expect(await client.getChainId()).toEqual(wasmd.expectedChainId); }); it("caches chain ID", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const openedClient = (client as unknown) as PrivateCosmWasmClient; const getCodeSpy = spyOn(openedClient.restClient, "nodeInfo").and.callThrough(); - expect(await client.getChainId()).toEqual("testing"); // from network - expect(await client.getChainId()).toEqual("testing"); // from cache + expect(await client.getChainId()).toEqual(wasmd.expectedChainId); // from network + expect(await client.getChainId()).toEqual(wasmd.expectedChainId); // from cache expect(getCodeSpy).toHaveBeenCalledTimes(1); }); @@ -71,7 +71,7 @@ describe("CosmWasmClient", () => { describe("getHeight", () => { it("gets height via last block", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const openedClient = (client as unknown) as PrivateCosmWasmClient; const blockLatestSpy = spyOn(openedClient.restClient, "blocksLatest").and.callThrough(); @@ -86,7 +86,7 @@ describe("CosmWasmClient", () => { it("gets height via authAccount once an address is known", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const openedClient = (client as unknown) as PrivateCosmWasmClient; const blockLatestSpy = spyOn(openedClient.restClient, "blocksLatest").and.callThrough(); @@ -111,7 +111,7 @@ describe("CosmWasmClient", () => { describe("getNonce", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); expect(await client.getNonce(unused.address)).toEqual({ accountNumber: 5, sequence: 0, @@ -120,7 +120,7 @@ describe("CosmWasmClient", () => { it("throws for missing accounts", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const missing = makeRandomAddress(); await client.getNonce(missing).then( () => fail("this must not succeed"), @@ -132,7 +132,7 @@ describe("CosmWasmClient", () => { describe("getAccount", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); expect(await client.getAccount(unused.address)).toEqual({ address: unused.address, accountNumber: 5, @@ -147,7 +147,7 @@ describe("CosmWasmClient", () => { it("returns undefined for missing accounts", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const missing = makeRandomAddress(); expect(await client.getAccount(missing)).toBeUndefined(); }); @@ -156,7 +156,7 @@ describe("CosmWasmClient", () => { describe("getBlock", () => { it("works for latest block", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const response = await client.getBlock(); // id @@ -176,7 +176,7 @@ describe("CosmWasmClient", () => { it("works for block by height", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const height = (await client.getBlock()).header.height; const response = await client.getBlock(height - 1); @@ -199,7 +199,7 @@ describe("CosmWasmClient", () => { describe("getIdentifier", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); expect(await client.getIdentifier(cosmoshub.tx)).toEqual(cosmoshub.id); }); }); @@ -208,7 +208,7 @@ describe("CosmWasmClient", () => { it("works", async () => { pendingWithoutWasmd(); const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const memo = "My first contract on chain"; const sendMsg: MsgSend = { @@ -255,7 +255,7 @@ describe("CosmWasmClient", () => { describe("getCodes", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const result = await client.getCodes(); expect(result.length).toBeGreaterThanOrEqual(1); const [first] = result; @@ -272,7 +272,7 @@ describe("CosmWasmClient", () => { describe("getCodeDetails", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const result = await client.getCodeDetails(1); const expectedInfo: Code = { @@ -291,7 +291,7 @@ describe("CosmWasmClient", () => { it("caches downloads", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const openedClient = (client as unknown) as PrivateCosmWasmClient; const getCodeSpy = spyOn(openedClient.restClient, "getCode").and.callThrough(); @@ -306,7 +306,7 @@ describe("CosmWasmClient", () => { describe("getContracts", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const result = await client.getContracts(1); expect(result.length).toBeGreaterThanOrEqual(3); const [hash, isa, jade] = result; @@ -334,7 +334,7 @@ describe("CosmWasmClient", () => { describe("getContract", () => { it("works for HASH instance", async () => { pendingWithoutWasmd(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const hash = await client.getContract("cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5"); expect(hash).toEqual({ address: "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", @@ -373,7 +373,7 @@ describe("CosmWasmClient", () => { if (wasmdEnabled()) { pendingWithoutWasmd(); const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes => + const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes => pen.sign(signBytes), ); const { codeId } = await client.upload(getRandomizedHackatom()); @@ -387,7 +387,7 @@ describe("CosmWasmClient", () => { pendingWithoutWasmd(); assert(contract); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const raw = await client.queryContractRaw(contract.address, configKey); assert(raw, "must get result"); expect(JSON.parse(fromUtf8(raw))).toEqual({ @@ -401,7 +401,7 @@ describe("CosmWasmClient", () => { pendingWithoutWasmd(); assert(contract); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const raw = await client.queryContractRaw(contract.address, otherKey); expect(raw).toBeNull(); }); @@ -411,7 +411,7 @@ describe("CosmWasmClient", () => { assert(contract); const nonExistentAddress = makeRandomAddress(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); await client.queryContractRaw(nonExistentAddress, configKey).then( () => fail("must not succeed"), error => expect(error).toMatch(`No contract found at address "${nonExistentAddress}"`), @@ -426,7 +426,7 @@ describe("CosmWasmClient", () => { if (wasmdEnabled()) { pendingWithoutWasmd(); const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes => + const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes => pen.sign(signBytes), ); const { codeId } = await client.upload(getRandomizedHackatom()); @@ -440,7 +440,7 @@ describe("CosmWasmClient", () => { pendingWithoutWasmd(); assert(contract); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); const verifier = await client.queryContractSmart(contract.address, { verifier: {} }); expect(fromAscii(verifier)).toEqual(contract.initMsg.verifier); }); @@ -449,7 +449,7 @@ describe("CosmWasmClient", () => { pendingWithoutWasmd(); assert(contract); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); await client.queryContractSmart(contract.address, { broken: {} }).then( () => fail("must not succeed"), error => expect(error).toMatch(/Error parsing QueryMsg/i), @@ -460,7 +460,7 @@ describe("CosmWasmClient", () => { pendingWithoutWasmd(); const nonExistentAddress = makeRandomAddress(); - const client = new CosmWasmClient(wasmdEndpoint); + const client = new CosmWasmClient(wasmd.endpoint); await client.queryContractSmart(nonExistentAddress, { verifier: {} }).then( () => fail("must not succeed"), error => expect(error).toMatch(`No contract found at address "${nonExistentAddress}"`), diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index 7eec81d4..36b6c1ce 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -24,8 +24,8 @@ import { tendermintIdMatcher, tendermintOptionalIdMatcher, tendermintShortHashMatcher, + wasmd, wasmdEnabled, - wasmdEndpoint, } from "./testutils.spec"; import { Coin, @@ -166,7 +166,7 @@ async function executeContract( describe("RestClient", () => { it("can be constructed", () => { - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); expect(client).toBeTruthy(); }); @@ -175,7 +175,7 @@ describe("RestClient", () => { describe("authAccounts", () => { it("works for unused account without pubkey", async () => { pendingWithoutWasmd(); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const { height, result } = await client.authAccounts(unusedAccount.address); expect(height).toMatch(tendermintHeightMatcher); expect(result).toEqual({ @@ -202,7 +202,7 @@ describe("RestClient", () => { // 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(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const { result } = await client.authAccounts(faucet.address); expect(result.value).toEqual( jasmine.objectContaining({ @@ -214,7 +214,7 @@ describe("RestClient", () => { // This property is used by CosmWasmClient.getAccount it("returns empty address for non-existent account", async () => { pendingWithoutWasmd(); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const nonExistentAccount = makeRandomAddress(); const { result } = await client.authAccounts(nonExistentAccount); expect(result).toEqual({ @@ -229,7 +229,7 @@ describe("RestClient", () => { describe("blocksLatest", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const response = await client.blocksLatest(); // id @@ -262,7 +262,7 @@ describe("RestClient", () => { describe("blocks", () => { it("works for block by height", async () => { pendingWithoutWasmd(); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const height = parseInt((await client.blocksLatest()).block.header.height, 10); const response = await client.blocks(height - 1); @@ -298,7 +298,7 @@ describe("RestClient", () => { describe("nodeInfo", () => { it("works", async () => { pendingWithoutWasmd(); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const { node_info, application_version } = await client.nodeInfo(); expect(node_info).toEqual({ @@ -339,7 +339,7 @@ describe("RestClient", () => { beforeAll(async () => { if (wasmdEnabled()) { const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new SigningCosmWasmClient(wasmdEndpoint, faucet.address, signBytes => + const client = new SigningCosmWasmClient(wasmd.endpoint, faucet.address, signBytes => pen.sign(signBytes), ); @@ -353,7 +353,7 @@ describe("RestClient", () => { const result = await client.sendTokens(recipient, transferAmount); await sleep(50); // wait until tx is indexed - const txDetails = await new RestClient(wasmdEndpoint).txsById(result.transactionHash); + const txDetails = await new RestClient(wasmd.endpoint).txsById(result.transactionHash); posted = { sender: faucet.address, recipient: recipient, @@ -367,7 +367,7 @@ describe("RestClient", () => { it("can query transactions by height", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const result = await client.txsQuery(`tx.height=${posted.height}&limit=26`); expect(parseInt(result.count, 10)).toEqual(1); expect(parseInt(result.limit, 10)).toEqual(26); @@ -380,7 +380,7 @@ describe("RestClient", () => { it("can query transactions by ID", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const result = await client.txsQuery(`tx.hash=${posted.hash}&limit=26`); expect(parseInt(result.count, 10)).toEqual(1); expect(parseInt(result.limit, 10)).toEqual(26); @@ -393,7 +393,7 @@ describe("RestClient", () => { it("can query transactions by sender", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const result = await client.txsQuery(`message.sender=${posted.sender}&limit=200`); expect(parseInt(result.count, 10)).toBeGreaterThanOrEqual(1); expect(parseInt(result.limit, 10)).toEqual(200); @@ -407,7 +407,7 @@ describe("RestClient", () => { it("can query transactions by recipient", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const result = await client.txsQuery(`transfer.recipient=${posted.recipient}&limit=200`); expect(parseInt(result.count, 10)).toEqual(1); expect(parseInt(result.limit, 10)).toEqual(200); @@ -422,7 +422,7 @@ describe("RestClient", () => { pending("This combination is broken 🤷‍♂️. Handle client-side at higher level."); pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const hashQuery = `tx.hash=${posted.hash}`; { @@ -449,7 +449,7 @@ describe("RestClient", () => { it("can filter by recipient and tx.minheight", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const recipientQuery = `transfer.recipient=${posted.recipient}`; { @@ -476,7 +476,7 @@ describe("RestClient", () => { it("can filter by recipient and tx.maxheight", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const recipientQuery = `transfer.recipient=${posted.recipient}`; { @@ -503,7 +503,7 @@ describe("RestClient", () => { it("can query by tags (module + code_id)", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const result = await client.txsQuery(`message.module=wasm&message.code_id=${deployedErc20.codeId}`); expect(parseInt(result.count, 10)).toBeGreaterThanOrEqual(4); @@ -549,7 +549,7 @@ describe("RestClient", () => { it("can query by tags (module + code_id + action)", async () => { pendingWithoutWasmd(); assert(posted); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); { const uploads = await client.txsQuery( @@ -606,7 +606,7 @@ describe("RestClient", () => { describe("encodeTx", () => { it("works for cosmoshub example", async () => { pendingWithoutWasmd(); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); expect(await client.encodeTx(cosmoshub.tx)).toEqual(fromBase64(cosmoshub.tx_data)); }); }); @@ -641,7 +641,7 @@ describe("RestClient", () => { gas: "890000", }; - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const { account_number, sequence } = (await client.authAccounts(faucet.address)).result.value; const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account_number, sequence); @@ -655,7 +655,7 @@ describe("RestClient", () => { it("can upload, instantiate and execute wasm", async () => { pendingWithoutWasmd(); const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const transferAmount: readonly Coin[] = [ { @@ -729,7 +729,7 @@ describe("RestClient", () => { it("can list upload code", async () => { pendingWithoutWasmd(); const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); // check with contracts were here first to compare const existingInfos = await client.listCodeInfo(); @@ -769,7 +769,7 @@ describe("RestClient", () => { it("can list contracts and get info", async () => { pendingWithoutWasmd(); const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic); - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const beneficiaryAddress = makeRandomAddress(); const transferAmount: readonly Coin[] = [ { @@ -830,7 +830,7 @@ describe("RestClient", () => { }); describe("contract state", () => { - const client = new RestClient(wasmdEndpoint); + const client = new RestClient(wasmd.endpoint); const noContract = makeRandomAddress(); const expectedKey = toAscii("config"); let contractAddress: string | undefined; diff --git a/packages/sdk/src/testutils.spec.ts b/packages/sdk/src/testutils.spec.ts index 21683703..ce680689 100644 --- a/packages/sdk/src/testutils.spec.ts +++ b/packages/sdk/src/testutils.spec.ts @@ -78,7 +78,10 @@ export const deployedErc20 = { ], }; -export const wasmdEndpoint = "http://localhost:1317"; +export const wasmd = { + endpoint: "http://localhost:1317", + expectedChainId: "testing", +}; export const faucet = { mnemonic: