Skip to content

Commit

Permalink
chore: Update nodejs example
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Nov 29, 2023
1 parent 0d9a08e commit df8d892
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 322 deletions.
6 changes: 6 additions & 0 deletions examples/nodejs/.env.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ACCOUNT_ADDRESS='0x...'
ACCOUNT_PRIVATEKEY='0x...'

# testnet:
NEXT_PUBLIC_GREENFIELD_RPC_URL=https://gnfd-testnet-fullnode-tendermint-ap.bnbchain.org
NEXT_PUBLIC_GREEN_CHAIN_ID=5600
20 changes: 12 additions & 8 deletions examples/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,28 @@ Install dependencies:
pnpm install
```

Build package:
Build packages:

```bash
pnpm run -F "./packages/**" -r build
```

### Run the demo application

Fill your account's private key and address in [env.js](./env.js).
```bash
> cp .env.simple .env
```

Fill your account's private key and address in [env](./env).

## Execure example

```bash
> node storage.js
> node ./cases/storage.js
```

* [storages.js](./storage.js)
* [account.js](./account.js)
* [policy.js](./policy.js)
* [query.js](./query.js)
* [transfer.js](./transfer.js)
* [storages.js](./cases/storage.js)
* [account.js](./cases/account.js)
* [policy.js](./cases/policy.js)
* [query.js](./cases/query.js)
* [transfer.js](./cases/transfer.js)
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const { client, selectSp, generateString } = require('./client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
require('dotenv').config();
const { client, selectSp, generateString } = require('../client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('../env');

(async () => {
const accountInfo = await client.account.getAccount(ACCOUNT_ADDRESS);

console.log('accountInfo', accountInfo);

const accountBalance = await client.account.getAccountBalance({
address: ACCOUNT_ADDRESS,
denom: 'BNB',
Expand Down
29 changes: 14 additions & 15 deletions examples/nodejs/policy.js → examples/nodejs/cases/policy.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
const { client, selectSp, generateString } = require('./client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
require('dotenv').config();
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,
creator: ACCOUNT_ADDRESS,
extra: EXTRA,
groupName: GROUP_NAME,
});

const simulateInfo = await createGroupTx.simulate({
const createGroupTxSimulateInfo = await createGroupTx.simulate({
denom: 'BNB',
});

console.log('simulateInfo', simulateInfo);
console.log('createGroupTxSimulateInfo', createGroupTxSimulateInfo);

const createGroupTxRes = await createGroupTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo?.gasLimit),
gasPrice: simulateInfo?.gasPrice || '5000000000',
gasLimit: Number(createGroupTxSimulateInfo?.gasLimit),
gasPrice: createGroupTxSimulateInfo?.gasPrice || '5000000000',
payer: ACCOUNT_ADDRESS,
granter: '',
privateKey: ACCOUNT_PRIVATEKEY,
Expand All @@ -36,16 +37,14 @@ const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
extra: newExtra,
});

const simulateInfo = await updateGroupTx.simulate({
const updateGroupExtraSimulateInfo = await updateGroupTx.simulate({
denom: 'BNB',
});

expect(simulateInfo).not.toBeNull();

const updateGroupTxRes = await updateGroupTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
gasLimit: Number(updateGroupExtraSimulateInfo.gasLimit),
gasPrice: updateGroupExtraSimulateInfo.gasPrice,
granter: '',
payer: ACCOUNT_ADDRESS,
privateKey: ACCOUNT_PRIVATEKEY,
Expand All @@ -56,13 +55,13 @@ const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
groupName: GROUP_NAME,
operator: ACCOUNT_ADDRESS,
});
const simulateInfo = await deleteGroupTx.simulate({
const deleteGroupTxSimulateInfo = await deleteGroupTx.simulate({
denom: 'BNB',
});
const deleteGroupTxRes = await deleteGroupTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo.gasLimit),
gasPrice: simulateInfo.gasPrice,
gasLimit: Number(deleteGroupTxSimulateInfo.gasLimit),
gasPrice: deleteGroupTxSimulateInfo.gasPrice,
granter: '',
payer: ACCOUNT_ADDRESS,
privateKey: ACCOUNT_PRIVATEKEY,
Expand Down
5 changes: 3 additions & 2 deletions examples/nodejs/query.js → examples/nodejs/cases/query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { client, selectSp, generateString } = require('./client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
require('dotenv').config();
const { client, selectSp, generateString } = require('../client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('../env');

(async () => {
const spInfo = await selectSp();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const mimeTypes = require('mime-types');
const { getCheckSums } = require('@bnb-chain/greenfiled-file-handle');
const { client, selectSp, generateString } = require('./client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
const { client, selectSp, generateString } = require('../client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('../env');

const filePath = './CHANGELOG.md';
const bucketName = generateString(10);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { client, selectSp, generateString } = require('./client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
require('dotenv').config();
const { client, selectSp, generateString } = require('../client');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('../env');

(async () => {
const transferTx = await client.account.transfer({
Expand Down
11 changes: 5 additions & 6 deletions examples/nodejs/client.js → examples/nodejs/client/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { Client } = require('@bnb-chain/greenfield-js-sdk');
const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');

const client = Client.create('https://gnfd-testnet-fullnode-tendermint-ap.bnbchain.org', '5600');
const client = Client.create(
process.env.NEXT_PUBLIC_GREENFIELD_RPC_URL,
process.env.NEXT_PUBLIC_GREEN_CHAIN_ID,
);

const getSps = async () => {
const sps = await client.sp.getStorageProviders();
const finalSps = (sps ?? []).filter((v) => v.endpoint.includes('https'));
const finalSps = (sps ?? []).filter((v) => v.endpoint.includes('nodereal'));

return finalSps;
};
Expand Down Expand Up @@ -56,8 +57,6 @@ const generateString = (length) => {

module.exports = {
client,
ACCOUNT_ADDRESS,
ACCOUNT_PRIVATEKEY,
selectSp,
generateString,
};
7 changes: 0 additions & 7 deletions examples/nodejs/env.js

This file was deleted.

7 changes: 7 additions & 0 deletions examples/nodejs/env/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const ACCOUNT_ADDRESS = process.env.ACCOUNT_ADDRESS;
const ACCOUNT_PRIVATEKEY = process.env.ACCOUNT_PRIVATEKEY;

module.exports = {
ACCOUNT_ADDRESS,
ACCOUNT_PRIVATEKEY,
};
1 change: 1 addition & 0 deletions examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"@bnb-chain/greenfield-js-sdk": "workspace:*",
"@bnb-chain/greenfiled-file-handle": "workspace:*",
"dotenv": "^16.0.3",
"mime-types": "^2.1.35"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit df8d892

Please sign in to comment.