From bc87f8e87adc070c77855421e7970215c19919d7 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 6 Feb 2020 17:46:40 +0100 Subject: [PATCH] Add some code snippets --- .eslintignore | 1 + packages/cli/README.md | 38 +++++++++++++++++++++++++-- packages/cli/examples/local_faucet.ts | 21 +++++++++++++++ packages/cli/package.json | 1 + packages/cli/src/cli.ts | 34 +++++++++++++++++++++++- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 packages/cli/examples/local_faucet.ts diff --git a/.eslintignore b/.eslintignore index f373a53f..6942c0df 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,5 +4,6 @@ build/ custom_types/ dist/ docs/ +examples/ generated/ types/ diff --git a/packages/cli/README.md b/packages/cli/README.md index 474ba32c..40da3cca 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -39,8 +39,42 @@ $ cosmwasm-cli ## Getting started 1. Install `@cosmwasm/cli` and run `cosmwasm-cli` as shown above -2. TODO: write README inspired by - https://github.com/iov-one/iov-core/blob/master/packages/iov-cli/README.md +2. Start a local wasmd blockchain +3. Start with `./bin/cosmwasm-cli --init examples/local_faucet.ts` +4. Play around as in the following example code + +```ts +// Get account information +const account = (await client.authAccounts(faucetAddress)).result.value; + +// Craft a send transaction +const emptyAddress = Bech32.encode("cosmos", Random.getBytes(20)); +const memo = "My first contract on chain"; +const sendTokensMsg: types.MsgSend = { + type: "cosmos-sdk/MsgSend", + value: { + from_address: faucetAddress, + to_address: emptyAddress, + amount: [ + { + denom: "ucosm", + amount: "1234567", + }, + ], + }, +}; + +const signBytes = makeSignBytes([sendTokensMsg], defaultFee, defaultNetworkId, memo, account) as SignableBytes; +const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256); +const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature); +const signedTx: types.StdTx = { + msg: [sendTokensMsg], + fee: defaultFee, + memo: memo, + signatures: [signature], + } +const postResult = await client.postTx(marshalTx(signedTx)); +``` ## License diff --git a/packages/cli/examples/local_faucet.ts b/packages/cli/examples/local_faucet.ts new file mode 100644 index 00000000..2b6c6522 --- /dev/null +++ b/packages/cli/examples/local_faucet.ts @@ -0,0 +1,21 @@ +const defaultHttpUrl = "http://localhost:1317"; +const defaultNetworkId = "testing"; +const defaultFee: types.StdFee = { + amount: [ + { + amount: "5000", + denom: "ucosm", + }, + ], + gas: "890000", +}; + +const faucetMnemonic = + "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 faucetPath = HdPaths.cosmos(0); +const faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6"; + +const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic); +const signer = await wallet.createIdentity("unused_value" as ChainId, faucetPath); + +const client = new RestClient(defaultHttpUrl); diff --git a/packages/cli/package.json b/packages/cli/package.json index 575934a3..826a6326 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -34,6 +34,7 @@ ], "dependencies": { "@cosmwasm/sdk": "^0.0.3", + "@iov/bcp": "^2.0.0-alpha.7", "@iov/crypto": "^2.0.0-alpha.7", "@iov/encoding": "^2.0.0-alpha.7", "@iov/keycontrol": "^2.0.0-alpha.7", diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 5aba2625..57ecd587 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -1,6 +1,7 @@ import { ArgumentParser } from "argparse"; // tslint:disable-next-line:no-submodule-imports import colors = require("colors/safe"); +import * as fs from "fs"; import { join } from "path"; import { TsRepl } from "./tsrepl"; @@ -11,6 +12,10 @@ export function main(originalArgs: readonly string[]): void { action: "storeTrue", help: "Print version and exit", }); + parser.addArgument("--init", { + metavar: "FILEPATH", + help: "Read initial TypeScript code from file", + }); const maintainerGroup = parser.addArgumentGroup({ title: "Maintainer options", @@ -33,7 +38,30 @@ export function main(originalArgs: readonly string[]): void { } const imports = new Map([ - ["@cosmwasm/sdk", ["types", "RestClient"]], + ["@cosmwasm/sdk", ["encodeSecp256k1Signature", "makeSignBytes", "marshalTx", "types", "RestClient"]], + [ + "@iov/bcp", + [ + "Address", + "Algorithm", + "ChainId", + "Nonce", + "PrehashType", + "PubkeyBytes", + "SendTransaction", + "SignableBytes", + "TokenTicker", + "TransactionId", + // block info + "BlockInfoPending", + "BlockInfoSucceeded", + "BlockInfoFailed", + "BlockInfo", + "isBlockInfoPending", + "isBlockInfoSucceeded", + "isBlockInfoFailed", + ], + ], [ "@iov/crypto", [ @@ -134,6 +162,10 @@ export function main(originalArgs: readonly string[]): void { `; } + if (args.init) { + init += fs.readFileSync(args.init, "utf8") + "\n"; + } + const tsconfigPath = join(__dirname, "..", "tsconfig_repl.json"); const installationDir = join(__dirname, ".."); new TsRepl(tsconfigPath, init, !!args.debug, installationDir).start().catch(error => {