Add some code snippets

This commit is contained in:
Simon Warta 2020-02-06 17:46:40 +01:00
parent cf083e3fb0
commit bc87f8e87a
5 changed files with 92 additions and 3 deletions

View File

@ -4,5 +4,6 @@ build/
custom_types/
dist/
docs/
examples/
generated/
types/

View File

@ -39,8 +39,42 @@ $ cosmwasm-cli
## Getting started
1. Install `@cosmwasm/cli` and run `cosmwasm-cli` as shown above
2. TODO: write README inspired by
https://github.com/iov-one/iov-core/blob/master/packages/iov-cli/README.md
2. Start a local wasmd blockchain
3. Start with `./bin/cosmwasm-cli --init examples/local_faucet.ts`
4. Play around as in the following example code
```ts
// Get account information
const account = (await client.authAccounts(faucetAddress)).result.value;
// Craft a send transaction
const emptyAddress = Bech32.encode("cosmos", Random.getBytes(20));
const memo = "My first contract on chain";
const sendTokensMsg: types.MsgSend = {
type: "cosmos-sdk/MsgSend",
value: {
from_address: faucetAddress,
to_address: emptyAddress,
amount: [
{
denom: "ucosm",
amount: "1234567",
},
],
},
};
const signBytes = makeSignBytes([sendTokensMsg], defaultFee, defaultNetworkId, memo, account) as SignableBytes;
const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256);
const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature);
const signedTx: types.StdTx = {
msg: [sendTokensMsg],
fee: defaultFee,
memo: memo,
signatures: [signature],
}
const postResult = await client.postTx(marshalTx(signedTx));
```
## License

View File

@ -0,0 +1,21 @@
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 faucetPath = HdPaths.cosmos(0);
const faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6";
const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic);
const signer = await wallet.createIdentity("unused_value" as ChainId, faucetPath);
const client = new RestClient(defaultHttpUrl);

View File

@ -34,6 +34,7 @@
],
"dependencies": {
"@cosmwasm/sdk": "^0.0.3",
"@iov/bcp": "^2.0.0-alpha.7",
"@iov/crypto": "^2.0.0-alpha.7",
"@iov/encoding": "^2.0.0-alpha.7",
"@iov/keycontrol": "^2.0.0-alpha.7",

View File

@ -1,6 +1,7 @@
import { ArgumentParser } from "argparse";
// tslint:disable-next-line:no-submodule-imports
import colors = require("colors/safe");
import * as fs from "fs";
import { join } from "path";
import { TsRepl } from "./tsrepl";
@ -11,6 +12,10 @@ export function main(originalArgs: readonly string[]): void {
action: "storeTrue",
help: "Print version and exit",
});
parser.addArgument("--init", {
metavar: "FILEPATH",
help: "Read initial TypeScript code from file",
});
const maintainerGroup = parser.addArgumentGroup({
title: "Maintainer options",
@ -33,7 +38,30 @@ export function main(originalArgs: readonly string[]): void {
}
const imports = new Map<string, readonly string[]>([
["@cosmwasm/sdk", ["types", "RestClient"]],
["@cosmwasm/sdk", ["encodeSecp256k1Signature", "makeSignBytes", "marshalTx", "types", "RestClient"]],
[
"@iov/bcp",
[
"Address",
"Algorithm",
"ChainId",
"Nonce",
"PrehashType",
"PubkeyBytes",
"SendTransaction",
"SignableBytes",
"TokenTicker",
"TransactionId",
// block info
"BlockInfoPending",
"BlockInfoSucceeded",
"BlockInfoFailed",
"BlockInfo",
"isBlockInfoPending",
"isBlockInfoSucceeded",
"isBlockInfoFailed",
],
],
[
"@iov/crypto",
[
@ -134,6 +162,10 @@ export function main(originalArgs: readonly string[]): void {
`;
}
if (args.init) {
init += fs.readFileSync(args.init, "utf8") + "\n";
}
const tsconfigPath = join(__dirname, "..", "tsconfig_repl.json");
const installationDir = join(__dirname, "..");
new TsRepl(tsconfigPath, init, !!args.debug, installationDir).start().catch(error => {