@upcoming/bee-js 0.17.0 → 9.4.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/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  > Write your code in CJS, MJS or TypeScript.
15
15
 
16
- > Intended to be used with Bee version 2.4.0.
16
+ > Intended to be used with Bee version 2.5.0.
17
17
 
18
18
  ## Quick start
19
19
 
@@ -250,7 +250,7 @@ main()
250
250
  async function main() {
251
251
  const bee = new Bee(SWARM_GATEWAY_URL)
252
252
  const { reference } = await bee.uploadData(NULL_STAMP, 'Hello, World!')
253
- console.log(reference)
253
+ console.log(reference.toHex())
254
254
  }
255
255
  ```
256
256
 
@@ -272,7 +272,7 @@ async function getOrCreatePostageBatch() {
272
272
  if (usable) {
273
273
  batchId = usable.batchID
274
274
  } else {
275
- batchId = await bee.createPostageBatch('500000000', 20)
275
+ batchId = await bee.buyStorage(Size.fromGigabytes(1), Duration.fromDays(7))
276
276
  }
277
277
  }
278
278
  ```
@@ -348,6 +348,9 @@ const bee = new Bee('http://localhost:1633', {
348
348
  Stay up to date by joining the [official Discord](https://discord.gg/GU22h2utj6) and by keeping an eye on the
349
349
  [releases tab](https://github.com/ethersphere/bee-js/releases).
350
350
 
351
+ We are using [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) for our commit messages and pull
352
+ requests, following the [Semantic Versioning](https://semver.org/) rules.
353
+
351
354
  There are some ways you can make this module better:
352
355
 
353
356
  - Consult our [open issues](https://github.com/ethersphere/bee-js/issues) and take on one of them
@@ -374,6 +377,8 @@ After making changes, link the package to your project by running `npm link` in
374
377
 
375
378
  ### Test
376
379
 
380
+ [Code coverage](https://bah5acgza26tlmya36bdiu5cdfs3hh22hqthkhfv6cvq2ugxqrv5aw267ydlq.bzz.limo/)
381
+
377
382
  Tests are currently run against a mainnet Bee nodes. This is temporary and this section will be revised in the future.
378
383
 
379
384
  ## License
package/dist/cjs/bee.js CHANGED
@@ -47,6 +47,7 @@ const grantee = __importStar(require("./modules/grantee"));
47
47
  const gsoc = __importStar(require("./modules/gsoc"));
48
48
  const pinning = __importStar(require("./modules/pinning"));
49
49
  const pss = __importStar(require("./modules/pss"));
50
+ const rchash_1 = require("./modules/rchash");
50
51
  const status = __importStar(require("./modules/status"));
51
52
  const stewardship = __importStar(require("./modules/stewardship"));
52
53
  const tag = __importStar(require("./modules/tag"));
@@ -84,6 +85,7 @@ class Bee {
84
85
  if (options?.signer) {
85
86
  this.signer = new typed_bytes_1.PrivateKey(options.signer);
86
87
  }
88
+ this.network = options?.network ?? 'gnosis';
87
89
  this.requestOptions = {
88
90
  baseURL: this.url,
89
91
  timeout: options?.timeout ?? 0,
@@ -864,6 +866,12 @@ class Bee {
864
866
  reference = new typed_bytes_1.Reference(reference);
865
867
  return (0, envelope_1.postEnvelope)(this.getRequestOptionsForCall(options), postageBatchId, reference);
866
868
  }
869
+ /**
870
+ * Get reserve commitment hash duration seconds
871
+ */
872
+ async rchash(depth, anchor1, anchor2, options) {
873
+ return (0, rchash_1.rchash)(this.getRequestOptionsForCall(options), depth, anchor1, anchor2);
874
+ }
867
875
  /**
868
876
  * Ping the Bee node to see if there is a live Bee node on the given URL.
869
877
  *
@@ -1138,7 +1146,7 @@ class Bee {
1138
1146
  * @throws TypeError if non-integer value is passed to amount or depth
1139
1147
  *
1140
1148
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1141
- * @see [Bee Debug API reference - `POST /stamps`](https://docs.ethswarm.org/debug-api/#tag/Postage-Stamps/paths/~1stamps~1{amount}~1{depth}/post)
1149
+ * @see [Bee Debug API reference - `POST /stamps`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1{amount}~1{depth}/post)
1142
1150
  */
1143
1151
  async createPostageBatch(amount, depth, options, requestOptions) {
1144
1152
  const amountString = (0, type_1.asNumberString)(amount, { min: 0n, name: 'amount' });
@@ -1149,7 +1157,7 @@ class Bee {
1149
1157
  throw new error_1.BeeArgumentError(`Depth has to be between ${types_1.STAMPS_DEPTH_MIN}..${types_1.STAMPS_DEPTH_MAX}`, depth);
1150
1158
  }
1151
1159
  const chainState = await this.getChainState();
1152
- const minimumAmount = BigInt(chainState.currentPrice) * 17280n;
1160
+ const minimumAmount = BigInt(chainState.currentPrice) * 17280n + 1n;
1153
1161
  if (BigInt(amountString) < minimumAmount) {
1154
1162
  throw new error_1.BeeArgumentError(`Amount has to be at least ${minimumAmount} (1 day at current price ${chainState.currentPrice})`, amountString);
1155
1163
  }
@@ -1161,7 +1169,7 @@ class Bee {
1161
1169
  }
1162
1170
  async buyStorage(size, duration, options, requestOptions) {
1163
1171
  const chainState = await this.getChainState(requestOptions);
1164
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1172
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1165
1173
  const depth = (0, stamps_1.getDepthForSize)(size);
1166
1174
  if (options) {
1167
1175
  options = (0, type_1.preparePostageBatchOptions)(options);
@@ -1170,7 +1178,7 @@ class Bee {
1170
1178
  }
1171
1179
  async getStorageCost(size, duration, options) {
1172
1180
  const chainState = await this.getChainState(options);
1173
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1181
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1174
1182
  const depth = (0, stamps_1.getDepthForSize)(size);
1175
1183
  return (0, stamps_1.getStampCost)(depth, amount);
1176
1184
  }
@@ -1181,19 +1189,19 @@ class Bee {
1181
1189
  if (delta <= 0) {
1182
1190
  throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
1183
1191
  }
1184
- await this.topUpBatch(batch.batchID, BigInt(batch.amount) * 2n ** BigInt(delta - 1), options);
1192
+ await this.topUpBatch(batch.batchID, BigInt(batch.amount) * 2n ** BigInt(delta - 1) + 1n, options);
1185
1193
  return this.diluteBatch(batch.batchID, depth, options);
1186
1194
  }
1187
1195
  async extendStorageDuration(postageBatchId, duration, options) {
1188
1196
  const batch = await this.getPostageBatch(postageBatchId, options);
1189
1197
  const chainState = await this.getChainState(options);
1190
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1198
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1191
1199
  return this.topUpBatch(batch.batchID, amount, options);
1192
1200
  }
1193
1201
  async getExtensionCost(postageBatchId, size, duration, options) {
1194
1202
  const batch = await this.getPostageBatch(postageBatchId, options);
1195
1203
  const chainState = await this.getChainState(options);
1196
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1204
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1197
1205
  const depth = (0, stamps_1.getDepthForSize)(size);
1198
1206
  const currentValue = (0, stamps_1.getStampCost)(batch.depth, batch.amount);
1199
1207
  const newValue = (0, stamps_1.getStampCost)(depth, amount);
@@ -1213,7 +1221,7 @@ class Bee {
1213
1221
  async getDurationExtensionCost(postageBatchId, duration, options) {
1214
1222
  const batch = await this.getPostageBatch(postageBatchId, options);
1215
1223
  const chainState = await this.getChainState(options);
1216
- const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice);
1224
+ const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1217
1225
  return (0, stamps_1.getStampCost)(batch.depth, amount);
1218
1226
  }
1219
1227
  /**
@@ -1227,7 +1235,7 @@ class Bee {
1227
1235
  * @param options Request options
1228
1236
  *
1229
1237
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1230
- * @see [Bee Debug API reference - `PATCH /stamps/topup/${id}/${amount}`](https://docs.ethswarm.org/debug-api/#tag/Postage-Stamps/paths/~1stamps~1topup~1{id}~1{amount}/patch)
1238
+ * @see [Bee Debug API reference - `PATCH /stamps/topup/${id}/${amount}`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1topup~1{batch_id}~1{amount}/patch)
1231
1239
  */
1232
1240
  async topUpBatch(postageBatchId, amount, options) {
1233
1241
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
@@ -1246,7 +1254,7 @@ class Bee {
1246
1254
  * @param options Request options
1247
1255
  *
1248
1256
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1249
- * @see [Bee Debug API reference - `PATCH /stamps/topup/${id}/${amount}`](https://docs.ethswarm.org/debug-api/#tag/Postage-Stamps/paths/~1stamps~1topup~1{id}~1{amount}/patch)
1257
+ * @see [Bee Debug API reference - `PATCH /stamps/topup/${id}/${amount}`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1dilute~1%7Bbatch_id%7D~1%7Bdepth%7D/patch)
1250
1258
  */
1251
1259
  async diluteBatch(postageBatchId, depth, options) {
1252
1260
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
@@ -1259,7 +1267,7 @@ class Bee {
1259
1267
  * @param postageBatchId Batch ID
1260
1268
  *
1261
1269
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1262
- * @see [Bee Debug API reference - `GET /stamps/${id}`](https://docs.ethswarm.org/debug-api/#tag/Postage-Stamps/paths/~1stamps~1{id}/get)
1270
+ * @see [Bee Debug API reference - `GET /stamps/${id}`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1%7Bbatch_id%7D/get)
1263
1271
  */
1264
1272
  async getPostageBatch(postageBatchId, options) {
1265
1273
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
@@ -1271,7 +1279,7 @@ class Bee {
1271
1279
  * @param postageBatchId Batch ID
1272
1280
  *
1273
1281
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1274
- * @see [Bee Debug API reference - `GET /stamps/${id}/buckets`](https://docs.ethswarm.org/debug-api/#tag/Postage-Stamps/paths/~1stamps~1{id}~1buckets/get)
1282
+ * @see [Bee Debug API reference - `GET /stamps/${id}/buckets`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps~1%7Bbatch_id%7D~1buckets/get)
1275
1283
  */
1276
1284
  async getPostageBatchBuckets(postageBatchId, options) {
1277
1285
  postageBatchId = new typed_bytes_1.BatchId(postageBatchId);
@@ -1281,15 +1289,32 @@ class Bee {
1281
1289
  * Return all postage batches that has the node available.
1282
1290
  *
1283
1291
  * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1284
- * @see [Bee Debug API reference - `GET /stamps`](https://docs.ethswarm.org/debug-api/#tag/Postage-Stamps/paths/~1stamps/get)
1292
+ * @see [Bee Debug API reference - `GET /stamps`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps/get)
1293
+ * @deprecated Use `getPostageBatches` instead
1285
1294
  */
1286
1295
  async getAllPostageBatch(options) {
1287
- return stamps.getAllPostageBatches(this.getRequestOptionsForCall(options));
1296
+ return stamps.getAllPostageBatches(this.getRequestOptionsForCall(options)); // TODO: remove in June 2025
1288
1297
  }
1289
1298
  /**
1290
1299
  * Return all globally available postage batches.
1300
+ * @deprecated Use `getGlobalPostageBatches` instead
1291
1301
  */
1292
1302
  async getAllGlobalPostageBatch(options) {
1303
+ return stamps.getGlobalPostageBatches(this.getRequestOptionsForCall(options)); // TODO: remove in June 2025
1304
+ }
1305
+ /**
1306
+ * Return all postage batches that belong to the node.
1307
+ *
1308
+ * @see [Bee docs - Keep your data alive / Postage stamps](https://docs.ethswarm.org/docs/develop/access-the-swarm/introduction/#keep-your-data-alive)
1309
+ * @see [Bee Debug API reference - `GET /stamps`](https://docs.ethswarm.org/api/#tag/Postage-Stamps/paths/~1stamps/get)
1310
+ */
1311
+ async getPostageBatches(options) {
1312
+ return stamps.getAllPostageBatches(this.getRequestOptionsForCall(options));
1313
+ }
1314
+ /**
1315
+ * Return all globally available postage batches.
1316
+ */
1317
+ async getGlobalPostageBatches(options) {
1293
1318
  return stamps.getGlobalPostageBatches(this.getRequestOptionsForCall(options));
1294
1319
  }
1295
1320
  /**
@@ -114,6 +114,7 @@ function makeFeedReader(requestOptions, topic, owner) {
114
114
  return {
115
115
  payload: update.payload,
116
116
  feedIndex,
117
+ feedIndexNext: feedIndex.next(),
117
118
  };
118
119
  };
119
120
  const downloadPayload = async (options) => {
@@ -128,6 +129,7 @@ function makeFeedReader(requestOptions, topic, owner) {
128
129
  return {
129
130
  payload,
130
131
  feedIndex,
132
+ feedIndexNext: feedIndex.next(),
131
133
  };
132
134
  };
133
135
  const downloadReference = async (options) => {
@@ -139,6 +141,7 @@ function makeFeedReader(requestOptions, topic, owner) {
139
141
  return {
140
142
  reference: new typed_bytes_1.Reference(payload.payload.toUint8Array()),
141
143
  feedIndex: payload.feedIndex,
144
+ feedIndexNext: payload.feedIndexNext ?? payload.feedIndex.next(),
142
145
  };
143
146
  };
144
147
  return {
@@ -20,6 +20,22 @@ class Fork {
20
20
  }
21
21
  static split(a, b) {
22
22
  const commonPart = cafe_utility_1.Binary.commonPrefix(a.prefix, b.prefix);
23
+ if (commonPart.length === a.prefix.length) {
24
+ const remainingB = b.prefix.slice(commonPart.length);
25
+ b.node.path = b.prefix.slice(commonPart.length);
26
+ b.prefix = b.prefix.slice(commonPart.length);
27
+ b.node.parent = a.node;
28
+ a.node.forks.set(remainingB[0], b);
29
+ return a;
30
+ }
31
+ if (commonPart.length === b.prefix.length) {
32
+ const remainingA = a.prefix.slice(commonPart.length);
33
+ a.node.path = a.prefix.slice(commonPart.length);
34
+ a.prefix = a.prefix.slice(commonPart.length);
35
+ a.node.parent = b.node;
36
+ b.node.forks.set(remainingA[0], a);
37
+ return b;
38
+ }
23
39
  const node = new MantarayNode({ path: commonPart });
24
40
  const newAFork = new Fork(a.prefix.slice(commonPart.length), a.node);
25
41
  const newBFork = new Fork(b.prefix.slice(commonPart.length), b.node);
@@ -58,13 +74,13 @@ class Fork {
58
74
  const prefixLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
59
75
  const prefix = reader.read(prefixLength);
60
76
  reader.read(30 - prefixLength);
61
- const targetAddress = reader.read(32);
77
+ const selfAddress = reader.read(32);
62
78
  let metadata = undefined;
63
79
  if (isType(type, TYPE_WITH_METADATA)) {
64
80
  const metadataLength = cafe_utility_1.Binary.uint16ToNumber(reader.read(2), 'BE');
65
81
  metadata = JSON.parse(DECODER.decode(reader.read(metadataLength)));
66
82
  }
67
- return new Fork(prefix, new MantarayNode({ targetAddress, metadata, path: prefix }));
83
+ return new Fork(prefix, new MantarayNode({ selfAddress, metadata, path: prefix }));
68
84
  }
69
85
  }
70
86
  exports.Fork = Fork;
@@ -175,15 +191,16 @@ class MantarayNode {
175
191
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
176
192
  */
177
193
  static async unmarshal(bee, reference, options, requestOptions) {
194
+ reference = new typed_bytes_1.Reference(reference);
178
195
  const data = (await bee.downloadData(reference, options, requestOptions)).toUint8Array();
179
- return this.unmarshalFromData(data);
196
+ return this.unmarshalFromData(data, reference.toUint8Array());
180
197
  }
181
198
  /**
182
199
  * Unmarshals a MantarayNode from the given data.
183
200
  *
184
201
  * Do not forget calling `loadRecursively` on the returned node to load the entire tree.
185
202
  */
186
- static unmarshalFromData(data) {
203
+ static unmarshalFromData(data, selfAddress) {
187
204
  const obfuscationKey = data.subarray(0, 32);
188
205
  const decrypted = cafe_utility_1.Binary.xorCypher(data.subarray(32), obfuscationKey);
189
206
  const reader = new cafe_utility_1.Uint8ArrayReader(decrypted);
@@ -193,7 +210,7 @@ class MantarayNode {
193
210
  }
194
211
  const targetAddressLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
195
212
  const targetAddress = targetAddressLength === 0 ? __1.NULL_ADDRESS : reader.read(targetAddressLength);
196
- const node = new MantarayNode({ targetAddress, obfuscationKey });
213
+ const node = new MantarayNode({ selfAddress, targetAddress, obfuscationKey });
197
214
  const forkBitmap = reader.read(32);
198
215
  for (let i = 0; i < 256; i++) {
199
216
  if (cafe_utility_1.Binary.getBit(forkBitmap, i, 'LE')) {
@@ -290,7 +307,10 @@ class MantarayNode {
290
307
  */
291
308
  async loadRecursively(bee, options, requestOptions) {
292
309
  for (const fork of this.forks.values()) {
293
- const node = await MantarayNode.unmarshal(bee, fork.node.targetAddress, options, requestOptions);
310
+ if (!fork.node.selfAddress) {
311
+ throw Error('MantarayNode#loadRecursively fork.node.selfAddress is not set');
312
+ }
313
+ const node = await MantarayNode.unmarshal(bee, fork.node.selfAddress, options, requestOptions);
294
314
  fork.node.targetAddress = node.targetAddress;
295
315
  fork.node.forks = node.forks;
296
316
  fork.node.path = fork.prefix;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rchash = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
5
+ const http_1 = require("../utils/http");
6
+ const RCHASH_ENDPOINT = 'rchash';
7
+ async function rchash(requestOptions, depth, anchor1, anchor2) {
8
+ const response = await (0, http_1.http)(requestOptions, {
9
+ responseType: 'json',
10
+ url: `${RCHASH_ENDPOINT}/${depth}/${anchor1}/${anchor2}`,
11
+ });
12
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
13
+ return cafe_utility_1.Types.asNumber(body.durationSeconds, { name: 'durationSeconds' });
14
+ }
15
+ exports.rchash = rchash;
@@ -69,5 +69,8 @@ class Bytes {
69
69
  equals(other) {
70
70
  return this.toHex() === new Bytes(other).toHex();
71
71
  }
72
+ represent() {
73
+ return this.toHex();
74
+ }
72
75
  }
73
76
  exports.Bytes = Bytes;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Duration = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
4
5
  class Duration {
5
6
  constructor(seconds) {
6
7
  this.seconds = Math.ceil(seconds);
@@ -47,5 +48,8 @@ class Duration {
47
48
  toEndDate(startDate) {
48
49
  return new Date((startDate ?? new Date()).getTime() + this.seconds * 1000);
49
50
  }
51
+ represent() {
52
+ return cafe_utility_1.Dates.secondsToHumanTime(this.seconds);
53
+ }
50
54
  }
51
55
  exports.Duration = Duration;
@@ -8,6 +8,13 @@ const axios_1 = __importDefault(require("axios"));
8
8
  const cafe_utility_1 = require("cafe-utility");
9
9
  const index_1 = require("../index");
10
10
  const { AxiosError } = axios_1.default;
11
+ const originalObjectKeys = Object.keys;
12
+ Object.keys = function (o) {
13
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(o)) {
14
+ return ['axios_workaround_for_buffer'];
15
+ }
16
+ return originalObjectKeys(o);
17
+ };
11
18
  const MAX_FAILED_ATTEMPTS = 100000;
12
19
  const DELAY_FAST = 200;
13
20
  const DELAY_SLOW = 1000;
@@ -19,6 +19,12 @@ class Size {
19
19
  static fromBytes(bytes) {
20
20
  return new Size(bytes);
21
21
  }
22
+ static fromKilobytes(kilobytes) {
23
+ return new Size(kilobytes * 1000);
24
+ }
25
+ static fromMegabytes(megabytes) {
26
+ return new Size(megabytes * 1000 * 1000);
27
+ }
22
28
  static fromGigabytes(gigabytes) {
23
29
  return new Size(gigabytes * 1000 * 1000 * 1000);
24
30
  }
@@ -31,5 +37,8 @@ class Size {
31
37
  toFormattedString() {
32
38
  return cafe_utility_1.Numbers.convertBytes(this.bytes, 1000);
33
39
  }
40
+ represent() {
41
+ return this.toFormattedString();
42
+ }
34
43
  }
35
44
  exports.Size = Size;
@@ -6,6 +6,7 @@ const bytes_1 = require("./bytes");
6
6
  const duration_1 = require("./duration");
7
7
  const tokens_1 = require("./tokens");
8
8
  const type_1 = require("./type");
9
+ const MAX_UTILIZATION = 0.9;
9
10
  /**
10
11
  * Utility function that calculates usage of postage batch based on its utilization, depth and bucket depth.
11
12
  *
@@ -30,35 +31,27 @@ function getStampTheoreticalBytes(depth) {
30
31
  exports.getStampTheoreticalBytes = getStampTheoreticalBytes;
31
32
  /**
32
33
  * Based on https://docs.ethswarm.org/docs/learn/technology/contracts/postage-stamp/#effective-utilisation-table
34
+ * Optimised for encrypted, medium erasure coding
33
35
  */
34
- const utilisationRateMap = {
35
- 22: 0.2867,
36
- 23: 0.4956,
37
- 24: 0.6433,
38
- 25: 0.7478,
39
- 26: 0.8217,
40
- 27: 0.8739,
41
- 28: 0.9108,
42
- 29: 0.9369,
43
- 30: 0.9554,
44
- 31: 0.9685,
45
- 32: 0.9777,
46
- 33: 0.9842,
47
- 34: 0.9889,
48
- };
49
36
  const effectiveSizeBreakpoints = [
50
- [22, 4.93],
51
- [23, 17.03],
52
- [24, 44.21],
53
- [25, 102.78],
54
- [26, 225.87],
55
- [27, 480.44],
56
- [28, 1001.44],
57
- [29, 2060.27],
58
- [30, 4201.9],
59
- [31, 8519.02],
60
- [32, 17199.89],
61
- [33, 34628.46],
37
+ [17, 0.00004089],
38
+ [18, 0.00609],
39
+ [19, 0.10249],
40
+ [20, 0.62891],
41
+ [21, 2.38],
42
+ [22, 7.07],
43
+ [23, 18.24],
44
+ [24, 43.04],
45
+ [25, 96.5],
46
+ [26, 208.52],
47
+ [27, 435.98],
48
+ [28, 908.81],
49
+ [29, 1870],
50
+ [30, 3810],
51
+ [31, 7730],
52
+ [32, 15610],
53
+ [33, 31430],
54
+ [34, 63150],
62
55
  ];
63
56
  /**
64
57
  * Utility function that calculates the effective size of a postage batch based on its depth.
@@ -69,16 +62,23 @@ const effectiveSizeBreakpoints = [
69
62
  * @returns {number} The effective size of the postage batch in bytes.
70
63
  */
71
64
  function getStampEffectiveBytes(depth) {
72
- if (depth < 22) {
65
+ if (depth < 17) {
73
66
  return 0;
74
67
  }
75
- const utilRate = utilisationRateMap[depth] ?? 0.99;
76
- return Math.ceil(getStampTheoreticalBytes(depth) * utilRate);
68
+ const breakpoint = effectiveSizeBreakpoints.find(([d, size]) => {
69
+ if (depth === d) {
70
+ return size;
71
+ }
72
+ });
73
+ if (breakpoint) {
74
+ return breakpoint[1] * 1000 * 1000 * 1000;
75
+ }
76
+ return Math.ceil(getStampTheoreticalBytes(depth) * MAX_UTILIZATION);
77
77
  }
78
78
  exports.getStampEffectiveBytes = getStampEffectiveBytes;
79
79
  function getStampEffectiveBytesBreakpoints() {
80
80
  const map = new Map();
81
- for (let i = 22; i < 35; i++) {
81
+ for (let i = 17; i < 35; i++) {
82
82
  map.set(i, getStampEffectiveBytes(i));
83
83
  }
84
84
  return map;
@@ -98,7 +98,7 @@ exports.getStampCost = getStampCost;
98
98
  *
99
99
  * @returns {number} The TTL of the postage batch.
100
100
  */
101
- function getStampDuration(amount, pricePerBlock, blockTime = 5) {
101
+ function getStampDuration(amount, pricePerBlock, blockTime) {
102
102
  const amountBigInt = BigInt((0, type_1.asNumberString)(amount));
103
103
  return duration_1.Duration.fromSeconds(Number((amountBigInt * BigInt(blockTime)) / BigInt(pricePerBlock)));
104
104
  }
@@ -110,8 +110,8 @@ exports.getStampDuration = getStampDuration;
110
110
  * @param pricePerBlock The price per block in PLUR.
111
111
  * @param blockTime The block time in seconds.
112
112
  */
113
- function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
114
- return (BigInt(duration.toSeconds()) / BigInt(blockTime)) * BigInt(pricePerBlock);
113
+ function getAmountForDuration(duration, pricePerBlock, blockTime) {
114
+ return (BigInt(duration.toSeconds()) / BigInt(blockTime)) * BigInt(pricePerBlock) + 1n;
115
115
  }
116
116
  exports.getAmountForDuration = getAmountForDuration;
117
117
  /**
@@ -122,11 +122,11 @@ exports.getAmountForDuration = getAmountForDuration;
122
122
  */
123
123
  function getDepthForSize(size) {
124
124
  for (const [depth, sizeBreakpoint] of effectiveSizeBreakpoints) {
125
- if (size.toGigabytes() <= sizeBreakpoint) {
125
+ if (size.toBytes() <= sizeBreakpoint * 1000 * 1000 * 1000) {
126
126
  return depth;
127
127
  }
128
128
  }
129
- return 34;
129
+ return 35;
130
130
  }
131
131
  exports.getDepthForSize = getDepthForSize;
132
132
  function convertEnvelopeToMarshaledStamp(envelope) {
@@ -69,6 +69,7 @@ function prepareUploadOptions(value, name = 'UploadOptions') {
69
69
  const object = cafe_utility_1.Types.asObject(value, { name });
70
70
  return {
71
71
  act: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'act' }), object.act),
72
+ actHistoryAddress: cafe_utility_1.Types.asOptional(x => new typed_bytes_1.Reference(x), object.actHistoryAddress),
72
73
  deferred: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'deferred' }), object.deferred),
73
74
  encrypt: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'encrypt' }), object.encrypt),
74
75
  pin: cafe_utility_1.Types.asOptional(x => cafe_utility_1.Types.asBoolean(x, { name: 'pin' }), object.pin),
@@ -163,6 +163,7 @@ class Topic extends bytes_1.Bytes {
163
163
  }
164
164
  exports.Topic = Topic;
165
165
  Topic.LENGTH = 32;
166
+ const MAX_UINT64 = new Uint8Array(8).fill(0xff, 0, 8);
166
167
  class FeedIndex extends bytes_1.Bytes {
167
168
  constructor(bytes) {
168
169
  super(bytes, 8);
@@ -173,7 +174,13 @@ class FeedIndex extends bytes_1.Bytes {
173
174
  toBigInt() {
174
175
  return cafe_utility_1.Binary.uint64ToNumber(this.bytes, 'BE');
175
176
  }
177
+ next() {
178
+ if (cafe_utility_1.Binary.equals(this.bytes, MAX_UINT64)) {
179
+ return FeedIndex.fromBigInt(0n);
180
+ }
181
+ return FeedIndex.fromBigInt(this.toBigInt() + 1n);
182
+ }
176
183
  }
177
184
  exports.FeedIndex = FeedIndex;
178
185
  FeedIndex.LENGTH = 8;
179
- FeedIndex.MINUS_ONE = new FeedIndex(new Uint8Array(8).fill(0xff, 0, 8));
186
+ FeedIndex.MINUS_ONE = new FeedIndex(MAX_UINT64);