Skip to content

Commit

Permalink
Merge pull request #1376 from cosmos/tendermint-0.37
Browse files Browse the repository at this point in the history
Add a Tendermint 0.37 client
  • Loading branch information
webmaster128 committed Mar 6, 2023
2 parents 002bee6 + d107fde commit 2c1f779
Show file tree
Hide file tree
Showing 31 changed files with 444 additions and 210 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to
- @cosmjs/proto-signing: Remove `fromJSON`/`toJSON` from `TsProtoGeneratedType`
such that generated types are not required to generate those anymore. The
methods were provided by ts-proto but we never needed them. ([#1329])
- @cosmjs/stargate: Rename `fromTendermint34Event` to `fromTendermintEvent` and
let it support both Tendermint 0.34 and 0.37 events as input.

[#1002]: https://github.com/cosmos/cosmjs/issues/1002
[#1240]: https://github.com/cosmos/cosmjs/pull/1240
Expand All @@ -29,10 +31,23 @@ and this project adheres to
[#1329]: https://github.com/cosmos/cosmjs/pull/1329

### Added

- @cosmjs/stargate: Add `granteeGrants` and `granterGrants` queries to
`AuthzExtension` ([#1308]).
- @cosmjs/tendermint-rpc: Add new `Tendermint37Client` and remove unused
`Tendermint35Client`; Add `TendermintClient` as a union type for
`Tendermint34Client` or `Tendermint37Client` and
`isTendermint34Client`/`isTendermint37Client` to get the specific type
([#1376]).
- @cosmjs/stargate: Add constructors `StargateClient.create` and
`SigningStargateClient.createWithSigner` to construct with a given Tendermint
client ([#1376]).
- @cosmjs/cosmwasm-stargate: Add constructors `CosmWasmClient.create` and
`SigningCosmWasmClient.createWithSigner` to construct with a given Tendermint
client ([#1376]).

[#1308]: https://github.com/cosmos/cosmjs/pull/1308
[#1376]: https://github.com/cosmos/cosmjs/pull/1376

## [0.29.5] - 2022-12-07

Expand Down
40 changes: 40 additions & 0 deletions packages/cli/examples/tendermint0.37.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { coins, makeCosmoshubPath } from "@cosmjs/amino";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { assertIsDeliverTxSuccess, calculateFee, GasPrice, SigningStargateClient } from "@cosmjs/stargate";
import { Tendermint37Client } from "@cosmjs/tendermint-rpc";

// Network config
const prefix = "wasm";
const rpcEndpoint = "http://146.190.50.102:26657"; // or 137.184.83.82:26657
const gasPrice = GasPrice.fromString("0.001stake");

// Wallet wasm16jd84xm6yerfaafvtp7s6tpetdqkpu6wxumszp
const mnemonic = "royal next favorite duck plastic august rent knee strong weather father opinion";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: prefix });
const [account] = await wallet.getAccounts();
console.log("Signer address:", account.address);

// Setup client. In contrast to most other examples out there, we create the Tendermint client
// explicitly. Otherwise the 0.34 client will be used.
const tmClient = await Tendermint37Client.connect(rpcEndpoint);
const version = (await tmClient.status()).nodeInfo.version;
console.log("Tendermint version:", version);
const client = await SigningStargateClient.createWithSigner(tmClient, wallet, { gasPrice: gasPrice });

// Get my balance
const balance = await client.getAllBalances(account.address);
console.log("Balance:", balance);

// Send a transaction
const recipient = "wasm142u9fgcjdlycfcez3lw8x6x5h7rfjlnfaallkd";
const result = await client.sendTokens(
account.address,
recipient,
coins(1, "stake"),
1.5, // In the current testnet the default multiplier of 1.3 is not sufficient 🤷‍♂️
"Have fun with this gift",
);
assertIsDeliverTxSuccess(result);
console.log("Successfully broadcasted:", result);

client.disconnect();
35 changes: 27 additions & 8 deletions packages/cosmwasm-stargate/src/cosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
BroadcastTxError,
Coin,
DeliverTxResponse,
fromTendermint34Event,
fromTendermintEvent,
IndexedTx,
isSearchByHeightQuery,
isSearchBySentFromOrToQuery,
Expand All @@ -25,7 +25,12 @@ import {
TimeoutError,
TxExtension,
} from "@cosmjs/stargate";
import { HttpEndpoint, Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc";
import {
HttpEndpoint,
Tendermint34Client,
TendermintClient,
toRfc3339WithNanoseconds,
} from "@cosmjs/tendermint-rpc";
import { assert, sleep } from "@cosmjs/utils";
import {
CodeInfoResponse,
Expand Down Expand Up @@ -77,26 +82,40 @@ export interface ContractCodeHistoryEntry {

/** Use for testing only */
export interface PrivateCosmWasmClient {
readonly tmClient: Tendermint34Client | undefined;
readonly tmClient: TendermintClient | undefined;
readonly queryClient:
| (QueryClient & AuthExtension & BankExtension & TxExtension & WasmExtension)
| undefined;
}

export class CosmWasmClient {
private readonly tmClient: Tendermint34Client | undefined;
private readonly tmClient: TendermintClient | undefined;
private readonly queryClient:
| (QueryClient & AuthExtension & BankExtension & TxExtension & WasmExtension)
| undefined;
private readonly codesCache = new Map<number, CodeDetails>();
private chainId: string | undefined;

/**
* Creates an instance by connecting to the given Tendermint RPC endpoint.
*
* For now this uses the Tendermint 0.34 client. If you need Tendermint 0.37
* support, see `create`.
*/
public static async connect(endpoint: string | HttpEndpoint): Promise<CosmWasmClient> {
const tmClient = await Tendermint34Client.connect(endpoint);
return CosmWasmClient.create(tmClient);
}

/**
* Creates an instance from a manually created Tendermint client.
* Use this to use `Tendermint37Client` instead of `Tendermint34Client`.
*/
public static async create(tmClient: TendermintClient): Promise<CosmWasmClient> {
return new CosmWasmClient(tmClient);
}

protected constructor(tmClient: Tendermint34Client | undefined) {
protected constructor(tmClient: TendermintClient | undefined) {
if (tmClient) {
this.tmClient = tmClient;
this.queryClient = QueryClient.withExtensions(
Expand All @@ -109,11 +128,11 @@ export class CosmWasmClient {
}
}

protected getTmClient(): Tendermint34Client | undefined {
protected getTmClient(): TendermintClient | undefined {
return this.tmClient;
}

protected forceGetTmClient(): Tendermint34Client {
protected forceGetTmClient(): TendermintClient {
if (!this.tmClient) {
throw new Error(
"Tendermint client not available. You cannot use online functionality in offline mode.",
Expand Down Expand Up @@ -464,7 +483,7 @@ export class CosmWasmClient {
height: tx.height,
hash: toHex(tx.hash).toUpperCase(),
code: tx.result.code,
events: tx.result.events.map(fromTendermint34Event),
events: tx.result.events.map(fromTendermintEvent),
rawLog: tx.result.log || "",
tx: tx.tx,
gasUsed: tx.result.gasUsed,
Expand Down
22 changes: 20 additions & 2 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
SignerData,
StdFee,
} from "@cosmjs/stargate";
import { HttpEndpoint, Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { HttpEndpoint, Tendermint34Client, TendermintClient } from "@cosmjs/tendermint-rpc";
import { assert, assertDefined } from "@cosmjs/utils";
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { MsgDelegate, MsgUndelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx";
Expand Down Expand Up @@ -183,12 +183,30 @@ export class SigningCosmWasmClient extends CosmWasmClient {
private readonly aminoTypes: AminoTypes;
private readonly gasPrice: GasPrice | undefined;

/**
* Creates an instance by connecting to the given Tendermint RPC endpoint.
*
* For now this uses the Tendermint 0.34 client. If you need Tendermint 0.37
* support, see `createWithSigner`.
*/
public static async connectWithSigner(
endpoint: string | HttpEndpoint,
signer: OfflineSigner,
options: SigningCosmWasmClientOptions = {},
): Promise<SigningCosmWasmClient> {
const tmClient = await Tendermint34Client.connect(endpoint);
return SigningCosmWasmClient.createWithSigner(tmClient, signer, options);
}

/**
* Creates an instance from a manually created Tendermint client.
* Use this to use `Tendermint37Client` instead of `Tendermint34Client`.
*/
public static async createWithSigner(
tmClient: TendermintClient,
signer: OfflineSigner,
options: SigningCosmWasmClientOptions = {},
): Promise<SigningCosmWasmClient> {
return new SigningCosmWasmClient(tmClient, signer, options);
}

Expand All @@ -209,7 +227,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
}

protected constructor(
tmClient: Tendermint34Client | undefined,
tmClient: TendermintClient | undefined,
signer: OfflineSigner,
options: SigningCosmWasmClientOptions,
) {
Expand Down
10 changes: 5 additions & 5 deletions packages/stargate/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fromUtf8 } from "@cosmjs/encoding";
import { tendermint34 } from "@cosmjs/tendermint-rpc";
import { tendermint34, tendermint37 } from "@cosmjs/tendermint-rpc";

/**
* An event attribute.
Expand Down Expand Up @@ -30,16 +30,16 @@ export interface Event {
}

/**
* Takes a Tendemrint 0.34 event with binary encoded key and value
* Takes a Tendermint 0.34 or 0.37 event with binary encoded key and value
* and converts it into an `Event` with string attributes.
*/
export function fromTendermint34Event(event: tendermint34.Event): Event {
export function fromTendermintEvent(event: tendermint34.Event | tendermint37.Event): Event {
return {
type: event.type,
attributes: event.attributes.map(
(attr): Attribute => ({
key: fromUtf8(attr.key, true),
value: fromUtf8(attr.value, true),
key: typeof attr.key == "string" ? attr.key : fromUtf8(attr.key, true),
value: typeof attr.value == "string" ? attr.value : fromUtf8(attr.value, true),
}),
),
};
Expand Down
2 changes: 1 addition & 1 deletion packages/stargate/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { Account, accountFromAny, AccountParser } from "./accounts";
export { AminoConverter, AminoConverters, AminoTypes } from "./aminotypes";
export { Attribute, Event, fromTendermint34Event } from "./events";
export { Attribute, Event, fromTendermintEvent } from "./events";
export { calculateFee, GasPrice } from "./fee";
export * as logs from "./logs";
export {
Expand Down
Loading

0 comments on commit 2c1f779

Please sign in to comment.