@ickb/sdk 1000.0.82 → 9000.0.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/README.md +81 -32
- package/dist/constants.d.ts +13 -22
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +80 -76
- package/dist/constants.js.map +1 -1
- package/dist/conversion/estimate.d.ts +39 -0
- package/dist/conversion/estimate.d.ts.map +1 -0
- package/dist/conversion/estimate.js +117 -0
- package/dist/conversion/estimate.js.map +1 -0
- package/dist/conversion/maturity.d.ts +15 -0
- package/dist/conversion/maturity.d.ts.map +1 -0
- package/dist/conversion/maturity.js +112 -0
- package/dist/conversion/maturity.js.map +1 -0
- package/dist/conversion/plans.d.ts +20 -0
- package/dist/conversion/plans.d.ts.map +1 -0
- package/dist/conversion/plans.js +106 -0
- package/dist/conversion/plans.js.map +1 -0
- package/dist/conversion/projection.d.ts +22 -0
- package/dist/conversion/projection.d.ts.map +1 -0
- package/dist/conversion/projection.js +91 -0
- package/dist/conversion/projection.js.map +1 -0
- package/dist/conversion/types.d.ts +252 -0
- package/dist/conversion/types.d.ts.map +1 -0
- package/dist/conversion/types.js +2 -0
- package/dist/conversion/types.js.map +1 -0
- package/dist/conversion/withdrawal_ring.d.ts +36 -0
- package/dist/conversion/withdrawal_ring.d.ts.map +1 -0
- package/dist/conversion/withdrawal_ring.js +87 -0
- package/dist/conversion/withdrawal_ring.js.map +1 -0
- package/dist/dao.d.ts +86 -0
- package/dist/dao.d.ts.map +1 -0
- package/dist/dao.js +113 -0
- package/dist/dao.js.map +1 -0
- package/dist/index.d.ts +51 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -3
- package/dist/index.js.map +1 -1
- package/dist/logic.d.ts +85 -0
- package/dist/logic.d.ts.map +1 -0
- package/dist/logic.js +184 -0
- package/dist/logic.js.map +1 -0
- package/dist/order/cells.d.ts +207 -0
- package/dist/order/cells.d.ts.map +1 -0
- package/dist/order/cells.js +427 -0
- package/dist/order/cells.js.map +1 -0
- package/dist/order/conversion.d.ts +43 -0
- package/dist/order/conversion.d.ts.map +1 -0
- package/dist/order/conversion.js +123 -0
- package/dist/order/conversion.js.map +1 -0
- package/dist/order/fee.d.ts +9 -0
- package/dist/order/fee.d.ts.map +1 -0
- package/dist/order/fee.js +24 -0
- package/dist/order/fee.js.map +1 -0
- package/dist/order/fill.d.ts +36 -0
- package/dist/order/fill.d.ts.map +1 -0
- package/dist/order/fill.js +53 -0
- package/dist/order/fill.js.map +1 -0
- package/dist/order/info.d.ts +62 -0
- package/dist/order/info.d.ts.map +1 -0
- package/dist/order/info.js +99 -0
- package/dist/order/info.js.map +1 -0
- package/dist/order/master.d.ts +49 -0
- package/dist/order/master.d.ts.map +1 -0
- package/dist/order/master.js +27 -0
- package/dist/order/master.js.map +1 -0
- package/dist/order/matcher.d.ts +43 -0
- package/dist/order/matcher.d.ts.map +1 -0
- package/dist/order/matcher.js +123 -0
- package/dist/order/matcher.js.map +1 -0
- package/dist/order/order.d.ts +59 -0
- package/dist/order/order.d.ts.map +1 -0
- package/dist/order/order.js +239 -0
- package/dist/order/order.js.map +1 -0
- package/dist/order/order_data.d.ts +51 -0
- package/dist/order/order_data.d.ts.map +1 -0
- package/dist/order/order_data.js +64 -0
- package/dist/order/order_data.js.map +1 -0
- package/dist/order/ratio.d.ts +42 -0
- package/dist/order/ratio.d.ts.map +1 -0
- package/dist/order/ratio.js +82 -0
- package/dist/order/ratio.js.map +1 -0
- package/dist/order/relative.d.ts +43 -0
- package/dist/order/relative.d.ts.map +1 -0
- package/dist/order/relative.js +54 -0
- package/dist/order/relative.js.map +1 -0
- package/dist/owned_owner.d.ts +113 -0
- package/dist/owned_owner.d.ts.map +1 -0
- package/dist/owned_owner.js +280 -0
- package/dist/owned_owner.js.map +1 -0
- package/dist/sdk.d.ts +71 -42
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +388 -209
- package/dist/sdk.js.map +1 -1
- package/dist/send/sign_and_send_transaction.d.ts +46 -0
- package/dist/send/sign_and_send_transaction.d.ts.map +1 -0
- package/dist/send/sign_and_send_transaction.js +101 -0
- package/dist/send/sign_and_send_transaction.js.map +1 -0
- package/dist/send/wait_transaction.d.ts +41 -0
- package/dist/send/wait_transaction.d.ts.map +1 -0
- package/dist/send/wait_transaction.js +204 -0
- package/dist/send/wait_transaction.js.map +1 -0
- package/dist/udt.d.ts +123 -0
- package/dist/udt.d.ts.map +1 -0
- package/dist/udt.js +244 -0
- package/dist/udt.js.map +1 -0
- package/dist/utils/chain.d.ts +38 -0
- package/dist/utils/chain.d.ts.map +1 -0
- package/dist/utils/chain.js +20 -0
- package/dist/utils/chain.js.map +1 -0
- package/dist/utils/codec.d.ts +19 -0
- package/dist/utils/codec.d.ts.map +1 -0
- package/dist/utils/codec.js +66 -0
- package/dist/utils/codec.js.map +1 -0
- package/dist/utils/transaction_header.d.ts +17 -0
- package/dist/utils/transaction_header.d.ts.map +1 -0
- package/dist/utils/transaction_header.js +44 -0
- package/dist/utils/transaction_header.js.map +1 -0
- package/dist/utils/utils.d.ts +126 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +151 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +19 -26
- package/src/constants.ts +84 -181
- package/src/conversion/estimate.ts +157 -0
- package/src/conversion/maturity.ts +159 -0
- package/src/conversion/plans.ts +144 -0
- package/src/conversion/projection.ts +128 -0
- package/src/conversion/types.ts +287 -0
- package/src/conversion/withdrawal_ring.ts +126 -0
- package/src/dao.ts +172 -0
- package/src/index.ts +89 -3
- package/src/logic.ts +295 -0
- package/src/order/cells.ts +520 -0
- package/src/order/conversion.ts +173 -0
- package/src/order/fee.ts +30 -0
- package/src/order/fill.ts +83 -0
- package/src/order/info.ts +178 -0
- package/src/order/master.ts +42 -0
- package/src/order/matcher.ts +191 -0
- package/src/order/order.ts +302 -0
- package/src/order/order_data.ts +128 -0
- package/src/order/ratio.ts +144 -0
- package/src/order/relative.ts +106 -0
- package/src/owned_owner.ts +394 -0
- package/src/sdk.ts +555 -440
- package/src/send/sign_and_send_transaction.ts +120 -0
- package/src/send/wait_transaction.ts +271 -0
- package/src/udt.ts +317 -0
- package/src/utils/chain.ts +43 -0
- package/src/utils/codec.ts +74 -0
- package/src/utils/transaction_header.ts +66 -0
- package/src/utils/utils.ts +237 -0
- package/dist/codec.d.ts +0 -3
- package/dist/codec.d.ts.map +0 -1
- package/dist/codec.js +0 -58
- package/dist/codec.js.map +0 -1
- package/src/codec.ts +0 -138
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { OrderData } from "./order_data.js";
|
|
2
|
+
// The resolver's own group, keyed by its order cell: matching and transactions use this
|
|
3
|
+
// parse, so a caller's own wrapper is only ever a key into it.
|
|
4
|
+
const resolvedOrderGroups = new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* Represents a parsed order cell on the blockchain.
|
|
7
|
+
*
|
|
8
|
+
* Implements `ValueComponents` to expose the cell's raw data and its
|
|
9
|
+
* value breakdown (CKB and UDT).
|
|
10
|
+
*/
|
|
11
|
+
export class OrderCell {
|
|
12
|
+
/** Raw live cell that carries the order lock and UDT type. */
|
|
13
|
+
cell;
|
|
14
|
+
/** Decoded order payload from `cell.outputData`. */
|
|
15
|
+
data;
|
|
16
|
+
/** CKB capacity available for matching after occupied capacity is reserved. */
|
|
17
|
+
ckbUnoccupied;
|
|
18
|
+
/** Absolute total order value in the order's comparison units. */
|
|
19
|
+
absTotal;
|
|
20
|
+
/** Absolute matched progress in the order's comparison units. */
|
|
21
|
+
absProgress;
|
|
22
|
+
constructor({ cell, data, ckbUnoccupied, absTotal, absProgress, }) {
|
|
23
|
+
this.cell = cell;
|
|
24
|
+
this.data = data;
|
|
25
|
+
this.ckbUnoccupied = ckbUnoccupied;
|
|
26
|
+
this.absTotal = absTotal;
|
|
27
|
+
this.absProgress = absProgress;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Gets the order CKB amount.
|
|
31
|
+
*
|
|
32
|
+
* @returns The CKB amount as a `ccc.FixedPoint`.
|
|
33
|
+
*/
|
|
34
|
+
get ckbValue() {
|
|
35
|
+
return this.cell.cellOutput.capacity;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Gets the order UDT amount.
|
|
39
|
+
*
|
|
40
|
+
* @returns The UDT amount as a `ccc.FixedPoint`.
|
|
41
|
+
*/
|
|
42
|
+
get udtValue() {
|
|
43
|
+
return this.data.udtValue;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Attempts to parse an OrderCell from a raw cell.
|
|
47
|
+
*
|
|
48
|
+
* Returns `undefined` if parsing or validation fails.
|
|
49
|
+
*
|
|
50
|
+
* @param cell - The raw chain cell to convert.
|
|
51
|
+
* @returns An `OrderCell` instance or `undefined`.
|
|
52
|
+
*/
|
|
53
|
+
static tryFrom(cell) {
|
|
54
|
+
try {
|
|
55
|
+
return OrderCell.mustFrom(cell);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Parses and validates a raw cell as OrderCell.
|
|
63
|
+
*
|
|
64
|
+
* Throws if the cell is invalid or data validation fails.
|
|
65
|
+
*
|
|
66
|
+
* @param cell - The raw chain cell to convert.
|
|
67
|
+
* @returns A new `OrderCell` instance.
|
|
68
|
+
* @throws When decoding or validation fails.
|
|
69
|
+
*/
|
|
70
|
+
static mustFrom(cell) {
|
|
71
|
+
let data;
|
|
72
|
+
try {
|
|
73
|
+
data = OrderData.decode(cell.outputData);
|
|
74
|
+
data.validate();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw new Error(`Invalid order payload at ${cell.outPoint.toHex()}`, {
|
|
78
|
+
cause: error,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const udtValue = data.udtValue;
|
|
82
|
+
const ckbUnoccupied = cell.capacityFree;
|
|
83
|
+
const { ckbToUdt, udtToCkb } = data.info;
|
|
84
|
+
const isCkb2Udt = data.info.isCkb2Udt();
|
|
85
|
+
const isUdt2Ckb = data.info.isUdt2Ckb();
|
|
86
|
+
const isDualRatio = isCkb2Udt && isUdt2Ckb;
|
|
87
|
+
// Compute total values in base units
|
|
88
|
+
const ckb2UdtValue = isCkb2Udt
|
|
89
|
+
? ckbUnoccupied * ckbToUdt.ckbScale + udtValue * ckbToUdt.udtScale
|
|
90
|
+
: 0n;
|
|
91
|
+
const udt2CkbValue = isUdt2Ckb
|
|
92
|
+
? ckbUnoccupied * udtToCkb.ckbScale + udtValue * udtToCkb.udtScale
|
|
93
|
+
: 0n;
|
|
94
|
+
const absTotal = absoluteOrderTotal(ckb2UdtValue, udt2CkbValue, data.info);
|
|
95
|
+
const absProgress = absoluteOrderProgress({
|
|
96
|
+
absTotal,
|
|
97
|
+
ckbUnoccupied,
|
|
98
|
+
isCkb2Udt,
|
|
99
|
+
isDualRatio,
|
|
100
|
+
udtScale: ckbToUdt.udtScale,
|
|
101
|
+
udtValue,
|
|
102
|
+
udtToCkbCkbScale: udtToCkb.ckbScale,
|
|
103
|
+
});
|
|
104
|
+
return new OrderCell({ cell, data, ckbUnoccupied, absTotal, absProgress });
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Checks whether the CKB-to-UDT side is enabled and has nonzero inventory.
|
|
108
|
+
*
|
|
109
|
+
* @remarks Executable-match checks for fee, minimum output, and ratio bounds happen during matching.
|
|
110
|
+
*/
|
|
111
|
+
isCkb2UdtMatchable() {
|
|
112
|
+
return this.data.info.isCkb2Udt() && this.ckbUnoccupied > 0n;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Checks whether the UDT-to-CKB side is enabled and has nonzero inventory.
|
|
116
|
+
*
|
|
117
|
+
* @remarks Executable-match checks for fee, minimum output, and ratio bounds happen during matching.
|
|
118
|
+
*/
|
|
119
|
+
isUdt2CkbMatchable() {
|
|
120
|
+
return this.data.info.isUdt2Ckb() && this.data.udtValue > 0n;
|
|
121
|
+
}
|
|
122
|
+
/** Returns true when either side is enabled and has nonzero inventory. */
|
|
123
|
+
isMatchable() {
|
|
124
|
+
return this.isCkb2UdtMatchable() || this.isUdt2CkbMatchable();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Checks if the order is fulfilled.
|
|
128
|
+
*
|
|
129
|
+
* @returns True if the order is fulfilled (not matchable), otherwise false.
|
|
130
|
+
*/
|
|
131
|
+
isFulfilled() {
|
|
132
|
+
return !this.isMatchable();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Retrieves the master out point of the order.
|
|
136
|
+
* @returns The master out point associated with the order.
|
|
137
|
+
*/
|
|
138
|
+
getMaster() {
|
|
139
|
+
return this.data.getMaster(this.cell.outPoint);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Validates a descendant order against this origin order.
|
|
143
|
+
*
|
|
144
|
+
* @remarks
|
|
145
|
+
* This confusion-attack guard requires the same order script, UDT type,
|
|
146
|
+
* master, and info, while forbidding lower total value or lower progress.
|
|
147
|
+
* See {@link https://github.com/ickb/whitepaper/issues/19}.
|
|
148
|
+
*/
|
|
149
|
+
validate(descendant) {
|
|
150
|
+
if (!this.data.isMint()) {
|
|
151
|
+
throw new Error("Origin is not a mint order");
|
|
152
|
+
}
|
|
153
|
+
if (!this.cell.cellOutput.lock.eq(descendant.cell.cellOutput.lock)) {
|
|
154
|
+
throw new Error("Order script different");
|
|
155
|
+
}
|
|
156
|
+
const udt = this.cell.cellOutput.type;
|
|
157
|
+
if (udt === undefined || descendant.cell.cellOutput.type?.eq(udt) !== true) {
|
|
158
|
+
throw new Error("UDT type is different");
|
|
159
|
+
}
|
|
160
|
+
if (!descendant.getMaster().eq(this.getMaster())) {
|
|
161
|
+
throw new Error("Master is different");
|
|
162
|
+
}
|
|
163
|
+
if (!this.data.info.eq(descendant.data.info)) {
|
|
164
|
+
throw new Error("Info is different");
|
|
165
|
+
}
|
|
166
|
+
if (this.absTotal > descendant.absTotal) {
|
|
167
|
+
throw new Error("Total value is lower than the original one");
|
|
168
|
+
}
|
|
169
|
+
if (this.absProgress > descendant.absProgress) {
|
|
170
|
+
throw new Error("Progress is lower than the original one");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Checks if the descendant order is valid against this order.
|
|
175
|
+
* @param descendant - The descendant order to validate.
|
|
176
|
+
* @returns True if the descendant is valid, otherwise false.
|
|
177
|
+
*/
|
|
178
|
+
isValid(descendant) {
|
|
179
|
+
try {
|
|
180
|
+
this.validate(descendant);
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Resolves the best valid descendant order.
|
|
189
|
+
*
|
|
190
|
+
* @remarks
|
|
191
|
+
* The genuine mint origin is the immutable validation baseline. Resolution
|
|
192
|
+
* prefers greater progress and then greater total value; candidate identity is
|
|
193
|
+
* irrelevant. Equal-score non-mint candidates are ambiguous, while the mint
|
|
194
|
+
* origin wins an equal-score tie when it is still live.
|
|
195
|
+
*/
|
|
196
|
+
resolve(descendants) {
|
|
197
|
+
let resolution;
|
|
198
|
+
for (const descendant of descendants) {
|
|
199
|
+
if (!this.isValid(descendant)) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
resolution = betterOrderResolution(resolution, descendant);
|
|
203
|
+
}
|
|
204
|
+
if (resolution?.isAmbiguous === true) {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
return resolution?.order;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function absoluteOrderTotal(ckb2UdtValue, udt2CkbValue, info) {
|
|
211
|
+
if (ckb2UdtValue === 0n) {
|
|
212
|
+
return udt2CkbValue;
|
|
213
|
+
}
|
|
214
|
+
if (udt2CkbValue === 0n) {
|
|
215
|
+
return ckb2UdtValue;
|
|
216
|
+
}
|
|
217
|
+
const { ckbToUdt, udtToCkb } = info;
|
|
218
|
+
return ((ckb2UdtValue * udtToCkb.ckbScale * udtToCkb.udtScale +
|
|
219
|
+
udt2CkbValue * ckbToUdt.ckbScale * ckbToUdt.udtScale) >>
|
|
220
|
+
1n);
|
|
221
|
+
}
|
|
222
|
+
function absoluteOrderProgress(options) {
|
|
223
|
+
if (options.isDualRatio) {
|
|
224
|
+
return options.absTotal;
|
|
225
|
+
}
|
|
226
|
+
if (options.isCkb2Udt) {
|
|
227
|
+
return options.udtValue * options.udtScale;
|
|
228
|
+
}
|
|
229
|
+
return options.ckbUnoccupied * options.udtToCkbCkbScale;
|
|
230
|
+
}
|
|
231
|
+
function betterOrderResolution(current, candidate) {
|
|
232
|
+
if (current === undefined || compareOrderScore(current.order, candidate) < 0) {
|
|
233
|
+
return { isAmbiguous: false, order: candidate };
|
|
234
|
+
}
|
|
235
|
+
if (compareOrderScore(current.order, candidate) > 0) {
|
|
236
|
+
return current;
|
|
237
|
+
}
|
|
238
|
+
if (current.order.cell.outPoint.eq(candidate.cell.outPoint)) {
|
|
239
|
+
return current;
|
|
240
|
+
}
|
|
241
|
+
if (candidate.data.isMint() && !current.order.data.isMint()) {
|
|
242
|
+
return { isAmbiguous: false, order: candidate };
|
|
243
|
+
}
|
|
244
|
+
if (!candidate.data.isMint() && current.order.data.isMint()) {
|
|
245
|
+
return current;
|
|
246
|
+
}
|
|
247
|
+
return { ...current, isAmbiguous: true };
|
|
248
|
+
}
|
|
249
|
+
function compareOrderScore(left, right) {
|
|
250
|
+
if (left.absProgress !== right.absProgress) {
|
|
251
|
+
return left.absProgress < right.absProgress ? -1 : 1;
|
|
252
|
+
}
|
|
253
|
+
if (left.absTotal !== right.absTotal) {
|
|
254
|
+
return left.absTotal < right.absTotal ? -1 : 1;
|
|
255
|
+
}
|
|
256
|
+
return 0;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Represents a master cell
|
|
260
|
+
*/
|
|
261
|
+
export class MasterCell {
|
|
262
|
+
/** Raw live master cell that anchors an order group. */
|
|
263
|
+
cell;
|
|
264
|
+
/**
|
|
265
|
+
* Gets the UDT value of the cell.
|
|
266
|
+
*
|
|
267
|
+
* For a Master Cell, the UDT amount is always zero.
|
|
268
|
+
*
|
|
269
|
+
* @returns The UDT amount as a `ccc.FixedPoint` (0n).
|
|
270
|
+
*/
|
|
271
|
+
udtValue = 0n;
|
|
272
|
+
/**
|
|
273
|
+
* Creates an instance of MasterCell.
|
|
274
|
+
* @param cell - The ccc.Cell instance to be wrapped by the MasterCell.
|
|
275
|
+
*/
|
|
276
|
+
constructor(cell) {
|
|
277
|
+
this.cell = cell;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Gets the CKB value of the cell.
|
|
281
|
+
*
|
|
282
|
+
* @returns The total CKB amount as a `ccc.FixedPoint`.
|
|
283
|
+
*/
|
|
284
|
+
get ckbValue() {
|
|
285
|
+
return this.cell.cellOutput.capacity;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Validates the MasterCell against an OrderCell.
|
|
289
|
+
* @param order - The OrderCell to validate against.
|
|
290
|
+
* @throws Error if the order script is different or if the master is different.
|
|
291
|
+
*/
|
|
292
|
+
validate(order) {
|
|
293
|
+
if (this.cell.cellOutput.type?.eq(order.cell.cellOutput.lock) !== true) {
|
|
294
|
+
throw new Error("Order script different");
|
|
295
|
+
}
|
|
296
|
+
if (!order.getMaster().eq(this.cell.outPoint)) {
|
|
297
|
+
throw new Error("Master is different");
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Represents a group of orders associated with a master cell.
|
|
303
|
+
*/
|
|
304
|
+
export class OrderGroup {
|
|
305
|
+
/** Master cell that authorizes and anchors the current order. */
|
|
306
|
+
master;
|
|
307
|
+
/** Current resolved order cell. */
|
|
308
|
+
order;
|
|
309
|
+
/** Origin order used to validate descendant progress and identity. */
|
|
310
|
+
origin;
|
|
311
|
+
/** The block that committed the origin, when known: how long the order has been on the book. */
|
|
312
|
+
blockNumber;
|
|
313
|
+
/**
|
|
314
|
+
* Creates an instance of OrderGroup.
|
|
315
|
+
* @param master - The master cell associated with the order group.
|
|
316
|
+
* @param order - The order within the group.
|
|
317
|
+
* @param origin - The original order associated with the group.
|
|
318
|
+
* @param blockNumber - The block that committed the origin, if committed.
|
|
319
|
+
*/
|
|
320
|
+
constructor(master, order, origin, blockNumber) {
|
|
321
|
+
this.master = master;
|
|
322
|
+
this.order = order;
|
|
323
|
+
this.origin = origin;
|
|
324
|
+
this.blockNumber = blockNumber;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Gets the CKB value of the group.
|
|
328
|
+
*
|
|
329
|
+
* @returns The total CKB amount as a `ccc.FixedPoint`, which is the sum of the CKB values of the order cell and the master cell.
|
|
330
|
+
*/
|
|
331
|
+
get ckbValue() {
|
|
332
|
+
return this.order.cell.cellOutput.capacity + this.master.cell.cellOutput.capacity;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Gets the UDT value of the group.
|
|
336
|
+
*
|
|
337
|
+
* @returns The UDT amount as a `ccc.FixedPoint`, derived from the order cell.
|
|
338
|
+
*/
|
|
339
|
+
get udtValue() {
|
|
340
|
+
return this.order.data.udtValue;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Tries to create an OrderGroup from the provided parameters.
|
|
344
|
+
* @param master - The master cell.
|
|
345
|
+
* @param order - The order within the group.
|
|
346
|
+
* @param origin - The original order.
|
|
347
|
+
* @returns An OrderGroup instance or undefined if creation fails.
|
|
348
|
+
*/
|
|
349
|
+
static tryFrom(master, order, origin, blockNumber) {
|
|
350
|
+
const og = new OrderGroup(master, order, origin, blockNumber);
|
|
351
|
+
if (og.isValid()) {
|
|
352
|
+
return og;
|
|
353
|
+
}
|
|
354
|
+
return undefined;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Validates the order group against its master and origin orders.
|
|
358
|
+
* @throws Will throw an error if validation fails.
|
|
359
|
+
*/
|
|
360
|
+
validate() {
|
|
361
|
+
this.master.validate(this.origin);
|
|
362
|
+
this.master.validate(this.order);
|
|
363
|
+
this.origin.validate(this.order);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Checks if the order group is valid.
|
|
367
|
+
* @returns True if the order group is valid, otherwise false.
|
|
368
|
+
*/
|
|
369
|
+
isValid() {
|
|
370
|
+
try {
|
|
371
|
+
this.validate();
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Checks if the specified lock is the owner of the master cell.
|
|
380
|
+
*
|
|
381
|
+
* @param locks - The locks to check ownership against.
|
|
382
|
+
* @returns True if the lock is the owner, otherwise false.
|
|
383
|
+
*/
|
|
384
|
+
isOwner(...locks) {
|
|
385
|
+
const lock = this.master.cell.cellOutput.lock;
|
|
386
|
+
return locks.some((l) => lock.eq(l));
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Records canonical resolver output without exposing forgeable provenance data. The
|
|
391
|
+
* group and its parsed parts are frozen, since matching and transactions build on them.
|
|
392
|
+
*/
|
|
393
|
+
export function attestResolvedOrderGroup(group) {
|
|
394
|
+
group.validate();
|
|
395
|
+
for (const order of [group.order, group.origin]) {
|
|
396
|
+
const { info } = order.data;
|
|
397
|
+
for (const part of [
|
|
398
|
+
order,
|
|
399
|
+
order.data,
|
|
400
|
+
order.data.master,
|
|
401
|
+
info,
|
|
402
|
+
info.ckbToUdt,
|
|
403
|
+
info.udtToCkb,
|
|
404
|
+
]) {
|
|
405
|
+
Object.freeze(part);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
Object.freeze(group.master);
|
|
409
|
+
resolvedOrderGroups.set(group.order.cell, Object.freeze(group));
|
|
410
|
+
return group;
|
|
411
|
+
}
|
|
412
|
+
/** Returns the resolver's group at a matching or transaction boundary. */
|
|
413
|
+
export function validatedOrderGroup(group) {
|
|
414
|
+
if (!(group instanceof OrderGroup)) {
|
|
415
|
+
throw new TypeError("Matching requires resolved OrderGroups from findOrders()");
|
|
416
|
+
}
|
|
417
|
+
const attested = resolvedOrderGroups.get(group.order.cell);
|
|
418
|
+
if (attested === undefined) {
|
|
419
|
+
throw new TypeError("OrderGroup was not produced by the order resolver");
|
|
420
|
+
}
|
|
421
|
+
if (group.master.cell !== attested.master.cell ||
|
|
422
|
+
group.origin.cell !== attested.origin.cell) {
|
|
423
|
+
throw new TypeError("OrderGroup does not match its resolver attestation");
|
|
424
|
+
}
|
|
425
|
+
return attested;
|
|
426
|
+
}
|
|
427
|
+
//# sourceMappingURL=cells.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cells.js","sourceRoot":"","sources":["../../src/order/cells.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,wFAAwF;AACxF,+DAA+D;AAC/D,MAAM,mBAAmB,GAAG,IAAI,OAAO,EAAwB,CAAC;AAEhE;;;;;GAKG;AACH,MAAM,OAAO,SAAS;IACpB,8DAA8D;IAC9C,IAAI,CAAW;IAC/B,oDAAoD;IACpC,IAAI,CAAY;IAChC,+EAA+E;IAC/D,aAAa,CAAiB;IAC9C,kEAAkE;IAClD,QAAQ,CAAU;IAClC,iEAAiE;IACjD,WAAW,CAAU;IAErC,YAAY,EACV,IAAI,EACJ,IAAI,EACJ,aAAa,EACb,QAAQ,EACR,WAAW,GAOZ;QACC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;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YAAY,UAAU,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,SAAS,CAAC,0DAA0D,CAAC,CAAC;IAClF,CAAC;IAED,MAAM,QAAQ,GAAG,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3D,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CAAC,mDAAmD,CAAC,CAAC;IAC3E,CAAC;IACD,IACE,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM,CAAC,IAAI;QAC1C,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM,CAAC,IAAI,EAC1C,CAAC;QACD,MAAM,IAAI,SAAS,CAAC,oDAAoD,CAAC,CAAC;IAC5E,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { type ExchangeRatio, type ValueComponents } from "../utils/utils.js";
|
|
3
|
+
import { Info } from "./info.js";
|
|
4
|
+
import { Ratio } from "./ratio.js";
|
|
5
|
+
/**
|
|
6
|
+
* Error thrown when an exact quote cannot be represented by the order ratio format.
|
|
7
|
+
*/
|
|
8
|
+
export declare class OrderConversionRepresentabilityError extends Error {
|
|
9
|
+
/** Creates a representability error with the default public conversion message. */
|
|
10
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Quotes a new order: the output-side amount, the CKB fee, and the order info.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* The returned `Info` preserves the quoted amount after fee adjustment so the
|
|
17
|
+
* minted order records the executable limit price.
|
|
18
|
+
*/
|
|
19
|
+
export declare function quoteConversion(isCkb2Udt: boolean, midpoint: ExchangeRatio, amounts: ValueComponents, options?: {
|
|
20
|
+
fee?: ccc.Num;
|
|
21
|
+
feeBase?: ccc.Num;
|
|
22
|
+
}): {
|
|
23
|
+
convertedAmount: ccc.FixedPoint;
|
|
24
|
+
ckbFee: ccc.FixedPoint;
|
|
25
|
+
info: Info;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The ratio that reproduces the quote exactly: the greatest Uint64 fraction at most
|
|
29
|
+
* `quotedOutput / inputAmount`, which must still round the input up to the quoted output.
|
|
30
|
+
*/
|
|
31
|
+
export declare function quotePreservingRatio(inputAmount: ccc.FixedPoint, quotedOutput: ccc.FixedPoint, isCkb2Udt: boolean): Ratio;
|
|
32
|
+
/**
|
|
33
|
+
* The greatest fraction at most `numerator / denominator` whose terms are at most
|
|
34
|
+
* `maxTerm`, reduced. It walks the continued fraction's convergents while they fit; the
|
|
35
|
+
* first that does not is replaced by the largest semiconvergent the bound allows, which
|
|
36
|
+
* is closer to the target than the last fitting convergent on the same side of it. Every
|
|
37
|
+
* other convergent lands above the target and is skipped.
|
|
38
|
+
*/
|
|
39
|
+
export declare function greatestBoundedFractionAtMost(numerator: bigint, denominator: bigint, maxTerm: bigint): {
|
|
40
|
+
numerator: bigint;
|
|
41
|
+
denominator: bigint;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=conversion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversion.d.ts","sourceRoot":"","sources":["../../src/order/conversion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,eAAe,EACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAInC;;GAEG;AACH,qBAAa,oCAAqC,SAAQ,KAAK;IAC7D,mFAAmF;IACnF,YACE,OAAO,SAAiE,EACxE,OAAO,CAAC,EAAE,YAAY,EAIvB;CACF;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,SAAS,EAAE,OAAO,EAClB,QAAQ,EAAE,aAAa,EACvB,OAAO,EAAE,eAAe,EACxB,OAAO,CAAC,EAAE;IACR,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC;IACd,OAAO,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC;CACnB,GACA;IAAE,eAAe,EAAE,GAAG,CAAC,UAAU,CAAC;IAAC,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC;IAAC,IAAI,EAAE,IAAI,CAAA;CAAE,CAwBzE;AAgCD;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,GAAG,CAAC,UAAU,EAC3B,YAAY,EAAE,GAAG,CAAC,UAAU,EAC5B,SAAS,EAAE,OAAO,GACjB,KAAK,CAuBP;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAC3C,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,GACd;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CA+B5C"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { ceilDiv, minBigInt, } from "../utils/utils.js";
|
|
3
|
+
import { Info } from "./info.js";
|
|
4
|
+
import { Ratio } from "./ratio.js";
|
|
5
|
+
const maxUint64 = (1n << 64n) - 1n;
|
|
6
|
+
/**
|
|
7
|
+
* Error thrown when an exact quote cannot be represented by the order ratio format.
|
|
8
|
+
*/
|
|
9
|
+
export class OrderConversionRepresentabilityError extends Error {
|
|
10
|
+
/** Creates a representability error with the default public conversion message. */
|
|
11
|
+
constructor(message = "Order conversion quote cannot be represented as Uint64 ratio", options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.name = "OrderConversionRepresentabilityError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Quotes a new order: the output-side amount, the CKB fee, and the order info.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* The returned `Info` preserves the quoted amount after fee adjustment so the
|
|
21
|
+
* minted order records the executable limit price.
|
|
22
|
+
*/
|
|
23
|
+
export function quoteConversion(isCkb2Udt, midpoint, amounts, options) {
|
|
24
|
+
const fee = options?.fee ?? 0n;
|
|
25
|
+
// Generic denominator for callers that pass a fee without a scale; Stack's
|
|
26
|
+
// own default pair is owned publicly by the SDK, not by this entity.
|
|
27
|
+
const feeBase = options?.feeBase ?? 100000n;
|
|
28
|
+
const base = Ratio.from(midpoint);
|
|
29
|
+
const amount = isCkb2Udt ? amounts.ckbValue : amounts.udtValue;
|
|
30
|
+
const { aScale, bScale } = feeAdjustedScales(base, isCkb2Udt, fee, feeBase);
|
|
31
|
+
const convertedAmount = ceilDiv(amount * aScale, bScale);
|
|
32
|
+
let ckbFee = 0n;
|
|
33
|
+
if (amount > 0n && fee !== 0n) {
|
|
34
|
+
ckbFee = isCkb2Udt
|
|
35
|
+
? amount - base.convert(false, convertedAmount, false)
|
|
36
|
+
: base.convert(false, amount, false) - convertedAmount;
|
|
37
|
+
}
|
|
38
|
+
// Every order this SDK creates carries the default minimum match, the one the bot's
|
|
39
|
+
// remainder rule is sized to.
|
|
40
|
+
const info = Info.create(isCkb2Udt, quotePreservingRatio(amount, convertedAmount, isCkb2Udt));
|
|
41
|
+
return { convertedAmount, ckbFee, info };
|
|
42
|
+
}
|
|
43
|
+
/** The direction's scales with the fee taken out of the output side, reduced. */
|
|
44
|
+
function feeAdjustedScales(ratio, isCkb2Udt, fee, feeBase) {
|
|
45
|
+
if (fee < 0n) {
|
|
46
|
+
throw new Error("Fee cannot be negative");
|
|
47
|
+
}
|
|
48
|
+
if (feeBase <= 0n) {
|
|
49
|
+
throw new Error("Fee base must be positive");
|
|
50
|
+
}
|
|
51
|
+
if (fee >= feeBase) {
|
|
52
|
+
throw new Error("Fee too big relative to feeBase");
|
|
53
|
+
}
|
|
54
|
+
if (!ratio.isPopulated()) {
|
|
55
|
+
throw new Error("Invalid ExchangeRatio");
|
|
56
|
+
}
|
|
57
|
+
let { ckbScale: aScale, udtScale: bScale } = ratio;
|
|
58
|
+
if (!isCkb2Udt) {
|
|
59
|
+
[aScale, bScale] = [bScale, aScale];
|
|
60
|
+
}
|
|
61
|
+
aScale *= feeBase - fee;
|
|
62
|
+
bScale *= feeBase;
|
|
63
|
+
const divisor = ccc.gcd(aScale, bScale);
|
|
64
|
+
return { aScale: aScale / divisor, bScale: bScale / divisor };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The ratio that reproduces the quote exactly: the greatest Uint64 fraction at most
|
|
68
|
+
* `quotedOutput / inputAmount`, which must still round the input up to the quoted output.
|
|
69
|
+
*/
|
|
70
|
+
export function quotePreservingRatio(inputAmount, quotedOutput, isCkb2Udt) {
|
|
71
|
+
if (inputAmount < 0n || quotedOutput < 0n) {
|
|
72
|
+
throw new Error("Order conversion amounts cannot be negative");
|
|
73
|
+
}
|
|
74
|
+
if (inputAmount === 0n || quotedOutput === 0n) {
|
|
75
|
+
throw new OrderConversionRepresentabilityError("Order conversion quote must have positive input and output");
|
|
76
|
+
}
|
|
77
|
+
const { numerator, denominator } = greatestBoundedFractionAtMost(quotedOutput, inputAmount, maxUint64);
|
|
78
|
+
if (numerator <= 0n || (quotedOutput - 1n) * denominator >= inputAmount * numerator) {
|
|
79
|
+
throw new OrderConversionRepresentabilityError();
|
|
80
|
+
}
|
|
81
|
+
return Ratio.from({
|
|
82
|
+
ckbScale: isCkb2Udt ? numerator : denominator,
|
|
83
|
+
udtScale: isCkb2Udt ? denominator : numerator,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The greatest fraction at most `numerator / denominator` whose terms are at most
|
|
88
|
+
* `maxTerm`, reduced. It walks the continued fraction's convergents while they fit; the
|
|
89
|
+
* first that does not is replaced by the largest semiconvergent the bound allows, which
|
|
90
|
+
* is closer to the target than the last fitting convergent on the same side of it. Every
|
|
91
|
+
* other convergent lands above the target and is skipped.
|
|
92
|
+
*/
|
|
93
|
+
export function greatestBoundedFractionAtMost(numerator, denominator, maxTerm) {
|
|
94
|
+
let best = { numerator: 0n, denominator: 1n };
|
|
95
|
+
let [previousP, previousQ, p, q] = [0n, 1n, 1n, 0n];
|
|
96
|
+
let [n, d] = [numerator, denominator];
|
|
97
|
+
while (d !== 0n) {
|
|
98
|
+
const quotient = n / d;
|
|
99
|
+
let term = quotient;
|
|
100
|
+
if (p !== 0n) {
|
|
101
|
+
term = minBigInt(term, (maxTerm - previousP) / p);
|
|
102
|
+
}
|
|
103
|
+
if (q !== 0n) {
|
|
104
|
+
term = minBigInt(term, (maxTerm - previousQ) / q);
|
|
105
|
+
}
|
|
106
|
+
const candidate = {
|
|
107
|
+
numerator: term * p + previousP,
|
|
108
|
+
denominator: term * q + previousQ,
|
|
109
|
+
};
|
|
110
|
+
if (candidate.denominator > 0n &&
|
|
111
|
+
candidate.numerator * denominator <= candidate.denominator * numerator &&
|
|
112
|
+
candidate.numerator * best.denominator > best.numerator * candidate.denominator) {
|
|
113
|
+
best = candidate;
|
|
114
|
+
}
|
|
115
|
+
if (term < quotient) {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
[previousP, previousQ, p, q] = [p, q, candidate.numerator, candidate.denominator];
|
|
119
|
+
[n, d] = [d, n % d];
|
|
120
|
+
}
|
|
121
|
+
return best;
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=conversion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversion.js","sourceRoot":"","sources":["../../src/order/conversion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EACL,OAAO,EACP,SAAS,GAGV,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,C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c } from "@ckb-ccc/core";
|
|
2
|
+
import type { OrderGroup } from "./cells.js";
|
|
3
|
+
/**
|
|
4
|
+
* The mining fee one more matched order adds to a prepared transaction at the fee rate
|
|
5
|
+
* (shannons per thousand bytes): its input, its replacement output with data, and its
|
|
6
|
+
* empty witness, sized for the largest order of the pool.
|
|
7
|
+
*/
|
|
8
|
+
export declare function partialOrderFee(orderPool: OrderGroup[], feeRate: ccc.Num): bigint;
|
|
9
|
+
//# sourceMappingURL=fee.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fee.d.ts","sourceRoot":"","sources":["../../src/order/fee.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAe7C;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,UAAU,EAAE,EAAE,OAAO,EAAE,GAAG,CAAC,GAAG,GAAG,MAAM,CAOjF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { ceilDiv } from "../utils/utils.js";
|
|
3
|
+
const CELL_INPUT_SERIALIZED_SIZE = 44;
|
|
4
|
+
// CellOutput table/script wrappers plus DynVec offset; output data Bytes plus offset.
|
|
5
|
+
const CELL_OUTPUT_SERIALIZATION_OVERHEAD = 60;
|
|
6
|
+
const OUTPUT_DATA_SERIALIZATION_OVERHEAD = 8;
|
|
7
|
+
// The supported SDK completion prepares a later signer witness, inserting one
|
|
8
|
+
// empty witness-vector entry for each preceding order input.
|
|
9
|
+
const EMPTY_WITNESS_SERIALIZATION_SIZE = 8;
|
|
10
|
+
const PREPARED_PARTIAL_SERIALIZATION_OVERHEAD = CELL_INPUT_SERIALIZED_SIZE +
|
|
11
|
+
CELL_OUTPUT_SERIALIZATION_OVERHEAD +
|
|
12
|
+
OUTPUT_DATA_SERIALIZATION_OVERHEAD +
|
|
13
|
+
EMPTY_WITNESS_SERIALIZATION_SIZE;
|
|
14
|
+
/**
|
|
15
|
+
* The mining fee one more matched order adds to a prepared transaction at the fee rate
|
|
16
|
+
* (shannons per thousand bytes): its input, its replacement output with data, and its
|
|
17
|
+
* empty witness, sized for the largest order of the pool.
|
|
18
|
+
*/
|
|
19
|
+
export function partialOrderFee(orderPool, feeRate) {
|
|
20
|
+
const orderSize = orderPool.reduce((largest, group) => Math.max(largest, group.order.cell.occupiedSize), 0);
|
|
21
|
+
const bytes = ccc.numFrom(orderSize + PREPARED_PARTIAL_SERIALIZATION_OVERHEAD);
|
|
22
|
+
return ceilDiv(bytes * feeRate, 1000n);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=fee.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fee.js","sourceRoot":"","sources":["../../src/order/fee.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAG5C,MAAM,0BAA0B,GAAG,EAAE,CAAC;AACtC,sFAAsF;AACtF,MAAM,kCAAkC,GAAG,EAAE,CAAC;AAC9C,MAAM,kCAAkC,GAAG,CAAC,CAAC;AAC7C,8EAA8E;AAC9E,6DAA6D;AAC7D,MAAM,gCAAgC,GAAG,CAAC,CAAC;AAC3C,MAAM,uCAAuC,GAC3C,0BAA0B;IAC1B,kCAAkC;IAClC,kCAAkC;IAClC,gCAAgC,CAAC;AAEnC;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,SAAuB,EAAE,OAAgB;IACvE,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAChC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,EACpE,CAAC,CACF,CAAC;IACF,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,uCAAuC,CAAC,CAAC;IAC/E,OAAO,OAAO,CAAC,KAAK,GAAG,OAAO,EAAE,KAAK,CAAC,CAAC;AACzC,CAAC"}
|