From 23c3706ff0d31beac73892525c14ca78aabc59e5 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 22 Jul 2021 14:01:00 +0200 Subject: [PATCH] Add CosmWasm CLI example --- .circleci/config.yml | 1 + packages/cli/examples/cosmwasm.ts | 66 +++++++++++++++++++++++++++++++ packages/cli/run_examples.sh | 3 ++ 3 files changed, 70 insertions(+) create mode 100755 packages/cli/examples/cosmwasm.ts diff --git a/.circleci/config.yml b/.circleci/config.yml index d0b0ff1909..d8f5c15855 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -159,6 +159,7 @@ jobs: environment: SKIP_BUILD: 1 command: | + yarn node ./bin/cosmwasm-cli --init examples/cosmwasm.ts --code "process.exit(0)" yarn node ./bin/cosmwasm-cli --init examples/coralnet.ts --code "process.exit(0)" yarn node ./bin/cosmwasm-cli --init examples/delegate.ts --code "process.exit(0)" yarn node ./bin/cosmwasm-cli --init examples/faucet_addresses.ts --code "process.exit(0)" diff --git a/packages/cli/examples/cosmwasm.ts b/packages/cli/examples/cosmwasm.ts new file mode 100755 index 0000000000..05549a6f9b --- /dev/null +++ b/packages/cli/examples/cosmwasm.ts @@ -0,0 +1,66 @@ +import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"; +import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; +import { calculateFee, GasPrice } from "@cosmjs/stargate"; +import * as fs from "fs"; + +const rpcEndpoint = "http://localhost:26659"; + +// Example user from scripts/wasmd/README.md +const alice = { + mnemonic: "enlist hip relief stomach skate base shallow young switch frequent cry park", + address0: "wasm14qemq0vw6y3gc3u3e0aty2e764u4gs5lndxgyk", + address1: "wasm1hhg2rlu9jscacku2wwckws7932qqqu8xm5ca8y", + address2: "wasm1xv9tklw7d82sezh9haa573wufgy59vmwnxhnsl", + address3: "wasm17yg9mssjenmc3jkqth6ulcwj9cxujrxxg9nmzk", + address4: "wasm1f7j7ryulwjfe9ljplvhtcaxa6wqgula3nh873j", +}; + +async function main(hackatomWasmPath: string) { + const gasPrice = GasPrice.fromString("0.025ucosm"); + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: "wasm" }); + const client = await SigningCosmWasmClient.connectWithSigner(rpcEndpoint, wallet); + + // Upload contract + const wasm = fs.readFileSync(hackatomWasmPath); + const uploadFee = calculateFee(1_500_000, gasPrice); + const codeMeta = { + source: "https://crates.io/api/v1/crates/hackatom/not-yet-released/download", + builder: "cosmwasm/rust-optimizer:0.10.8", + }; + const uploadReceipt = await client.upload( + alice.address0, + wasm, + uploadFee, + codeMeta, + "Upload hackatom contract", + ); + console.info("Upload succeeded. Receipt:", uploadReceipt); + + // Instantiate + const instantiateFee = calculateFee(500_000, gasPrice); + // This contract specific message is passed to the contract + const msg = { + beneficiary: alice.address1, + verifier: alice.address0, + }; + const { contractAddress } = await client.instantiate( + alice.address0, + uploadReceipt.codeId, + msg, + "My instance", + instantiateFee, + { memo: `Create a hackatom instance` }, + ); + console.info(`Contract instantiated at: `, contractAddress); + + // Execute contract + const executeFee = calculateFee(300_000, gasPrice); + const result = await client.execute(alice.address0, contractAddress, { release: {} }, executeFee); + const wasmEvent = result.logs[0].events.find((e) => e.type === "wasm"); + console.info("The `wasm` event emitted by the contract execution:", wasmEvent); +} + +const repoRoot = process.cwd() + "/../.."; // This assumes you are in `packages/cli` +const hackatom = `${repoRoot}/scripts/wasmd/contracts/hackatom.wasm`; +await main(hackatom); +console.info("The show is over."); diff --git a/packages/cli/run_examples.sh b/packages/cli/run_examples.sh index 4216086d6b..489d53405b 100755 --- a/packages/cli/run_examples.sh +++ b/packages/cli/run_examples.sh @@ -2,6 +2,9 @@ set -o errexit -o nounset -o pipefail command -v shellcheck >/dev/null && shellcheck "$0" +if [ -n "${SIMAPP_ENABLED:-}" ]; then + yarn node ./bin/cosmwasm-cli --init examples/cosmwasm.ts --code "process.exit(0)" +fi yarn node ./bin/cosmwasm-cli --init examples/coralnet.ts --code "process.exit(0)" if [ -n "${LAUNCHPAD_ENABLED:-}" ]; then yarn node ./bin/cosmwasm-cli --init examples/delegate.ts --code "process.exit(0)"