From ee0b6a660438912c829c23372431141c6ea0ef32 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 20 Mar 2020 22:35:22 +0100 Subject: [PATCH] Complete mask demo --- packages/cli/MASK.md | 78 +++++++++++++++++++++++++++++++---- packages/cli/examples/mask.ts | 9 +--- 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/packages/cli/MASK.md b/packages/cli/MASK.md index 476b5655..dc6e0f2a 100644 --- a/packages/cli/MASK.md +++ b/packages/cli/MASK.md @@ -8,6 +8,8 @@ on the Demo Net. Start with `./bin/cosmwasm-cli --init examples/helpers.ts examples/mask.ts` (note the addition of `examples/mask.ts`) +## Setup + Ensure the account is set up: ```ts @@ -81,6 +83,10 @@ client.execute(foo, ercMsg); smartQuery(client, foo, { balance: { address: mask } }) ``` +## Usage + +### Sending Native Tokens + Now, let's send some tokens from it: ```ts @@ -94,6 +100,8 @@ client.getAccount(rand) client.getAccount(mask) ``` +### Sending "ERC20" Tokens + And call the ERC20 contract from it: ```ts @@ -106,7 +114,9 @@ smartQuery(client, foo, { balance: { address: rand } }) smartQuery(client, foo, { balance: { address: mask } }) ``` -And now... let's use OpaqueMsg to call into native blockchain messages. +### Staking via OpaqueMsg + +And now... let's use `OpaqueMsg` to call into native blockchain messages. Here we will trigger a staking command. This is an "opaque" command, so neither cosmwams-js nor the cosmwasm contract understands it. It is passed verbatim from the client to the wasmd blockchain (reflected by mask, so using the mask address). @@ -114,25 +124,72 @@ the wasmd blockchain (reflected by mask, so using the mask address). To view this properly, we will have to use the cli tooling: ```sh -TODO +wasmcli config node https://rpc.demo-07.cosmwasm.com:443 +wasmcli config trust-node true + +wasmcli query staking validators +wasmcli query staking delegations-to cosmosvaloper1e8gslcu2u2p5zp9rgj8alz4q3lt6hvywqppf23 + +# create a demo tx to show how it looks +wasmcli tx staking delegate cosmosvaloper1e8gslcu2u2p5zp9rgj8alz4q3lt6hvywqppf23 300000ustake --generate-only --chain-id testing ``` To create such a message, we need to produce the amino json encoding of a staking message. That does involve a bit of investigation, but looks like: ```json -TODO +{ + "type": "cosmos-sdk/MsgDelegate", + "value": { + "delegator_address": "", + "validator_address": "cosmosvaloper1e8gslcu2u2p5zp9rgj8alz4q3lt6hvywqppf23", + "amount": { + "denom": "ustake", + "amount": "300000" + } + } +} ``` +Run the following (taking the operator address for the validator from the cli output) + ```ts -const staking = {} -const callOpaque: HandleMsg = { reflectmsg: { msgs: [opaqueMsg(staking)]}}; -client.execute(mask, callOpaque) +mask +address + +// get some staking tokens for the mask +hitFaucet(defaultFaucetUrl, mask, "STAKE") +client.getAccount(mask) + +.editor +const staking2 = { + type: "cosmos-sdk/MsgDelegate", + value: { + delegator_address: mask, + validator_address: "cosmosvaloper1e8gslcu2u2p5zp9rgj8alz4q3lt6hvywqppf23", + amount: { + denom: "ustake", + amount: "300000" + } + } +}; +^D + +const callOpaque2: HandleMsg = { reflectmsg: { msgs: [opaqueMsg(staking2)]}}; +client.execute(mask, callOpaque2) + +// Note: currently this returns an error about "Event type must be one of message, transfer, wasm; got delegate" +// That is on the client parsing the logs on success. Don't worry, it will be fixed soon. +// https://github.com/CosmWasm/cosmwasm-js/issues/157 ``` Now validate this with the CLI tooling: + ```sh -TODO +wasmcli query staking delegations-to cosmosvaloper1e8gslcu2u2p5zp9rgj8alz4q3lt6hvywqppf23 + +// use the address from node repl +wasmcli query staking delegations ``` The opaqueMsg style is a bit more tricky as it places the burden of transaction construction upon the @@ -141,6 +198,8 @@ We plan to add custom types for some popular native messages to make this bindin and also allow these to be triggered by internal contract logic (they cannot form opaque messages, but rather just relay opaque messages formed by the clients). +## Transfering Owner + Happy hacking using the mask contract. And to make this a bit more interesting, note that you can transfer control of this mask. By transfering ownership, we transfer control of our `ucosm` native , our `FOO` erc20 token, and our open staking position in one fell swoop, without the other modules/contracts being aware of the change. @@ -177,3 +236,8 @@ client.execute(mask, withdraw) aliceClient.execute(mask, withdraw) smartQuery(client, foo, { balance: { address: alice } }) ``` + +Please explore the use-cases of the Mask. More than a production-ready contract (which it may be), +it is designed to be a tool for devs to explore the potential of composition and re-dispatching messages. +See what you can do here, then use this knowledge to build your own contract that calls other contracts. +This is working today, you just have to return the right messages from `handle`. diff --git a/packages/cli/examples/mask.ts b/packages/cli/examples/mask.ts index cecbd2b7..b1a4f4cb 100644 --- a/packages/cli/examples/mask.ts +++ b/packages/cli/examples/mask.ts @@ -1,11 +1,4 @@ -/* - * This file was generated with ❤️ by wasm.glass and is licensed - * for you under WTFPL OR 0BSD OR Unlicense OR MIT OR BSD-3-Clause. - * Note that different terms apply for the contract's source code and schema. - * Type generation powered by json-schema-to-typescript. - */ - - +// types auto-generated by wasm.glass and cleaned up manually export type HandleMsg = | { reflectmsg: {