Merge pull request #763 from cosmos/675-gas-used

Add gasUsed to Stargate broadcast responses
This commit is contained in:
Will Clark 2021-04-14 15:35:24 +02:00 committed by GitHub
commit 2f4b39f926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 10 deletions

View File

@ -65,6 +65,10 @@ and this project adheres to
- @cosmjs/tendermint-rpc: Add `pubkeyToAddress`, `pubkeyToRawAddress`,
`rawEd25519PubkeyToRawAddress`, and `rawSecp256k1PubkeyToRawAddress` helper
functions.
- @cosmjs/stargate: `StargateClient.broadcastTx` and `.getTx` results now
include `gasUsed` and `gasWanted` properties.
- @cosmjs/cosmwasm-stargate: `CosmWasmClient.broadcastTx` and `.getTx` results
now include `gasUsed` and `gasWanted` properties.
### Changed
@ -119,6 +123,8 @@ and this project adheres to
- @cosmjs/cosmwasm-stargate: `CosmWasmClient.broadcastTx` now uses sync mode and
then polls for the transaction before resolving. The timeout and poll interval
can be configured.
- @cosmjs/tendermint-rpc: Tendermint v34 `TxData` type now includes `codeSpace`,
`gasWanted`, and `gasUsed` properties.
### Deprecated

View File

@ -224,6 +224,8 @@ export class CosmWasmClient {
height: result.height,
rawLog: result.rawLog,
transactionHash: txId,
gasUsed: result.gasUsed,
gasWanted: result.gasWanted,
}
: pollForTx(txId);
};
@ -382,6 +384,8 @@ export class CosmWasmClient {
code: tx.result.code,
rawLog: tx.result.log || "",
tx: tx.tx,
gasUsed: tx.result.gasUsed,
gasWanted: tx.result.gasWanted,
};
});
}

View File

@ -332,7 +332,8 @@ describe("StargateClient", () => {
const txResult = await client.broadcastTx(txRawBytes);
assertIsBroadcastTxSuccess(txResult);
const { rawLog, transactionHash } = txResult;
const { gasUsed, rawLog, transactionHash } = txResult;
expect(gasUsed).toBeGreaterThan(0);
expect(rawLog).toMatch(/{"key":"amount","value":"1234567ucosm"}/);
expect(transactionHash).toMatch(/^[0-9A-F]{64}$/);

View File

@ -1,11 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { toHex } from "@cosmjs/encoding";
import { Uint53 } from "@cosmjs/math";
import {
BroadcastTxSyncResponse,
Tendermint34Client,
toRfc3339WithNanoseconds,
} from "@cosmjs/tendermint-rpc";
import { Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc";
import { sleep } from "@cosmjs/utils";
import { Account, accountFromAny } from "./accounts";
@ -63,6 +59,8 @@ export interface IndexedTx {
readonly code: number;
readonly rawLog: string;
readonly tx: Uint8Array;
readonly gasUsed: number;
readonly gasWanted: number;
}
export interface SequenceResponse {
@ -83,6 +81,8 @@ export interface BroadcastTxSuccess {
readonly transactionHash: string;
readonly rawLog?: string;
readonly data?: readonly MsgData[];
readonly gasUsed: number;
readonly gasWanted: number;
}
export type BroadcastTxResponse = BroadcastTxSuccess | BroadcastTxFailure;
@ -303,6 +303,8 @@ export class StargateClient {
height: result.height,
rawLog: result.rawLog,
transactionHash: txId,
gasUsed: result.gasUsed,
gasWanted: result.gasWanted,
}
: pollForTx(txId);
};
@ -310,7 +312,7 @@ export class StargateClient {
return new Promise((resolve, reject) =>
this.forceGetTmClient()
.broadcastTxSync({ tx })
.then(({ hash }: BroadcastTxSyncResponse) => pollForTx(toHex(hash).toUpperCase()))
.then(({ hash }) => pollForTx(toHex(hash).toUpperCase()))
.then(resolve, reject)
.finally(() => clearTimeout(txPollTimeout)),
);
@ -325,6 +327,8 @@ export class StargateClient {
code: tx.result.code,
rawLog: tx.result.log || "",
tx: tx.tx,
gasUsed: tx.result.gasUsed,
gasWanted: tx.result.gasWanted,
};
});
}

View File

@ -126,19 +126,25 @@ function decodeEvents(events: readonly RpcEvent[]): readonly responses.Event[] {
}
interface RpcTxData {
readonly codespace?: string;
readonly code?: number;
readonly log?: string;
/** base64 encoded */
readonly data?: string;
readonly events?: readonly RpcEvent[];
readonly gas_wanted?: string;
readonly gas_used?: string;
}
function decodeTxData(data: RpcTxData): responses.TxData {
return {
data: may(fromBase64, data.data),
log: data.log,
code: Integer.parse(assertNumber(optional<number>(data.code, 0))),
codeSpace: data.codespace,
log: data.log,
data: may(fromBase64, data.data),
events: data.events ? decodeEvents(data.events) : [],
gasWanted: Integer.parse(optional<string>(data.gas_wanted, "0")),
gasUsed: Integer.parse(optional<string>(data.gas_used, "0")),
};
}

View File

@ -176,10 +176,12 @@ export interface Event {
export interface TxData {
readonly code: number;
readonly codeSpace?: string;
readonly log?: string;
readonly data?: Uint8Array;
readonly events: readonly Event[];
// readonly fees?: any;
readonly gasWanted: number;
readonly gasUsed: number;
}
export interface TxProof {