@upcoming/bee-js 0.15.1 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/bee.js CHANGED
@@ -1159,24 +1159,24 @@ class Bee {
1159
1159
  }
1160
1160
  return stamp;
1161
1161
  }
1162
- async buyStorage(gigabytes, duration, options, requestOptions) {
1162
+ async buyStorage(size, duration, options, requestOptions) {
1163
1163
  const chainState = await this.getChainState(requestOptions);
1164
1164
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1165
- const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1165
+ const depth = (0, stamps_1.getDepthForSize)(size);
1166
1166
  if (options) {
1167
1167
  options = (0, type_1.preparePostageBatchOptions)(options);
1168
1168
  }
1169
1169
  return this.createPostageBatch(amount, depth, options, requestOptions);
1170
1170
  }
1171
- async getStorageCost(gigabytes, duration, options) {
1171
+ async getStorageCost(size, duration, options) {
1172
1172
  const chainState = await this.getChainState(options);
1173
1173
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1174
- const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1174
+ const depth = (0, stamps_1.getDepthForSize)(size);
1175
1175
  return (0, stamps_1.getStampCost)(depth, amount);
1176
1176
  }
1177
- async extendStorageSize(postageBatchId, gigabytes, options) {
1177
+ async extendStorageSize(postageBatchId, size, options) {
1178
1178
  const batch = await this.getPostageBatch(postageBatchId, options);
1179
- const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1179
+ const depth = (0, stamps_1.getDepthForSize)(size);
1180
1180
  const delta = depth - batch.depth;
1181
1181
  if (delta <= 0) {
1182
1182
  throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
@@ -1190,18 +1190,18 @@ class Bee {
1190
1190
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1191
1191
  return this.topUpBatch(batch.batchID, amount, options);
1192
1192
  }
1193
- async getExtensionCost(postageBatchId, gigabytes, duration, options) {
1193
+ async getExtensionCost(postageBatchId, size, duration, options) {
1194
1194
  const batch = await this.getPostageBatch(postageBatchId, options);
1195
1195
  const chainState = await this.getChainState(options);
1196
1196
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1197
- const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1197
+ const depth = (0, stamps_1.getDepthForSize)(size);
1198
1198
  const currentValue = (0, stamps_1.getStampCost)(batch.depth, batch.amount);
1199
1199
  const newValue = (0, stamps_1.getStampCost)(depth, amount);
1200
1200
  return newValue.minus(currentValue);
1201
1201
  }
1202
- async getSizeExtensionCost(postageBatchId, gigabytes, options) {
1202
+ async getSizeExtensionCost(postageBatchId, size, options) {
1203
1203
  const batch = await this.getPostageBatch(postageBatchId, options);
1204
- const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1204
+ const depth = (0, stamps_1.getDepthForSize)(size);
1205
1205
  const delta = depth - batch.depth;
1206
1206
  if (delta <= 0) {
1207
1207
  throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
package/dist/cjs/index.js CHANGED
@@ -26,7 +26,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.Stamper = exports.BeeDev = exports.Bee = exports.Utils = exports.Duration = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
29
+ exports.Stamper = exports.BeeDev = exports.Bee = exports.Size = exports.Utils = exports.Duration = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
30
30
  const bee_1 = require("./bee");
31
31
  Object.defineProperty(exports, "Bee", { enumerable: true, get: function () { return bee_1.Bee; } });
32
32
  const bee_dev_1 = require("./bee-dev");
@@ -48,5 +48,7 @@ var duration_1 = require("./utils/duration");
48
48
  Object.defineProperty(exports, "Duration", { enumerable: true, get: function () { return duration_1.Duration; } });
49
49
  __exportStar(require("./utils/error"), exports);
50
50
  exports.Utils = __importStar(require("./utils/expose"));
51
+ var size_1 = require("./utils/size");
52
+ Object.defineProperty(exports, "Size", { enumerable: true, get: function () { return size_1.Size; } });
51
53
  __exportStar(require("./utils/tokens"), exports);
52
54
  __exportStar(require("./utils/typed-bytes"), exports);
@@ -4,6 +4,7 @@ exports.diluteBatch = exports.topUpBatch = exports.createPostageBatch = exports.
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const duration_1 = require("../../utils/duration");
6
6
  const http_1 = require("../../utils/http");
7
+ const size_1 = require("../../utils/size");
7
8
  const stamps_1 = require("../../utils/stamps");
8
9
  const type_1 = require("../../utils/type");
9
10
  const typed_bytes_1 = require("../../utils/typed-bytes");
@@ -56,9 +57,10 @@ async function getAllPostageBatches(requestOptions) {
56
57
  blockNumber: cafe_utility_1.Types.asNumber(x.blockNumber, { name: 'blockNumber' }),
57
58
  immutableFlag: cafe_utility_1.Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
58
59
  usage,
59
- size: (0, stamps_1.getStampEffectiveBytes)(depth),
60
- remainingSize: Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage)),
61
- theoreticalSize: (0, stamps_1.getStampTheoreticalBytes)(depth),
60
+ usageText: `${Math.round(usage * 100)}%`,
61
+ size: size_1.Size.fromBytes((0, stamps_1.getStampEffectiveBytes)(depth)),
62
+ remainingSize: size_1.Size.fromBytes(Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage))),
63
+ theoreticalSize: size_1.Size.fromBytes((0, stamps_1.getStampTheoreticalBytes)(depth)),
62
64
  duration,
63
65
  };
64
66
  });
@@ -88,9 +90,10 @@ async function getPostageBatch(requestOptions, postageBatchId) {
88
90
  blockNumber: cafe_utility_1.Types.asNumber(body.blockNumber, { name: 'blockNumber' }),
89
91
  immutableFlag: cafe_utility_1.Types.asBoolean(body.immutableFlag, { name: 'immutableFlag' }),
90
92
  usage,
91
- size: (0, stamps_1.getStampEffectiveBytes)(depth),
92
- remainingSize: Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage)),
93
- theoreticalSize: (0, stamps_1.getStampTheoreticalBytes)(depth),
93
+ usageText: `${Math.round(usage * 100)}%`,
94
+ size: size_1.Size.fromBytes((0, stamps_1.getStampEffectiveBytes)(depth)),
95
+ remainingSize: size_1.Size.fromBytes(Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage))),
96
+ theoreticalSize: size_1.Size.fromBytes((0, stamps_1.getStampTheoreticalBytes)(depth)),
94
97
  duration,
95
98
  };
96
99
  }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Size = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ /**
6
+ * Represents a size in bytes.
7
+ *
8
+ * Uses 1000 instead of 1024 for converting between units.
9
+ * This is to stay consistent with the Swarm papers
10
+ * on theoretical and effective storage capacity.
11
+ */
12
+ class Size {
13
+ constructor(bytes) {
14
+ this.bytes = Math.ceil(bytes);
15
+ if (bytes < 0) {
16
+ throw Error('Size must be at least 0');
17
+ }
18
+ }
19
+ static fromBytes(bytes) {
20
+ return new Size(bytes);
21
+ }
22
+ static fromGigabytes(gigabytes) {
23
+ return new Size(gigabytes * 1000 * 1000 * 1000);
24
+ }
25
+ toBytes() {
26
+ return this.bytes;
27
+ }
28
+ toGigabytes() {
29
+ return this.bytes / 1000 / 1000 / 1000;
30
+ }
31
+ toFormattedString() {
32
+ return cafe_utility_1.Numbers.convertBytes(this.bytes, 1000);
33
+ }
34
+ }
35
+ exports.Size = Size;
@@ -115,14 +115,14 @@ function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
115
115
  }
116
116
  exports.getAmountForDuration = getAmountForDuration;
117
117
  /**
118
- * Utility function that calculates the depth required for a postage batch to achieve the specified effective size in gigabytes.
118
+ * Utility function that calculates the depth required for a postage batch to achieve the specified effective size
119
119
  *
120
- * @param gigabytes
120
+ * @param size The effective size of the postage batch
121
121
  * @returns
122
122
  */
123
- function getDepthForSize(gigabytes) {
124
- for (const [depth, size] of effectiveSizeBreakpoints) {
125
- if (gigabytes <= size) {
123
+ function getDepthForSize(size) {
124
+ for (const [depth, sizeBreakpoint] of effectiveSizeBreakpoints) {
125
+ if (size.toGigabytes() <= sizeBreakpoint) {
126
126
  return depth;
127
127
  }
128
128
  }