mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Start with some helpers
This commit is contained in:
parent
708f7744e7
commit
0544836a60
88
packages/cli/examples/helpers.ts
Normal file
88
packages/cli/examples/helpers.ts
Normal 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
32
scripts/guide.md
Normal 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
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user