From d9bcd821c16f873debf0675943cabfbbb22f29fc Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 11 Feb 2020 10:55:48 +0100 Subject: [PATCH] Added helper to parse Uint8Array as Utf8-encoded json for smart queries --- packages/cli/README.md | 10 +++++----- packages/cli/examples/helpers.ts | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/cli/README.md b/packages/cli/README.md index c541c306..e4f8ed3c 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -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 diff --git a/packages/cli/examples/helpers.ts b/packages/cli/examples/helpers.ts index b3761e5a..5f175a05 100644 --- a/packages/cli/examples/helpers.ts +++ b/packages/cli/examples/helpers.ts @@ -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 => { + const bin = await client.queryContractSmart(addr, query); + return JSON.parse(fromUtf8(bin)); +} + + const randomAddress = async (): Promise => { const mnemonic = Bip39.encode(Random.getBytes(16)).toString(); const randomPen = await Secp256k1Pen.fromMnemonic(mnemonic);