diff --git a/examples/nodejs/package.json b/examples/nodejs/package.json index 7f636cdf..4a00480a 100644 --- a/examples/nodejs/package.json +++ b/examples/nodejs/package.json @@ -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" diff --git a/examples/nodejs/storage.js b/examples/nodejs/storage.js index 3204f195..25d39fcf 100644 --- a/examples/nodejs/storage.js +++ b/examples/nodejs/storage.js @@ -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, @@ -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); })(); diff --git a/examples/nodejs/transfer.js b/examples/nodejs/transfer.js new file mode 100644 index 00000000..35ac4207 --- /dev/null +++ b/examples/nodejs/transfer.js @@ -0,0 +1,32 @@ +const { client, selectSp, generateString } = require('./client'); +const { ACCOUNT_ADDRESS, ACCOUNT_PRIVATEKEY } = require('./env'); + +(async () => { + const transferTx = await client.account.transfer({ + fromAddress: ACCOUNT_ADDRESS, + toAddress: '0x0000000000000000000000000000000000000001', + amount: [ + { + denom: 'BNB', + amount: '1', + }, + ], + }); + + const simulateInfo = await transferTx.simulate({ + denom: 'BNB', + }); + + console.log('simulateInfo', simulateInfo); + + const res = await transferTx.broadcast({ + denom: 'BNB', + gasLimit: Number(simulateInfo.gasLimit), + gasPrice: simulateInfo.gasPrice, + payer: ACCOUNT_ADDRESS, + granter: '', + privateKey: ACCOUNT_PRIVATEKEY, + }); + + console.log('res', res); +})(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c48d5dcf..c00df824 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -241,6 +241,9 @@ importers: '@bnb-chain/greenfiled-file-handle': specifier: workspace:* version: link:../../packages/file-handle + mime-types: + specifier: ^2.1.35 + version: 2.1.35 devDependencies: cross-env: specifier: ^7.0.3 @@ -387,7 +390,7 @@ importers: version: 29.5.0 '@rollup/plugin-typescript': specifier: ^11.1.3 - version: 11.1.3(tslib@2.5.2)(typescript@4.9.5) + version: 11.1.3(rollup@2.79.1)(tslib@2.5.2)(typescript@4.9.5) '@types/jest': specifier: ^29.5.1 version: 29.5.1 @@ -414,7 +417,7 @@ importers: version: 0.4.11 jest: specifier: ^29.5.0 - version: 29.5.0(@types/node@20.6.0)(ts-node@10.9.1) + version: 29.5.0(@types/node@18.16.18)(ts-node@10.9.1) mime: specifier: ^3.0.0 version: 3.0.0 @@ -423,7 +426,7 @@ importers: version: 29.1.0(@babel/core@7.22.15)(jest@29.5.0)(typescript@4.9.5) ts-node: specifier: ^10.9.1 - version: 10.9.1(@types/node@20.6.0)(typescript@4.9.5) + version: 10.9.1(@types/node@18.16.18)(typescript@4.9.5) tslib: specifier: ^2.5.0 version: 2.5.2 @@ -657,7 +660,7 @@ packages: '@babel/traverse': 7.22.15 '@babel/types': 7.22.15 convert-source-map: 1.9.0 - debug: 4.3.4 + debug: 4.3.4(supports-color@8.1.1) gensync: 1.0.0-beta.2 json5: 2.2.3 semver: 6.3.1 @@ -2042,7 +2045,7 @@ packages: '@babel/helper-split-export-declaration': 7.22.6 '@babel/parser': 7.22.15 '@babel/types': 7.22.15 - debug: 4.3.4 + debug: 4.3.4(supports-color@8.1.1) globals: 11.12.0 transitivePeerDependencies: - supports-color @@ -4981,25 +4984,6 @@ packages: typescript: 4.9.5 dev: true - /@rollup/plugin-typescript@11.1.3(tslib@2.5.2)(typescript@4.9.5): - resolution: {integrity: sha512-8o6cNgN44kQBcpsUJTbTXMTtb87oR1O0zgP3Dxm71hrNgparap3VujgofEilTYJo+ivf2ke6uy3/E5QEaiRlDA==} - engines: {node: '>=14.0.0'} - peerDependencies: - rollup: ^2.14.0||^3.0.0 - tslib: '*' - typescript: '>=3.7.0' - peerDependenciesMeta: - rollup: - optional: true - tslib: - optional: true - dependencies: - '@rollup/pluginutils': 5.0.2 - resolve: 1.22.2 - tslib: 2.5.2 - typescript: 4.9.5 - dev: true - /@rollup/pluginutils@3.1.0(rollup@2.79.1): resolution: {integrity: sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==} engines: {node: '>= 8.0.0'} @@ -5012,20 +4996,6 @@ packages: rollup: 2.79.1 dev: true - /@rollup/pluginutils@5.0.2: - resolution: {integrity: sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==} - engines: {node: '>=14.0.0'} - peerDependencies: - rollup: ^1.20.0||^2.0.0||^3.0.0 - peerDependenciesMeta: - rollup: - optional: true - dependencies: - '@types/estree': 1.0.1 - estree-walker: 2.0.2 - picomatch: 2.3.1 - dev: true - /@rollup/pluginutils@5.0.2(rollup@2.79.1): resolution: {integrity: sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==} engines: {node: '>=14.0.0'} @@ -5833,6 +5803,7 @@ packages: /@types/node@20.6.0: resolution: {integrity: sha512-najjVq5KN2vsH2U/xyh2opaSEz6cZMR2SetLIlxlj08nOcmPOemJmUK2o4kUzfLqfrWE0PIrNeE16XhYDd3nqg==} + dev: false /@types/normalize-package-data@2.4.1: resolution: {integrity: sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==} @@ -8822,17 +8793,6 @@ packages: dependencies: ms: 2.1.3 - /debug@4.3.4: - resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} - engines: {node: '>=6.0'} - peerDependencies: - supports-color: '*' - peerDependenciesMeta: - supports-color: - optional: true - dependencies: - ms: 2.1.2 - /debug@4.3.4(supports-color@8.1.1): resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} engines: {node: '>=6.0'} @@ -11850,7 +11810,7 @@ packages: resolution: {integrity: sha512-n3s8EwkdFIJCG3BPKBYvskgXGoy88ARzvegkitk60NxRdwltLOTaH7CUiMRXvwYorl0Q712iEjcWB+fK/MrWVw==} engines: {node: '>=10'} dependencies: - debug: 4.3.4 + debug: 4.3.4(supports-color@8.1.1) istanbul-lib-coverage: 3.2.0 source-map: 0.6.1 transitivePeerDependencies: @@ -11932,7 +11892,7 @@ packages: - supports-color dev: true - /jest-cli@29.5.0(@types/node@20.6.0)(ts-node@10.9.1): + /jest-cli@29.5.0(@types/node@18.16.18)(ts-node@10.9.1): resolution: {integrity: sha512-L1KcP1l4HtfwdxXNFCL5bmUbLQiKrakMUriBEcc1Vfz6gx31ORKdreuWvmQVBit+1ss9NNR3yxjwfwzZNdQXJw==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -11949,7 +11909,7 @@ packages: exit: 0.1.2 graceful-fs: 4.2.11 import-local: 3.1.0 - jest-config: 29.5.0(@types/node@20.6.0)(ts-node@10.9.1) + jest-config: 29.5.0(@types/node@18.16.18)(ts-node@10.9.1) jest-util: 29.5.0 jest-validate: 29.5.0 prompts: 2.4.2 @@ -11995,47 +11955,7 @@ packages: pretty-format: 29.5.0 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.9.1(@types/node@20.6.0)(typescript@4.9.5) - transitivePeerDependencies: - - supports-color - dev: true - - /jest-config@29.5.0(@types/node@20.6.0)(ts-node@10.9.1): - resolution: {integrity: sha512-kvDUKBnNJPNBmFFOhDbm59iu1Fii1Q6SxyhXfvylq3UTHbg6o7j/g8k2dZyXWLvfdKB1vAPxNZnMgtKJcmu3kA==} - engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} - peerDependencies: - '@types/node': '*' - ts-node: '>=9.0.0' - peerDependenciesMeta: - '@types/node': - optional: true - ts-node: - optional: true - dependencies: - '@babel/core': 7.22.15 - '@jest/test-sequencer': 29.5.0 - '@jest/types': 29.5.0 - '@types/node': 20.6.0 - babel-jest: 29.5.0(@babel/core@7.22.15) - chalk: 4.1.2 - ci-info: 3.8.0 - deepmerge: 4.3.1 - glob: 7.2.3 - graceful-fs: 4.2.11 - jest-circus: 29.5.0 - jest-environment-node: 29.5.0 - jest-get-type: 29.4.3 - jest-regex-util: 29.4.3 - jest-resolve: 29.5.0 - jest-runner: 29.5.0 - jest-util: 29.5.0 - jest-validate: 29.5.0 - micromatch: 4.0.5 - parse-json: 5.2.0 - pretty-format: 29.5.0 - slash: 3.0.0 - strip-json-comments: 3.1.1 - ts-node: 10.9.1(@types/node@20.6.0)(typescript@4.9.5) + ts-node: 10.9.1(@types/node@18.16.18)(typescript@4.9.5) transitivePeerDependencies: - supports-color dev: true @@ -12341,7 +12261,7 @@ packages: merge-stream: 2.0.0 supports-color: 8.1.1 - /jest@29.5.0(@types/node@20.6.0)(ts-node@10.9.1): + /jest@29.5.0(@types/node@18.16.18)(ts-node@10.9.1): resolution: {integrity: sha512-juMg3he2uru1QoXX078zTa7pO85QyB9xajZc6bU+d9yEGwrKX6+vGmJQ3UdVZsvTEUARIdObzH68QItim6OSSQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -12354,7 +12274,7 @@ packages: '@jest/core': 29.5.0(ts-node@10.9.1) '@jest/types': 29.5.0 import-local: 3.1.0 - jest-cli: 29.5.0(@types/node@20.6.0)(ts-node@10.9.1) + jest-cli: 29.5.0(@types/node@18.16.18)(ts-node@10.9.1) transitivePeerDependencies: - '@types/node' - supports-color @@ -16690,7 +16610,7 @@ packages: '@babel/core': 7.22.15 bs-logger: 0.2.6 fast-json-stable-stringify: 2.1.0 - jest: 29.5.0(@types/node@20.6.0)(ts-node@10.9.1) + jest: 29.5.0(@types/node@18.16.18)(ts-node@10.9.1) jest-util: 29.5.0 json5: 2.2.3 lodash.memoize: 4.1.2 @@ -16731,37 +16651,6 @@ packages: yn: 3.1.1 dev: true - /ts-node@10.9.1(@types/node@20.6.0)(typescript@4.9.5): - resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==} - hasBin: true - peerDependencies: - '@swc/core': '>=1.2.50' - '@swc/wasm': '>=1.2.50' - '@types/node': '*' - typescript: '>=2.7' - peerDependenciesMeta: - '@swc/core': - optional: true - '@swc/wasm': - optional: true - dependencies: - '@cspotcode/source-map-support': 0.8.1 - '@tsconfig/node10': 1.0.9 - '@tsconfig/node12': 1.0.11 - '@tsconfig/node14': 1.0.3 - '@tsconfig/node16': 1.0.3 - '@types/node': 20.6.0 - acorn: 8.10.0 - acorn-walk: 8.2.0 - arg: 4.1.3 - create-require: 1.1.1 - diff: 4.0.2 - make-error: 1.3.6 - typescript: 4.9.5 - v8-compile-cache-lib: 3.0.1 - yn: 3.1.1 - dev: true - /tsconfig-paths@3.14.2: resolution: {integrity: sha512-o/9iXgCYc5L/JxCHPe3Hvh8Q/2xm5Z+p18PESBU6Ff33695QnCHBEjcytY2q19ua7Mbl/DavtBOLq+oG0RCL+g==} dependencies: