-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})(); |