diff --git a/.circleci/config.yml b/.circleci/config.yml index 25d5c737..63d92452 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -305,14 +305,7 @@ jobs: working_directory: packages/cli environment: SKIP_BUILD: 1 - command: | - ./bin/cosmwasm-cli --init examples/coralnet.ts --code "process.exit(0)" - ./bin/cosmwasm-cli --init examples/delegate.ts --code "process.exit(0)" - ./bin/cosmwasm-cli --init examples/faucet_addresses.ts --code "process.exit(0)" - ./bin/cosmwasm-cli --init examples/generate_address.ts --code "process.exit(0)" - ./bin/cosmwasm-cli --init examples/helpers.ts --code "process.exit(0)" - ./bin/cosmwasm-cli --init examples/local_faucet.ts --code "process.exit(0)" - ./bin/cosmwasm-cli --init examples/mask.ts --code "process.exit(0)" + command: ./run_examples.sh - run: name: Stop chains command: | diff --git a/packages/cli/examples/coralnet.ts b/packages/cli/examples/coralnet.ts index c8488040..f1fc9cc2 100644 --- a/packages/cli/examples/coralnet.ts +++ b/packages/cli/examples/coralnet.ts @@ -1,3 +1,7 @@ +import { HdPath } from "@cosmjs/crypto"; +import { CosmWasmFeeTable, SigningCosmWasmClient } from "@cosmjs/cosmwasm-launchpad"; +import { GasPrice, GasLimits, makeCosmoshubPath, Secp256k1HdWallet } from "@cosmjs/launchpad"; + interface Options { readonly httpUrl: string; readonly bech32prefix: string; diff --git a/packages/cli/examples/delegate.ts b/packages/cli/examples/delegate.ts index f99da7e7..f84314bf 100644 --- a/packages/cli/examples/delegate.ts +++ b/packages/cli/examples/delegate.ts @@ -1,3 +1,5 @@ +import { coin, coins, makeSignDoc, makeStdTx, CosmosClient, MsgDelegate, Secp256k1HdWallet } from "@cosmjs/launchpad"; + const wallet = await Secp256k1HdWallet.fromMnemonic( "enlist hip relief stomach skate base shallow young switch frequent cry park", ); diff --git a/packages/cli/examples/faucet_addresses.ts b/packages/cli/examples/faucet_addresses.ts index 604484e9..78128815 100644 --- a/packages/cli/examples/faucet_addresses.ts +++ b/packages/cli/examples/faucet_addresses.ts @@ -1,3 +1,6 @@ +import { toBase64 } from "@cosmjs/encoding"; +import { makeCosmoshubPath, Secp256k1HdWallet } from "@cosmjs/launchpad"; + const mnemonic = "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; diff --git a/packages/cli/examples/generate_address.ts b/packages/cli/examples/generate_address.ts index bb0787b6..21914d2e 100644 --- a/packages/cli/examples/generate_address.ts +++ b/packages/cli/examples/generate_address.ts @@ -1,3 +1,6 @@ +import { Bip39, Random } from "@cosmjs/crypto"; +import { encodeSecp256k1Pubkey, Secp256k1HdWallet } from "@cosmjs/launchpad"; + const mnemonic = Bip39.encode(Random.getBytes(16)).toString(); const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic); const [{ address, pubkey }] = await wallet.getAccounts(); diff --git a/packages/cli/examples/helpers.ts b/packages/cli/examples/helpers.ts index 9187b353..8b229e7f 100644 --- a/packages/cli/examples/helpers.ts +++ b/packages/cli/examples/helpers.ts @@ -1,3 +1,10 @@ +import axios from "axios"; +import * as fs from "fs"; + +import { Bip39, Random } from "@cosmjs/crypto"; +import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-launchpad"; +import { logs, GasPrice, Secp256k1HdWallet } from "@cosmjs/launchpad"; + interface Options { httpUrl: string; networkId: string; diff --git a/packages/cli/examples/local_faucet.ts b/packages/cli/examples/local_faucet.ts index 98f27217..afa4d960 100644 --- a/packages/cli/examples/local_faucet.ts +++ b/packages/cli/examples/local_faucet.ts @@ -1,3 +1,5 @@ +import { LcdClient, Secp256k1HdWallet, StdFee } from "@cosmjs/launchpad"; + const defaultHttpUrl = "http://localhost:1317"; const defaultNetworkId = "testing"; const defaultFee: StdFee = { diff --git a/packages/cli/examples/mask.ts b/packages/cli/examples/mask.ts index 7a36b0ba..caf9cf9f 100644 --- a/packages/cli/examples/mask.ts +++ b/packages/cli/examples/mask.ts @@ -1,3 +1,6 @@ +import { toBase64, toUtf8 } from "@cosmjs/encoding"; +import { Coin } from "@cosmjs/launchpad"; + // types auto-generated by wasm.glass and cleaned up manually export type HandleMsg = | { diff --git a/packages/cli/examples/stargate.ts b/packages/cli/examples/stargate.ts new file mode 100644 index 00000000..f4577461 --- /dev/null +++ b/packages/cli/examples/stargate.ts @@ -0,0 +1,23 @@ +import { makeCosmoshubPath, DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; +import { assertIsBroadcastTxSuccess, SigningStargateClient } from "@cosmjs/stargate"; + +const mnemonic = "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; +const path = makeCosmoshubPath(3); +const prefix = "cosmos"; +const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, path, prefix); +const [firstAccount] = await wallet.getAccounts(); +console.log("Signer address:", firstAccount.address); + +const rpcEndpoint = "ws://localhost:26658"; +const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet); + +const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; +const amount = { + denom: "ucosm", + amount: "1234567", +}; +const result = await client.sendTokens(firstAccount.address, recipient, [amount], "Have fun with your star coins"); +assertIsBroadcastTxSuccess(result); +console.log("Successfully broadcasted:", result); + +client.disconnect(); diff --git a/packages/cli/package.json b/packages/cli/package.json index cc471cab..a972eb46 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -40,11 +40,15 @@ ], "dependencies": { "@cosmjs/cosmwasm-launchpad": "^0.24.0-alpha.24", + "@cosmjs/cosmwasm-stargate": "^0.24.0-alpha.24", "@cosmjs/crypto": "^0.24.0-alpha.24", "@cosmjs/encoding": "^0.24.0-alpha.24", "@cosmjs/faucet-client": "^0.24.0-alpha.24", "@cosmjs/launchpad": "^0.24.0-alpha.24", "@cosmjs/math": "^0.24.0-alpha.24", + "@cosmjs/proto-signing": "^0.24.0-alpha.24", + "@cosmjs/stargate": "^0.24.0-alpha.24", + "@cosmjs/tendermint-rpc": "^0.24.0-alpha.24", "@cosmjs/utils": "^0.24.0-alpha.24", "axios": "^0.21.1", "babylon": "^6.18.0", diff --git a/packages/cli/run_examples.sh b/packages/cli/run_examples.sh new file mode 100755 index 00000000..fde36611 --- /dev/null +++ b/packages/cli/run_examples.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -o errexit -o nounset -o pipefail +command -v shellcheck >/dev/null && shellcheck "$0" + +PATH="$(realpath ./bin):$PATH" + +cosmwasm-cli --init examples/coralnet.ts --code "process.exit(0)" +if [ -n "${LAUNCHPAD_ENABLED:-}" ]; then + cosmwasm-cli --init examples/delegate.ts --code "process.exit(0)" +fi +cosmwasm-cli --init examples/faucet_addresses.ts --code "process.exit(0)" +cosmwasm-cli --init examples/generate_address.ts --code "process.exit(0)" +cosmwasm-cli --init examples/helpers.ts --code "process.exit(0)" +cosmwasm-cli --init examples/local_faucet.ts --code "process.exit(0)" +cosmwasm-cli --init examples/mask.ts --code "process.exit(0)" +if [ -n "${SIMAPP_ENABLED:-}" ]; then + cosmwasm-cli --init examples/stargate.ts --code "process.exit(0)" +fi diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index efbf55d1..97d0c36d 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -7,6 +7,20 @@ import { TsRepl } from "./tsrepl"; import colors = require("colors/safe"); +export async function installedPackages(): Promise { + return new Promise((resolve, reject) => { + fs.readFile(__dirname + "/../package.json", { encoding: "utf8" }, (error, data) => { + if (error) { + reject(error); + } else { + const packagejson = JSON.parse(data); + const deps = Object.keys(packagejson.dependencies).sort(); + resolve(deps); + } + }); + }); +} + export async function main(originalArgs: readonly string[]): Promise { const args = yargs .options({ @@ -35,117 +49,47 @@ export async function main(originalArgs: readonly string[]): Promise { .group(["debug", "selftest"], "Maintainer options") .parse(originalArgs); - const imports = new Map([ - [ - "@cosmjs/cosmwasm-launchpad", - [ - // cosmwasmclient - "Account", - "Block", - "BlockHeader", - "Code", - "CodeDetails", - "Contract", - "ContractCodeHistoryEntry", - "CosmWasmClient", - "GetSequenceResult", - "SearchByHeightQuery", - "SearchBySentFromOrToQuery", - "SearchByTagsQuery", - "SearchTxQuery", - "SearchTxFilter", - // signingcosmwasmclient - "ExecuteResult", - "CosmWasmFeeTable", - "InstantiateResult", - "SigningCosmWasmClient", - "UploadMeta", - "UploadResult", - ], - ], - [ - "@cosmjs/crypto", - [ - "Bip39", - "Ed25519", - "Ed25519Keypair", - "EnglishMnemonic", - "HdPath", - "Random", - "Secp256k1", - "Sha256", - "sha256", - "Sha512", - "sha512", - "Slip10", - "Slip10Curve", - "Slip10RawIndex", - ], - ], - [ - "@cosmjs/encoding", - ["fromAscii", "fromBase64", "fromHex", "fromUtf8", "toAscii", "toBase64", "toHex", "toUtf8", "Bech32"], - ], - ["@cosmjs/faucet-client", ["FaucetClient"]], - [ - "@cosmjs/launchpad", - [ - "coin", - "coins", - "decodeAminoPubkey", - "decodeBech32Pubkey", - "encodeAminoPubkey", - "encodeBech32Pubkey", - "encodeSecp256k1Pubkey", - "encodeSecp256k1Signature", - "logs", - "makeCosmoshubPath", - "makeSignDoc", - "makeStdTx", - "IndexedTx", - "BroadcastTxResult", - "Coin", - "CosmosClient", - "GasLimits", - "GasPrice", - "Msg", - "MsgDelegate", - "MsgSend", - "LcdClient", - "OfflineSigner", - "PubKey", - "pubkeyToAddress", - "Secp256k1HdWallet", - "Secp256k1Wallet", - "SigningCosmosClient", - "StdFee", - "StdSignDoc", - "StdTx", - ], - ], - ["@cosmjs/math", ["Decimal", "Int53", "Uint32", "Uint53", "Uint64"]], - ["@cosmjs/utils", ["assert", "arrayContentEquals", "sleep"]], - ]); - console.info(colors.green("Initializing session for you. Have fun!")); - console.info(colors.yellow("Available imports:")); - console.info(colors.yellow(" * axios")); - console.info(colors.yellow(" * fs")); - for (const [moduleName, symbols] of imports.entries()) { - console.info(colors.yellow(` * from ${moduleName}: ${symbols.join(", ")}`)); - } - - let init = ` - import axios from "axios"; - import * as fs from "fs"; - `; - for (const [moduleName, symbols] of imports.entries()) { - init += `import { ${symbols.join(", ")} } from "${moduleName}";\n`; - } + const visiblePackages = (await installedPackages()).filter( + (name) => name.startsWith("@cosmjs/") || name === "axios", + ); + console.info(colors.yellow("The following packages have been installed and can be imported:")); + console.info(colors.yellow(visiblePackages.join(", "))); + let init = ""; if (args.selftest) { // execute some trival stuff and exit init += ` + import axios from "axios"; + import * as fs from "fs"; + + import { + fromAscii, + fromBase64, + fromHex, + fromUtf8, + toAscii, + toBase64, + toHex, + toUtf8, + Bech32, + } from "@cosmjs/encoding"; + import { sha512, Bip39, Random } from "@cosmjs/crypto"; + import { + coins, + encodeAminoPubkey, + encodeBech32Pubkey, + decodeBech32Pubkey, + decodeAminoPubkey, + makeCosmoshubPath, + makeSignDoc, + Secp256k1HdWallet, + Secp256k1Wallet, + StdFee, + } from "@cosmjs/launchpad"; + import { Decimal } from "@cosmjs/math"; + import { assert, arrayContentEquals, sleep } from "@cosmjs/utils"; + await sleep(123); const readmeContent = fs.readFileSync(process.cwd() + "/README.md");