From 7f4f4f6441368ab518f7ef3c1c10f9fbce8c95f9 Mon Sep 17 00:00:00 2001 From: Youngjoon Lee Date: Thu, 21 Jan 2021 18:30:28 +0900 Subject: [PATCH] Add AOL example (#30) --- example/aol.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 example/aol.js diff --git a/example/aol.js b/example/aol.js new file mode 100644 index 0000000..f36ecd9 --- /dev/null +++ b/example/aol.js @@ -0,0 +1,65 @@ +// import panaceajs from '../dist/index.js'; +import panaceajs from '@medibloc/panacea-js'; + +const txBroadcastMode = 'block'; +const chainId = 'testing'; + +const client = new panaceajs.Client('http://127.0.0.1:1317'); + +// Replace address and mnemonic with the real values, before running this example code. +async function getAccount() { + const account = new panaceajs.Account(await client.Account.getAccount('panacea1d58s72gu0mjkw0lkgyvr0eqzz3mv74awfsjslz')); + account.setPrivKeyFromMnemonic('document public coconut mandate dawn agree satisfy family earth glimpse extend gas toddler pattern horror bar detect borrow bunker buddy harvest explain purpose fury'); + return account; +} + +async function addRecord(account, topicName, key, value) { + const fee = new panaceajs.Fee(); + fee.setFee('1000000umed'); + + let tx = new panaceajs.Tx({ + chainId, + sequence: account.sequence, + accountNumber: account.account_number, + fee, + }); + + const msg = new panaceajs.Message.AOL.AddRecord({ + topicName: topicName, + key: key, + value: value, + writerAddress: account.address, + ownerAddress: account.address, + }); + tx.addMsgs(msg); + + tx = account.sign(tx); + account.increaseSequence(); + + const res = await client.Tendermint.broadcastTx(tx.convertToBroadcastTx(txBroadcastMode)); + console.log(res); + + return JSON.parse(Buffer.from(res.data, 'hex').toString()); +} + +async function getRecord(account, topicName, offset) { + const record = await client.AOL.getRecord(account.address, topicName, offset); + console.log(record); +} + +async function main() { + try { + const account = await getAccount(); + console.log(account); + + const res = await addRecord(account, 'my-topic', 'my-key', 'my-value'); + console.log(res); + + await getRecord(account, 'my-topic', res.value.offset); + } catch (e) { + console.error(e); + } +} + +main(); +