Skip to content

Commit

Permalink
chore: Nodejs example update
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Sep 8, 2023
1 parent c502902 commit 6d94a48
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/nodejs/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# GreenField JS SDK Node.js

> Only supprt query up to now.
* [storage](./storage.js)
65 changes: 65 additions & 0 deletions examples/nodejs/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { Client } = require('@bnb-chain/greenfield-js-sdk');

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

const ACCOUNT_ADDRESS = '';
const ACCOUNT_PRIVATEKEY = '';

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

return finalSps;
};

const getAllSps = async () => {
const sps = await getSps();

return sps.map((sp) => {
return {
address: sp.operatorAddress,
endpoint: sp.endpoint,
name: sp.description?.moniker,
};
});
};

const selectSp = async () => {
const finalSps = await getSps();

const selectIndex = Math.floor(Math.random() * finalSps.length);

const secondarySpAddresses = [
...finalSps.slice(0, selectIndex),
...finalSps.slice(selectIndex + 1),
].map((item) => item.operatorAddress);
const selectSpInfo = {
id: finalSps[selectIndex].id,
endpoint: finalSps[selectIndex].endpoint,
primarySpAddress: finalSps[selectIndex]?.operatorAddress,
sealAddress: finalSps[selectIndex].sealAddress,
secondarySpAddresses,
};

return selectSpInfo;
};

const generateString = (length) => {
const characters = 'abcdefghijklmnopqrstuvwxyz';

let result = '';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}

return result;
};

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

This file was deleted.

45 changes: 45 additions & 0 deletions examples/nodejs/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const {
client,
selectSp,
ACCOUNT_ADDRESS,
ACCOUNT_PRIVATEKEY,
generateString,
} = require('./client');

(async () => {
const bucketName = generateString(10);
const spInfo = await selectSp();
const createBucketTx = await client.bucket.createBucket(
{
bucketName: bucketName,
creator: ACCOUNT_ADDRESS,
visibility: 'VISIBILITY_TYPE_PUBLIC_READ',
chargedReadQuota: '0',
spInfo: {
primarySpAddress: spInfo.primarySpAddress,
},
paymentAddress: ACCOUNT_ADDRESS,
},
{
type: 'ECDSA',
privateKey: ACCOUNT_PRIVATEKEY,
},
);

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

console.log('simulateInfo', simulateInfo);

const res = await createBucketTx.broadcast({
denom: 'BNB',
gasLimit: Number(simulateInfo?.gasLimit),
gasPrice: simulateInfo?.gasPrice || '5000000000',
payer: ACCOUNT_ADDRESS,
granter: '',
privateKey: ACCOUNT_PRIVATEKEY,
});

console.log('res', res);
})();

0 comments on commit 6d94a48

Please sign in to comment.