Skip to content

Commit

Permalink
chore(example): Create Object on Nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Oct 20, 2023
1 parent 7094b6e commit 9322b95
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 135 deletions.
3 changes: 2 additions & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {},
"dependencies": {
"@bnb-chain/greenfield-js-sdk": "workspace:*",
"@bnb-chain/greenfiled-file-handle": "workspace:*"
"@bnb-chain/greenfiled-file-handle": "workspace:*",
"mime-types": "^2.1.35"
},
"devDependencies": {
"cross-env": "^7.0.3"
Expand Down
62 changes: 55 additions & 7 deletions examples/nodejs/storage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
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 filePath = './CHANGELOG.md';
const bucketName = generateString(10);
const objectName = generateString(10);
const fileBuffer = fs.readFileSync(filePath);
const extname = path.extname(filePath);
const fileType = mimeTypes.lookup(extname);

(async () => {
const bucketName = generateString(10);
const spInfo = await selectSp();

// create bucket example:
const createBucketTx = await client.bucket.createBucket(
{
bucketName: bucketName,
Expand All @@ -21,20 +33,56 @@ const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env');
},
);

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

console.log('createBucketTxSimulateInfo', createBucketTxSimulateInfo);

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

console.log('simulateInfo', simulateInfo);
console.log('create bucket success', createBucketTxRes);

// create object example:
const hashResult = await getCheckSums(fileBuffer);
const { contentLength, expectCheckSums } = hashResult;

const createObjectTx = await client.object.createObject(
{
bucketName: bucketName,
objectName: objectName,
creator: ACCOUNT_ADDRESS,
visibility: 'VISIBILITY_TYPE_PRIVATE',
fileType: fileType,
redundancyType: 'REDUNDANCY_EC_TYPE',
contentLength,
expectCheckSums: JSON.parse(expectCheckSums),
},
{
type: 'ECDSA',
privateKey: ACCOUNT_PRIVATEKEY,
},
);

const createObjectTxSimulateInfo = await createObjectTx.simulate({
denom: 'BNB',
});

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

console.log('res', res);
console.log('create object success', createObjectTxRes);
})();
143 changes: 16 additions & 127 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9322b95

Please sign in to comment.