@ickb/sdk 1000.0.43 → 1000.0.71
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/codec.d.ts +14 -0
- package/dist/cjs/codec.d.ts.map +1 -0
- package/dist/cjs/codec.js +125 -0
- package/dist/cjs/codec.js.map +1 -0
- package/dist/cjs/constants.d.ts +49 -0
- package/dist/cjs/constants.d.ts.map +1 -0
- package/dist/cjs/constants.js +156 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -78
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +17 -232
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/sdk.d.ts +178 -0
- package/dist/cjs/sdk.d.ts.map +1 -0
- package/dist/cjs/sdk.js +362 -0
- package/dist/cjs/sdk.js.map +1 -0
- package/dist/esm/codec.d.ts +14 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +122 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/constants.d.ts +49 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +152 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/index.d.ts +3 -78
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -230
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/sdk.d.ts +178 -0
- package/dist/esm/sdk.d.ts.map +1 -0
- package/dist/esm/sdk.js +358 -0
- package/dist/esm/sdk.js.map +1 -0
- package/package.json +9 -9
- package/src/codec.ts +138 -0
- package/src/constants.ts +200 -0
- package/src/index.ts +3 -407
- package/src/sdk.ts +513 -0
package/dist/cjs/sdk.js
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IckbSdk = void 0;
|
|
4
|
+
const core_1 = require("@ckb-ccc/core");
|
|
5
|
+
const utils_1 = require("@ickb/utils");
|
|
6
|
+
const core_2 = require("@ickb/core");
|
|
7
|
+
const order_1 = require("@ickb/order");
|
|
8
|
+
const constants_js_1 = require("./constants.js");
|
|
9
|
+
const codec_js_1 = require("./codec.js");
|
|
10
|
+
/**
|
|
11
|
+
* SDK for managing iCKB operations.
|
|
12
|
+
*/
|
|
13
|
+
class IckbSdk {
|
|
14
|
+
/**
|
|
15
|
+
* Creates an instance of IckbSdk.
|
|
16
|
+
*
|
|
17
|
+
* @param ownedOwner - The manager for owned owner operations.
|
|
18
|
+
* @param ickbLogic - The manager for iCKB logic operations.
|
|
19
|
+
* @param order - The manager for order operations.
|
|
20
|
+
* @param capacity - The capacity manager instance.
|
|
21
|
+
* @param bots - An array of bot lock scripts.
|
|
22
|
+
*/
|
|
23
|
+
constructor(ownedOwner, ickbLogic, order, capacity, bots) {
|
|
24
|
+
this.ownedOwner = ownedOwner;
|
|
25
|
+
this.ickbLogic = ickbLogic;
|
|
26
|
+
this.order = order;
|
|
27
|
+
this.capacity = capacity;
|
|
28
|
+
this.bots = bots;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates an instance of IckbSdk from script dependencies.
|
|
32
|
+
*
|
|
33
|
+
* @param args - Parameters matching those of getConfig.
|
|
34
|
+
* @returns A new instance of IckbSdk.
|
|
35
|
+
*/
|
|
36
|
+
static from(...args) {
|
|
37
|
+
const { managers: { ownedOwner, ickbLogic, order, capacity }, bots, } = (0, constants_js_1.getConfig)(...args);
|
|
38
|
+
return new IckbSdk(ownedOwner, ickbLogic, order, capacity, bots);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Previews the conversion between CKB and UDT.
|
|
42
|
+
*
|
|
43
|
+
* This method calculates a conversion preview using an exchange ratio midpoint.
|
|
44
|
+
* Optionally, a fee may be applied that influences the effective conversion rate,
|
|
45
|
+
* scaling the converted amount by (feeBase - fee) / feeBase.
|
|
46
|
+
*
|
|
47
|
+
* @param isCkb2Udt - Indicates the conversion direction:
|
|
48
|
+
* - true: Convert CKB to UDT.
|
|
49
|
+
* - false: Convert UDT to CKB.
|
|
50
|
+
* @param amounts - An object containing value components (amounts for CKB and UDT).
|
|
51
|
+
* @param system - The current system state containing exchange ratio, fee rate, tip, and order-related information.
|
|
52
|
+
* @param options - Optional parameters for fee and matching:
|
|
53
|
+
* - fee: The fee to apply in integer terms (defaults to 1n for 0.001% fee).
|
|
54
|
+
* - feeBase: The base used for fee scaling (defaults to 100000n).
|
|
55
|
+
*
|
|
56
|
+
* @returns An object with:
|
|
57
|
+
* - convertedAmount: The estimated converted amount as a FixedPoint.
|
|
58
|
+
* - ckbFee: The fee (or gain) in CKB, as a FixedPoint.
|
|
59
|
+
* - info: Additional conversion metadata.
|
|
60
|
+
* - maturity: Optional maturity information (as ccc.Num) if meets criteria.
|
|
61
|
+
*/
|
|
62
|
+
static estimate(isCkb2Udt, amounts, system, options) {
|
|
63
|
+
// Apply a 0.001% default fee if none provided.
|
|
64
|
+
options = {
|
|
65
|
+
fee: 1n,
|
|
66
|
+
feeBase: 100000n,
|
|
67
|
+
...options,
|
|
68
|
+
};
|
|
69
|
+
const { convertedAmount, ckbFee, info } = order_1.OrderManager.convert(isCkb2Udt, system.exchangeRatio, amounts, options);
|
|
70
|
+
// If the fee meets a threshold, calculate the order maturity; otherwise, maturity is undefined.
|
|
71
|
+
const maturity = ckbFee >= 10n * system.feeRate
|
|
72
|
+
? IckbSdk.maturity({ info, amounts }, system)
|
|
73
|
+
: undefined;
|
|
74
|
+
return { convertedAmount, ckbFee, info, maturity };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Estimates the maturity for an order formatted as a Unix timestamp.
|
|
78
|
+
*
|
|
79
|
+
* Depending on the order type and amount remaining, the method calculates an estimated timestamp
|
|
80
|
+
* when the order (or part thereof) might be fulfilled.
|
|
81
|
+
*
|
|
82
|
+
* @param o - Either an OrderCell or an object containing order Info and value components.
|
|
83
|
+
* @param system - The current system state.
|
|
84
|
+
* @returns The Unix timestamp of estimated maturity as a bigint (in milliseconds) or undefined if not applicable.
|
|
85
|
+
*/
|
|
86
|
+
static maturity(o, system) {
|
|
87
|
+
const info = "info" in o ? o.info : o.data.info;
|
|
88
|
+
const amounts = "amounts" in o
|
|
89
|
+
? o.amounts
|
|
90
|
+
: { ckbValue: o.ckbUnoccupied, udtValue: o.udtValue };
|
|
91
|
+
// Dual-ratio orders have no fixed maturity.
|
|
92
|
+
if (info.isDualRatio()) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const isCkb2Udt = info.isCkb2Udt();
|
|
96
|
+
const amount = isCkb2Udt ? amounts.ckbValue : amounts.udtValue;
|
|
97
|
+
const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
|
|
98
|
+
// If order is already fulfilled.
|
|
99
|
+
if (amount === 0n) {
|
|
100
|
+
return 0n;
|
|
101
|
+
}
|
|
102
|
+
const { tip, exchangeRatio, orderPool, ckbAvailable, ckbMaturing } = system;
|
|
103
|
+
// Create a reference ratio instance for comparison.
|
|
104
|
+
const b = new order_1.Info(ratio, ratio, 1);
|
|
105
|
+
let ckb = isCkb2Udt ? amount : 0n;
|
|
106
|
+
let udt = isCkb2Udt ? 0n : amount;
|
|
107
|
+
for (const o of orderPool) {
|
|
108
|
+
const a = o.data.info;
|
|
109
|
+
if (a.isCkb2Udt()) {
|
|
110
|
+
// If not isCkb2Udt or a worse ratio, add available CKB.
|
|
111
|
+
if (!isCkb2Udt || a.ckb2UdtCompare(b) < 0) {
|
|
112
|
+
ckb += o.ckbUnoccupied;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
// Conversely for UDT to CKB orders.
|
|
117
|
+
if (isCkb2Udt || a.udt2CkbCompare(b) < 0) {
|
|
118
|
+
udt += o.udtValue;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// Adjust ckb by the converted UDT amount.
|
|
123
|
+
ckb -= (0, core_2.convert)(false, udt, exchangeRatio);
|
|
124
|
+
// Minimum maturity of 10 minutes (in milliseconds).
|
|
125
|
+
let maturity = 10n * 60n * 1000n;
|
|
126
|
+
if (isCkb2Udt) {
|
|
127
|
+
// For CKB to UDT orders, extend maturity based on the CKB amount.
|
|
128
|
+
if (ckb > 0n) {
|
|
129
|
+
maturity *= 1n + ckb / core_1.ccc.fixedPointFrom("200000");
|
|
130
|
+
}
|
|
131
|
+
return maturity + ("info" in o ? BigInt(Date.now()) : tip.timestamp);
|
|
132
|
+
}
|
|
133
|
+
// For UDT to CKB orders, add available CKB.
|
|
134
|
+
ckb += ckbAvailable;
|
|
135
|
+
if (ckb >= 0) {
|
|
136
|
+
return maturity + ("info" in o ? BigInt(Date.now()) : tip.timestamp);
|
|
137
|
+
}
|
|
138
|
+
// Find the earliest maturity in the ckbMaturing array that satisfies the required CKB.
|
|
139
|
+
const ckbNeeded = -ckb;
|
|
140
|
+
const i = (0, utils_1.binarySearch)(ckbMaturing.length,
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
142
|
+
(n) => ckbMaturing[n].ckbCumulative >= ckbNeeded);
|
|
143
|
+
return ckbMaturing[i]?.maturity;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Mints a new order cell and appends it to the transaction.
|
|
147
|
+
*
|
|
148
|
+
* The method performs the following operations:
|
|
149
|
+
* - Creates order cell data using provided amounts and order information.
|
|
150
|
+
* - Adds required cell dependencies and UDT handlers to the transaction.
|
|
151
|
+
* - Appends the order cell to the transaction outputs.
|
|
152
|
+
*
|
|
153
|
+
* @param tx - The smart transaction to which the order cell is added.
|
|
154
|
+
* @param user - The user, represented either as a Signer or a Script.
|
|
155
|
+
* @param info - The order information meta data (usually computed via OrderManager.convert).
|
|
156
|
+
* @param amounts - The value components for the order, including:
|
|
157
|
+
* - ckbValue: The CKB amount (may include an internal surplus).
|
|
158
|
+
* - udtValue: The UDT amount.
|
|
159
|
+
*
|
|
160
|
+
* @returns A Promise resolving to void.
|
|
161
|
+
*/
|
|
162
|
+
async request(tx, user, info, amounts) {
|
|
163
|
+
// If the user is provided as a Signer, extract the recommended lock script.
|
|
164
|
+
user =
|
|
165
|
+
"codeHash" in user
|
|
166
|
+
? user
|
|
167
|
+
: (await user.getRecommendedAddressObj()).script;
|
|
168
|
+
this.order.mint(tx, user, info, amounts);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Melts (cancels) the specified order groups.
|
|
172
|
+
*
|
|
173
|
+
* For each order group, if the option is set to process fulfilled orders only,
|
|
174
|
+
* it filters accordingly. Then, for every valid group, the master and order cells are added
|
|
175
|
+
* as inputs to the transaction.
|
|
176
|
+
*
|
|
177
|
+
* @param tx - The smart transaction to which the inputs are added.
|
|
178
|
+
* @param groups - An array of order groups to be melted.
|
|
179
|
+
* @param options - Optional parameters:
|
|
180
|
+
* - isFulfilledOnly: If true, only order groups with fully or partially fulfilled orders are processed.
|
|
181
|
+
*
|
|
182
|
+
* @returns void
|
|
183
|
+
*/
|
|
184
|
+
collect(tx, groups, options) {
|
|
185
|
+
this.order.melt(tx, groups, options);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Retrieves the L1 state from the blockchain.
|
|
189
|
+
*
|
|
190
|
+
* The method performs the following:
|
|
191
|
+
* - Obtains the current block tip and calculates the exchange ratio.
|
|
192
|
+
* - Fetches available CKB and the maturing CKB based on bot capacities and deposit snapshots.
|
|
193
|
+
* - Filters orders into user-owned and system orders based on the provided locks.
|
|
194
|
+
* - Estimates user-owned orders maturity.
|
|
195
|
+
*
|
|
196
|
+
* @param client - The blockchain client interface.
|
|
197
|
+
* @param locks - An array of lock scripts used to filter user cells.
|
|
198
|
+
* @returns A promise that resolves to an object containing:
|
|
199
|
+
* - system: The system state (fee rate, tip header, exchange ratio, order pool, etc.).
|
|
200
|
+
* - user: The user's orders grouped as an array of OrderGroup.
|
|
201
|
+
*/
|
|
202
|
+
async getL1State(client, locks) {
|
|
203
|
+
const tip = await client.getTipHeader();
|
|
204
|
+
const exchangeRatio = order_1.Ratio.from((0, core_2.ickbExchangeRatio)(tip));
|
|
205
|
+
// Parallel fetching of system components.
|
|
206
|
+
const [{ ckbAvailable, ckbMaturing }, orders, feeRate] = await Promise.all([
|
|
207
|
+
this.getCkb(client, tip),
|
|
208
|
+
(0, utils_1.collect)(this.order.findOrders(client)),
|
|
209
|
+
client.getFeeRate(),
|
|
210
|
+
]);
|
|
211
|
+
const midInfo = new order_1.Info(exchangeRatio, exchangeRatio, 1);
|
|
212
|
+
const userOrders = [];
|
|
213
|
+
const systemOrders = [];
|
|
214
|
+
for (const group of orders) {
|
|
215
|
+
if (group.isOwner(...locks)) {
|
|
216
|
+
userOrders.push(group);
|
|
217
|
+
}
|
|
218
|
+
const { order } = group;
|
|
219
|
+
const info = order.data.info;
|
|
220
|
+
if ((order.isCkb2UdtMatchable() && info.ckb2UdtCompare(midInfo) < 0) ||
|
|
221
|
+
(order.isUdt2CkbMatchable() && info.udt2CkbCompare(midInfo) < 0)) {
|
|
222
|
+
systemOrders.push(order);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const system = {
|
|
226
|
+
feeRate,
|
|
227
|
+
tip,
|
|
228
|
+
exchangeRatio,
|
|
229
|
+
orderPool: systemOrders,
|
|
230
|
+
ckbAvailable,
|
|
231
|
+
ckbMaturing,
|
|
232
|
+
};
|
|
233
|
+
// Estimates user orders maturity.
|
|
234
|
+
for (const { order } of userOrders) {
|
|
235
|
+
order.maturity = IckbSdk.maturity(order, system);
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
system,
|
|
239
|
+
user: {
|
|
240
|
+
orders: userOrders,
|
|
241
|
+
},
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Retrieves available CKB and maturing CKB values from the blockchain.
|
|
246
|
+
*
|
|
247
|
+
* This method:
|
|
248
|
+
* - Fetches bot withdrawal requests and deposit snapshots.
|
|
249
|
+
* - Aggregates available CKB balances from bot capacities.
|
|
250
|
+
* - Calculates maturing CKB values (with their expected maturity timestamps)
|
|
251
|
+
* based on deposit pool snapshots or via direct deposit cell lookups.
|
|
252
|
+
* - Sorts and cumulatively sums the maturing values for later lookup.
|
|
253
|
+
*
|
|
254
|
+
* @param client - The blockchain client used for fetching data.
|
|
255
|
+
* @param tip - The current block tip header.
|
|
256
|
+
* @returns A Promise that resolves with:
|
|
257
|
+
* - ckbAvailable: The total available CKB (as a FixedPoint).
|
|
258
|
+
* - ckbMaturing: An array of maturing CKB objects containing the cumulative CKB
|
|
259
|
+
* and the associated maturity timestamp.
|
|
260
|
+
*/
|
|
261
|
+
async getCkb(client, tip) {
|
|
262
|
+
const opts = {
|
|
263
|
+
onChain: true,
|
|
264
|
+
tip,
|
|
265
|
+
};
|
|
266
|
+
// Start fetching bot iCKB withdrawal requests.
|
|
267
|
+
const promiseBotWithdrawals = (0, utils_1.collect)(this.ownedOwner.findWithdrawalGroups(client, this.bots, opts));
|
|
268
|
+
// Initialize deposit pool snapshot.
|
|
269
|
+
let poolSnapshotHex = "0x";
|
|
270
|
+
let poolSnapshotEpoch = utils_1.Epoch.from([0n, 0n, 1n]);
|
|
271
|
+
// Map to track each bot's available CKB (minus a reserved amount for internal operations).
|
|
272
|
+
const bot2Ckb = new Map();
|
|
273
|
+
const reserved = -core_1.ccc.fixedPointFrom("2000");
|
|
274
|
+
for await (const c of this.capacity.findCapacities(client, this.bots, opts)) {
|
|
275
|
+
const key = (0, utils_1.hexFrom)(c.cell.cellOutput.lock);
|
|
276
|
+
const ckb = (bot2Ckb.get(key) ?? reserved) + c.ckbValue;
|
|
277
|
+
bot2Ckb.set(key, ckb);
|
|
278
|
+
// Find the most recent deposit pool snapshot from bot cell output data.
|
|
279
|
+
const outputData = c.cell.outputData;
|
|
280
|
+
if (outputData.length % 256 === 2) {
|
|
281
|
+
const h = await (0, utils_1.getHeader)(client, {
|
|
282
|
+
type: "txHash",
|
|
283
|
+
value: c.cell.outPoint.txHash,
|
|
284
|
+
});
|
|
285
|
+
const e = utils_1.Epoch.from(h.epoch);
|
|
286
|
+
if (poolSnapshotEpoch.compare(e) < 0) {
|
|
287
|
+
poolSnapshotHex = outputData;
|
|
288
|
+
poolSnapshotEpoch = e;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const ckbMaturing = new Array();
|
|
293
|
+
for (const wr of await promiseBotWithdrawals) {
|
|
294
|
+
if (wr.owned.isReady) {
|
|
295
|
+
// Update the bot's CKB based on withdrawal if the bot is ready.
|
|
296
|
+
const key = (0, utils_1.hexFrom)(wr.owner.cell.cellOutput.lock);
|
|
297
|
+
const ckb = (bot2Ckb.get(key) ?? reserved) + wr.ckbValue;
|
|
298
|
+
bot2Ckb.set(key, ckb);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
// Otherwise, add to maturing amounts.
|
|
302
|
+
ckbMaturing.push({
|
|
303
|
+
ckbValue: wr.ckbValue,
|
|
304
|
+
maturity: wr.owned.maturity.toUnix(tip),
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
// Sum available CKB across all bot lock scripts.
|
|
308
|
+
let ckbAvailable = 0n;
|
|
309
|
+
for (const ckb of bot2Ckb.values()) {
|
|
310
|
+
if (ckb > 0n) {
|
|
311
|
+
ckbAvailable += ckb;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// Estimate available CKB from deposit pool snapshot.
|
|
315
|
+
const tipEpoch = utils_1.Epoch.from(tip.epoch);
|
|
316
|
+
const oneCycle = utils_1.Epoch.from([180n, 0n, 1n]);
|
|
317
|
+
if (poolSnapshotHex !== "0x") {
|
|
318
|
+
const eNumber = tip.epoch[0];
|
|
319
|
+
let start = utils_1.Epoch.from([eNumber - (eNumber % 180n), 0n, 1n]);
|
|
320
|
+
const step = utils_1.Epoch.from([0n, 180n, 1024n]);
|
|
321
|
+
const depositSize = (0, core_2.convert)(false, core_2.ICKB_DEPOSIT_CAP, tip);
|
|
322
|
+
for (const binAmount of codec_js_1.PoolSnapshot.decode(poolSnapshotHex)) {
|
|
323
|
+
const end = start.add(step);
|
|
324
|
+
if (binAmount > 0) {
|
|
325
|
+
ckbMaturing.push({
|
|
326
|
+
ckbValue: BigInt(binAmount) * depositSize,
|
|
327
|
+
maturity: tipEpoch.compare(tipEpoch) < 0
|
|
328
|
+
? // If the bin has already started, assume worst-case timing.
|
|
329
|
+
end.add(oneCycle).toUnix(tip)
|
|
330
|
+
: // Otherwise, use the bin end as the maturity.
|
|
331
|
+
end.toUnix(tip),
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
start = end;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
// Without snapshot data, fetch deposits directly.
|
|
339
|
+
for await (const d of this.ickbLogic.findDeposits(client, opts)) {
|
|
340
|
+
ckbMaturing.push({
|
|
341
|
+
ckbValue: d.ckbValue,
|
|
342
|
+
maturity: d.maturity.toUnix(tip),
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
// Sort maturing CKB entries by their maturity timestamp.
|
|
347
|
+
ckbMaturing.sort((a, b) => Number(a.maturity - b.maturity));
|
|
348
|
+
// Calculate cumulative maturing CKB values.
|
|
349
|
+
let cumulative = 0n;
|
|
350
|
+
const ckbCumulativeMaturing = [];
|
|
351
|
+
for (const { ckbValue, maturity } of ckbMaturing) {
|
|
352
|
+
cumulative += ckbValue;
|
|
353
|
+
ckbCumulativeMaturing.push({ ckbCumulative: cumulative, maturity });
|
|
354
|
+
}
|
|
355
|
+
return {
|
|
356
|
+
ckbAvailable,
|
|
357
|
+
ckbMaturing: ckbCumulativeMaturing,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
exports.IckbSdk = IckbSdk;
|
|
362
|
+
//# sourceMappingURL=sdk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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
|
+
* Codec for encoding and decoding a pool snapshot.
|
|
4
|
+
*
|
|
5
|
+
* The PoolSnapshotCodec encodes an array of 1024 numbers representing event counts for bins,
|
|
6
|
+
* where the number of bits used per bin is dynamically computed as the ceiling of log2(max + 1)
|
|
7
|
+
* across all bins. The codec provides methods to encode the array into a Uint8Array and decode
|
|
8
|
+
* a Uint8Array back to the original array of numbers.
|
|
9
|
+
*
|
|
10
|
+
* @remarks The total number of bits for encoding all the bins is computed as bitsPerBin * 1024. The codec
|
|
11
|
+
* does not have a fixed byteLength since it depends on the data.
|
|
12
|
+
*/
|
|
13
|
+
export declare const PoolSnapshot: mol.Codec<number[], number[]>;
|
|
14
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,GAAG,EAAE,MAAM,eAAe,CAAC;AAKzC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,+BAkDvB,CAAC"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { ccc, mol } from "@ckb-ccc/core";
|
|
2
|
+
import { max } from "@ickb/utils";
|
|
3
|
+
const N = 1024;
|
|
4
|
+
/**
|
|
5
|
+
* Codec for encoding and decoding a pool snapshot.
|
|
6
|
+
*
|
|
7
|
+
* The PoolSnapshotCodec encodes an array of 1024 numbers representing event counts for bins,
|
|
8
|
+
* where the number of bits used per bin is dynamically computed as the ceiling of log2(max + 1)
|
|
9
|
+
* across all bins. The codec provides methods to encode the array into a Uint8Array and decode
|
|
10
|
+
* a Uint8Array back to the original array of numbers.
|
|
11
|
+
*
|
|
12
|
+
* @remarks The total number of bits for encoding all the bins is computed as bitsPerBin * 1024. The codec
|
|
13
|
+
* does not have a fixed byteLength since it depends on the data.
|
|
14
|
+
*/
|
|
15
|
+
export const PoolSnapshot = mol.Codec.from({
|
|
16
|
+
/**
|
|
17
|
+
* Encodes an array of 1024 bin counts into a Uint8Array.
|
|
18
|
+
*
|
|
19
|
+
* @param bins - An array of 1024 numbers. Each number represents the event count for a bin.
|
|
20
|
+
* @returns A Uint8Array containing the packed bit representation of the bin counts.
|
|
21
|
+
* @throws Error if the input array does not contain exactly 1024 elements.
|
|
22
|
+
*/
|
|
23
|
+
encode: (bins) => {
|
|
24
|
+
if (bins.length !== N) {
|
|
25
|
+
throw new Error("Expected 1024 bins");
|
|
26
|
+
}
|
|
27
|
+
const bitsPerBin = computeBits(bins);
|
|
28
|
+
const buffer = new Uint8Array((bitsPerBin * N) >> 3);
|
|
29
|
+
let bitOffset = 0;
|
|
30
|
+
for (const count of bins) {
|
|
31
|
+
packBits(buffer, bitOffset, bitsPerBin, count);
|
|
32
|
+
bitOffset += bitsPerBin;
|
|
33
|
+
}
|
|
34
|
+
return buffer;
|
|
35
|
+
},
|
|
36
|
+
/**
|
|
37
|
+
* Decodes a buffer into an array of 1024 bin counts.
|
|
38
|
+
*
|
|
39
|
+
* The function automatically computes the number of bits per bin from the buffer length,
|
|
40
|
+
* expecting that the total number of bits in the buffer is a multiple of 1024.
|
|
41
|
+
*
|
|
42
|
+
* @param bufferLike - The input that can be converted into a Uint8Array.
|
|
43
|
+
* @returns An array of 1024 numbers representing the decoded bin counts.
|
|
44
|
+
* @throws Error if the buffer length is invalid (i.e., its total bit count is not divisible by 1024).
|
|
45
|
+
*/
|
|
46
|
+
decode: (bufferLike) => {
|
|
47
|
+
const buffer = ccc.bytesFrom(bufferLike);
|
|
48
|
+
// Determine bitsPerBin from the fixed structure (totalBits divided by N).
|
|
49
|
+
const bitsPerBin = (buffer.byteLength * 8) / N;
|
|
50
|
+
if (!Number.isInteger(bitsPerBin)) {
|
|
51
|
+
throw new Error("Invalid buffer length for 1024 bins");
|
|
52
|
+
}
|
|
53
|
+
const bins = new Array(N);
|
|
54
|
+
let bitOffset = 0;
|
|
55
|
+
for (let i = 0; i < N; i++) {
|
|
56
|
+
bins[i] = unpackBits(buffer, bitOffset, bitsPerBin);
|
|
57
|
+
bitOffset += bitsPerBin;
|
|
58
|
+
}
|
|
59
|
+
return bins;
|
|
60
|
+
},
|
|
61
|
+
// Note: The byteLength is not fixed as it depends on the data (i.e., the maximum bin count).
|
|
62
|
+
});
|
|
63
|
+
/**
|
|
64
|
+
* Computes the minimal number of bits required to represent the maximum number in the bins.
|
|
65
|
+
*
|
|
66
|
+
* Given an array of numbers that represent counts for each bin, this function calculates
|
|
67
|
+
* the number of bits required to represent the maximum bin count. A minimum of 1 bit is
|
|
68
|
+
* always returned.
|
|
69
|
+
*
|
|
70
|
+
* @param bins - An array of numbers, each representing a bin's value.
|
|
71
|
+
* @returns The minimal number of bits required to represent the highest value among the bins.
|
|
72
|
+
*/
|
|
73
|
+
function computeBits(bins) {
|
|
74
|
+
return Math.ceil(Math.log2(1 + max(1, ...bins)));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Packs a given numeric value into a Uint8Array (bit array) at the specified bit offset and width.
|
|
78
|
+
*
|
|
79
|
+
* The function encodes the given number into the provided buffer, using the specified number of bits (width)
|
|
80
|
+
* starting from the specified bit offset. Each bit of the value is written into the buffer.
|
|
81
|
+
*
|
|
82
|
+
* @param buffer - The Uint8Array into which the value will be packed.
|
|
83
|
+
* @param bitOffset - The starting bit index in the array where the value should be packed.
|
|
84
|
+
* @param width - The number of bits to use for packing the value.
|
|
85
|
+
* @param value - The numeric value to pack.
|
|
86
|
+
*/
|
|
87
|
+
function packBits(buffer, bitOffset, width, value) {
|
|
88
|
+
let offset = bitOffset;
|
|
89
|
+
for (let i = 0; i < width; i++) {
|
|
90
|
+
const byteIndex = offset >> 3;
|
|
91
|
+
const bitInByte = offset % 8;
|
|
92
|
+
const bit = (value >> i) & 1;
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
94
|
+
buffer[byteIndex] |= bit << bitInByte;
|
|
95
|
+
offset++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Unpacks a numeric value from a Uint8Array (bit array) starting at a given bit offset with a specified width.
|
|
100
|
+
*
|
|
101
|
+
* The function reads bits from the buffer starting at the bit offset and reconstructs a numeric value
|
|
102
|
+
* using the specified number of bits (width).
|
|
103
|
+
*
|
|
104
|
+
* @param buffer - The Uint8Array from which the value will be unpacked.
|
|
105
|
+
* @param bitOffset - The starting bit index in the buffer.
|
|
106
|
+
* @param width - The number of bits to read for unpacking the value.
|
|
107
|
+
* @returns The numeric value resulting from decoding the specified bits.
|
|
108
|
+
*/
|
|
109
|
+
function unpackBits(buffer, bitOffset, width) {
|
|
110
|
+
let value = 0;
|
|
111
|
+
let offset = bitOffset;
|
|
112
|
+
for (let i = 0; i < width; i++) {
|
|
113
|
+
const byteIndex = offset >> 3;
|
|
114
|
+
const bitInByte = offset % 8;
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
116
|
+
const bit = (buffer[byteIndex] >> bitInByte) & 1;
|
|
117
|
+
value |= bit << i;
|
|
118
|
+
offset++;
|
|
119
|
+
}
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAElC,MAAM,CAAC,GAAG,IAAI,CAAC;AAEf;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,CAAC,KAAK,CAAC,IAAI,CAAW;IACnD;;;;;;OAMG;IACH,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;QACf,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CA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|
|
2
|
+
import { IckbUdtManager, LogicManager, OwnedOwnerManager } from "@ickb/core";
|
|
3
|
+
import { DaoManager } from "@ickb/dao";
|
|
4
|
+
import { OrderManager } from "@ickb/order";
|
|
5
|
+
import { CapacityManager, type ScriptDeps } from "@ickb/utils";
|
|
6
|
+
/**
|
|
7
|
+
* The iCKB UDT name.
|
|
8
|
+
*/
|
|
9
|
+
export declare const UDT_NAME = "iCKB";
|
|
10
|
+
/**
|
|
11
|
+
* The iCKB UDT symbol.
|
|
12
|
+
*/
|
|
13
|
+
export declare const UDT_SYMBOL = "iCKB";
|
|
14
|
+
/**
|
|
15
|
+
* The iCKB UDT number of decimals.
|
|
16
|
+
*/
|
|
17
|
+
export declare const UDT_DECIMALS = 8;
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves the configuration for the given deployment environment.
|
|
20
|
+
*
|
|
21
|
+
* Accepts either a string identifier ("mainnet" or "testnet") or a custom configuration
|
|
22
|
+
* object containing script dependencies for a devnet.
|
|
23
|
+
*
|
|
24
|
+
* It sets up various managers (UDT, Logic, OwnedOwner, Order, Dao, Capacity) and also
|
|
25
|
+
* aggregates a unique list of known bot scripts.
|
|
26
|
+
*
|
|
27
|
+
* @param deps - Either a network identifier ("mainnet"/"testnet") or an object containing
|
|
28
|
+
* explicit script dependencies for devnet.
|
|
29
|
+
* @param bots - An optional array of bot script-like objects to augment the list of known bots.
|
|
30
|
+
* @returns An object containing the instantiated managers and bots.
|
|
31
|
+
*/
|
|
32
|
+
export declare function getConfig(deps: "mainnet" | "testnet" | {
|
|
33
|
+
udt: ScriptDeps;
|
|
34
|
+
ickbLogic: ScriptDeps;
|
|
35
|
+
ownedOwner: ScriptDeps;
|
|
36
|
+
order: ScriptDeps;
|
|
37
|
+
dao: ScriptDeps;
|
|
38
|
+
}, bots?: ccc.ScriptLike[]): {
|
|
39
|
+
managers: {
|
|
40
|
+
udt: IckbUdtManager;
|
|
41
|
+
ickbLogic: LogicManager;
|
|
42
|
+
ownedOwner: OwnedOwnerManager;
|
|
43
|
+
order: OrderManager;
|
|
44
|
+
dao: DaoManager;
|
|
45
|
+
capacity: CapacityManager;
|
|
46
|
+
};
|
|
47
|
+
bots: ccc.Script[];
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAU,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAEvE;;GAEG;AACH,eAAO,MAAM,QAAQ,SAAS,CAAC;AAE/B;;GAEG;AACH,eAAO,MAAM,UAAU,SAAS,CAAC;AAEjC;;GAEG;AACH,eAAO,MAAM,YAAY,IAAI,CAAC;AAE9B;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CACvB,IAAI,EACA,SAAS,GACT,SAAS,GACT;IAEE,GAAG,EAAE,UAAU,CAAC;IAChB,SAAS,EAAE,UAAU,CAAC;IACtB,UAAU,EAAE,UAAU,CAAC;IACvB,KAAK,EAAE,UAAU,CAAC;IAClB,GAAG,EAAE,UAAU,CAAC;CACjB,EACL,IAAI,GAAE,GAAG,CAAC,UAAU,EAAO,GAC1B;IACD,QAAQ,EAAE;QACR,GAAG,EAAE,cAAc,CAAC;QACpB,SAAS,EAAE,YAAY,CAAC;QACxB,UAAU,EAAE,iBAAiB,CAAC;QAC9B,KAAK,EAAE,YAAY,CAAC;QACpB,GAAG,EAAE,UAAU,CAAC;QAChB,QAAQ,EAAE,eAAe,CAAC;KAC3B,CAAC;IACF,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC;CACpB,CA6BA"}
|