Skip to content

Commit

Permalink
chore(example): Nodejs example (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 authored Oct 20, 2023
1 parent a4682de commit 1e000f2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
> node storage.js
```

[More examples](../../packages/chain-sdk/tests/)
[More examples](../../packages/js-sdk/tests/)
13 changes: 13 additions & 0 deletions examples/nodejs/account.js
Original file line number Diff line number Diff line change
@@ -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();
})();
70 changes: 70 additions & 0 deletions examples/nodejs/policy.js
Original file line number Diff line number Diff line change
@@ -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,
});
})();
12 changes: 12 additions & 0 deletions examples/nodejs/query.js
Original file line number Diff line number Diff line change
@@ -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),
});
})();

0 comments on commit 1e000f2

Please sign in to comment.