Fix undefined this in decodeBroadcastTxAsync

This commit is contained in:
Simon Warta 2021-11-20 10:24:33 +01:00
parent 4962be9729
commit cd5bc69012
5 changed files with 103 additions and 33 deletions

View File

@ -12,6 +12,13 @@ and this project adheres to
`string` as input types for the amount. This is useful if your values exceed
the safe integer range.
### Fixed
- @cosmjs/tendermint-rpc: Fix undefined `this` in `decodeBroadcastTxAsync` and
`broadcastTxAsync` ([#937]).
[#937]: https://github.com/cosmos/cosmjs/pull/937
## [0.26.4] - 2021-10-28
### Fixed

View File

@ -121,8 +121,8 @@ function decodeEvent(event: RpcEvent): responses.Event {
};
}
function decodeEvents(events: readonly RpcEvent[]): readonly responses.Event[] {
return assertArray(events).map(decodeEvent);
function decodeEvents(events: readonly RpcEvent[] | undefined): readonly responses.Event[] {
return assertArray(events ?? []).map(decodeEvent);
}
interface RpcTxData {
@ -130,7 +130,7 @@ interface RpcTxData {
readonly log?: string;
/** base64 encoded */
readonly data?: string;
readonly events: readonly RpcEvent[];
readonly events?: readonly RpcEvent[];
}
function decodeTxData(data: RpcTxData): responses.TxData {
@ -787,7 +787,7 @@ export class Responses {
}
public static decodeBroadcastTxAsync(response: JsonRpcSuccessResponse): responses.BroadcastTxAsyncResponse {
return this.decodeBroadcastTxSync(response);
return Responses.decodeBroadcastTxSync(response);
}
public static decodeBroadcastTxCommit(

View File

@ -47,6 +47,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
client.disconnect();
});
describe("broadcastTxCommit", () => {
it("can broadcast a transaction", async () => {
pendingWithoutTendermint();
const client = await Tendermint33Client.create(rpcFactory());
@ -64,6 +65,36 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
client.disconnect();
});
});
describe("broadcastTxSync", () => {
it("can broadcast a transaction", async () => {
pendingWithoutTendermint();
const client = await Tendermint33Client.create(rpcFactory());
const tx = buildKvTx(randomString(), randomString());
const response = await client.broadcastTxSync({ tx: tx });
expect(response.hash.length).toEqual(32);
// verify success
expect(response.code).toBeFalsy();
client.disconnect();
});
});
describe("broadcastTxAsync", () => {
it("can broadcast a transaction", async () => {
pendingWithoutTendermint();
const client = await Tendermint33Client.create(rpcFactory());
const tx = buildKvTx(randomString(), randomString());
const response = await client.broadcastTxAsync({ tx: tx });
// TODO: Remove any cast after https://github.com/cosmos/cosmjs/issues/938
expect((response as any).hash.length).toEqual(32);
client.disconnect();
});
});
it("gets the same tx hash from backend as calculated locally", async () => {
pendingWithoutTendermint();

View File

@ -829,7 +829,7 @@ export class Responses {
}
public static decodeBroadcastTxAsync(response: JsonRpcSuccessResponse): responses.BroadcastTxAsyncResponse {
return this.decodeBroadcastTxSync(response);
return Responses.decodeBroadcastTxSync(response);
}
public static decodeBroadcastTxCommit(

View File

@ -47,6 +47,7 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
client.disconnect();
});
describe("broadcastTxCommit", () => {
it("can broadcast a transaction", async () => {
pendingWithoutTendermint();
const client = await Tendermint34Client.create(rpcFactory());
@ -64,6 +65,37 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
client.disconnect();
});
});
describe("broadcastTxSync", () => {
it("can broadcast a transaction", async () => {
pendingWithoutTendermint();
const client = await Tendermint34Client.create(rpcFactory());
const tx = buildKvTx(randomString(), randomString());
const response = await client.broadcastTxSync({ tx: tx });
expect(response.hash.length).toEqual(32);
// verify success
expect(response.code).toBeFalsy();
expect(response.codeSpace).toBeFalsy();
client.disconnect();
});
});
describe("broadcastTxAsync", () => {
it("can broadcast a transaction", async () => {
pendingWithoutTendermint();
const client = await Tendermint34Client.create(rpcFactory());
const tx = buildKvTx(randomString(), randomString());
const response = await client.broadcastTxAsync({ tx: tx });
// TODO: Remove any cast after https://github.com/cosmos/cosmjs/issues/938
expect((response as any).hash.length).toEqual(32);
client.disconnect();
});
});
it("gets the same tx hash from backend as calculated locally", async () => {
pendingWithoutTendermint();