diff --git a/packages/cli/MASK.md b/packages/cli/MASK.md new file mode 100644 index 00000000..9e6e13b1 --- /dev/null +++ b/packages/cli/MASK.md @@ -0,0 +1,61 @@ +# Using the Mask + +This assumes you have already run through the sample in the [README](./README.md). +And have an initialized account. You can use any connect method (sharing with cli, +customize blockchain) if you want. We will show uploading mask and using it +on the Demo Net. + +Start with `./bin/cosmwasm-cli --init examples/helpers.ts examples/mask.ts` +(note the addition of `examples/mask.ts`) + +Ensure the account is set up: + +```ts +// you can hand-copy a mnemonic here, but this is easiest for reuse between sessions +// it creates a random one first time, then reads it in the future +const mnemonic = loadOrCreateMnemonic("foo.key"); +const {address, client} = await connect(mnemonic, {}); +address +client.getAccount(); +``` + +We will use [mask v0.1.0](https://github.com/CosmWasm/cosmwasm-examples/tree/mask-0.1.0/mask), +the hash is [defined here](https://github.com/CosmWasm/cosmwasm-examples/blob/mask-0.1.0/mask/hash.txt): +`1f50bbff503fd9c7bfe713bbf42b309cf88ef299fa76e0242051c9a7e25649a3`. The following will check if +it is already uploaded: + +```ts +const hash = "1f50bbff503fd9c7bfe713bbf42b309cf88ef299fa76e0242051c9a7e25649a3" +client.getCodes().then(codes => codes.filter(x => x.checksum === hash)) +``` + +If it is not uploaded, we will upload it: + +```ts +// Either download the code +const wasmUrl = "https://github.com/CosmWasm/cosmwasm-examples/blob/mask-0.1.0/mask/contract.wasm?raw=true"; +const wasm = await downloadWasm(wasmUrl); + +// Or load from local file +const wasmFile = ".../cosmwasm-examples/mask/contract.wasm" +const wasm = fs.readFileSync(wasmFile); + +// Then upload it +const up = await client.upload(wasm, { source: "https://crates.io/api/v1/crates/cw-mask/0.1.0/download", builder: "confio/cosmwasm-opt:0.7.3"}); +up +up.logs[0].events[0] +``` + +Now, make an instance (as above): + +```ts +// get the proper codeId +const codes = await client.getCodes() +const codeId = codes.filter(x => x.checksum === hash).map(x => x.id)[0] + +// instantiate one contract +const maskResp = await client.instantiate(codeId, {}, "My Mask"); +const mask = maskResp.contractAddress; +``` + +TODO: using it with proper types diff --git a/packages/cli/README.md b/packages/cli/README.md index 54236361..e6f3bea0 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -242,7 +242,12 @@ const {address, client} = await connect(mnemonic, regenOptions); address ``` -Once you have access to the same key as in the cli, you can use those tokens to play with contracts +Once you have access to the same key as in the cli, you can use those tokens to play with contracts. + +## Diving into Contracts + +Check out the [mask documentation](./MASK.md) to view how to use some custom helpers to upload code and use non-trivial contracts +with proper types. ## License diff --git a/packages/cli/examples/helpers.ts b/packages/cli/examples/helpers.ts index 9db4aed9..8f631908 100644 --- a/packages/cli/examples/helpers.ts +++ b/packages/cli/examples/helpers.ts @@ -93,3 +93,14 @@ const randomAddress = async (prefix: string): Promise => { const pubkey = encodeSecp256k1Pubkey(pen.pubkey); return pubkeyToAddress(pubkey, prefix); } + +const downloadWasm = async (url: string): Promise => { + const r = await axios.get(url, { responseType: "arraybuffer"}); + if (r.status !== 200) { + throw new Error(`Download error: ${r.status}`); + } + return r.data; +} + +const getAttibute = (logs: readonly logs.Log[], key: string): string|undefined => + logs[0].events[0].attributes.find(x => x.key == key)?.value diff --git a/packages/cli/examples/mask.ts b/packages/cli/examples/mask.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 253bcc98..6c1e9fc9 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -34,6 +34,7 @@ export function main(originalArgs: readonly string[]): void { [ "encodeSecp256k1Pubkey", "encodeSecp256k1Signature", + "logs", "makeSignBytes", "marshalTx", "Pen",