Start with some helpers

This commit is contained in:
Ethan Frey 2020-02-10 15:51:31 +01:00
parent 708f7744e7
commit 0544836a60
2 changed files with 120 additions and 0 deletions

View File

@ -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<string> {
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<readonly logs.Log[]> {
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;
}

32
scripts/guide.md Normal file
View File

@ -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
```