@ethersphere/bee-js 9.4.1 → 9.6.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
@@ -896,6 +896,17 @@ class Bee {
896
896
  }
897
897
  return true;
898
898
  }
899
+ /**
900
+ * Checks the `/gateway` endpoint to see if the remote API is a gateway.
901
+ *
902
+ * Do note that this is not a standard way to check for gateway nodes,
903
+ * but some of the gateway tooling expose this endpoint.
904
+ *
905
+ * @param options
906
+ */
907
+ async isGateway(options) {
908
+ return status.isGateway(this.getRequestOptionsForCall(options));
909
+ }
899
910
  // Legacy debug API
900
911
  async getNodeAddresses(options) {
901
912
  return connectivity.getNodeAddresses(this.getRequestOptionsForCall(options));
@@ -1011,13 +1022,24 @@ class Bee {
1011
1022
  return chequebook.cashoutLastCheque(this.getRequestOptionsForCall(requestOptions), address, options);
1012
1023
  }
1013
1024
  /**
1014
- * Deposit tokens from overlay address into chequebook
1025
+ * Deposit tokens from node wallet into chequebook
1015
1026
  *
1016
1027
  * @param amount Amount of tokens to deposit (must be positive integer)
1017
1028
  * @param gasPrice Gas Price in WEI for the transaction call
1018
1029
  * @return string Hash of the transaction
1030
+ * @deprecated Use `depositBZZToChequebook` instead.
1019
1031
  */
1020
1032
  async depositTokens(amount, gasPrice, options) {
1033
+ return this.depositBZZToChequebook(amount, gasPrice, options);
1034
+ }
1035
+ /**
1036
+ * Deposit tokens from node wallet into chequebook
1037
+ *
1038
+ * @param amount Amount of tokens to deposit (must be positive integer)
1039
+ * @param gasPrice Gas Price in WEI for the transaction call
1040
+ * @return string Hash of the transaction
1041
+ */
1042
+ async depositBZZToChequebook(amount, gasPrice, options) {
1021
1043
  const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1022
1044
  let gasPriceString;
1023
1045
  if (gasPrice) {
@@ -1026,13 +1048,24 @@ class Bee {
1026
1048
  return chequebook.depositTokens(this.getRequestOptionsForCall(options), amountString, gasPriceString);
1027
1049
  }
1028
1050
  /**
1029
- * Withdraw tokens from the chequebook to the overlay address
1051
+ * Withdraw tokens from the chequebook to the node wallet
1030
1052
  *
1031
1053
  * @param amount Amount of tokens to withdraw (must be positive integer)
1032
1054
  * @param gasPrice Gas Price in WEI for the transaction call
1033
1055
  * @return string Hash of the transaction
1056
+ * @deprecated Use `withdrawBZZFromChequebook` instead.
1034
1057
  */
1035
1058
  async withdrawTokens(amount, gasPrice, options) {
1059
+ return this.withdrawBZZFromChequebook(amount, gasPrice, options);
1060
+ }
1061
+ /**
1062
+ * Withdraw tokens from the chequebook to the node wallet
1063
+ *
1064
+ * @param amount Amount of tokens to withdraw (must be positive integer)
1065
+ * @param gasPrice Gas Price in WEI for the transaction call
1066
+ * @return string Hash of the transaction
1067
+ */
1068
+ async withdrawBZZFromChequebook(amount, gasPrice, options) {
1036
1069
  // TODO: check BZZ in tests
1037
1070
  const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
1038
1071
  let gasPriceString;
@@ -1041,6 +1074,16 @@ class Bee {
1041
1074
  }
1042
1075
  return chequebook.withdrawTokens(this.getRequestOptionsForCall(options), amountString, gasPriceString);
1043
1076
  }
1077
+ async withdrawBZZToExternalWallet(amount, address, options) {
1078
+ amount = amount instanceof tokens_1.BZZ ? amount : tokens_1.BZZ.fromPLUR(amount);
1079
+ address = new typed_bytes_1.EthAddress(address);
1080
+ return states.withdrawBZZ(this.getRequestOptionsForCall(options), amount, address);
1081
+ }
1082
+ async withdrawDAIToExternalWallet(amount, address, options) {
1083
+ amount = amount instanceof tokens_1.DAI ? amount : tokens_1.DAI.fromWei(amount);
1084
+ address = new typed_bytes_1.EthAddress(address);
1085
+ return states.withdrawDAI(this.getRequestOptionsForCall(options), amount, address);
1086
+ }
1044
1087
  /*
1045
1088
  * Settlements endpoint
1046
1089
  */
@@ -1124,7 +1167,7 @@ class Bee {
1124
1167
  return states.getChainState(this.getRequestOptionsForCall(options));
1125
1168
  }
1126
1169
  /**
1127
- * Get wallet balances for xDai and BZZ of the Bee node
1170
+ * Get wallet balances for DAI and BZZ of the Bee node
1128
1171
  *
1129
1172
  * @param options
1130
1173
  */
@@ -1342,7 +1385,7 @@ class Bee {
1342
1385
  return transactions.rebroadcastTransaction(this.getRequestOptionsForCall(options), transactionHash);
1343
1386
  }
1344
1387
  /**
1345
- * Cancel currently pending transaction
1388
+ * Cancels a currently pending transaction
1346
1389
  * @param transactionHash
1347
1390
  * @param gasPrice
1348
1391
  */
@@ -1355,20 +1398,48 @@ class Bee {
1355
1398
  return transactions.cancelTransaction(this.getRequestOptionsForCall(options), transactionHash, gasPriceString);
1356
1399
  }
1357
1400
  /**
1358
- * Gets the staked amount of BZZ (in PLUR unit) as number string.
1401
+ * Gets the amount of staked BZZ
1359
1402
  *
1360
- * @param options
1403
+ * @param options HTTP request options, such as `headers` or `timeout`
1361
1404
  */
1362
1405
  async getStake(options) {
1363
1406
  return stake.getStake(this.getRequestOptionsForCall(options));
1364
1407
  }
1365
1408
  /**
1366
- * Deposits given amount of BZZ token (in PLUR unit).
1409
+ * Gets the amount of withdrawable staked BZZ.
1410
+ *
1411
+ * @param options HTTP request options, such as `headers` or `timeout`
1412
+ */
1413
+ async getWithdrawableStake(options) {
1414
+ return stake.getWithdrawableStake(this.getRequestOptionsForCall(options));
1415
+ }
1416
+ /**
1417
+ * Withdraws ALL surplus staked BZZ to the node wallet.
1418
+ *
1419
+ * Use the `getWithdrawableStake` method to check how much surplus stake is available.
1420
+ *
1421
+ * @param options HTTP request options, such as `headers` or `timeout`
1422
+ */
1423
+ async withdrawSurplusStake(options) {
1424
+ return stake.withdrawSurplusStake(this.getRequestOptionsForCall(options));
1425
+ }
1426
+ /**
1427
+ * Withdraws all staked BZZ to the node wallet.
1428
+ *
1429
+ * **Only available when the staking contract is paused and is in the process of being migrated to a new contract!**
1430
+ *
1431
+ * @param options HTTP request options, such as `headers` or `timeout`
1432
+ */
1433
+ async migrateStake(options) {
1434
+ return stake.migrateStake(this.getRequestOptionsForCall(options));
1435
+ }
1436
+ /**
1437
+ * Stakes the given amount of BZZ. Initial deposit must be at least 10 BZZ.
1367
1438
  *
1368
1439
  * Be aware that staked BZZ tokens can **not** be withdrawn.
1369
1440
  *
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
1441
+ * @param amount Amount of BZZ tokens to be staked. If not providing a `BZZ` instance, the amount is denoted in PLUR.
1442
+ * @param options HTTP request options, such as `headers` or `timeout`
1372
1443
  */
1373
1444
  async depositStake(amount, options, requestOptions) {
1374
1445
  const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
@@ -1378,9 +1449,9 @@ class Bee {
1378
1449
  return stake.stake(this.getRequestOptionsForCall(requestOptions), amountString, options);
1379
1450
  }
1380
1451
  /**
1381
- * Get current status of node in redistribution game
1452
+ * Gets current status of node in redistribution game
1382
1453
  *
1383
- * @param options
1454
+ * @param options HTTP request options, such as `headers` or `timeout`
1384
1455
  */
1385
1456
  async getRedistributionState(options) {
1386
1457
  return stake.getRedistributionState(this.getRequestOptionsForCall(options));
@@ -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;
@@ -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;