diff --git a/README.md b/README.md index 5c18e81..2faeab6 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ Reserve space for certain address in Filestorage in bytes. filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace, privateKey); ``` -Create directory using external signing (Metamask etc): +Reserve space using external signing (Metamask etc): ```javascript filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace); @@ -274,6 +274,74 @@ filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace); | `String` reservedSpace | Reserved space in bytes | | `String` privateKey | _(optional)_ sChain owner private key, to sign transactions | +#### Get reserved space + +Get information about reserved space for account in bytes. + +```javascript +filestorage.getReservedSpace(address); +``` + +##### Parameters + +| Parameter | Description | +| ------------------------- | ----------------------------------------------------------- | +| `String` address | Account address | + +##### Returns + +| Parameter | Description | +| ---------------------- | ------------------------------------------------ | +| `Number` reservedSpace | Reserved space in bytes | + +#### Get occupied space + +Get information about occupied (actually used) space for account in bytes. + +```javascript +filestorage.getOccupiedSpace(address); +``` + +##### Parameters + +| Parameter | Description | +| ------------------------- | ----------------------------------------------------------- | +| `String` address | Account address | + +##### Returns + +| Parameter | Description | +| ---------------------- | ------------------------------------------------ | +| `Number` occupiedSpace | Occupied space in bytes | + +#### Get total space in Filestorage + +Get information about total space in Filestorage in bytes. + +```javascript +filestorage.getTotalSpace(); +``` + +##### Returns + +| Parameter | Description | +| ---------------------- | ------------------------------------------------ | +| `Number` space | Total space in Filestorage in bytes | + +#### Get total reserved space + +Get information about total reserved space (sum of all reserved spaces) in bytes. + +```javascript +filestorage.getTotalReservedSpace(); +``` + +##### Returns + +| Parameter | Description | +| ---------------------- | ------------------------------------------------ | +| `Number` reservedSpace | Total reserved space in bytes | + ## Development ### Install diff --git a/package-lock.json b/package-lock.json index acdc0d0..d33af0b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@skalenetwork/filestorage.js", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -736,9 +736,9 @@ "integrity": "sha512-9NET910DNaIPngYnLLPeg+Ogzqsi9uM4mSboU5y6p8S5DzMTVEsJZrawi+BoDNUVBa2DhJqQYUFvMDfgU062LQ==" }, "@skalenetwork/filestorage": { - "version": "1.0.0-develop.2", - "resolved": "https://registry.npmjs.org/@skalenetwork/filestorage/-/filestorage-1.0.0-develop.2.tgz", - "integrity": "sha512-3llZ0Gtj5DgMY/rFYjuevdSkcZh7lYPgd9AGGDE5y65Kab5PR6J9TPMrIs2vyAIwuJacmLgRMnKbc1KP0pBLUg==", + "version": "1.0.0-develop.5", + "resolved": "https://registry.npmjs.org/@skalenetwork/filestorage/-/filestorage-1.0.0-develop.5.tgz", + "integrity": "sha512-stfUvoHWO8MCjp+wytPI8i9HeOIUDvkvqwGzsopCkOflGS9Ty4BBd8xyJaLlFwAYz6I9/e9B7y4aq3lw6i0AJQ==", "requires": { "@openzeppelin/upgrades-core": "^1.5.1" } @@ -6989,9 +6989,9 @@ "dev": true }, "node-gyp-build": { - "version": "4.2.3", - "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.2.3.tgz", - "integrity": "sha512-MN6ZpzmfNCRM+3t57PTJHgHyw/h4OWnZ6mR8P5j/uZtqQr46RRuDE/P+g3n0YR/AiYXeWixZZzaip77gdICfRg==" + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.3.0.tgz", + "integrity": "sha512-iWjXZvmboq0ja1pUGULQBexmxq8CV4xBhX7VDOTbL7ZR4FOowwY/VOtRxBN/yKxmdGoIp4j5ysNT4u3S2pDQ3Q==" }, "node-libs-browser": { "version": "2.2.1", diff --git a/package.json b/package.json index f14048e..fe9b25e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@skalenetwork/filestorage.js", - "version": "1.0.0", + "version": "1.0.1", "description": "SKALE Filestorage client tools", "keywords": [ "SKALE", @@ -29,7 +29,7 @@ "version": "node -e \"console.log(require('./package.json').version);\"" }, "dependencies": { - "@skalenetwork/filestorage": "1.0.0-develop.2", + "@skalenetwork/filestorage": "1.0.0-develop.5", "streamsaver": "^1.0.1", "web3": "1.2.6" }, diff --git a/scripts/generate_config.sh b/scripts/generate_config.sh index a752064..19026ba 100644 --- a/scripts/generate_config.sh +++ b/scripts/generate_config.sh @@ -1,4 +1,4 @@ -VERSION=$(npm view @skalenetwork/filestorage version) +VERSION=$(node -p "require('@skalenetwork/filestorage/package.json').version") ARTIFACTS_PATH=$PWD/test/data/artifacts.json curl -LJO https://github.com/skalenetwork/filestorage/releases/download/$VERSION/artifacts.json mv artifacts.json $ARTIFACTS_PATH diff --git a/src/FilestorageContract.js b/src/FilestorageContract.js index dda8a72..5cff0b5 100644 --- a/src/FilestorageContract.js +++ b/src/FilestorageContract.js @@ -203,6 +203,54 @@ class FilestorageContract { let txData = this.contract.methods.reserveSpace(addressToReserve, reservedSpace); return await transactions.send(this.web3, ownerAddress, privateKey, txData, constants.STANDARD_GAS); } + + /** + * Javascript wrapper for solidity function getReservedSpace. Get information about reserved space for account + * + * @function getReservedSpace + * + * @param {string} address - Account address + * @returns {number} Reserved space in bytes + */ + async getReservedSpace(address) { + return await this.contract.methods.getReservedSpace(address).call(); + } + + /** + * Javascript wrapper for solidity function getOccupiedSpace. Get information about occupied space for account + * + * @function getOccupiedSpace + * + * @param {string} address - Account address + * @returns {string} Occupied space in bytes + */ + async getOccupiedSpace(address) { + return await this.contract.methods.getOccupiedSpace(address).call(); + } + + /** + * Javascript wrapper for solidity function getOccupiedSpace. Get information about total allocated space for + * Filestorage + * + * @function getTotalSpace + * + * @returns {string} Total space in Filestorage in bytes + */ + async getTotalSpace() { + return await this.contract.methods.getTotalStorageSpace().call(); + } + + /** + * Javascript wrapper for solidity function getOccupiedSpace. Get information about total reserved space in + * Filestorage + * + * @function getTotalReservedSpace + * + * @returns {string} Total reserved space in Filestorage in bytes + */ + async getTotalReservedSpace() { + return await this.contract.methods.getTotalReservedSpace().call(); + } } module.exports = FilestorageContract; diff --git a/src/index.js b/src/index.js index 54c82c0..7b4e503 100644 --- a/src/index.js +++ b/src/index.js @@ -208,6 +208,56 @@ class FilestorageClient { if (this.enableLogs) console.log('Space was allocated'); } + /** + * Get information about reserved space for account + * + * @function getReservedSpace + * + * @param {string} address - Account address + * @returns {number} Reserved space in bytes + */ + async getReservedSpace(address) { + let space = await this.contract.getReservedSpace(address); + return Number(space); + } + + /** + * Get information about occupied space for account + * + * @function getOccupiedSpace + * + * @param {string} address - Account address + * @returns {number} Occupied space in bytes + */ + async getOccupiedSpace(address) { + let space = await this.contract.getOccupiedSpace(address); + return Number(space); + } + + /** + * Get information about total allocated space for Filestorage + * + * @function getTotalSpace + * + * @returns {number} Total space in Filestorage in bytes + */ + async getTotalSpace() { + let space = await this.contract.getTotalSpace(); + return Number(space); + } + + /** + * Get information about total reserved space in Filestorage + * + * @function getTotalReservedSpace + * + * @returns {number} Total reserved space in Filestorage in bytes + */ + async getTotalReservedSpace() { + let space = await this.contract.getTotalReservedSpace(); + return Number(space); + } + async _downloadFile(storagePath, stream) { let ptrPosition = 0; let i = 0; diff --git a/test/index.test.js b/test/index.test.js index 3bd31fe..64d8209 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -443,4 +443,49 @@ describe('Test FilestorageClient', function () { }); }); }); + + describe('getters', function () { + it('should return total storage space', async function () { + let space = await filestorage.getTotalSpace(); + assert.isNumber(space); + assert(space > 0); + }); + + it('should return total reserved storage space', async function () { + let space = await filestorage.getTotalReservedSpace(); + assert.isNumber(space); + assert(space > 0); + }); + + it('should return reserved storage space for account', async function () { + let owner = testHelper.getAddress(process.env.SCHAIN_OWNER_PK); + await filestorage.reserveSpace( + owner, + testConstants.SPACE_TEST_ADDRESS, + 200, + process.env.SCHAIN_OWNER_PK + ); + let space = await filestorage.getReservedSpace(testConstants.SPACE_TEST_ADDRESS); + assert.isNumber(space); + assert(space === 200); + await filestorage.reserveSpace( + owner, + testConstants.SPACE_TEST_ADDRESS, + 300, + process.env.SCHAIN_OWNER_PK + ); + space = await filestorage.getReservedSpace(testConstants.SPACE_TEST_ADDRESS); + assert(space === 300); + }); + + it('should return total occupied storage space', async function () { + let initSpace = await filestorage.getOccupiedSpace(address); + assert.isNumber(initSpace); + let fileName = 'test_' + randomstring.generate(); + let data = Buffer.from(fileName); + await filestorage.uploadFile(address, fileName, data, privateKey); + let afterSpace = await filestorage.getOccupiedSpace(address); + assert(afterSpace - initSpace === data.length); + }); + }); });