diff --git a/CHANGELOG.md b/CHANGELOG.md index 6db38764..dc5e761d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to ## [Unreleased] ### Added +- [\#71](https://github.com/line/lbmjs/pull/71) @lbmjs/finschia: Add custom GetBlockWithTxs for lbm ### Changed diff --git a/packages/finschia/src/finschiaclient.ts b/packages/finschia/src/finschiaclient.ts index 32c58ac9..cec74492 100644 --- a/packages/finschia/src/finschiaclient.ts +++ b/packages/finschia/src/finschiaclient.ts @@ -37,11 +37,9 @@ import { setupMintExtension, setupSlashingExtension, setupStakingExtension, - setupTxExtension, StakingExtension, StargateClientOptions, TimeoutError, - TxExtension, } from "@cosmjs/stargate"; import { SlashingExtension } from "@cosmjs/stargate/build/modules"; import { AuthzExtension } from "@cosmjs/stargate/build/modules/authz/queries"; @@ -71,8 +69,10 @@ import { setupIbcExtension, setupNodeExtension, setupTokenExtension, + setupTx2Extension, setupWasmplusExtension, TokenExtension, + Tx2Extension, WasmplusExtension, } from "./modules"; @@ -91,7 +91,7 @@ export type QueryClientWithExtensions = QueryClient & MintExtension & StakingExtension & TokenExtension & - TxExtension & + Tx2Extension & WasmExtension & WasmplusExtension & NodeExtension; @@ -113,7 +113,7 @@ function createQueryClientWithExtensions(tmClient: Tendermint34Client): QueryCli setupMintExtension, setupStakingExtension, setupTokenExtension, - setupTxExtension, + setupTx2Extension, setupWasmExtension, setupWasmplusExtension, setupNodeExtension, diff --git a/packages/finschia/src/index.ts b/packages/finschia/src/index.ts index ecaedfae..306e4e72 100644 --- a/packages/finschia/src/index.ts +++ b/packages/finschia/src/index.ts @@ -130,6 +130,7 @@ export { WasmplusExtension, wasmTypes, } from "./modules"; +export { setupTx2Extension, Tx2Extension } from "./modules"; export { makeLinkPath } from "./paths"; export { SigningFinschiaClient, UploadAndInstantiateResult } from "./signingfinschiaclient"; export { finschiaRegistryTypes } from "./types"; diff --git a/packages/finschia/src/modules/index.ts b/packages/finschia/src/modules/index.ts index 037cea80..cec91b09 100644 --- a/packages/finschia/src/modules/index.ts +++ b/packages/finschia/src/modules/index.ts @@ -120,6 +120,7 @@ export { tokenTypes, } from "./token/messages"; export { setupTokenExtension, TokenExtension } from "./token/queries"; +export { setupTx2Extension, Tx2Extension } from "./tx2/queries"; export { wasmTypes } from "./wasm/messages"; export { createWasmplusAminoConverters } from "./wasmplus/aminomessages"; export { diff --git a/packages/finschia/src/modules/tx2/queries.spec.ts b/packages/finschia/src/modules/tx2/queries.spec.ts new file mode 100644 index 00000000..016c8140 --- /dev/null +++ b/packages/finschia/src/modules/tx2/queries.spec.ts @@ -0,0 +1,73 @@ +import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; +import { coins, MsgSendEncodeObject, QueryClient } from "@cosmjs/stargate"; +import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; +import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx"; +import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx"; + +import { makeLinkPath } from "../../paths"; +import { SigningFinschiaClient } from "../../signingfinschiaclient"; +import { + defaultSigningClientOptions, + faucet, + makeRandomAddress, + pendingWithoutSimapp, + simapp, +} from "../../testutils.spec"; +import { longify } from "../../utils"; +import { setupTx2Extension, Tx2Extension } from "./queries"; + +async function makeClientWithTx2(rpcUrl: string): Promise<[QueryClient & Tx2Extension, Tendermint34Client]> { + const tmClient = await Tendermint34Client.connect(rpcUrl); + return [QueryClient.withExtensions(tmClient, setupTx2Extension), tmClient]; +} + +async function sendDummyTx() { + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, { + hdPaths: [makeLinkPath(0)], + prefix: simapp.prefix, + }); + const client = await SigningFinschiaClient.connectWithSigner( + simapp.tendermintUrl, + wallet, + defaultSigningClientOptions, + ); + const defaultFee = { + amount: coins(250000, simapp.denomFee), + gas: "1500000", + }; + const msg: MsgSend = { + fromAddress: faucet.address0, + toAddress: makeRandomAddress(), + amount: coins(1234, simapp.denomFee), + }; + const dummyMsg: MsgSendEncodeObject = { + typeUrl: "/cosmos.bank.v1beta1.MsgSend", + value: msg, + }; + const signed = await client.sign(faucet.address0, [dummyMsg], defaultFee, ""); + return await client.broadcastTx(Uint8Array.from(TxRaw.encode(signed).finish())); +} + +describe("Tx2Extension", () => { + let client: QueryClient & Tx2Extension; + let tmClient: Tendermint34Client; + beforeAll(async () => { + const res = await makeClientWithTx2(simapp.tendermintUrl); + client = res[0]; + tmClient = res[1]; + }); + afterAll(() => { + tmClient.disconnect(); + }); + + it("getBlockWithTxs", async () => { + pendingWithoutSimapp(); + + const txRes = await sendDummyTx(); + const response = await client.tx2.getBlockWithTxs(longify(txRes.height)); + + expect(response.txs).toBeDefined(); + expect(response.blockId).toBeDefined(); + expect(response.block).toBeDefined(); + }); +}); diff --git a/packages/finschia/src/modules/tx2/queries.ts b/packages/finschia/src/modules/tx2/queries.ts new file mode 100644 index 00000000..2cc71711 --- /dev/null +++ b/packages/finschia/src/modules/tx2/queries.ts @@ -0,0 +1,34 @@ +import { + createPagination, + createProtobufRpcClient, + QueryClient, + setupTxExtension, + TxExtension, +} from "@cosmjs/stargate"; +import { GetBlockWithTxsResponse, ServiceClientImpl } from "lbmjs-types/lbm/tx/v1beta1/service"; +import Long from "long"; + +export interface Tx2Extension extends TxExtension { + readonly tx2: { + readonly getBlockWithTxs: (height: Long, paginationKey?: Uint8Array) => Promise; + }; +} + +export function setupTx2Extension(base: QueryClient): Tx2Extension { + const txExtension = setupTxExtension(base); + const rpc = createProtobufRpcClient(base); + + // GetBlockWithTxs(request: GetBlockWithTxsRequest): Promise; + const queryService = new ServiceClientImpl(rpc); + return { + tx: txExtension.tx, + tx2: { + getBlockWithTxs: async (height: Long, paginationKey?: Uint8Array) => { + return await queryService.GetBlockWithTxs({ + height: height, + pagination: createPagination(paginationKey), + }); + }, + }, + }; +}