Update helpers to modern api, demo net
This commit is contained in:
parent
8cf5c68b7e
commit
5c75e97f87
3
.gitignore
vendored
3
.gitignore
vendored
@ -14,3 +14,6 @@ node_modules/
|
||||
|
||||
# IDE-specific
|
||||
.vscode/
|
||||
|
||||
# demo mnemonics from cli
|
||||
*.key
|
||||
|
||||
@ -87,47 +87,54 @@ quick queries on a chain, you can use an extended set of helpers:
|
||||
4. Play around as in the following example code
|
||||
|
||||
```ts
|
||||
const account = (await client.authAccounts(faucetAddress)).result.value;
|
||||
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
|
||||
|
||||
client.getAccount();
|
||||
// if empty
|
||||
hitFaucet(defaultFaucetUrl, address, "COSM")
|
||||
client.getAccount();
|
||||
|
||||
// show all code and contracts
|
||||
client.listCodeInfo()
|
||||
client.listContractAddresses()
|
||||
client.getCodes()
|
||||
|
||||
// query the first contract
|
||||
const addr = (await client.listContractAddresses())[0]
|
||||
const info = await client.getContractInfo(addr)
|
||||
info.init_msg
|
||||
// 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
|
||||
|
||||
// see your balance here
|
||||
smartQuery(client, addr, { balance: { address: faucetAddress } })
|
||||
smartQuery(client, addr, { balance: { address } })
|
||||
|
||||
// make a new contract
|
||||
const initMsg = { name: "Foo Coin", symbol: "FOO", decimals: 2, initial_balances: [{address: faucetAddress, amount: "123456789"}]}
|
||||
const foo = await instantiateContract(client, pen, 1, initMsg);
|
||||
// no money? no problem.
|
||||
// let's make our own s**coin
|
||||
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]
|
||||
const fooAddr = foo.contractAddress;
|
||||
|
||||
smartQuery(client, foo, { balance: { address: faucetAddress } })
|
||||
// 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 rcpt = await randomAddress();
|
||||
// now we have some cash
|
||||
smartQuery(client, fooAddr, { balance: { address } })
|
||||
|
||||
const rcpt = await randomAddress("cosmos");
|
||||
rcpt
|
||||
smartQuery(client, foo, { balance: { address: rcpt } })
|
||||
smartQuery(client, fooAddr, { balance: { address: rcpt } })
|
||||
|
||||
const execMsg = { transfer: {recipient: rcpt, amount: "808"}}
|
||||
const exec = await executeContract(client, pen, foo, execMsg);
|
||||
const exec = await client.execute(fooAddr, execMsg);
|
||||
exec
|
||||
exec[0].events[0]
|
||||
smartQuery(client, foo, { balance: { address: rcpt } })
|
||||
```
|
||||
|
||||
## Other example codes
|
||||
|
||||
### Create random mnemonic and Cosmos address
|
||||
|
||||
```ts
|
||||
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(mnemonic);
|
||||
const pubkey = encodeSecp256k1Pubkey(pen.pubkey);
|
||||
const address = pubkeyToAddress(pubkey, "cosmos");
|
||||
exec.logs[0].events[0]
|
||||
smartQuery(client, fooAddr, { balance: { address: rcpt } })
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
@ -1,101 +1,100 @@
|
||||
/* eslint-disable @typescript-eslint/camelcase */
|
||||
import {
|
||||
logs
|
||||
} from "@cosmwasm/sdk";
|
||||
import { FeeTable } from "@cosmwasm/sdk/types/signingcosmwasmclient";
|
||||
import axios from "axios";
|
||||
import * as fs from "fs";
|
||||
|
||||
const defaultHttpUrl = "http://localhost:1317";
|
||||
const defaultNetworkId = "testing";
|
||||
const defaultFee: types.StdFee = {
|
||||
amount: [
|
||||
{
|
||||
amount: "5000",
|
||||
denom: "ucosm",
|
||||
},
|
||||
],
|
||||
gas: "890000",
|
||||
interface Options {
|
||||
httpUrl: string;
|
||||
networkId: string;
|
||||
feeToken: string;
|
||||
gasPrice: number;
|
||||
bech32prefix: string;
|
||||
};
|
||||
|
||||
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
|
||||
const instantiateContract = async (initClient: RestClient, initPen: Secp256k1Pen, codeId: number, msg: object, transferAmount?: types.Coin[]): Promise<string> => {
|
||||
const memo = "Create an ERC20 instance";
|
||||
const sender = pubkeyToAddress({ "type": types.pubkeyType.secp256k1, "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 = await initPen.sign(signBytes);
|
||||
const signedTx = {
|
||||
msg: [instantiateContractMsg],
|
||||
fee: defaultFee,
|
||||
memo: memo,
|
||||
signatures: [signature],
|
||||
};
|
||||
const result = await initClient.postTx(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;
|
||||
const defaultOptions: Options = {
|
||||
httpUrl: "https://lcd.demo-07.cosmwasm.com",
|
||||
networkId: "testing",
|
||||
feeToken: "ucosm",
|
||||
gasPrice: 0.025,
|
||||
bech32prefix: "cosmos",
|
||||
}
|
||||
|
||||
// helper functions
|
||||
const executeContract = async (execClient: RestClient, execPen: Secp256k1Pen, contractAddr: string, msg: object, transferAmount?: types.Coin[]): Promise<readonly logs.Log[]> => {
|
||||
const memo = "Create an ERC20 instance";
|
||||
const sender = pubkeyToAddress({ "type": types.pubkeyType.secp256k1, "value": toBase64(execPen.pubkey)}, "cosmos");
|
||||
const instantiateContractMsg = {
|
||||
type: "wasm/execute",
|
||||
value: {
|
||||
sender: sender,
|
||||
contract: contractAddr,
|
||||
msg: msg,
|
||||
sent_funds: transferAmount || [],
|
||||
},
|
||||
const defaultFaucetUrl = "https://faucet.demo-07.cosmwasm.com/credit";
|
||||
|
||||
const buildFeeTable = (feeToken: string, gasPrice: number): FeeTable => {
|
||||
const stdFee = (gas: number, denom: string, price: number) => {
|
||||
const amount = Math.floor(gas * price);
|
||||
return {
|
||||
amount: [{ amount: amount.toString(), denom: denom }],
|
||||
gas: gas.toString(),
|
||||
}
|
||||
};
|
||||
const account = (await execClient.authAccounts(faucetAddress)).result.value;
|
||||
const signBytes = makeSignBytes([instantiateContractMsg], defaultFee, networkId, memo, account);
|
||||
const signature = await execPen.sign(signBytes);
|
||||
const signedTx = {
|
||||
msg: [instantiateContractMsg],
|
||||
fee: defaultFee,
|
||||
memo: memo,
|
||||
signatures: [signature],
|
||||
};
|
||||
const result = await execClient.postTx(signedTx);
|
||||
if (result.code) {
|
||||
throw new Error(`Failed tx: (${result.code}): ${result.raw_log}`)
|
||||
|
||||
return {
|
||||
upload: stdFee(1000000, feeToken, gasPrice),
|
||||
init: stdFee(500000, feeToken, gasPrice),
|
||||
exec: stdFee(200000, feeToken, gasPrice),
|
||||
send: stdFee(80000, feeToken, gasPrice),
|
||||
}
|
||||
const execLogs = logs.parseLogs(result.logs);
|
||||
return execLogs;
|
||||
};
|
||||
|
||||
// TODO: hit faucet
|
||||
// if (config.faucetUrl) {
|
||||
// const acct = await client.getAccount();
|
||||
// if (!acct?.balance?.length) {
|
||||
// await ky.post(config.faucetUrl, { json: { ticker: "COSM", address } });
|
||||
// }
|
||||
// }
|
||||
|
||||
const penAddress = (pen: Secp256k1Pen, prefix: string): string => {
|
||||
const pubkey = encodeSecp256k1Pubkey(pen.pubkey);
|
||||
return pubkeyToAddress(pubkey, prefix);
|
||||
}
|
||||
|
||||
const connect = async (mnemonic: string, opts: Partial<Options>): Promise<{
|
||||
client: SigningCosmWasmClient,
|
||||
address: string,
|
||||
}> => {
|
||||
const options: Options = {...defaultOptions, ...opts};
|
||||
const feeTable = buildFeeTable(options.feeToken, options.gasPrice);
|
||||
const pen = await Secp256k1Pen.fromMnemonic(mnemonic);
|
||||
const address = penAddress(pen, options.bech32prefix);
|
||||
|
||||
const client = new SigningCosmWasmClient(options.httpUrl, address, signBytes => pen.sign(signBytes), feeTable);
|
||||
return {client, address};
|
||||
};
|
||||
|
||||
// smartQuery assumes the content is proper JSON data and parses before returning it
|
||||
const smartQuery = async (client: RestClient, addr: string, query: object): Promise<any> => {
|
||||
const smartQuery = async (client: CosmWasmClient, addr: string, query: object): Promise<any> => {
|
||||
const bin = await client.queryContractSmart(addr, query);
|
||||
return JSON.parse(fromUtf8(bin));
|
||||
}
|
||||
|
||||
|
||||
const randomAddress = async (): Promise<string> => {
|
||||
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
|
||||
const randomPen = await Secp256k1Pen.fromMnemonic(mnemonic);
|
||||
const pubkey = encodeSecp256k1Pubkey(randomPen.pubkey);
|
||||
return pubkeyToAddress(pubkey, "cosmos");
|
||||
// loadOrCreateMnemonic will try to load a mnemonic from the file.
|
||||
// If missing, it will generate a random one and save to the file.
|
||||
//
|
||||
// This is not secure, but does allow simple developer access to persist a
|
||||
// mnemonic between sessions
|
||||
const loadOrCreateMnemonic = (filename: string): string => {
|
||||
try {
|
||||
const mnemonic = fs.readFileSync(filename, "utf8");
|
||||
return mnemonic;
|
||||
} catch (err) {
|
||||
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
|
||||
fs.writeFileSync(filename, mnemonic, "utf8");
|
||||
return mnemonic;
|
||||
}
|
||||
}
|
||||
|
||||
const hitFaucet = async (faucetUrl: string, address: string, ticker: string): Promise<void> => {
|
||||
const r = await axios.post(defaultFaucetUrl, { ticker, address });
|
||||
console.log(r.status);
|
||||
console.log(r.data);
|
||||
}
|
||||
|
||||
const randomAddress = async (prefix: string): Promise<string> => {
|
||||
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
|
||||
const pen = await Secp256k1Pen.fromMnemonic(mnemonic);
|
||||
const pubkey = encodeSecp256k1Pubkey(pen.pubkey);
|
||||
return pubkeyToAddress(pubkey, prefix);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user