Skip to content

Commit 2fda8f5

Browse files
committed
Merge branch 'develop'
2 parents 8cb5438 + ec3d386 commit 2fda8f5

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lum-network/sdk-javascript",
3-
"version": "0.7.5",
3+
"version": "0.7.6",
44
"license": "Apache-2.0",
55
"description": "Javascript SDK library for NodeJS and Web browsers to interact with the Lum Network.",
66
"homepage": "https://github.com/lum-network/sdk-javascript#readme",

src/client/LumClient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
setupStakingExtension,
2222
AirdropExtension,
2323
setupAirdropExtension,
24+
TxExtension,
25+
setupTxExtension,
2426
} from '../extensions';
2527
import { setupSlashingExtension, SlashingExtension } from '../extensions/slashing';
2628
import { AuthzExtension, setupAuthzExtension } from '../extensions/authz';
@@ -40,7 +42,8 @@ export class LumClient {
4042
StakingExtension &
4143
SlashingExtension &
4244
FeegrantExtension &
43-
AirdropExtension;
45+
AirdropExtension &
46+
TxExtension;
4447
private chainId?: string;
4548

4649
/**
@@ -64,6 +67,7 @@ export class LumClient {
6467
setupSlashingExtension,
6568
setupFeegrantExtension,
6669
setupAirdropExtension,
70+
setupTxExtension,
6771
);
6872

6973
// Used for debugging while gasWanted, gasUsed and codespace are still waiting to be included in the code lib

src/extensions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './gov';
77
export * from './ibc';
88
export * from './mint';
99
export * from './staking';
10+
export * from './tx';

src/extensions/tx.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import Long from 'long';
2+
import { createProtobufRpcClient, QueryClient } from '@cosmjs/stargate';
3+
4+
import { SignMode } from '../codec/cosmos/tx/signing/v1beta1/signing';
5+
import { GetTxRequest, GetTxResponse, ServiceClientImpl, SimulateRequest, SimulateResponse } from '../codec/cosmos/tx/v1beta1/service';
6+
import { AuthInfo, Fee, Tx, TxBody } from '../codec/cosmos/tx/v1beta1/tx';
7+
import { Message } from '../messages';
8+
import { LumRegistry } from '../registry';
9+
import { publicKeyToProto } from '../utils';
10+
11+
export interface TxExtension {
12+
readonly tx: {
13+
getTx: (txId: string) => Promise<GetTxResponse>;
14+
simulate: (messages: readonly Message[], memo: string | undefined, pubkey: Uint8Array, sequence: number) => Promise<SimulateResponse>;
15+
};
16+
}
17+
18+
export function setupTxExtension(base: QueryClient): TxExtension {
19+
// Use this service to get easy typed access to query methods
20+
// This cannot be used for proof verification
21+
const rpc = createProtobufRpcClient(base);
22+
const queryService = new ServiceClientImpl(rpc);
23+
24+
return {
25+
tx: {
26+
getTx: async (hash: string) => {
27+
const request: GetTxRequest = {
28+
hash,
29+
};
30+
const response = await queryService.GetTx(request);
31+
return response;
32+
},
33+
simulate: async (messages: readonly Message[], memo: string | undefined, pubkey: Uint8Array, sequence: number) => {
34+
const request = SimulateRequest.fromPartial({
35+
tx: Tx.fromPartial({
36+
authInfo: AuthInfo.fromPartial({
37+
fee: Fee.fromPartial({}),
38+
signerInfos: [
39+
{
40+
publicKey: publicKeyToProto(pubkey),
41+
sequence: Long.fromNumber(sequence, true),
42+
modeInfo: { single: { mode: SignMode.SIGN_MODE_UNSPECIFIED } },
43+
},
44+
],
45+
}),
46+
body: TxBody.fromPartial({
47+
messages: messages.map((m) => {
48+
return { typeUrl: m.typeUrl, value: LumRegistry.encode(m) };
49+
}),
50+
memo: memo,
51+
}),
52+
signatures: [new Uint8Array()],
53+
}),
54+
// Sending serialized `txBytes` is the future. But
55+
// this is not available in Comsos SDK 0.42.
56+
txBytes: undefined,
57+
});
58+
const response = await queryService.Simulate(request);
59+
return response;
60+
},
61+
},
62+
};
63+
}

tests/client.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ describe('LumClient', () => {
3535
wsClt.disconnect();
3636
});
3737

38+
it.only('should be able to simulate transactions', async () => {
39+
const w3 = await LumWalletFactory.fromMnemonic(LumUtils.generateMnemonic());
40+
// Should reject invalid bech32 addresses
41+
await expect(
42+
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w1.getAddress(), 'toto', [{ denom: LumConstants.MicroLumDenom, amount: '1' }])], 'hello', w1.getPublicKey(), 0),
43+
).rejects.toThrow();
44+
// Should reject invalid signer
45+
await expect(
46+
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w3.getAddress(), w3.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '1' }])], 'hello', w3.getPublicKey(), 0),
47+
).rejects.toThrow();
48+
// Should reject invalid amounts
49+
await expect(
50+
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w1.getAddress(), w1.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '-1' }])], 'hello', w3.getPublicKey(), 0),
51+
).rejects.toThrow();
52+
// Should reject invalid sequences
53+
await expect(
54+
clt.queryClient.tx.simulate([LumMessages.BuildMsgSend(w1.getAddress(), w1.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '1' }])], 'hello', w3.getPublicKey(), -1),
55+
).rejects.toThrow();
56+
// Should return simulation in case of success
57+
const res = await clt.queryClient.tx.simulate(
58+
[LumMessages.BuildMsgSend(w1.getAddress(), w1.getAddress(), [{ denom: LumConstants.MicroLumDenom, amount: '1' }])],
59+
'hello',
60+
w3.getPublicKey(),
61+
0,
62+
);
63+
expect(res).toBeTruthy();
64+
expect(res.gasInfo).toBeTruthy();
65+
expect(res.result).toBeTruthy();
66+
});
67+
3868
it('should open a beam and close it', async () => {
3969
const beamId = randomString();
4070

0 commit comments

Comments
 (0)