@upcoming/bee-js 9.4.1 → 9.7.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
@@ -722,7 +722,8 @@ class Bee {
722
722
  for (let i = 0n; i < 0xffffn; i++) {
723
723
  const signer = new typed_bytes_1.PrivateKey(cafe_utility_1.Binary.numberToUint256(start + i, 'BE'));
724
724
  const socAddress = (0, soc_1.makeSOCAddress)(identifier, signer.publicKey().address());
725
- const actualProximity = 256 - cafe_utility_1.Binary.proximity(socAddress.toUint8Array(), targetOverlay.toUint8Array(), 256);
725
+ // TODO: test the significance of the hardcoded 256
726
+ const actualProximity = 256 - cafe_utility_1.Binary.proximity(socAddress.toUint8Array(), targetOverlay.toUint8Array());
726
727
  if (actualProximity <= 256 - proximity) {
727
728
  return signer;
728
729
  }
@@ -896,6 +897,17 @@ class Bee {
896
897
  }
897
898
  return true;
898
899
  }
900
+ /**
901
+ * Checks the `/gateway` endpoint to see if the remote API is a gateway.
902
+ *
903
+ * Do note that this is not a standard way to check for gateway nodes,
904
+ * but some of the gateway tooling expose this endpoint.
905
+ *
906
+ * @param options
907
+ */
908
+ async isGateway(options) {
909
+ return status.isGateway(this.getRequestOptionsForCall(options));
910
+ }
899
911
  // Legacy debug API
900
912
  async getNodeAddresses(options) {
901
913
  return connectivity.getNodeAddresses(this.getRequestOptionsForCall(options));
@@ -1011,13 +1023,24 @@ class Bee {
1011
1023
  return chequebook.cashoutLastCheque(this.getRequestOptionsForCall(requestOptions), address, options);
1012
1024
  }
1013
1025
  /**
1014
- * Deposit tokens from overlay address into chequebook
1026
+ * Deposit tokens from node wallet into chequebook
1015
1027
  *
1016
1028
  * @param amount Amount of tokens to deposit (must be positive integer)
1017
1029
  * @param gasPrice Gas Price in WEI for the transaction call
1018
1030
  * @return string Hash of the transaction
1031
+ * @deprecated Use `depositBZZToChequebook` instead.
1019
1032
  */
1020
1033
  async depositTokens(amount, gasPrice, options) {
1034
+ return this.depositBZZToChequebook(amount, gasPrice, options);
1035
+ }
1036
+ /**
1037
+ * Deposit tokens from node wallet into chequebook
1038
+ *
1039
+ * @param amount Amount of tokens to deposit (must be positive integer)
1040
+ * @param gasPrice Gas Price in WEI for the transaction call
1041
+ * @return string Hash of the transaction
1042
+ */
1043
+ async depositBZZToChequebook(amount, gasPrice, options) {
1021
1044
  const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1022
1045
  let gasPriceString;
1023
1046
  if (gasPrice) {
@@ -1026,13 +1049,24 @@ class Bee {
1026
1049
  return chequebook.depositTokens(this.getRequestOptionsForCall(options), amountString, gasPriceString);
1027
1050
  }
1028
1051
  /**
1029
- * Withdraw tokens from the chequebook to the overlay address
1052
+ * Withdraw tokens from the chequebook to the node wallet
1030
1053
  *
1031
1054
  * @param amount Amount of tokens to withdraw (must be positive integer)
1032
1055
  * @param gasPrice Gas Price in WEI for the transaction call
1033
1056
  * @return string Hash of the transaction
1057
+ * @deprecated Use `withdrawBZZFromChequebook` instead.
1034
1058
  */
1035
1059
  async withdrawTokens(amount, gasPrice, options) {
1060
+ return this.withdrawBZZFromChequebook(amount, gasPrice, options);
1061
+ }
1062
+ /**
1063
+ * Withdraw tokens from the chequebook to the node wallet
1064
+ *
1065
+ * @param amount Amount of tokens to withdraw (must be positive integer)
1066
+ * @param gasPrice Gas Price in WEI for the transaction call
1067
+ * @return string Hash of the transaction
1068
+ */
1069
+ async withdrawBZZFromChequebook(amount, gasPrice, options) {
1036
1070
  // TODO: check BZZ in tests
1037
1071
  const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1038
1072
  let gasPriceString;
@@ -1041,6 +1075,16 @@ class Bee {
1041
1075
  }
1042
1076
  return chequebook.withdrawTokens(this.getRequestOptionsForCall(options), amountString, gasPriceString);
1043
1077
  }
1078
+ async withdrawBZZToExternalWallet(amount, address, options) {
1079
+ amount = amount instanceof tokens_1.BZZ ? amount : tokens_1.BZZ.fromPLUR(amount);
1080
+ address = new typed_bytes_1.EthAddress(address);
1081
+ return states.withdrawBZZ(this.getRequestOptionsForCall(options), amount, address);
1082
+ }
1083
+ async withdrawDAIToExternalWallet(amount, address, options) {
1084
+ amount = amount instanceof tokens_1.DAI ? amount : tokens_1.DAI.fromWei(amount);
1085
+ address = new typed_bytes_1.EthAddress(address);
1086
+ return states.withdrawDAI(this.getRequestOptionsForCall(options), amount, address);
1087
+ }
1044
1088
  /*
1045
1089
  * Settlements endpoint
1046
1090
  */
@@ -1124,7 +1168,7 @@ class Bee {
1124
1168
  return states.getChainState(this.getRequestOptionsForCall(options));
1125
1169
  }
1126
1170
  /**
1127
- * Get wallet balances for xDai and BZZ of the Bee node
1171
+ * Get wallet balances for DAI and BZZ of the Bee node
1128
1172
  *
1129
1173
  * @param options
1130
1174
  */
@@ -1167,24 +1211,24 @@ class Bee {
1167
1211
  }
1168
1212
  return stamp;
1169
1213
  }
1170
- async buyStorage(size, duration, options, requestOptions) {
1214
+ async buyStorage(size, duration, options, requestOptions, encryption, erasureCodeLevel) {
1171
1215
  const chainState = await this.getChainState(requestOptions);
1172
1216
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1173
- const depth = (0, stamps_1.getDepthForSize)(size);
1217
+ const depth = (0, stamps_1.getDepthForSize)(size, encryption, erasureCodeLevel);
1174
1218
  if (options) {
1175
1219
  options = (0, type_1.preparePostageBatchOptions)(options);
1176
1220
  }
1177
1221
  return this.createPostageBatch(amount, depth, options, requestOptions);
1178
1222
  }
1179
- async getStorageCost(size, duration, options) {
1223
+ async getStorageCost(size, duration, options, encryption, erasureCodeLevel) {
1180
1224
  const chainState = await this.getChainState(options);
1181
1225
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1182
- const depth = (0, stamps_1.getDepthForSize)(size);
1226
+ const depth = (0, stamps_1.getDepthForSize)(size, encryption, erasureCodeLevel);
1183
1227
  return (0, stamps_1.getStampCost)(depth, amount);
1184
1228
  }
1185
- async extendStorageSize(postageBatchId, size, options) {
1229
+ async extendStorageSize(postageBatchId, size, options, encryption, erasureCodeLevel) {
1186
1230
  const batch = await this.getPostageBatch(postageBatchId, options);
1187
- const depth = (0, stamps_1.getDepthForSize)(size);
1231
+ const depth = (0, stamps_1.getDepthForSize)(size, encryption, erasureCodeLevel);
1188
1232
  const delta = depth - batch.depth;
1189
1233
  if (delta <= 0) {
1190
1234
  throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
@@ -1198,18 +1242,18 @@ class Bee {
1198
1242
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1199
1243
  return this.topUpBatch(batch.batchID, amount, options);
1200
1244
  }
1201
- async getExtensionCost(postageBatchId, size, duration, options) {
1245
+ async getExtensionCost(postageBatchId, size, duration, options, encryption, erasureCodeLevel) {
1202
1246
  const batch = await this.getPostageBatch(postageBatchId, options);
1203
1247
  const chainState = await this.getChainState(options);
1204
1248
  const amount = (0, stamps_1.getAmountForDuration)(duration, chainState.currentPrice, this.network === 'gnosis' ? 5 : 15);
1205
- const depth = (0, stamps_1.getDepthForSize)(size);
1249
+ const depth = (0, stamps_1.getDepthForSize)(size, encryption, erasureCodeLevel);
1206
1250
  const currentValue = (0, stamps_1.getStampCost)(batch.depth, batch.amount);
1207
1251
  const newValue = (0, stamps_1.getStampCost)(depth, amount);
1208
1252
  return newValue.minus(currentValue);
1209
1253
  }
1210
- async getSizeExtensionCost(postageBatchId, size, options) {
1254
+ async getSizeExtensionCost(postageBatchId, size, options, encryption, erasureCodeLevel) {
1211
1255
  const batch = await this.getPostageBatch(postageBatchId, options);
1212
- const depth = (0, stamps_1.getDepthForSize)(size);
1256
+ const depth = (0, stamps_1.getDepthForSize)(size, encryption, erasureCodeLevel);
1213
1257
  const delta = depth - batch.depth;
1214
1258
  if (delta <= 0) {
1215
1259
  throw new error_1.BeeArgumentError('New depth has to be greater than the original depth', depth);
@@ -1342,7 +1386,7 @@ class Bee {
1342
1386
  return transactions.rebroadcastTransaction(this.getRequestOptionsForCall(options), transactionHash);
1343
1387
  }
1344
1388
  /**
1345
- * Cancel currently pending transaction
1389
+ * Cancels a currently pending transaction
1346
1390
  * @param transactionHash
1347
1391
  * @param gasPrice
1348
1392
  */
@@ -1355,20 +1399,48 @@ class Bee {
1355
1399
  return transactions.cancelTransaction(this.getRequestOptionsForCall(options), transactionHash, gasPriceString);
1356
1400
  }
1357
1401
  /**
1358
- * Gets the staked amount of BZZ (in PLUR unit) as number string.
1402
+ * Gets the amount of staked BZZ
1359
1403
  *
1360
- * @param options
1404
+ * @param options HTTP request options, such as `headers` or `timeout`
1361
1405
  */
1362
1406
  async getStake(options) {
1363
1407
  return stake.getStake(this.getRequestOptionsForCall(options));
1364
1408
  }
1365
1409
  /**
1366
- * Deposits given amount of BZZ token (in PLUR unit).
1410
+ * Gets the amount of withdrawable staked BZZ.
1411
+ *
1412
+ * @param options HTTP request options, such as `headers` or `timeout`
1413
+ */
1414
+ async getWithdrawableStake(options) {
1415
+ return stake.getWithdrawableStake(this.getRequestOptionsForCall(options));
1416
+ }
1417
+ /**
1418
+ * Withdraws ALL surplus staked BZZ to the node wallet.
1419
+ *
1420
+ * Use the `getWithdrawableStake` method to check how much surplus stake is available.
1421
+ *
1422
+ * @param options HTTP request options, such as `headers` or `timeout`
1423
+ */
1424
+ async withdrawSurplusStake(options) {
1425
+ return stake.withdrawSurplusStake(this.getRequestOptionsForCall(options));
1426
+ }
1427
+ /**
1428
+ * Withdraws all staked BZZ to the node wallet.
1429
+ *
1430
+ * **Only available when the staking contract is paused and is in the process of being migrated to a new contract!**
1431
+ *
1432
+ * @param options HTTP request options, such as `headers` or `timeout`
1433
+ */
1434
+ async migrateStake(options) {
1435
+ return stake.migrateStake(this.getRequestOptionsForCall(options));
1436
+ }
1437
+ /**
1438
+ * Stakes the given amount of BZZ. Initial deposit must be at least 10 BZZ.
1367
1439
  *
1368
1440
  * Be aware that staked BZZ tokens can **not** be withdrawn.
1369
1441
  *
1370
- * @param amount Amount of BZZ token (in PLUR unit) to be staked. Minimum is 100_000_000_000_000_000 PLUR (10 BZZ).
1371
- * @param options
1442
+ * @param amount Amount of BZZ tokens to be staked. If not providing a `BZZ` instance, the amount is denoted in PLUR.
1443
+ * @param options HTTP request options, such as `headers` or `timeout`
1372
1444
  */
1373
1445
  async depositStake(amount, options, requestOptions) {
1374
1446
  const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
@@ -1378,9 +1450,9 @@ class Bee {
1378
1450
  return stake.stake(this.getRequestOptionsForCall(requestOptions), amountString, options);
1379
1451
  }
1380
1452
  /**
1381
- * Get current status of node in redistribution game
1453
+ * Gets current status of node in redistribution game
1382
1454
  *
1383
- * @param options
1455
+ * @param options HTTP request options, such as `headers` or `timeout`
1384
1456
  */
1385
1457
  async getRedistributionState(options) {
1386
1458
  return stake.getRedistributionState(this.getRequestOptionsForCall(options));
@@ -69,12 +69,12 @@ class Fork {
69
69
  }
70
70
  return cafe_utility_1.Binary.concatBytes(...data);
71
71
  }
72
- static unmarshal(reader) {
72
+ static unmarshal(reader, addressLength) {
73
73
  const type = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
74
74
  const prefixLength = cafe_utility_1.Binary.uint8ToNumber(reader.read(1));
75
75
  const prefix = reader.read(prefixLength);
76
76
  reader.read(30 - prefixLength);
77
- const selfAddress = reader.read(32);
77
+ const selfAddress = reader.read(addressLength);
78
78
  let metadata = undefined;
79
79
  if (isType(type, TYPE_WITH_METADATA)) {
80
80
  const metadataLength = cafe_utility_1.Binary.uint16ToNumber(reader.read(2), 'BE');
@@ -214,7 +214,7 @@ class MantarayNode {
214
214
  const forkBitmap = reader.read(32);
215
215
  for (let i = 0; i < 256; i++) {
216
216
  if (cafe_utility_1.Binary.getBit(forkBitmap, i, 'LE')) {
217
- const newFork = Fork.unmarshal(reader);
217
+ const newFork = Fork.unmarshal(reader, targetAddressLength);
218
218
  node.forks.set(i, newFork);
219
219
  newFork.node.parent = node;
220
220
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getRedistributionState = exports.stake = exports.getStake = void 0;
3
+ exports.getRedistributionState = exports.stake = exports.migrateStake = exports.withdrawSurplusStake = exports.getWithdrawableStake = exports.getStake = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const headers_1 = require("../../utils/headers");
6
6
  const http_1 = require("../../utils/http");
@@ -10,7 +10,7 @@ const typed_bytes_1 = require("../../utils/typed-bytes");
10
10
  const STAKE_ENDPOINT = 'stake';
11
11
  const REDISTRIBUTION_ENDPOINT = 'redistributionstate';
12
12
  /**
13
- * Gets the staked amount
13
+ * Gets the amount of staked BZZ
14
14
  *
15
15
  * @param requestOptions Options for making requests
16
16
  */
@@ -24,12 +24,46 @@ async function getStake(requestOptions) {
24
24
  return tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.stakedAmount, { name: 'stakedAmount' }));
25
25
  }
26
26
  exports.getStake = getStake;
27
+ /**
28
+ * Gets the amount of withdrawable staked BZZ
29
+ *
30
+ * @param requestOptions Options for making requests
31
+ */
32
+ async function getWithdrawableStake(requestOptions) {
33
+ const response = await (0, http_1.http)(requestOptions, {
34
+ method: 'get',
35
+ responseType: 'json',
36
+ url: `${STAKE_ENDPOINT}/withdrawable`,
37
+ });
38
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
39
+ return tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(body.withdrawableAmount, { name: 'withdrawableAmount' }));
40
+ }
41
+ exports.getWithdrawableStake = getWithdrawableStake;
42
+ async function withdrawSurplusStake(requestOptions) {
43
+ const response = await (0, http_1.http)(requestOptions, {
44
+ method: 'delete',
45
+ responseType: 'json',
46
+ url: `${STAKE_ENDPOINT}/withdrawable`,
47
+ });
48
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
49
+ return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asHexString(body.txHash, { name: 'txHash' }));
50
+ }
51
+ exports.withdrawSurplusStake = withdrawSurplusStake;
52
+ async function migrateStake(requestOptions) {
53
+ const response = await (0, http_1.http)(requestOptions, {
54
+ method: 'delete',
55
+ responseType: 'json',
56
+ url: STAKE_ENDPOINT,
57
+ });
58
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
59
+ return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asHexString(body.txHash, { name: 'txHash' }));
60
+ }
61
+ exports.migrateStake = migrateStake;
27
62
  /**
28
63
  * Stake given amount of tokens.
29
64
  *
30
65
  * @param requestOptions Options for making requests
31
66
  * @param amount
32
- * @param options
33
67
  */
34
68
  async function stake(requestOptions, amount, options) {
35
69
  const repsonse = await (0, http_1.http)(requestOptions, {
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWalletBalance = exports.getChainState = exports.getReserveState = void 0;
3
+ exports.withdrawDAI = exports.withdrawBZZ = exports.getWalletBalance = exports.getChainState = exports.getReserveState = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const http_1 = require("../../utils/http");
6
6
  const tokens_1 = require("../../utils/tokens");
7
7
  const type_1 = require("../../utils/type");
8
+ const typed_bytes_1 = require("../../utils/typed-bytes");
8
9
  const workaround_1 = require("../../utils/workaround");
9
10
  const RESERVE_STATE_ENDPOINT = 'reservestate';
10
11
  const WALLET_ENDPOINT = 'wallet';
@@ -69,3 +70,25 @@ async function getWalletBalance(requestOptions) {
69
70
  };
70
71
  }
71
72
  exports.getWalletBalance = getWalletBalance;
73
+ async function withdrawBZZ(requestOptions, amount, address) {
74
+ const response = await (0, http_1.http)(requestOptions, {
75
+ method: 'post',
76
+ url: `${WALLET_ENDPOINT}/withdraw/bzz`,
77
+ responseType: 'json',
78
+ params: { amount: amount.toPLURString(), address: address.toHex() },
79
+ });
80
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
81
+ return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asString(body.transactionHash, { name: 'transactionHash' }));
82
+ }
83
+ exports.withdrawBZZ = withdrawBZZ;
84
+ async function withdrawDAI(requestOptions, amount, address) {
85
+ const response = await (0, http_1.http)(requestOptions, {
86
+ method: 'post',
87
+ url: `${WALLET_ENDPOINT}/withdraw/nativetoken`,
88
+ responseType: 'json',
89
+ params: { amount: amount.toWeiString(), address: address.toHex() },
90
+ });
91
+ const body = cafe_utility_1.Types.asObject(response.data, { name: 'response.data' });
92
+ return new typed_bytes_1.TransactionId(cafe_utility_1.Types.asString(body.transactionHash, { name: 'transactionHash' }));
93
+ }
94
+ exports.withdrawDAI = withdrawDAI;
@@ -8,9 +8,9 @@ const cafe_utility_1 = require("cafe-utility");
8
8
  const major_js_1 = __importDefault(require("semver/functions/major.js"));
9
9
  const debug_1 = require("../../types/debug");
10
10
  const http_1 = require("../../utils/http");
11
- exports.SUPPORTED_BEE_VERSION_EXACT = '2.4.0-390a402e';
11
+ exports.SUPPORTED_BEE_VERSION_EXACT = '2.6.0-d0aa8b93';
12
12
  exports.SUPPORTED_BEE_VERSION = exports.SUPPORTED_BEE_VERSION_EXACT.split('-')[0];
13
- exports.SUPPORTED_API_VERSION = '7.2.0';
13
+ exports.SUPPORTED_API_VERSION = '7.3.0';
14
14
  const NODE_INFO_URL = 'node';
15
15
  const STATUS_URL = 'status';
16
16
  const HEALTH_URL = 'health';
@@ -36,6 +36,7 @@ async function getDebugStatus(requestOptions) {
36
36
  isReachable: cafe_utility_1.Types.asBoolean(body.isReachable, { name: 'isReachable' }),
37
37
  lastSyncedBlock: cafe_utility_1.Types.asNumber(body.lastSyncedBlock, { name: 'lastSyncedBlock' }),
38
38
  committedDepth: cafe_utility_1.Types.asNumber(body.committedDepth, { name: 'committedDepth' }),
39
+ isWarmingUp: cafe_utility_1.Types.asBoolean(body.isWarmingUp, { name: 'isWarmingUp' }),
39
40
  };
40
41
  }
41
42
  exports.getDebugStatus = getDebugStatus;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkConnection = void 0;
3
+ exports.isGateway = exports.checkConnection = void 0;
4
+ const cafe_utility_1 = require("cafe-utility");
4
5
  const http_1 = require("../utils/http");
5
6
  /**
6
7
  * Ping the base bee URL. If connection was not successful throw error
@@ -13,3 +14,16 @@ async function checkConnection(requestOptions) {
13
14
  });
14
15
  }
15
16
  exports.checkConnection = checkConnection;
17
+ async function isGateway(requestOptions) {
18
+ try {
19
+ const response = await (0, http_1.http)(requestOptions, {
20
+ url: '/gateway',
21
+ });
22
+ const data = cafe_utility_1.Types.asObject(response.data);
23
+ return cafe_utility_1.Types.asBoolean(data.gateway);
24
+ }
25
+ catch (error) {
26
+ return false;
27
+ }
28
+ }
29
+ exports.isGateway = isGateway;