Format .md files

This commit is contained in:
willclarktech 2020-06-09 14:45:05 +01:00
parent 4849c9559a
commit b404ef38af
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
2 changed files with 100 additions and 76 deletions

View File

@ -16,8 +16,8 @@ Ensure the account is set up:
// 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
const { address, client } = await connect(mnemonic, {});
address;
client.getAccount();
```
@ -27,8 +27,8 @@ the hash is [defined here](https://github.com/CosmWasm/cosmwasm-examples/blob/ma
it is already uploaded:
```ts
const hash = "1f50bbff503fd9c7bfe713bbf42b309cf88ef299fa76e0242051c9a7e25649a3"
client.getCodes().then(codes => codes.filter(x => x.checksum === hash))
const hash = "1f50bbff503fd9c7bfe713bbf42b309cf88ef299fa76e0242051c9a7e25649a3";
client.getCodes().then((codes) => codes.filter((x) => x.checksum === hash));
```
If it is not uploaded, we will upload it:
@ -39,21 +39,24 @@ const wasmUrl = "https://github.com/CosmWasm/cosmwasm-examples/blob/mask-0.1.0/m
const wasm = await downloadWasm(wasmUrl);
// Or load from local file
const wasmFile = ".../cosmwasm-examples/mask/contract.wasm"
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]
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]
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");
@ -61,26 +64,26 @@ const mask = maskResp.contractAddress;
// You can also find the contractAddress later (in a future session), like:
const contracts = await client.getContracts(codeId);
const mask = contracts.filter(x => x.label == "My Mask").map(x => x.address)[0];
const mask = contracts.filter((x) => x.label == "My Mask").map((x) => x.address)[0];
```
Now, let's use the mask. To do so, we need to load it up with some tokens
(both native and ERC20 - from the contract you deployed last time).
```ts
client.sendTokens(mask, [{amount: "500000", "denom": "ucosm"}])
client.getAccount(mask)
client.sendTokens(mask, [{ amount: "500000", denom: "ucosm" }]);
client.getAccount(mask);
// get the foo contract again...
const ercId = 1; // from earlier example, change this if different on your network
const ercs = await client.getContracts(ercId);
const foo = ercs.filter(x => x.label == "FOO").map(x => x.address)[0];
const foo = ercs.filter((x) => x.label == "FOO").map((x) => x.address)[0];
// send some erc tokens to the mask as before
smartQuery(client, foo, { balance: { address: mask } })
const ercMsg = { transfer: {recipient: mask, amount: "800000"}}
smartQuery(client, foo, { balance: { address: mask } });
const ercMsg = { transfer: { recipient: mask, amount: "800000" } };
client.execute(foo, ercMsg);
smartQuery(client, foo, { balance: { address: mask } })
smartQuery(client, foo, { balance: { address: mask } });
```
## Usage
@ -91,13 +94,15 @@ Now, let's send some tokens from it:
```ts
const rand = await randomAddress("cosmos");
client.getAccount(rand)
client.getAccount(mask)
client.getAccount(rand);
client.getAccount(mask);
const callSend: HandleMsg = { reflectmsg: { msgs: [sendMsg(mask, rand, [{amount: "80000", denom: "ucosm"}])]}};
client.execute(mask, callSend)
client.getAccount(rand)
client.getAccount(mask)
const callSend: HandleMsg = {
reflectmsg: { msgs: [sendMsg(mask, rand, [{ amount: "80000", denom: "ucosm" }])] },
};
client.execute(mask, callSend);
client.getAccount(rand);
client.getAccount(mask);
```
### Sending "ERC20" Tokens
@ -105,13 +110,15 @@ client.getAccount(mask)
And call the ERC20 contract from it:
```ts
smartQuery(client, foo, { balance: { address: rand } })
smartQuery(client, foo, { balance: { address: mask } })
smartQuery(client, foo, { balance: { address: rand } });
smartQuery(client, foo, { balance: { address: mask } });
const callContract: HandleMsg = { reflectmsg: { msgs: [contractMsg(foo, {transfer: {"amount": "80000", "recipient": rand}})]}};
client.execute(mask, callContract)
smartQuery(client, foo, { balance: { address: rand } })
smartQuery(client, foo, { balance: { address: mask } })
const callContract: HandleMsg = {
reflectmsg: { msgs: [contractMsg(foo, { transfer: { amount: "80000", recipient: rand } })] },
};
client.execute(mask, callContract);
smartQuery(client, foo, { balance: { address: rand } });
smartQuery(client, foo, { balance: { address: mask } });
```
### Staking via OpaqueMsg
@ -206,35 +213,37 @@ and our open staking position in one fell swoop, without the other modules/contr
```ts
const aliceMnem = loadOrCreateMnemonic("other.key");
const {address: alice, client: aliceClient} = await connect(aliceMnem, {});
alice
const { address: alice, client: aliceClient } = await connect(aliceMnem, {});
alice;
client.getAccount()
aliceClient.getAccount()
client.getAccount();
aliceClient.getAccount();
// send some minimal tokens
client.sendTokens(alice, [{amount: "500000", "denom": "ucosm"}])
aliceClient.getAccount()
client.sendTokens(alice, [{ amount: "500000", denom: "ucosm" }]);
aliceClient.getAccount();
// now, transfer ownership of the mask
const query: QueryMsg = { owner: {} };
smartQuery(client, mask, query);
const transferMsg: HandleMsg = { changeowner: { owner: alice }}
client.execute(mask, transferMsg)
const transferMsg: HandleMsg = { changeowner: { owner: alice } };
client.execute(mask, transferMsg);
smartQuery(client, mask, query);
```
From now own, alice can control the mask, not me.... And she can extract the erc20 tokens or anything else the mask controls
```ts
smartQuery(client, foo, { balance: { address: alice } })
smartQuery(client, foo, { balance: { address: alice } });
const withdraw: HandleMsg = { reflectmsg: { msgs: [contractMsg(foo, {transfer: {"amount": "80000", "recipient": alice}})]}};
const withdraw: HandleMsg = {
reflectmsg: { msgs: [contractMsg(foo, { transfer: { amount: "80000", recipient: alice } })] },
};
// this will error (me)
client.execute(mask, withdraw)
client.execute(mask, withdraw);
// this will succeed (alice)
aliceClient.execute(mask, withdraw)
smartQuery(client, foo, { balance: { address: alice } })
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),

View File

@ -64,14 +64,21 @@ const sendTokensMsg: MsgSend = {
},
};
const signBytes = makeSignBytes([sendTokensMsg], defaultFee, defaultNetworkId, memo, account_number, sequence);
const signBytes = makeSignBytes(
[sendTokensMsg],
defaultFee,
defaultNetworkId,
memo,
account_number,
sequence,
);
const signature = await pen.sign(signBytes);
const signedTx: StdTx = {
msg: [sendTokensMsg],
fee: defaultFee,
memo: memo,
signatures: [signature],
}
msg: [sendTokensMsg],
fee: defaultFee,
memo: memo,
signatures: [signature],
};
const postResult = await client.postTx(signedTx);
```
@ -91,12 +98,12 @@ Setup Account:
// 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
const { address, client } = await connect(mnemonic, {});
address;
client.getAccount();
// if empty - this only works with CosmWasm
hitFaucet(defaultFaucetUrl, address, "COSM")
hitFaucet(defaultFaucetUrl, address, "COSM");
client.getAccount();
```
@ -104,17 +111,17 @@ View contracts:
```ts
// show all code and contracts
client.getCodes()
client.getCodes();
// query the first contract for first code
const contracts = await client.getContracts(1);
contracts
const info = await client.getContract(contracts[0].address)
info
info.initMsg
contracts;
const info = await client.getContract(contracts[0].address);
info;
info.initMsg;
// see your balance here
smartQuery(client, addr, { balance: { address } })
smartQuery(client, addr, { balance: { address } });
```
Instantiate and use ERC20 contract:
@ -122,38 +129,46 @@ Instantiate and use ERC20 contract:
```ts
// no money? no problem.
// let's make our own s**coin - replace "FOO" with something else to avoid duplicates
const initMsg = { name: "Foo Coin", symbol: "FOO", decimals: 2, initial_balances: [{address, amount: "123456789"}]}
const initMsg = {
name: "Foo Coin",
symbol: "FOO",
decimals: 2,
initial_balances: [{ address, amount: "123456789" }],
};
const foo = await client.instantiate(1, initMsg, "FOO");
foo
foo.logs[0].events[0]
foo;
foo.logs[0].events[0];
const fooAddr = foo.contractAddress;
// we can also find this another way...
const fooAddr2 = await client.getContracts(1).then(contracts => contracts.filter(x => x.label == "FOO").map(x => x.address)[0])
[fooAddr, fooAddr2]
const fooAddr2 = await client
.getContracts(1)
.then((contracts) => contracts.filter((x) => x.label == "FOO").map((x) => x.address)[0])[
(fooAddr, fooAddr2)
];
// now we have some cash
smartQuery(client, fooAddr, { balance: { address } })
smartQuery(client, fooAddr, { balance: { address } });
const rcpt = await randomAddress("cosmos");
rcpt
smartQuery(client, fooAddr, { balance: { address: rcpt } })
rcpt;
smartQuery(client, fooAddr, { balance: { address: rcpt } });
const execMsg = { transfer: {recipient: rcpt, amount: "808"}}
const execMsg = { transfer: { recipient: rcpt, amount: "808" } };
const exec = await client.execute(fooAddr, execMsg);
exec
exec.logs[0].events[0]
smartQuery(client, fooAddr, { balance: { address: rcpt } })
exec;
exec.logs[0].events[0];
smartQuery(client, fooAddr, { balance: { address: rcpt } });
```
Or just send tokens:
```ts
client.getAccount(rcpt)
client.getAccount(rcpt);
const sent = await client.sendTokens(rcpt, [{amount: "1234", denom: "ucosm"}])
sent
foo.logs[0].events[0]
const sent = await client.sendTokens(rcpt, [{ amount: "1234", denom: "ucosm" }]);
sent;
foo.logs[0].events[0];
```
### Use Custom Network
@ -191,7 +206,7 @@ Hit the faucet with your address (in browser): https://regen.vitwit.com/faucet t
```ts
// should have tokens now
client.getAccount()
client.getAccount();
```
At this point you can continue all the other behaviors from above, looking at codes, etc.
@ -236,10 +251,10 @@ Load it up in cosmwasm-js: `./bin/cosmwasm-cli --init examples/helpers.ts`
```ts
const mnemonic = loadOrCreateMnemonic("wasmcli.key");
const {address, client} = await connect(mnemonic, regenOptions);
const { address, client } = await connect(mnemonic, regenOptions);
// this should match what you got on the cli - showing compatibility
address
address;
```
Once you have access to the same key as in the cli, you can use those tokens to play with contracts.