mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 13:47:12 +00:00
Add CosmWasm CLI example
This commit is contained in:
parent
a6b8e35b84
commit
23c3706ff0
@ -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)"
|
||||
|
66
packages/cli/examples/cosmwasm.ts
Executable file
66
packages/cli/examples/cosmwasm.ts
Executable file
@ -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.");
|
@ -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)"
|
||||
|
Loading…
x
Reference in New Issue
Block a user