From 1e000f25c761f68aa941f5354beedc57b0e0a2d7 Mon Sep 17 00:00:00 2001 From: rrr523 <59029880+rrr523@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:37:19 +0800 Subject: [PATCH] chore(example): Nodejs example (#375) --- examples/nodejs/README.md | 2 +- examples/nodejs/account.js | 13 +++++++ examples/nodejs/policy.js | 70 ++++++++++++++++++++++++++++++++++++++ examples/nodejs/query.js | 12 +++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 examples/nodejs/account.js create mode 100644 examples/nodejs/policy.js create mode 100644 examples/nodejs/query.js diff --git a/examples/nodejs/README.md b/examples/nodejs/README.md index 2b4115d9..1881966e 100644 --- a/examples/nodejs/README.md +++ b/examples/nodejs/README.md @@ -6,4 +6,4 @@ > node storage.js ``` -[More examples](../../packages/chain-sdk/tests/) +[More examples](../../packages/js-sdk/tests/) diff --git a/examples/nodejs/account.js b/examples/nodejs/account.js new file mode 100644 index 00000000..56d4a3e8 --- /dev/null +++ b/examples/nodejs/account.js @@ -0,0 +1,13 @@ +const { client, selectSp, generateString } = require('./client'); +const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env'); + +(async () => { + const accountInfo = await client.account.getAccount(ACCOUNT_ADDRESS); + + const accountBalance = await client.account.getAccountBalance({ + address: ACCOUNT_ADDRESS, + denom: 'BNB', + }); + + const moduleAccounts = await client.account.getModuleAccounts(); +})(); diff --git a/examples/nodejs/policy.js b/examples/nodejs/policy.js new file mode 100644 index 00000000..3802e000 --- /dev/null +++ b/examples/nodejs/policy.js @@ -0,0 +1,70 @@ +const { client, selectSp, generateString } = require('./client'); +const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env'); + +(async () => { + const GROUP_NAME = generateString(10); + const EXTRA = generateString(10); + + // create group tx + const createGroupTx = await client.group.createGroup({ + creator: ACCOUNT.address, + extra: EXTRA, + groupName: GROUP_NAME, + }); + + const simulateInfo = await createGroupTx.simulate({ + denom: 'BNB', + }); + + console.log('simulateInfo', simulateInfo); + + const createGroupTxRes = await createGroupTx.broadcast({ + denom: 'BNB', + gasLimit: Number(simulateInfo?.gasLimit), + gasPrice: simulateInfo?.gasPrice || '5000000000', + payer: ACCOUNT_ADDRESS, + granter: '', + privateKey: ACCOUNT_PRIVATEKEY, + }); + + // update group tx + const newExtra = generateString(10); + const updateGroupTx = await client.group.updateGroupExtra({ + groupName: GROUP_NAME, + groupOwner: ACCOUNT_ADDRESS, + operator: ACCOUNT_ADDRESS, + extra: newExtra, + }); + + const simulateInfo = await updateGroupTx.simulate({ + denom: 'BNB', + }); + + expect(simulateInfo).not.toBeNull(); + + const updateGroupTxRes = await updateGroupTx.broadcast({ + denom: 'BNB', + gasLimit: Number(simulateInfo.gasLimit), + gasPrice: simulateInfo.gasPrice, + granter: '', + payer: ACCOUNT_ADDRESS, + privateKey: ACCOUNT_PRIVATEKEY, + }); + + // delete group tx + const deleteGroupTx = await client.group.deleteGroup({ + groupName: GROUP_NAME, + operator: ACCOUNT_ADDRESS, + }); + const simulateInfo = await deleteGroupTx.simulate({ + denom: 'BNB', + }); + const deleteGroupTxRes = await deleteGroupTx.broadcast({ + denom: 'BNB', + gasLimit: Number(simulateInfo.gasLimit), + gasPrice: simulateInfo.gasPrice, + granter: '', + payer: ACCOUNT_ADDRESS, + privateKey: ACCOUNT_PRIVATEKEY, + }); +})(); diff --git a/examples/nodejs/query.js b/examples/nodejs/query.js new file mode 100644 index 00000000..ab341a1b --- /dev/null +++ b/examples/nodejs/query.js @@ -0,0 +1,12 @@ +const { client, selectSp, generateString } = require('./client'); +const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env'); + +(async () => { + const spInfo = await selectSp(); + + const queryLockFeeRes = await client.storage.queryLockFee({ + createAt: Long.fromInt(0), + primarySpAddress: spInfo.primarySpAddress, + payloadSize: Long.fromInt(1111), + }); +})();