From 0544836a609dddf350270ccd7514db4b2faf8cc2 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 10 Feb 2020 15:51:31 +0100 Subject: [PATCH] Start with some helpers --- packages/cli/examples/helpers.ts | 88 ++++++++++++++++++++++++++++++++ scripts/guide.md | 32 ++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 packages/cli/examples/helpers.ts create mode 100644 scripts/guide.md diff --git a/packages/cli/examples/helpers.ts b/packages/cli/examples/helpers.ts new file mode 100644 index 00000000..ee84fdff --- /dev/null +++ b/packages/cli/examples/helpers.ts @@ -0,0 +1,88 @@ +/* eslint-disable @typescript-eslint/camelcase */ +import { + logs, +} from "@cosmwasm/sdk"; + +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 faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6"; + +const pen = await Secp256k1Pen.fromMnemonic(faucetMnemonic); +const client = new RestClient(defaultHttpUrl); + +const networkId = "testing"; + + +// helper functions +async function instantiateContract(initClient: RestClient, initPen: Secp256k1Pen, codeId: number, msg: object, transferAmount?: types.Coin[]): Promise { + const memo = "Create an ERC20 instance"; + const sender = encodeAddress({ "type": "tendermint/Secp256k1PubKey", "value": toBase64(initPen.pubkey)}, "cosmos"); + const instantiateContractMsg = { + type: "wasm/instantiate", + value: { + sender: sender, + code_id: codeId.toString(), + init_msg: msg, + init_funds: transferAmount || [], + }, + }; + const account = (await initClient.authAccounts(faucetAddress)).result.value; + const signBytes = makeSignBytes([instantiateContractMsg], defaultFee, networkId, memo, account); + const signature = encodeSecp256k1Signature(initPen.pubkey, await initPen.createSignature(signBytes)); + const signedTx = { + msg: [instantiateContractMsg], + fee: defaultFee, + memo: memo, + signatures: [signature], + }; + const result = await initClient.postTx(marshalTx(signedTx)); + if (result.code) { + throw new Error(`Failed tx: (${result.code}): ${result.raw_log}`) + } + const instantiationLogs = logs.parseLogs(result.logs); + const contractAddress = logs.findAttribute(instantiationLogs, "message", "contract_address").value; + return contractAddress; +} + +// helper functions +async function executeContract(execClient: RestClient, execPen: Secp256k1Pen, contractAddr: string, msg: object, transferAmount?: types.Coin[]): Promise { + const memo = "Create an ERC20 instance"; + const sender = encodeAddress({ "type": "tendermint/Secp256k1PubKey", "value": toBase64(execPen.pubkey)}, "cosmos"); + const instantiateContractMsg = { + type: "wasm/execute", + value: { + sender: sender, + contract: contractAddr, + msg: msg, + sent_funds: transferAmount || [], + }, + }; + const account = (await execClient.authAccounts(faucetAddress)).result.value; + const signBytes = makeSignBytes([instantiateContractMsg], defaultFee, networkId, memo, account); + const signature = encodeSecp256k1Signature(execPen.pubkey, await execPen.createSignature(signBytes)); + const signedTx = { + msg: [instantiateContractMsg], + fee: defaultFee, + memo: memo, + signatures: [signature], + }; + const result = await execClient.postTx(marshalTx(signedTx)); + if (result.code) { + throw new Error(`Failed tx: (${result.code}): ${result.raw_log}`) + } + const execLogs = logs.parseLogs(result.logs); + return execLogs; +} + diff --git a/scripts/guide.md b/scripts/guide.md new file mode 100644 index 00000000..ce75709a --- /dev/null +++ b/scripts/guide.md @@ -0,0 +1,32 @@ +## Setup + +```sh +node ./scripts/cosm/deploy_erc20.js +``` + +## Make account + +```sh +cd packages/cli +./bin/cosmwasm-cli --init examples/helpers.ts +``` + +Now, check it and init/execute contracts: + +```js +const account = (await client.authAccounts(faucetAddress)).result.value; +account + +client.listCodeInfo() +client.listContractAddresses() + +// query this contract +const addr = (await client.listContractAddresses())[0] +const info = await client.getContractInfo(addr) +info.init_msg + +// try some actions +client.queryContractSmart(addr, { balance: { address: faucetAddress } }) + +// make a new contract +```