Use LcdClient in CosmosClient
This commit is contained in:
parent
1675f94f14
commit
4c6b2cce3b
@ -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,
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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<string> {
|
||||
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<number> {
|
||||
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<string> {
|
||||
// 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<Account | undefined> {
|
||||
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<Block> {
|
||||
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<PostTxResult> {
|
||||
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<readonly IndexedTx[]> {
|
||||
// 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(
|
||||
|
||||
@ -18,3 +18,4 @@ export {
|
||||
SearchTxsResponse,
|
||||
TxsResponse,
|
||||
} from "./base";
|
||||
export { LcdApiArray, LcdClient, LcdModule } from "./lcdclient";
|
||||
|
||||
@ -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);
|
||||
|
||||
7
packages/sdk38/types/cosmosclient.d.ts
vendored
7
packages/sdk38/types/cosmosclient.d.ts
vendored
@ -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;
|
||||
|
||||
1
packages/sdk38/types/lcdapi/index.d.ts
vendored
1
packages/sdk38/types/lcdapi/index.d.ts
vendored
@ -9,3 +9,4 @@ export {
|
||||
SearchTxsResponse,
|
||||
TxsResponse,
|
||||
} from "./base";
|
||||
export { LcdApiArray, LcdClient, LcdModule } from "./lcdclient";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user