From 4c6b2cce3b5f9e6ea2bbf5b07c3e0c499efb9583 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 7 Jul 2020 10:13:14 +0200 Subject: [PATCH] Use LcdClient in CosmosClient --- .../sdk38/src/cosmosclient.searchtx.spec.ts | 4 +-- packages/sdk38/src/cosmosclient.spec.ts | 8 +++--- packages/sdk38/src/cosmosclient.ts | 25 +++++++++---------- packages/sdk38/src/lcdapi/index.ts | 1 + .../sdk38/src/signingcosmosclient.spec.ts | 4 +-- packages/sdk38/types/cosmosclient.d.ts | 7 +++--- packages/sdk38/types/lcdapi/index.d.ts | 1 + 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/sdk38/src/cosmosclient.searchtx.spec.ts b/packages/sdk38/src/cosmosclient.searchtx.spec.ts index 9c42f8fb..cb6ca4de 100644 --- a/packages/sdk38/src/cosmosclient.searchtx.spec.ts +++ b/packages/sdk38/src/cosmosclient.searchtx.spec.ts @@ -4,9 +4,9 @@ import { assert, sleep } from "@cosmjs/utils"; import { coins } from "./coins"; import { CosmosClient, isPostTxFailure } from "./cosmosclient"; import { makeSignBytes } from "./encoding"; +import { LcdClient } from "./lcdapi"; import { isMsgSend, MsgSend } from "./msgs"; import { Secp256k1Pen } from "./pen"; -import { RestClient } from "./restclient"; import { SigningCosmosClient } from "./signingcosmosclient"; import { faucet, @@ -86,7 +86,7 @@ describe("CosmosClient.searchTx", () => { const transferAmount = coins(1234567, "ucosm"); const result = await client.sendTokens(recipient, transferAmount); await sleep(75); // wait until tx is indexed - const txDetails = await new RestClient(wasmd.endpoint).txById(result.transactionHash); + const txDetails = await new LcdClient(wasmd.endpoint).txById(result.transactionHash); sendSuccessful = { sender: faucet.address, recipient: recipient, diff --git a/packages/sdk38/src/cosmosclient.spec.ts b/packages/sdk38/src/cosmosclient.spec.ts index cdea2316..d05ddd94 100644 --- a/packages/sdk38/src/cosmosclient.spec.ts +++ b/packages/sdk38/src/cosmosclient.spec.ts @@ -43,7 +43,7 @@ describe("CosmosClient", () => { pendingWithoutWasmd(); const client = new CosmosClient(wasmd.endpoint); const openedClient = (client as unknown) as PrivateCosmWasmClient; - const getCodeSpy = spyOn(openedClient.restClient, "nodeInfo").and.callThrough(); + const getCodeSpy = spyOn(openedClient.lcdClient, "nodeInfo").and.callThrough(); expect(await client.getChainId()).toEqual(wasmd.chainId); // from network expect(await client.getChainId()).toEqual(wasmd.chainId); // from cache @@ -57,7 +57,7 @@ describe("CosmosClient", () => { pendingWithoutWasmd(); const client = new CosmosClient(wasmd.endpoint); const openedClient = (client as unknown) as PrivateCosmWasmClient; - const blockLatestSpy = spyOn(openedClient.restClient, "blocksLatest").and.callThrough(); + const blockLatestSpy = spyOn(openedClient.lcdClient, "blocksLatest").and.callThrough(); const height1 = await client.getHeight(); expect(height1).toBeGreaterThan(0); @@ -74,8 +74,8 @@ describe("CosmosClient", () => { const client = new CosmosClient(wasmd.endpoint); const openedClient = (client as unknown) as PrivateCosmWasmClient; - const blockLatestSpy = spyOn(openedClient.restClient, "blocksLatest").and.callThrough(); - const authAccountsSpy = spyOn(openedClient.restClient, "authAccounts").and.callThrough(); + const blockLatestSpy = spyOn(openedClient.lcdClient, "blocksLatest").and.callThrough(); + const authAccountsSpy = spyOn(openedClient.lcdClient, "authAccounts").and.callThrough(); const height1 = await client.getHeight(); expect(height1).toBeGreaterThan(0); diff --git a/packages/sdk38/src/cosmosclient.ts b/packages/sdk38/src/cosmosclient.ts index 890d2d8a..5ab8b2b2 100644 --- a/packages/sdk38/src/cosmosclient.ts +++ b/packages/sdk38/src/cosmosclient.ts @@ -3,10 +3,9 @@ import { fromBase64, fromHex, toHex } from "@cosmjs/encoding"; import { Uint53 } from "@cosmjs/math"; import { Coin } from "./coins"; -import { BroadcastMode } from "./lcdapi"; +import { BroadcastMode, LcdClient } from "./lcdapi"; import { Log, parseLogs } from "./logs"; import { decodeBech32Pubkey } from "./pubkey"; -import { RestClient } from "./restclient"; import { CosmosSdkTx, PubKey, StdTx } from "./types"; export interface GetNonceResult { @@ -131,11 +130,11 @@ export interface Block { /** Use for testing only */ export interface PrivateCosmWasmClient { - readonly restClient: RestClient; + readonly lcdClient: LcdClient; } export class CosmosClient { - protected readonly restClient: RestClient; + protected readonly lcdClient: LcdClient; /** Any address the chain considers valid (valid bech32 with proper prefix) */ protected anyValidAddress: string | undefined; @@ -151,12 +150,12 @@ export class CosmosClient { * @param broadcastMode Defines at which point of the transaction processing the postTx method (i.e. transaction broadcasting) returns */ public constructor(apiUrl: string, broadcastMode = BroadcastMode.Block) { - this.restClient = new RestClient(apiUrl, broadcastMode); + this.lcdClient = new LcdClient(apiUrl, broadcastMode); } public async getChainId(): Promise { if (!this.chainId) { - const response = await this.restClient.nodeInfo(); + const response = await this.lcdClient.nodeInfo(); const chainId = response.node_info.network; if (!chainId) throw new Error("Chain ID must not be empty"); this.chainId = chainId; @@ -167,12 +166,12 @@ export class CosmosClient { public async getHeight(): Promise { if (this.anyValidAddress) { - const { height } = await this.restClient.authAccounts(this.anyValidAddress); + const { height } = await this.lcdClient.authAccounts(this.anyValidAddress); return parseInt(height, 10); } else { // Note: this gets inefficient when blocks contain a lot of transactions since it // requires downloading and deserializing all transactions in the block. - const latest = await this.restClient.blocksLatest(); + const latest = await this.lcdClient.blocksLatest(); return parseInt(latest.block.header.height, 10); } } @@ -182,7 +181,7 @@ export class CosmosClient { */ public async getIdentifier(tx: CosmosSdkTx): Promise { // We consult the REST API because we don't have a local amino encoder - const response = await this.restClient.encodeTx(tx); + const response = await this.lcdClient.encodeTx(tx); const hash = new Sha256(fromBase64(response.tx)).digest(); return toHex(hash).toUpperCase(); } @@ -208,7 +207,7 @@ export class CosmosClient { } public async getAccount(address: string): Promise { - const account = await this.restClient.authAccounts(address); + const account = await this.lcdClient.authAccounts(address); const value = account.result.value; if (value.address === "") { return undefined; @@ -231,7 +230,7 @@ export class CosmosClient { */ public async getBlock(height?: number): Promise { const response = - height !== undefined ? await this.restClient.blocks(height) : await this.restClient.blocksLatest(); + height !== undefined ? await this.lcdClient.blocks(height) : await this.lcdClient.blocksLatest(); return { id: response.block_id.hash, @@ -288,7 +287,7 @@ export class CosmosClient { } public async postTx(tx: StdTx): Promise { - const result = await this.restClient.postTx(tx); + const result = await this.lcdClient.postTx(tx); if (!result.txhash.match(/^([0-9A-F][0-9A-F])+$/)) { throw new Error("Received ill-formatted txhash. Must be non-empty upper-case hex"); } @@ -311,7 +310,7 @@ export class CosmosClient { private async txsQuery(query: string): Promise { // TODO: we need proper pagination support const limit = 100; - const result = await this.restClient.txsQuery(`${query}&limit=${limit}`); + const result = await this.lcdClient.txsQuery(`${query}&limit=${limit}`); const pages = parseInt(result.page_total, 10); if (pages > 1) { throw new Error( diff --git a/packages/sdk38/src/lcdapi/index.ts b/packages/sdk38/src/lcdapi/index.ts index 59479bf3..04eeb8d9 100644 --- a/packages/sdk38/src/lcdapi/index.ts +++ b/packages/sdk38/src/lcdapi/index.ts @@ -18,3 +18,4 @@ export { SearchTxsResponse, TxsResponse, } from "./base"; +export { LcdApiArray, LcdClient, LcdModule } from "./lcdclient"; diff --git a/packages/sdk38/src/signingcosmosclient.spec.ts b/packages/sdk38/src/signingcosmosclient.spec.ts index 258c4f76..5ad36ce6 100644 --- a/packages/sdk38/src/signingcosmosclient.spec.ts +++ b/packages/sdk38/src/signingcosmosclient.spec.ts @@ -34,8 +34,8 @@ describe("SigningCosmosClient", () => { const client = new SigningCosmosClient(httpUrl, faucet.address, (signBytes) => pen.sign(signBytes)); const openedClient = (client as unknown) as PrivateCosmWasmClient; - const blockLatestSpy = spyOn(openedClient.restClient, "blocksLatest").and.callThrough(); - const authAccountsSpy = spyOn(openedClient.restClient, "authAccounts").and.callThrough(); + const blockLatestSpy = spyOn(openedClient.lcdClient, "blocksLatest").and.callThrough(); + const authAccountsSpy = spyOn(openedClient.lcdClient, "authAccounts").and.callThrough(); const height = await client.getHeight(); expect(height).toBeGreaterThan(0); diff --git a/packages/sdk38/types/cosmosclient.d.ts b/packages/sdk38/types/cosmosclient.d.ts index e83f6059..f5f31bc6 100644 --- a/packages/sdk38/types/cosmosclient.d.ts +++ b/packages/sdk38/types/cosmosclient.d.ts @@ -1,7 +1,6 @@ import { Coin } from "./coins"; -import { BroadcastMode } from "./lcdapi"; +import { BroadcastMode, LcdClient } from "./lcdapi"; import { Log } from "./logs"; -import { RestClient } from "./restclient"; import { CosmosSdkTx, PubKey, StdTx } from "./types"; export interface GetNonceResult { readonly accountNumber: number; @@ -95,10 +94,10 @@ export interface Block { } /** Use for testing only */ export interface PrivateCosmWasmClient { - readonly restClient: RestClient; + readonly lcdClient: LcdClient; } export declare class CosmosClient { - protected readonly restClient: RestClient; + protected readonly lcdClient: LcdClient; /** Any address the chain considers valid (valid bech32 with proper prefix) */ protected anyValidAddress: string | undefined; private chainId; diff --git a/packages/sdk38/types/lcdapi/index.d.ts b/packages/sdk38/types/lcdapi/index.d.ts index 5c36adce..e8512af4 100644 --- a/packages/sdk38/types/lcdapi/index.d.ts +++ b/packages/sdk38/types/lcdapi/index.d.ts @@ -9,3 +9,4 @@ export { SearchTxsResponse, TxsResponse, } from "./base"; +export { LcdApiArray, LcdClient, LcdModule } from "./lcdclient";