@upcoming/bee-js 0.8.1 → 0.10.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
@@ -59,6 +59,7 @@ const data_1 = require("./utils/data");
59
59
  const error_1 = require("./utils/error");
60
60
  const file_1 = require("./utils/file");
61
61
  const resource_locator_1 = require("./utils/resource-locator");
62
+ const stamps_1 = require("./utils/stamps");
62
63
  const tokens_1 = require("./utils/tokens");
63
64
  const type_1 = require("./utils/type");
64
65
  const typed_bytes_1 = require("./utils/typed-bytes");
@@ -1158,14 +1159,60 @@ class Bee {
1158
1159
  }
1159
1160
  return stamp;
1160
1161
  }
1162
+ async buyStorage(gigabytes, duration, options, requestOptions) {
1163
+ const chainState = await this.getChainState(requestOptions);
1164
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1165
+ const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1166
+ if (options) {
1167
+ options = (0, type_1.preparePostageBatchOptions)(options);
1168
+ }
1169
+ return this.createPostageBatch(amount, depth, options, requestOptions);
1170
+ }
1171
+ async getStorageCost(gigabytes, duration, options) {
1172
+ const chainState = await this.getChainState(options);
1173
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1174
+ const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1175
+ return (0, stamps_1.getStampCost)(depth, amount);
1176
+ }
1177
+ async extendStorageSize(postageBatchId, gigabytes, options) {
1178
+ const batch = await this.getPostageBatch(postageBatchId, options);
1179
+ const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1180
+ const delta = depth - batch.depth;
1181
+ if (delta <= 0) {
1182
+ throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
1183
+ }
1184
+ await this.topUpBatch(batch.batchID, BigInt(batch.amount) * 2n ** BigInt(delta - 1), options);
1185
+ return this.diluteBatch(batch.batchID, depth, options);
1186
+ }
1187
+ async extendStorageDuration(postageBatchId, duration, options) {
1188
+ const batch = await this.getPostageBatch(postageBatchId, options);
1189
+ const chainState = await this.getChainState(options);
1190
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1191
+ return this.topUpBatch(batch.batchID, amount, options);
1192
+ }
1193
+ async getSizeExtensionCost(postageBatchId, gigabytes, options) {
1194
+ const batch = await this.getPostageBatch(postageBatchId, options);
1195
+ const depth = (0, stamps_1.getDepthForSize)(gigabytes);
1196
+ const delta = depth - batch.depth;
1197
+ if (delta <= 0) {
1198
+ throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
1199
+ }
1200
+ const currentPaid = (0, stamps_1.getStampCost)(batch.depth, batch.amount);
1201
+ const newPaid = (0, stamps_1.getStampCost)(depth, batch.amount);
1202
+ return newPaid.minus(currentPaid);
1203
+ }
1204
+ async getDurationExtensionCost(postageBatchId, duration, options) {
1205
+ const batch = await this.getPostageBatch(postageBatchId, options);
1206
+ const chainState = await this.getChainState(options);
1207
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1208
+ return (0, stamps_1.getStampCost)(batch.depth, amount);
1209
+ }
1161
1210
  /**
1162
1211
  * Topup a fresh amount of BZZ to given Postage Batch.
1163
1212
  *
1164
1213
  * For better understanding what each parameter means and what are the optimal values please see
1165
1214
  * [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive).
1166
1215
  *
1167
- * **WARNING: THIS CREATES TRANSACTIONS THAT SPENDS MONEY**
1168
- *
1169
1216
  * @param postageBatchId Batch ID
1170
1217
  * @param amount Amount to be added to the batch
1171
1218
  * @param options Request options
@@ -1185,8 +1232,6 @@ class Bee {
1185
1232
  * For better understanding what each parameter means and what are the optimal values please see
1186
1233
  * [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive).
1187
1234
  *
1188
- * **WARNING: THIS CREATES TRANSACTIONS THAT SPENDS MONEY**
1189
- *
1190
1235
  * @param postageBatchId Batch ID
1191
1236
  * @param depth Amount to be added to the batch
1192
1237
  * @param options Request options
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.BeeDev = exports.Bee = exports.Utils = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
29
+ 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;
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");
@@ -42,6 +42,8 @@ __exportStar(require("./types"), exports);
42
42
  var bytes_1 = require("./utils/bytes");
43
43
  Object.defineProperty(exports, "Bytes", { enumerable: true, get: function () { return bytes_1.Bytes; } });
44
44
  __exportStar(require("./utils/constants"), exports);
45
+ var duration_1 = require("./utils/duration");
46
+ Object.defineProperty(exports, "Duration", { enumerable: true, get: function () { return duration_1.Duration; } });
45
47
  __exportStar(require("./utils/error"), exports);
46
48
  exports.Utils = __importStar(require("./utils/expose"));
47
49
  __exportStar(require("./utils/tokens"), exports);
@@ -2,7 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.diluteBatch = exports.topUpBatch = exports.createPostageBatch = exports.getPostageBatchBuckets = exports.getPostageBatch = exports.getAllPostageBatches = exports.getGlobalPostageBatches = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
+ const duration_1 = require("../../utils/duration");
5
6
  const http_1 = require("../../utils/http");
7
+ const stamps_1 = require("../../utils/stamps");
6
8
  const type_1 = require("../../utils/type");
7
9
  const typed_bytes_1 = require("../../utils/typed-bytes");
8
10
  const STAMPS_ENDPOINT = 'stamps';
@@ -35,19 +37,30 @@ async function getAllPostageBatches(requestOptions) {
35
37
  });
36
38
  const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
37
39
  const stamps = cafe_utility_1.Types.asArray(body.stamps, { name: 'stamps' }).map(x => cafe_utility_1.Types.asObject(x, { name: 'stamp' }));
38
- return stamps.map(x => ({
39
- batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(x.batchID, { name: 'batchID' })),
40
- utilization: cafe_utility_1.Types.asNumber(x.utilization, { name: 'utilization' }),
41
- usable: cafe_utility_1.Types.asBoolean(x.usable, { name: 'usable' }),
42
- label: cafe_utility_1.Types.asEmptiableString(x.label, { name: 'label' }),
43
- depth: cafe_utility_1.Types.asNumber(x.depth, { name: 'depth' }),
44
- amount: (0, type_1.asNumberString)(x.amount, { name: 'amount' }),
45
- bucketDepth: cafe_utility_1.Types.asNumber(x.bucketDepth, { name: 'bucketDepth' }),
46
- blockNumber: cafe_utility_1.Types.asNumber(x.blockNumber, { name: 'blockNumber' }),
47
- immutableFlag: cafe_utility_1.Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
48
- exists: cafe_utility_1.Types.asBoolean(x.exists, { name: 'exists' }),
49
- batchTTL: cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' }),
50
- }));
40
+ return stamps.map(x => {
41
+ const utilization = cafe_utility_1.Types.asNumber(x.utilization, { name: 'utilization' });
42
+ const depth = cafe_utility_1.Types.asNumber(x.depth, { name: 'depth' });
43
+ const bucketDepth = cafe_utility_1.Types.asNumber(x.bucketDepth, { name: 'bucketDepth' });
44
+ const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
45
+ const batchTTL = cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' });
46
+ const duration = duration_1.Duration.fromSeconds(batchTTL);
47
+ return {
48
+ batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(x.batchID, { name: 'batchID' })),
49
+ utilization,
50
+ usable: cafe_utility_1.Types.asBoolean(x.usable, { name: 'usable' }),
51
+ label: cafe_utility_1.Types.asEmptiableString(x.label, { name: 'label' }),
52
+ depth,
53
+ amount: (0, type_1.asNumberString)(x.amount, { name: 'amount' }),
54
+ bucketDepth,
55
+ blockNumber: cafe_utility_1.Types.asNumber(x.blockNumber, { name: 'blockNumber' }),
56
+ immutableFlag: cafe_utility_1.Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
57
+ usage,
58
+ size: (0, stamps_1.getStampEffectiveBytes)(depth),
59
+ remainingSize: Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage)),
60
+ theoreticalSize: (0, stamps_1.getStampTheoreticalBytes)(depth),
61
+ duration,
62
+ };
63
+ });
51
64
  }
52
65
  exports.getAllPostageBatches = getAllPostageBatches;
53
66
  async function getPostageBatch(requestOptions, postageBatchId) {
@@ -57,18 +70,27 @@ async function getPostageBatch(requestOptions, postageBatchId) {
57
70
  responseType: 'json',
58
71
  });
59
72
  const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
73
+ const utilization = cafe_utility_1.Types.asNumber(body.utilization, { name: 'utilization' });
74
+ const depth = cafe_utility_1.Types.asNumber(body.depth, { name: 'depth' });
75
+ const bucketDepth = cafe_utility_1.Types.asNumber(body.bucketDepth, { name: 'bucketDepth' });
76
+ const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
77
+ const batchTTL = cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' });
78
+ const duration = duration_1.Duration.fromSeconds(batchTTL);
60
79
  return {
61
80
  batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(body.batchID, { name: 'batchID' })),
62
- utilization: cafe_utility_1.Types.asNumber(body.utilization, { name: 'utilization' }),
81
+ utilization,
63
82
  usable: cafe_utility_1.Types.asBoolean(body.usable, { name: 'usable' }),
64
83
  label: cafe_utility_1.Types.asEmptiableString(body.label, { name: 'label' }),
65
- depth: cafe_utility_1.Types.asNumber(body.depth, { name: 'depth' }),
84
+ depth,
66
85
  amount: (0, type_1.asNumberString)(body.amount, { name: 'amount' }),
67
- bucketDepth: cafe_utility_1.Types.asNumber(body.bucketDepth, { name: 'bucketDepth' }),
86
+ bucketDepth,
68
87
  blockNumber: cafe_utility_1.Types.asNumber(body.blockNumber, { name: 'blockNumber' }),
69
88
  immutableFlag: cafe_utility_1.Types.asBoolean(body.immutableFlag, { name: 'immutableFlag' }),
70
- exists: cafe_utility_1.Types.asBoolean(body.exists, { name: 'exists' }),
71
- batchTTL: cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' }),
89
+ usage,
90
+ size: (0, stamps_1.getStampEffectiveBytes)(depth),
91
+ remainingSize: Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage)),
92
+ theoreticalSize: (0, stamps_1.getStampTheoreticalBytes)(depth),
93
+ duration,
72
94
  };
73
95
  }
74
96
  exports.getPostageBatch = getPostageBatch;
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NULL_IDENTIFIER = exports.NULL_ADDRESS = exports.NULL_TOPIC = exports.NULL_STAMP = exports.SWARM_GATEWAY_URL = void 0;
3
+ exports.NULL_OWNER = exports.NULL_IDENTIFIER = exports.NULL_ADDRESS = exports.NULL_TOPIC = exports.NULL_STAMP = exports.SWARM_GATEWAY_URL = void 0;
4
4
  const typed_bytes_1 = require("./typed-bytes");
5
5
  exports.SWARM_GATEWAY_URL = 'https://api.gateway.ethswarm.org';
6
6
  exports.NULL_STAMP = new typed_bytes_1.BatchId('0000000000000000000000000000000000000000000000000000000000000000');
7
7
  exports.NULL_TOPIC = new typed_bytes_1.Topic('0000000000000000000000000000000000000000000000000000000000000000');
8
8
  exports.NULL_ADDRESS = new Uint8Array(32);
9
9
  exports.NULL_IDENTIFIER = new Uint8Array(32);
10
+ exports.NULL_OWNER = new Uint8Array(20);
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Duration = void 0;
4
+ class Duration {
5
+ constructor(seconds) {
6
+ this.seconds = Math.ceil(seconds);
7
+ if (seconds <= 0) {
8
+ throw Error('Duration must be greater than 0');
9
+ }
10
+ }
11
+ static fromSeconds(seconds) {
12
+ return new Duration(seconds);
13
+ }
14
+ static fromHours(hours) {
15
+ return new Duration(hours * 60 * 60);
16
+ }
17
+ static fromDays(days) {
18
+ return new Duration(days * 24 * 60 * 60);
19
+ }
20
+ static fromWeeks(weeks) {
21
+ return new Duration(weeks * 7 * 24 * 60 * 60);
22
+ }
23
+ static fromYears(years) {
24
+ return new Duration(years * 365 * 24 * 60 * 60);
25
+ }
26
+ static fromEndDate(endDate, startDate) {
27
+ return new Duration((endDate.getTime() - (startDate ?? new Date()).getTime()) / 1000);
28
+ }
29
+ toSeconds() {
30
+ return this.seconds;
31
+ }
32
+ toHours() {
33
+ return this.seconds / 60 / 60;
34
+ }
35
+ toDays() {
36
+ return this.seconds / 24 / 60 / 60;
37
+ }
38
+ toWeeks() {
39
+ return this.seconds / 7 / 24 / 60 / 60;
40
+ }
41
+ toYears() {
42
+ return this.seconds / 365 / 24 / 60 / 60;
43
+ }
44
+ toEndDate(startDate) {
45
+ return new Date((startDate ?? new Date()).getTime() + this.seconds * 1000);
46
+ }
47
+ }
48
+ exports.Duration = Duration;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStampUsage = exports.getStampTtlSeconds = exports.getStampMaximumCapacityBytes = exports.getStampEffectiveBytes = exports.getStampCost = exports.getDepthForCapacity = exports.getAmountForTtl = exports.getRedundancyStats = exports.getRedundancyStat = exports.approximateOverheadForRedundancyLevel = exports.makeMaxTarget = exports.getFolderSize = exports.makeCollectionFromFileList = exports.getCollectionSize = void 0;
3
+ exports.getStampUsage = exports.getStampTheoreticalBytes = exports.getStampEffectiveBytes = exports.getStampDuration = exports.getStampCost = exports.getDepthForSize = exports.getAmountForDuration = exports.getRedundancyStats = exports.getRedundancyStat = exports.approximateOverheadForRedundancyLevel = exports.makeMaxTarget = exports.getFolderSize = exports.makeCollectionFromFileList = exports.getCollectionSize = void 0;
4
4
  var collection_1 = require("./collection");
5
5
  Object.defineProperty(exports, "getCollectionSize", { enumerable: true, get: function () { return collection_1.getCollectionSize; } });
6
6
  Object.defineProperty(exports, "makeCollectionFromFileList", { enumerable: true, get: function () { return collection_1.makeCollectionFromFileList; } });
@@ -13,10 +13,10 @@ Object.defineProperty(exports, "approximateOverheadForRedundancyLevel", { enumer
13
13
  Object.defineProperty(exports, "getRedundancyStat", { enumerable: true, get: function () { return redundancy_1.getRedundancyStat; } });
14
14
  Object.defineProperty(exports, "getRedundancyStats", { enumerable: true, get: function () { return redundancy_1.getRedundancyStats; } });
15
15
  var stamps_1 = require("./stamps");
16
- Object.defineProperty(exports, "getAmountForTtl", { enumerable: true, get: function () { return stamps_1.getAmountForTtl; } });
17
- Object.defineProperty(exports, "getDepthForCapacity", { enumerable: true, get: function () { return stamps_1.getDepthForCapacity; } });
16
+ Object.defineProperty(exports, "getAmountForDuration", { enumerable: true, get: function () { return stamps_1.getAmountForDuration; } });
17
+ Object.defineProperty(exports, "getDepthForSize", { enumerable: true, get: function () { return stamps_1.getDepthForSize; } });
18
18
  Object.defineProperty(exports, "getStampCost", { enumerable: true, get: function () { return stamps_1.getStampCost; } });
19
+ Object.defineProperty(exports, "getStampDuration", { enumerable: true, get: function () { return stamps_1.getStampDuration; } });
19
20
  Object.defineProperty(exports, "getStampEffectiveBytes", { enumerable: true, get: function () { return stamps_1.getStampEffectiveBytes; } });
20
- Object.defineProperty(exports, "getStampMaximumCapacityBytes", { enumerable: true, get: function () { return stamps_1.getStampMaximumCapacityBytes; } });
21
- Object.defineProperty(exports, "getStampTtlSeconds", { enumerable: true, get: function () { return stamps_1.getStampTtlSeconds; } });
21
+ Object.defineProperty(exports, "getStampTheoreticalBytes", { enumerable: true, get: function () { return stamps_1.getStampTheoreticalBytes; } });
22
22
  Object.defineProperty(exports, "getStampUsage", { enumerable: true, get: function () { return stamps_1.getStampUsage; } });
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.marshalStamp = exports.convertEnvelopeToMarshaledStamp = exports.getDepthForCapacity = exports.getAmountForTtl = exports.getStampTtlSeconds = exports.getStampCost = exports.getStampEffectiveBytes = exports.getStampMaximumCapacityBytes = exports.getStampUsage = void 0;
3
+ exports.marshalStamp = exports.convertEnvelopeToMarshaledStamp = exports.getDepthForSize = exports.getAmountForDuration = exports.getStampDuration = exports.getStampCost = exports.getStampEffectiveBytes = exports.getStampTheoreticalBytes = exports.getStampUsage = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const bytes_1 = require("./bytes");
6
+ const duration_1 = require("./duration");
6
7
  const tokens_1 = require("./tokens");
7
8
  const type_1 = require("./type");
8
9
  /**
@@ -17,16 +18,16 @@ function getStampUsage(utilization, depth, bucketDepth) {
17
18
  }
18
19
  exports.getStampUsage = getStampUsage;
19
20
  /**
20
- * Utility function that calculates the theoritical maximum capacity of a postage batch based on its depth.
21
+ * Utility function that calculates the theoritical maximum size of a postage batch based on its depth.
21
22
  *
22
- * For smaller depths (up to 20), this may provide less accurate results.
23
+ * For smaller depths (up to 22), this may provide less accurate results.
23
24
  *
24
- * @returns {number} The maximum capacity of the postage batch in bytes.
25
+ * @returns {number} The maximum theoretical size of the postage batch in bytes.
25
26
  */
26
- function getStampMaximumCapacityBytes(depth) {
27
- return 2 ** depth * 4096;
27
+ function getStampTheoreticalBytes(depth) {
28
+ return 4096 * 2 ** depth;
28
29
  }
29
- exports.getStampMaximumCapacityBytes = getStampMaximumCapacityBytes;
30
+ exports.getStampTheoreticalBytes = getStampTheoreticalBytes;
30
31
  /**
31
32
  * Based on https://docs.ethswarm.org/docs/learn/technology/contracts/postage-stamp/#effective-utilisation-table
32
33
  */
@@ -45,20 +46,34 @@ const utilisationRateMap = {
45
46
  33: 0.9842,
46
47
  34: 0.9889,
47
48
  };
49
+ const effectiveSizeBreakpoints = [
50
+ [22, 4.93],
51
+ [23, 17.03],
52
+ [24, 44.21],
53
+ [25, 102.78],
54
+ [26, 225.86],
55
+ [27, 480.43],
56
+ [28, 1.0],
57
+ [29, 2.06],
58
+ [30, 4.2],
59
+ [31, 8.52],
60
+ [32, 17.2],
61
+ [33, 34.63],
62
+ ];
48
63
  /**
49
- * Utility function that calculates the effective volume of a postage batch based on its depth.
64
+ * Utility function that calculates the effective size of a postage batch based on its depth.
50
65
  *
51
- * Below 22 depth the effective volume is 0
66
+ * Below 22 depth the effective size is 0
52
67
  * Above 34 it's always > 99%
53
68
  *
54
- * @returns {number} The effective volume of the postage batch in bytes.
69
+ * @returns {number} The effective size of the postage batch in bytes.
55
70
  */
56
71
  function getStampEffectiveBytes(depth) {
57
72
  if (depth < 22) {
58
73
  return 0;
59
74
  }
60
75
  const utilRate = utilisationRateMap[depth] ?? 0.99;
61
- return getStampMaximumCapacityBytes(depth) * utilRate;
76
+ return Math.ceil(getStampTheoreticalBytes(depth) * utilRate);
62
77
  }
63
78
  exports.getStampEffectiveBytes = getStampEffectiveBytes;
64
79
  /**
@@ -74,37 +89,39 @@ exports.getStampCost = getStampCost;
74
89
  *
75
90
  * For more accurate results, get the price per block and block time from the Bee node or the blockchain.
76
91
  *
77
- * @returns {number} The TTL of the postage batch in seconds.
92
+ * @returns {number} The TTL of the postage batch.
78
93
  */
79
- function getStampTtlSeconds(amount, pricePerBlock = 24000, blockTime = 5) {
94
+ function getStampDuration(amount, pricePerBlock, blockTime = 5) {
80
95
  const amountBigInt = BigInt((0, type_1.asNumberString)(amount));
81
- return (amountBigInt * BigInt(blockTime)) / BigInt(pricePerBlock);
96
+ return duration_1.Duration.fromSeconds(Number((amountBigInt * BigInt(blockTime)) / BigInt(pricePerBlock)));
82
97
  }
83
- exports.getStampTtlSeconds = getStampTtlSeconds;
98
+ exports.getStampDuration = getStampDuration;
84
99
  /**
85
- * Utility function that calculates the amount of tokens required to maintain a given Time To Live (TTL) for a postage batch.
86
- *
87
- * This function estimates the required amount based on the provided TTL in days.
100
+ * Get the postage batch `amount` required for a given `duration`.
88
101
  *
89
- * @param {number} days - The Time To Live (TTL) in days.
90
- * @returns {NumberString} The estimated amount of tokens needed for the specified TTL.
102
+ * @param duration A duration object representing the duration of the storage.
103
+ * @param pricePerBlock The price per block in PLUR.
104
+ * @param blockTime The block time in seconds.
91
105
  */
92
- function getAmountForTtl(days, pricePerBlock = 24000, blockTime = 5) {
93
- return (days <= 0 ? 1n : BigInt(days)) * ((24n * 60n * 60n * BigInt(pricePerBlock)) / BigInt(blockTime));
106
+ function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
107
+ return (BigInt(duration.toSeconds()) / BigInt(blockTime)) * BigInt(pricePerBlock);
94
108
  }
95
- exports.getAmountForTtl = getAmountForTtl;
109
+ exports.getAmountForDuration = getAmountForDuration;
96
110
  /**
97
- * Utility function that calculates the depth required for a postage batch to achieve the specified capacity in gigabytes.
111
+ * Utility function that calculates the depth required for a postage batch to achieve the specified effective size in gigabytes.
98
112
  *
99
- * The depth is determined based on the given gigabytes, and the result is adjusted to a minimum depth of 18.
100
- *
101
- * @param {number} gigabytes - The desired capacity of the postage batch in gigabytes.
102
- * @returns {number} The calculated depth necessary to achieve the specified capacity.
113
+ * @param gigabytes
114
+ * @returns
103
115
  */
104
- function getDepthForCapacity(gigabytes) {
105
- return gigabytes <= 1 ? 18 : Math.ceil(Math.log2(Math.ceil(gigabytes)) + 18);
116
+ function getDepthForSize(gigabytes) {
117
+ for (const [depth, size] of effectiveSizeBreakpoints) {
118
+ if (gigabytes <= size) {
119
+ return depth;
120
+ }
121
+ }
122
+ return 34;
106
123
  }
107
- exports.getDepthForCapacity = getDepthForCapacity;
124
+ exports.getDepthForSize = getDepthForSize;
108
125
  function convertEnvelopeToMarshaledStamp(envelope) {
109
126
  return marshalStamp(envelope.signature, envelope.batchId.toUint8Array(), envelope.timestamp, envelope.index);
110
127
  }