Skip to content

Commit

Permalink
Feature/add get block with txs (#71)
Browse files Browse the repository at this point in the history
* feat: add custom GetBlockWithTxs for lbm
---------
Signed-off-by: zemyblue <[email protected]>
Co-authored-by: zemyblue <[email protected]>
Co-authored-by: Youngtaek Yoon <[email protected]>
  • Loading branch information
jaeseung-bae authored Feb 15, 2023
1 parent b455367 commit 08f1c2e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions packages/finschia/src/finschiaclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -71,8 +69,10 @@ import {
setupIbcExtension,
setupNodeExtension,
setupTokenExtension,
setupTx2Extension,
setupWasmplusExtension,
TokenExtension,
Tx2Extension,
WasmplusExtension,
} from "./modules";

Expand All @@ -91,7 +91,7 @@ export type QueryClientWithExtensions = QueryClient &
MintExtension &
StakingExtension &
TokenExtension &
TxExtension &
Tx2Extension &
WasmExtension &
WasmplusExtension &
NodeExtension;
Expand All @@ -113,7 +113,7 @@ function createQueryClientWithExtensions(tmClient: Tendermint34Client): QueryCli
setupMintExtension,
setupStakingExtension,
setupTokenExtension,
setupTxExtension,
setupTx2Extension,
setupWasmExtension,
setupWasmplusExtension,
setupNodeExtension,
Expand Down
1 change: 1 addition & 0 deletions packages/finschia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions packages/finschia/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
73 changes: 73 additions & 0 deletions packages/finschia/src/modules/tx2/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
34 changes: 34 additions & 0 deletions packages/finschia/src/modules/tx2/queries.ts
Original file line number Diff line number Diff line change
@@ -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<GetBlockWithTxsResponse>;
};
}

export function setupTx2Extension(base: QueryClient): Tx2Extension {
const txExtension = setupTxExtension(base);
const rpc = createProtobufRpcClient(base);

// GetBlockWithTxs(request: GetBlockWithTxsRequest): Promise<GetBlockWithTxsResponse>;
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),
});
},
},
};
}

0 comments on commit 08f1c2e

Please sign in to comment.