Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add get block with txs #71

Merged
merged 8 commits into from
Feb 15, 2023
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 @@ -72,8 +70,10 @@ import {
setupNodeExtension,
setupTokenExtension,
setupWasmplusExtension,
setupTx2Extension,
TokenExtension,
WasmplusExtension,
Tx2Extension,
} from "./modules";

export type QueryClientWithExtensions = QueryClient &
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 { Tx2Extension, setupTx2Extension } 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 @@ -128,3 +128,4 @@ export {
wasmplusTypes,
} from "./wasmplus/messages";
export { setupWasmplusExtension, WasmplusExtension } from "./wasmplus/queries";
export { setupTx2Extension, Tx2Extension } from "./tx2/queries";
62 changes: 62 additions & 0 deletions packages/finschia/src/modules/tx2/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { coins, coin, QueryClient } from "@cosmjs/stargate";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";

import { makeLinkPath } from "../../paths";
import { SigningFinschiaClient } from "../../signingfinschiaclient";
import { defaultSigningClientOptions, faucet, 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 defaultFee = {
amount: coins(250000, "cony"),
jaeseung-bae marked this conversation as resolved.
Show resolved Hide resolved
gas: "1500000", // 1.5 million
};
const client = await SigningFinschiaClient.connectWithSigner(
simapp.tendermintUrl,
wallet,
defaultSigningClientOptions,
);
const sendAmount = coin("1000", "cony");
const msg = {
typeUrl: "/lbm.foundation.v1.MsgFundTreasury",
0Tech marked this conversation as resolved.
Show resolved Hide resolved
value: { from: faucet.address0, amount: [sendAmount] },
};

return client.signAndBroadcast(faucet.address0, [msg], defaultFee);
}

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();
});

// Currently ignored test, because current lbm-app doesn't expose 'lbm.tx.v1beta1.Service' endpoint
0Tech marked this conversation as resolved.
Show resolved Hide resolved
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),
});
},
},
};
}