@upcoming/bee-js 0.9.0 → 0.10.1
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/modules/debug/stamps.js +5 -8
- package/dist/cjs/modules/debug/states.js +2 -1
- package/dist/cjs/utils/expose.js +3 -5
- package/dist/cjs/utils/stamps.js +12 -46
- package/dist/cjs/utils/workaround.js +27 -0
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/modules/debug/stamps.js +8 -15
- package/dist/mjs/modules/debug/states.js +3 -2
- package/dist/mjs/utils/expose.js +1 -1
- package/dist/mjs/utils/stamps.js +10 -41
- package/dist/mjs/utils/workaround.js +22 -0
- package/dist/types/types/index.d.ts +12 -9
- package/dist/types/utils/expose.d.ts +1 -1
- package/dist/types/utils/stamps.d.ts +6 -31
- package/dist/types/utils/workaround.d.ts +2 -0
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@ const http_1 = require("../../utils/http");
|
|
|
7
7
|
const stamps_1 = require("../../utils/stamps");
|
|
8
8
|
const type_1 = require("../../utils/type");
|
|
9
9
|
const typed_bytes_1 = require("../../utils/typed-bytes");
|
|
10
|
+
const workaround_1 = require("../../utils/workaround");
|
|
10
11
|
const STAMPS_ENDPOINT = 'stamps';
|
|
11
12
|
const BATCHES_ENDPOINT = 'batches';
|
|
12
13
|
async function getGlobalPostageBatches(requestOptions) {
|
|
@@ -42,7 +43,7 @@ async function getAllPostageBatches(requestOptions) {
|
|
|
42
43
|
const depth = cafe_utility_1.Types.asNumber(x.depth, { name: 'depth' });
|
|
43
44
|
const bucketDepth = cafe_utility_1.Types.asNumber(x.bucketDepth, { name: 'bucketDepth' });
|
|
44
45
|
const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
|
|
45
|
-
const batchTTL = cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' });
|
|
46
|
+
const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(x.batchTTL, { name: 'batchTTL' }));
|
|
46
47
|
const duration = duration_1.Duration.fromSeconds(batchTTL);
|
|
47
48
|
return {
|
|
48
49
|
batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(x.batchID, { name: 'batchID' })),
|
|
@@ -54,12 +55,10 @@ async function getAllPostageBatches(requestOptions) {
|
|
|
54
55
|
bucketDepth,
|
|
55
56
|
blockNumber: cafe_utility_1.Types.asNumber(x.blockNumber, { name: 'blockNumber' }),
|
|
56
57
|
immutableFlag: cafe_utility_1.Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
|
|
57
|
-
exists: cafe_utility_1.Types.asBoolean(x.exists, { name: 'exists' }),
|
|
58
|
-
batchTTL,
|
|
59
58
|
usage,
|
|
60
59
|
size: (0, stamps_1.getStampEffectiveBytes)(depth),
|
|
61
60
|
remainingSize: Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage)),
|
|
62
|
-
theoreticalSize: (0, stamps_1.
|
|
61
|
+
theoreticalSize: (0, stamps_1.getStampTheoreticalBytes)(depth),
|
|
63
62
|
duration,
|
|
64
63
|
};
|
|
65
64
|
});
|
|
@@ -76,7 +75,7 @@ async function getPostageBatch(requestOptions, postageBatchId) {
|
|
|
76
75
|
const depth = cafe_utility_1.Types.asNumber(body.depth, { name: 'depth' });
|
|
77
76
|
const bucketDepth = cafe_utility_1.Types.asNumber(body.bucketDepth, { name: 'bucketDepth' });
|
|
78
77
|
const usage = (0, stamps_1.getStampUsage)(utilization, depth, bucketDepth);
|
|
79
|
-
const batchTTL = cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' });
|
|
78
|
+
const batchTTL = (0, workaround_1.normalizeBatchTTL)(cafe_utility_1.Types.asNumber(body.batchTTL, { name: 'batchTTL' }));
|
|
80
79
|
const duration = duration_1.Duration.fromSeconds(batchTTL);
|
|
81
80
|
return {
|
|
82
81
|
batchID: new typed_bytes_1.BatchId(cafe_utility_1.Types.asString(body.batchID, { name: 'batchID' })),
|
|
@@ -88,12 +87,10 @@ async function getPostageBatch(requestOptions, postageBatchId) {
|
|
|
88
87
|
bucketDepth,
|
|
89
88
|
blockNumber: cafe_utility_1.Types.asNumber(body.blockNumber, { name: 'blockNumber' }),
|
|
90
89
|
immutableFlag: cafe_utility_1.Types.asBoolean(body.immutableFlag, { name: 'immutableFlag' }),
|
|
91
|
-
exists: cafe_utility_1.Types.asBoolean(body.exists, { name: 'exists' }),
|
|
92
|
-
batchTTL,
|
|
93
90
|
usage,
|
|
94
91
|
size: (0, stamps_1.getStampEffectiveBytes)(depth),
|
|
95
92
|
remainingSize: Math.ceil((0, stamps_1.getStampEffectiveBytes)(depth) * (1 - usage)),
|
|
96
|
-
theoreticalSize: (0, stamps_1.
|
|
93
|
+
theoreticalSize: (0, stamps_1.getStampTheoreticalBytes)(depth),
|
|
97
94
|
duration,
|
|
98
95
|
};
|
|
99
96
|
}
|
|
@@ -5,6 +5,7 @@ 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 workaround_1 = require("../../utils/workaround");
|
|
8
9
|
const RESERVE_STATE_ENDPOINT = 'reservestate';
|
|
9
10
|
const WALLET_ENDPOINT = 'wallet';
|
|
10
11
|
const CHAIN_STATE_ENDPOINT = 'chainstate';
|
|
@@ -43,7 +44,7 @@ async function getChainState(requestOptions) {
|
|
|
43
44
|
block: cafe_utility_1.Types.asNumber(body.block, { name: 'block' }),
|
|
44
45
|
chainTip: cafe_utility_1.Types.asNumber(body.chainTip, { name: 'chainTip' }),
|
|
45
46
|
totalAmount: (0, type_1.asNumberString)(body.totalAmount, { name: 'totalAmount' }),
|
|
46
|
-
currentPrice: cafe_utility_1.Types.asNumber(body.currentPrice, { name: 'currentPrice' }),
|
|
47
|
+
currentPrice: (0, workaround_1.normalizeCurrentPrice)(cafe_utility_1.Types.asNumber(body.currentPrice, { name: 'currentPrice' })),
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
50
|
exports.getChainState = getChainState;
|
package/dist/cjs/utils/expose.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStampUsage = exports.
|
|
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; } });
|
|
@@ -14,11 +14,9 @@ Object.defineProperty(exports, "getRedundancyStat", { enumerable: true, get: fun
|
|
|
14
14
|
Object.defineProperty(exports, "getRedundancyStats", { enumerable: true, get: function () { return redundancy_1.getRedundancyStats; } });
|
|
15
15
|
var stamps_1 = require("./stamps");
|
|
16
16
|
Object.defineProperty(exports, "getAmountForDuration", { enumerable: true, get: function () { return stamps_1.getAmountForDuration; } });
|
|
17
|
-
Object.defineProperty(exports, "getAmountForTtl", { enumerable: true, get: function () { return stamps_1.getAmountForTtl; } });
|
|
18
|
-
Object.defineProperty(exports, "getDepthForCapacity", { enumerable: true, get: function () { return stamps_1.getDepthForCapacity; } });
|
|
19
17
|
Object.defineProperty(exports, "getDepthForSize", { enumerable: true, get: function () { return stamps_1.getDepthForSize; } });
|
|
20
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; } });
|
|
21
20
|
Object.defineProperty(exports, "getStampEffectiveBytes", { enumerable: true, get: function () { return stamps_1.getStampEffectiveBytes; } });
|
|
22
|
-
Object.defineProperty(exports, "
|
|
23
|
-
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; } });
|
|
24
22
|
Object.defineProperty(exports, "getStampUsage", { enumerable: true, get: function () { return stamps_1.getStampUsage; } });
|
package/dist/cjs/utils/stamps.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.marshalStamp = exports.convertEnvelopeToMarshaledStamp = exports.getDepthForSize = exports.
|
|
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,20 +18,14 @@ function getStampUsage(utilization, depth, bucketDepth) {
|
|
|
17
18
|
}
|
|
18
19
|
exports.getStampUsage = getStampUsage;
|
|
19
20
|
/**
|
|
20
|
-
* Utility function that calculates the theoritical maximum
|
|
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
|
|
23
|
-
*
|
|
24
|
-
* @deprecated Use `getStampTheoreticalBytes` instead.
|
|
23
|
+
* For smaller depths (up to 22), this may provide less accurate results.
|
|
25
24
|
*
|
|
26
|
-
* @returns {number} The maximum
|
|
25
|
+
* @returns {number} The maximum theoretical size of the postage batch in bytes.
|
|
27
26
|
*/
|
|
28
|
-
function getStampMaximumCapacityBytes(depth) {
|
|
29
|
-
return 2 ** depth * 4096;
|
|
30
|
-
}
|
|
31
|
-
exports.getStampMaximumCapacityBytes = getStampMaximumCapacityBytes;
|
|
32
27
|
function getStampTheoreticalBytes(depth) {
|
|
33
|
-
return
|
|
28
|
+
return 4096 * 2 ** depth;
|
|
34
29
|
}
|
|
35
30
|
exports.getStampTheoreticalBytes = getStampTheoreticalBytes;
|
|
36
31
|
/**
|
|
@@ -78,15 +73,14 @@ function getStampEffectiveBytes(depth) {
|
|
|
78
73
|
return 0;
|
|
79
74
|
}
|
|
80
75
|
const utilRate = utilisationRateMap[depth] ?? 0.99;
|
|
81
|
-
return Math.ceil(
|
|
76
|
+
return Math.ceil(getStampTheoreticalBytes(depth) * utilRate);
|
|
82
77
|
}
|
|
83
78
|
exports.getStampEffectiveBytes = getStampEffectiveBytes;
|
|
84
79
|
/**
|
|
85
80
|
* Utility function that calculates the cost of a postage batch based on its depth and amount.
|
|
86
81
|
*/
|
|
87
82
|
function getStampCost(depth, amount) {
|
|
88
|
-
|
|
89
|
-
return tokens_1.BZZ.fromPLUR(2n ** BigInt(depth) * amountBigInt);
|
|
83
|
+
return tokens_1.BZZ.fromPLUR(2n ** BigInt(depth) * BigInt(amount));
|
|
90
84
|
}
|
|
91
85
|
exports.getStampCost = getStampCost;
|
|
92
86
|
/**
|
|
@@ -94,27 +88,13 @@ exports.getStampCost = getStampCost;
|
|
|
94
88
|
*
|
|
95
89
|
* For more accurate results, get the price per block and block time from the Bee node or the blockchain.
|
|
96
90
|
*
|
|
97
|
-
* @returns {number} The TTL of the postage batch
|
|
91
|
+
* @returns {number} The TTL of the postage batch.
|
|
98
92
|
*/
|
|
99
|
-
function
|
|
93
|
+
function getStampDuration(amount, pricePerBlock, blockTime = 5) {
|
|
100
94
|
const amountBigInt = BigInt((0, type_1.asNumberString)(amount));
|
|
101
|
-
return (amountBigInt * BigInt(blockTime)) / BigInt(pricePerBlock);
|
|
102
|
-
}
|
|
103
|
-
exports.getStampTtlSeconds = getStampTtlSeconds;
|
|
104
|
-
/**
|
|
105
|
-
* Utility function that calculates the amount of tokens required to maintain a given Time To Live (TTL) for a postage batch.
|
|
106
|
-
*
|
|
107
|
-
* This function estimates the required amount based on the provided TTL in days.
|
|
108
|
-
*
|
|
109
|
-
* @deprecated Use `getAmountForDuration` instead.
|
|
110
|
-
*
|
|
111
|
-
* @param {number} days - The Time To Live (TTL) in days.
|
|
112
|
-
* @returns {NumberString} The estimated amount of tokens needed for the specified TTL.
|
|
113
|
-
*/
|
|
114
|
-
function getAmountForTtl(days, pricePerBlock = 24000, blockTime = 5) {
|
|
115
|
-
return (days <= 0 ? 1n : BigInt(days)) * ((24n * 60n * 60n * BigInt(pricePerBlock)) / BigInt(blockTime));
|
|
95
|
+
return duration_1.Duration.fromSeconds(Number((amountBigInt * BigInt(blockTime)) / BigInt(pricePerBlock)));
|
|
116
96
|
}
|
|
117
|
-
exports.
|
|
97
|
+
exports.getStampDuration = getStampDuration;
|
|
118
98
|
/**
|
|
119
99
|
* Get the postage batch `amount` required for a given `duration`.
|
|
120
100
|
*
|
|
@@ -126,20 +106,6 @@ function getAmountForDuration(duration, pricePerBlock, blockTime = 5) {
|
|
|
126
106
|
return (BigInt(duration.toSeconds()) / BigInt(blockTime)) * BigInt(pricePerBlock);
|
|
127
107
|
}
|
|
128
108
|
exports.getAmountForDuration = getAmountForDuration;
|
|
129
|
-
/**
|
|
130
|
-
* Utility function that calculates the depth required for a postage batch to achieve the specified capacity in gigabytes.
|
|
131
|
-
*
|
|
132
|
-
* The depth is determined based on the given gigabytes, and the result is adjusted to a minimum depth of 18.
|
|
133
|
-
*
|
|
134
|
-
* @deprecated Use `getDepthForSize` instead.
|
|
135
|
-
*
|
|
136
|
-
* @param {number} gigabytes - The desired capacity of the postage batch in gigabytes.
|
|
137
|
-
* @returns {number} The calculated depth necessary to achieve the specified capacity.
|
|
138
|
-
*/
|
|
139
|
-
function getDepthForCapacity(gigabytes) {
|
|
140
|
-
return gigabytes <= 1 ? 18 : Math.ceil(Math.log2(Math.ceil(gigabytes)) + 18);
|
|
141
|
-
}
|
|
142
|
-
exports.getDepthForCapacity = getDepthForCapacity;
|
|
143
109
|
/**
|
|
144
110
|
* Utility function that calculates the depth required for a postage batch to achieve the specified effective size in gigabytes.
|
|
145
111
|
*
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// TODO: Remove this file after the issue is fixed
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.normalizeCurrentPrice = exports.normalizeBatchTTL = void 0;
|
|
5
|
+
function normalizeBatchTTL(batchTTL) {
|
|
6
|
+
if (!Number.isInteger(batchTTL)) {
|
|
7
|
+
return 1;
|
|
8
|
+
}
|
|
9
|
+
if (batchTTL < 1) {
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
if (batchTTL > 315569260) {
|
|
13
|
+
return 315569260;
|
|
14
|
+
}
|
|
15
|
+
return batchTTL;
|
|
16
|
+
}
|
|
17
|
+
exports.normalizeBatchTTL = normalizeBatchTTL;
|
|
18
|
+
function normalizeCurrentPrice(currentPrice) {
|
|
19
|
+
if (!Number.isInteger(currentPrice)) {
|
|
20
|
+
return 24000;
|
|
21
|
+
}
|
|
22
|
+
if (currentPrice < 24000) {
|
|
23
|
+
return 24000;
|
|
24
|
+
}
|
|
25
|
+
return currentPrice;
|
|
26
|
+
}
|
|
27
|
+
exports.normalizeCurrentPrice = normalizeCurrentPrice;
|