From 6d94a485dd3c415018ab2d9a0124978804937ca5 Mon Sep 17 00:00:00 2001 From: rrr523 Date: Fri, 8 Sep 2023 15:08:02 +0800 Subject: [PATCH] chore: Nodejs example update --- examples/nodejs/README.md | 2 +- examples/nodejs/client.js | 65 ++++++++++++++++++++++++++++++++++++++ examples/nodejs/index.js | 43 ------------------------- examples/nodejs/storage.js | 45 ++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 44 deletions(-) create mode 100644 examples/nodejs/client.js delete mode 100644 examples/nodejs/index.js create mode 100644 examples/nodejs/storage.js diff --git a/examples/nodejs/README.md b/examples/nodejs/README.md index af6b289e..6cad9c0f 100644 --- a/examples/nodejs/README.md +++ b/examples/nodejs/README.md @@ -1,3 +1,3 @@ # GreenField JS SDK Node.js -> Only supprt query up to now. +* [storage](./storage.js) diff --git a/examples/nodejs/client.js b/examples/nodejs/client.js new file mode 100644 index 00000000..af7da41d --- /dev/null +++ b/examples/nodejs/client.js @@ -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, +}; diff --git a/examples/nodejs/index.js b/examples/nodejs/index.js deleted file mode 100644 index 32390f6f..00000000 --- a/examples/nodejs/index.js +++ /dev/null @@ -1,43 +0,0 @@ -const { Client } = require('@bnb-chain/greenfield-js-sdk'); -// const { getCheckSums } = require('@bnb-chain/greenfiled-file-handle/files'); -// const fs = require('fs'); - -// const client = Client.create('https://gnfd-dev.qa.bnbchain.world', '8981'); - -const client = Client.create('https://gnfd.qa.bnbchain.world', '9000'); - -(async () => { - const createBucketTx = await client.bucket.createBucket({ - bucketName: 'foo', - creator: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae', - visibility: 'VISIBILITY_TYPE_PUBLIC_READ', - chargedReadQuota: '0', - spInfo: { - primarySpAddress: '0x66d06FFe266B46C6F0730cC9Ec2fc5B811cdA085', - }, - signType: 'authTypeV1', - privateKey: '0x6547492644d0136f76ef65e3bd04a77d079ed38028f747700c6c6063564d7032', - // signType: 'offChainAuth', - // domain: window.location.origin, - // seedString: offChainData.seedString, - }); - - 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: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae', - granter: '', - privateKey: '0x6547492644d0136f76ef65e3bd04a77d079ed38028f747700c6c6063564d7032', - }); - - if (res.code === 0) { - console.log('success'); - } -})(); diff --git a/examples/nodejs/storage.js b/examples/nodejs/storage.js new file mode 100644 index 00000000..ec2e88e8 --- /dev/null +++ b/examples/nodejs/storage.js @@ -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); +})();