Added helper to parse Uint8Array as Utf8-encoded json for smart queries

This commit is contained in:
Ethan Frey 2020-02-11 10:55:48 +01:00
parent c3bd498524
commit d9bcd821c1
2 changed files with 12 additions and 5 deletions

View File

@ -100,23 +100,23 @@ const info = await client.getContractInfo(addr)
info.init_msg
// see your balance here
client.queryContractSmart(addr, { balance: { address: faucetAddress } })
smartQuery(client, addr, { balance: { address: faucetAddress } })
// 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);
client.queryContractSmart(foo, { balance: { address: faucetAddress } })
smartQuery(client, foo, { balance: { address: faucetAddress } })
const rcpt = await randomAddress();
rcpt
client.queryContractSmart(foo, { balance: { address: rcpt } })
smartQuery(client, foo, { balance: { address: rcpt } })
const execMsg = { transfer: {recipient: rcpt, amount: "808"}}
const exec = await executeContract(client, pen, foo, execMsg);
exec
client.queryContractSmart(foo, { balance: { address: rcpt } })
exec[0].events[0]
smartQuery(client, foo, { balance: { address: rcpt } })
```
## Other example codes

View File

@ -86,6 +86,13 @@ const executeContract = async (execClient: RestClient, execPen: Secp256k1Pen, co
return execLogs;
}
// 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 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);