stargate: Add getBlock method to client
This commit is contained in:
parent
f969e661e2
commit
cd7c5c2efa
@ -3,6 +3,7 @@ import { Bech32, fromBase64 } from "@cosmjs/encoding";
|
||||
import { Secp256k1Wallet } from "@cosmjs/launchpad";
|
||||
import { makeSignBytes, omitDefaults, Registry } from "@cosmjs/proto-signing";
|
||||
import { assert, sleep } from "@cosmjs/utils";
|
||||
import { ReadonlyDate } from "readonly-date";
|
||||
|
||||
import { cosmos } from "./generated/codecimpl";
|
||||
import { assertIsBroadcastTxSuccess, PrivateStargateClient, StargateClient } from "./stargateclient";
|
||||
@ -12,6 +13,7 @@ import {
|
||||
nonExistentAddress,
|
||||
pendingWithoutSimapp,
|
||||
simapp,
|
||||
tendermintIdMatcher,
|
||||
unused,
|
||||
validator,
|
||||
} from "./testutils.spec";
|
||||
@ -133,6 +135,53 @@ describe("StargateClient", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getBlock", () => {
|
||||
it("works for latest block", async () => {
|
||||
pendingWithoutSimapp();
|
||||
const client = await StargateClient.connect(simapp.tendermintUrl);
|
||||
const response = await client.getBlock();
|
||||
|
||||
expect(response).toEqual(
|
||||
jasmine.objectContaining({
|
||||
id: jasmine.stringMatching(tendermintIdMatcher),
|
||||
header: jasmine.objectContaining({
|
||||
chainId: await client.getChainId(),
|
||||
}),
|
||||
txs: [],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(response.header.height).toBeGreaterThanOrEqual(1);
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
|
||||
ReadonlyDate.now() - 5_000,
|
||||
);
|
||||
});
|
||||
|
||||
it("works for block by height", async () => {
|
||||
pendingWithoutSimapp();
|
||||
const client = await StargateClient.connect(simapp.tendermintUrl);
|
||||
const height = (await client.getBlock()).header.height;
|
||||
const response = await client.getBlock(height - 1);
|
||||
|
||||
expect(response).toEqual(
|
||||
jasmine.objectContaining({
|
||||
id: jasmine.stringMatching(tendermintIdMatcher),
|
||||
header: jasmine.objectContaining({
|
||||
height: height - 1,
|
||||
chainId: await client.getChainId(),
|
||||
}),
|
||||
txs: [],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
|
||||
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
|
||||
ReadonlyDate.now() - 5_000,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getBalance", () => {
|
||||
it("works for different existing balances", async () => {
|
||||
pendingWithoutSimapp();
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { Bech32, toAscii, toHex } from "@cosmjs/encoding";
|
||||
import { Coin, decodeAminoPubkey, PubKey } from "@cosmjs/launchpad";
|
||||
import { Uint64 } from "@cosmjs/math";
|
||||
import { Block, Coin, decodeAminoPubkey, PubKey } from "@cosmjs/launchpad";
|
||||
import { Uint53, Uint64 } from "@cosmjs/math";
|
||||
import { decodeAny } from "@cosmjs/proto-signing";
|
||||
import { broadcastTxCommitSuccess, Client as TendermintClient } from "@cosmjs/tendermint-rpc";
|
||||
import { arrayContentEquals, assert, assertDefined } from "@cosmjs/utils";
|
||||
@ -151,6 +151,23 @@ export class StargateClient {
|
||||
}
|
||||
}
|
||||
|
||||
public async getBlock(height?: number): Promise<Block> {
|
||||
const response = await this.tmClient.block(height);
|
||||
return {
|
||||
id: toHex(response.blockId.hash).toUpperCase(),
|
||||
header: {
|
||||
version: {
|
||||
block: new Uint53(response.block.header.version.block).toString(),
|
||||
app: new Uint53(response.block.header.version.app).toString(),
|
||||
},
|
||||
height: response.block.header.height,
|
||||
chainId: response.block.header.chainId,
|
||||
time: response.block.header.time.toISOString(),
|
||||
},
|
||||
txs: response.block.txs,
|
||||
};
|
||||
}
|
||||
|
||||
public async getBalance(address: string, searchDenom: string): Promise<Coin | null> {
|
||||
// balance key is a bit tricker, using some prefix stores
|
||||
// https://github.com/cosmwasm/cosmos-sdk/blob/80f7ff62f79777a487d0c7a53c64b0f7e43c47b9/x/bank/keeper/view.go#L74-L77
|
||||
|
||||
@ -54,3 +54,5 @@ export const validator = {
|
||||
};
|
||||
|
||||
export const nonExistentAddress = "cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd";
|
||||
|
||||
export const tendermintIdMatcher = /^[0-9A-F]{64}$/;
|
||||
|
||||
3
packages/stargate/types/stargateclient.d.ts
vendored
3
packages/stargate/types/stargateclient.d.ts
vendored
@ -1,4 +1,4 @@
|
||||
import { Coin, PubKey } from "@cosmjs/launchpad";
|
||||
import { Block, Coin, PubKey } from "@cosmjs/launchpad";
|
||||
import { Client as TendermintClient } from "@cosmjs/tendermint-rpc";
|
||||
export interface Account {
|
||||
/** Bech32 account address */
|
||||
@ -46,6 +46,7 @@ export declare class StargateClient {
|
||||
getHeight(): Promise<number>;
|
||||
getAccount(searchAddress: string): Promise<Account | null>;
|
||||
getSequence(address: string): Promise<SequenceResponse | null>;
|
||||
getBlock(height?: number): Promise<Block>;
|
||||
getBalance(address: string, searchDenom: string): Promise<Coin | null>;
|
||||
/**
|
||||
* Queries all balances for all denoms that belong to this address.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user