@ckb-ccc/core 1.10.0 → 1.11.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/CHANGELOG.md +43 -4
- package/dist/ckb/hash.d.ts +18 -0
- package/dist/ckb/hash.d.ts.map +1 -0
- package/dist/ckb/hash.js +19 -0
- package/dist/ckb/index.d.ts +1 -0
- package/dist/ckb/index.d.ts.map +1 -1
- package/dist/ckb/index.js +1 -0
- package/dist/ckb/transaction.d.ts +179 -11
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +197 -59
- package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.advanced.js +63 -62
- package/dist/client/clientPublicTestnet.advanced.d.ts +0 -23
- package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.advanced.js +63 -146
- package/dist/client/knownScript.d.ts +1 -3
- package/dist/client/knownScript.d.ts.map +1 -1
- package/dist/client/knownScript.js +0 -4
- package/dist/hasher/hasherCkb.d.ts +0 -16
- package/dist/hasher/hasherCkb.d.ts.map +1 -1
- package/dist/hasher/hasherCkb.js +0 -18
- package/dist/molecule/codec.d.ts +30 -10
- package/dist/molecule/codec.d.ts.map +1 -1
- package/dist/molecule/codec.js +46 -13
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts +18 -6
- package/dist/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist/signer/ckb/signerCkbScriptReadonly.js +18 -6
- package/dist/signer/signer/index.d.ts +11 -1
- package/dist/signer/signer/index.d.ts.map +1 -1
- package/dist/signer/signer/index.js +23 -0
- package/dist.commonjs/ckb/hash.d.ts +18 -0
- package/dist.commonjs/ckb/hash.d.ts.map +1 -0
- package/dist.commonjs/ckb/hash.js +22 -0
- package/dist.commonjs/ckb/index.d.ts +1 -0
- package/dist.commonjs/ckb/index.d.ts.map +1 -1
- package/dist.commonjs/ckb/index.js +1 -0
- package/dist.commonjs/ckb/transaction.d.ts +179 -11
- package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
- package/dist.commonjs/ckb/transaction.js +197 -59
- package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicMainnet.advanced.js +63 -62
- package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +0 -23
- package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicTestnet.advanced.js +64 -147
- package/dist.commonjs/client/knownScript.d.ts +1 -3
- package/dist.commonjs/client/knownScript.d.ts.map +1 -1
- package/dist.commonjs/client/knownScript.js +0 -4
- package/dist.commonjs/hasher/hasherCkb.d.ts +0 -16
- package/dist.commonjs/hasher/hasherCkb.d.ts.map +1 -1
- package/dist.commonjs/hasher/hasherCkb.js +2 -21
- package/dist.commonjs/molecule/codec.d.ts +30 -10
- package/dist.commonjs/molecule/codec.d.ts.map +1 -1
- package/dist.commonjs/molecule/codec.js +46 -13
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts +18 -6
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.d.ts.map +1 -1
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +18 -6
- package/dist.commonjs/signer/signer/index.d.ts +11 -1
- package/dist.commonjs/signer/signer/index.d.ts.map +1 -1
- package/dist.commonjs/signer/signer/index.js +23 -0
- package/package.json +1 -1
- package/prettier.config.mjs +1 -1
- package/src/ckb/hash.ts +28 -0
- package/src/ckb/index.ts +1 -0
- package/src/ckb/transaction.ts +231 -52
- package/src/client/clientPublicMainnet.advanced.ts +72 -70
- package/src/client/clientPublicTestnet.advanced.ts +72 -165
- package/src/client/knownScript.ts +0 -5
- package/src/hasher/hasherCkb.ts +0 -26
- package/src/molecule/codec.ts +50 -13
- package/src/signer/ckb/signerCkbScriptReadonly.ts +24 -8
- package/src/signer/signer/index.ts +34 -0
|
@@ -134,6 +134,29 @@ class Signer {
|
|
|
134
134
|
async getAddresses() {
|
|
135
135
|
return this.getAddressObjs().then((addresses) => addresses.map((address) => address.toString()));
|
|
136
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Find cells of this signer
|
|
139
|
+
*
|
|
140
|
+
* @param filter - The filter for the search key.
|
|
141
|
+
* @param withData - Whether to include cell data in the response.
|
|
142
|
+
* @param order - The order of the returned cells, can be "asc" or "desc".
|
|
143
|
+
* @param limit - The maximum number of cells for every querying chunk.
|
|
144
|
+
* @returns A async generator that yields all matching cells
|
|
145
|
+
*/
|
|
146
|
+
async *findCellsOnChain(filter, withData, order, limit) {
|
|
147
|
+
const scripts = await this.getAddressObjs();
|
|
148
|
+
for (const { script } of scripts) {
|
|
149
|
+
for await (const cell of this.client.findCellsOnChain({
|
|
150
|
+
script,
|
|
151
|
+
scriptType: "lock",
|
|
152
|
+
filter,
|
|
153
|
+
scriptSearchMode: "exact",
|
|
154
|
+
withData,
|
|
155
|
+
}, order, limit)) {
|
|
156
|
+
yield cell;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
137
160
|
/**
|
|
138
161
|
* Find cells of this signer
|
|
139
162
|
*
|
package/package.json
CHANGED
package/prettier.config.mjs
CHANGED
package/src/ckb/hash.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { hashCkb } from "../hasher/hasherCkb.js";
|
|
2
|
+
import { Hex } from "../hex/index.js";
|
|
3
|
+
import { NumLike, numLeToBytes } from "../num/index.js";
|
|
4
|
+
import { CellInput, CellInputLike } from "./transaction.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Computes the Type ID hash of the given data.
|
|
8
|
+
* @public
|
|
9
|
+
*
|
|
10
|
+
* @param cellInputLike - The first cell input of the transaction.
|
|
11
|
+
* @param outputIndex - The output index of the Type ID cell.
|
|
12
|
+
* @returns The hexadecimal string representation of the hash.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const hash = hashTypeId(cellInput, outputIndex); // Outputs something like "0x..."
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export function hashTypeId(
|
|
21
|
+
cellInputLike: CellInputLike,
|
|
22
|
+
outputIndex: NumLike,
|
|
23
|
+
): Hex {
|
|
24
|
+
return hashCkb(
|
|
25
|
+
CellInput.from(cellInputLike).toBytes(),
|
|
26
|
+
numLeToBytes(outputIndex, 8),
|
|
27
|
+
);
|
|
28
|
+
}
|
package/src/ckb/index.ts
CHANGED
package/src/ckb/transaction.ts
CHANGED
|
@@ -235,29 +235,58 @@ export class CellOutput extends mol.Entity.Base<CellOutputLike, CellOutput>() {
|
|
|
235
235
|
|
|
236
236
|
/**
|
|
237
237
|
* Creates a CellOutput instance from a CellOutputLike object.
|
|
238
|
+
* This method supports automatic capacity calculation when capacity is 0 or omitted.
|
|
238
239
|
*
|
|
239
240
|
* @param cellOutput - A CellOutputLike object or an instance of CellOutput.
|
|
241
|
+
* Can also be an object without capacity when outputData is provided.
|
|
242
|
+
* @param outputData - Optional output data used for automatic capacity calculation.
|
|
243
|
+
* When provided and capacity is 0, the capacity will be calculated
|
|
244
|
+
* as occupiedSize + outputData.length.
|
|
240
245
|
* @returns A CellOutput instance.
|
|
241
246
|
*
|
|
242
247
|
* @example
|
|
243
248
|
* ```typescript
|
|
244
|
-
*
|
|
249
|
+
* // Basic usage with explicit capacity
|
|
250
|
+
* const cellOutput1 = CellOutput.from({
|
|
245
251
|
* capacity: 1000n,
|
|
246
252
|
* lock: { codeHash: "0x...", hashType: "type", args: "0x..." },
|
|
247
253
|
* type: { codeHash: "0x...", hashType: "type", args: "0x..." }
|
|
248
254
|
* });
|
|
255
|
+
*
|
|
256
|
+
* // Automatic capacity calculation
|
|
257
|
+
* const cellOutput2 = CellOutput.from({
|
|
258
|
+
* lock: { codeHash: "0x...", hashType: "type", args: "0x..." }
|
|
259
|
+
* }, "0x1234"); // Capacity will be calculated automatically
|
|
249
260
|
* ```
|
|
250
261
|
*/
|
|
251
|
-
static from(cellOutput: CellOutputLike): CellOutput
|
|
262
|
+
static from(cellOutput: CellOutputLike, outputData?: HexLike): CellOutput;
|
|
263
|
+
static from(
|
|
264
|
+
cellOutput: Omit<CellOutputLike, "capacity"> &
|
|
265
|
+
Partial<Pick<CellOutputLike, "capacity">>,
|
|
266
|
+
outputData: HexLike,
|
|
267
|
+
): CellOutput;
|
|
268
|
+
static from(
|
|
269
|
+
cellOutput: Omit<CellOutputLike, "capacity"> &
|
|
270
|
+
Partial<Pick<CellOutputLike, "capacity">>,
|
|
271
|
+
outputData?: HexLike,
|
|
272
|
+
): CellOutput {
|
|
252
273
|
if (cellOutput instanceof CellOutput) {
|
|
253
274
|
return cellOutput;
|
|
254
275
|
}
|
|
255
276
|
|
|
256
|
-
|
|
257
|
-
numFrom(cellOutput.capacity),
|
|
277
|
+
const output = new CellOutput(
|
|
278
|
+
numFrom(cellOutput.capacity ?? 0),
|
|
258
279
|
Script.from(cellOutput.lock),
|
|
259
280
|
apply(Script.from, cellOutput.type),
|
|
260
281
|
);
|
|
282
|
+
|
|
283
|
+
if (output.capacity === Zero) {
|
|
284
|
+
output.capacity = fixedPointFrom(
|
|
285
|
+
output.occupiedSize + bytesFrom(outputData ?? "0x").length,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return output;
|
|
261
290
|
}
|
|
262
291
|
|
|
263
292
|
/**
|
|
@@ -308,9 +337,37 @@ export class Cell {
|
|
|
308
337
|
|
|
309
338
|
/**
|
|
310
339
|
* Creates a Cell instance from a CellLike object.
|
|
340
|
+
* This method accepts either `outPoint` or `previousOutput` to specify the cell's location,
|
|
341
|
+
* and supports automatic capacity calculation for the cell output.
|
|
311
342
|
*
|
|
312
|
-
* @param cell - A CellLike object or an instance of Cell.
|
|
343
|
+
* @param cell - A CellLike object or an instance of Cell. The object can use either:
|
|
344
|
+
* - `outPoint`: For referencing a cell output
|
|
345
|
+
* - `previousOutput`: For referencing a cell input (alternative name for outPoint)
|
|
346
|
+
* The cellOutput can omit capacity for automatic calculation.
|
|
313
347
|
* @returns A Cell instance.
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* ```typescript
|
|
351
|
+
* // Using outPoint with explicit capacity
|
|
352
|
+
* const cell1 = Cell.from({
|
|
353
|
+
* outPoint: { txHash: "0x...", index: 0 },
|
|
354
|
+
* cellOutput: {
|
|
355
|
+
* capacity: 1000n,
|
|
356
|
+
* lock: { codeHash: "0x...", hashType: "type", args: "0x..." }
|
|
357
|
+
* },
|
|
358
|
+
* outputData: "0x"
|
|
359
|
+
* });
|
|
360
|
+
*
|
|
361
|
+
* // Using previousOutput with automatic capacity calculation
|
|
362
|
+
* const cell2 = Cell.from({
|
|
363
|
+
* previousOutput: { txHash: "0x...", index: 0 },
|
|
364
|
+
* cellOutput: {
|
|
365
|
+
* lock: { codeHash: "0x...", hashType: "type", args: "0x..." }
|
|
366
|
+
* // capacity will be calculated automatically
|
|
367
|
+
* },
|
|
368
|
+
* outputData: "0x1234"
|
|
369
|
+
* });
|
|
370
|
+
* ```
|
|
314
371
|
*/
|
|
315
372
|
|
|
316
373
|
static from(cell: CellLike): Cell {
|
|
@@ -320,7 +377,7 @@ export class Cell {
|
|
|
320
377
|
|
|
321
378
|
return new Cell(
|
|
322
379
|
OutPoint.from("outPoint" in cell ? cell.outPoint : cell.previousOutput),
|
|
323
|
-
CellOutput.from(cell.cellOutput),
|
|
380
|
+
CellOutput.from(cell.cellOutput, cell.outputData),
|
|
324
381
|
hexFrom(cell.outputData),
|
|
325
382
|
);
|
|
326
383
|
}
|
|
@@ -1001,6 +1058,9 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1001
1058
|
|
|
1002
1059
|
/**
|
|
1003
1060
|
* Copy every properties from another transaction.
|
|
1061
|
+
* This method replaces all properties of the current transaction with those from the provided transaction.
|
|
1062
|
+
*
|
|
1063
|
+
* @param txLike - The transaction-like object to copy properties from.
|
|
1004
1064
|
*
|
|
1005
1065
|
* @example
|
|
1006
1066
|
* ```typescript
|
|
@@ -1092,19 +1152,9 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1092
1152
|
return tx;
|
|
1093
1153
|
}
|
|
1094
1154
|
const outputs =
|
|
1095
|
-
tx.outputs?.map((output, i) =>
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
capacity: output.capacity ?? 0,
|
|
1099
|
-
});
|
|
1100
|
-
if (o.capacity === Zero) {
|
|
1101
|
-
o.capacity = fixedPointFrom(
|
|
1102
|
-
o.occupiedSize +
|
|
1103
|
-
(apply(bytesFrom, tx.outputsData?.[i])?.length ?? 0),
|
|
1104
|
-
);
|
|
1105
|
-
}
|
|
1106
|
-
return o;
|
|
1107
|
-
}) ?? [];
|
|
1155
|
+
tx.outputs?.map((output, i) =>
|
|
1156
|
+
CellOutput.from(output, tx.outputsData?.[i] ?? []),
|
|
1157
|
+
) ?? [];
|
|
1108
1158
|
const outputsData = outputs.map((_, i) =>
|
|
1109
1159
|
hexFrom(tx.outputsData?.[i] ?? "0x"),
|
|
1110
1160
|
);
|
|
@@ -1332,7 +1382,7 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1332
1382
|
*
|
|
1333
1383
|
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
1334
1384
|
* @param client - The client for complete extra infos in the transaction.
|
|
1335
|
-
* @returns A promise that resolves to the
|
|
1385
|
+
* @returns A promise that resolves to the found index, or undefined if no matching input is found.
|
|
1336
1386
|
*
|
|
1337
1387
|
* @example
|
|
1338
1388
|
* ```typescript
|
|
@@ -1359,7 +1409,7 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1359
1409
|
*
|
|
1360
1410
|
* @param scriptLike - The script associated with the transaction, represented as a ScriptLike object.
|
|
1361
1411
|
* @param client - The client for complete extra infos in the transaction.
|
|
1362
|
-
* @returns A promise that resolves to the
|
|
1412
|
+
* @returns A promise that resolves to the found index, or undefined if no matching input is found.
|
|
1363
1413
|
*
|
|
1364
1414
|
* @example
|
|
1365
1415
|
* ```typescript
|
|
@@ -1472,14 +1522,14 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1472
1522
|
* Set output data at index.
|
|
1473
1523
|
*
|
|
1474
1524
|
* @param index - The index of the output data.
|
|
1475
|
-
* @param
|
|
1525
|
+
* @param data - The data to set.
|
|
1476
1526
|
*
|
|
1477
1527
|
* @example
|
|
1478
1528
|
* ```typescript
|
|
1479
|
-
*
|
|
1529
|
+
* tx.setOutputDataAt(0, "0x00");
|
|
1480
1530
|
* ```
|
|
1481
1531
|
*/
|
|
1482
|
-
setOutputDataAt(index: number,
|
|
1532
|
+
setOutputDataAt(index: number, data: HexLike): void {
|
|
1483
1533
|
if (this.outputsData.length < index) {
|
|
1484
1534
|
this.outputsData.push(
|
|
1485
1535
|
...Array.from(
|
|
@@ -1489,7 +1539,7 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1489
1539
|
);
|
|
1490
1540
|
}
|
|
1491
1541
|
|
|
1492
|
-
this.outputsData[index] = hexFrom(
|
|
1542
|
+
this.outputsData[index] = hexFrom(data);
|
|
1493
1543
|
}
|
|
1494
1544
|
|
|
1495
1545
|
/**
|
|
@@ -1564,16 +1614,7 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1564
1614
|
Partial<Pick<CellOutputLike, "capacity">>,
|
|
1565
1615
|
outputData: HexLike = "0x",
|
|
1566
1616
|
): number {
|
|
1567
|
-
const
|
|
1568
|
-
...outputLike,
|
|
1569
|
-
capacity: outputLike.capacity ?? 0,
|
|
1570
|
-
});
|
|
1571
|
-
if (output.capacity === Zero) {
|
|
1572
|
-
output.capacity = fixedPointFrom(
|
|
1573
|
-
output.occupiedSize + bytesFrom(outputData).length,
|
|
1574
|
-
);
|
|
1575
|
-
}
|
|
1576
|
-
const len = this.outputs.push(output);
|
|
1617
|
+
const len = this.outputs.push(CellOutput.from(outputLike, outputData));
|
|
1577
1618
|
this.setOutputDataAt(len - 1, outputData);
|
|
1578
1619
|
|
|
1579
1620
|
return len;
|
|
@@ -1998,21 +2039,22 @@ export class Transaction extends mol.Entity.Base<
|
|
|
1998
2039
|
|
|
1999
2040
|
let leastFee = Zero;
|
|
2000
2041
|
let leastExtraCapacity = Zero;
|
|
2001
|
-
|
|
2042
|
+
let collected = 0;
|
|
2043
|
+
|
|
2044
|
+
// ===
|
|
2045
|
+
// Usually, for the worst situation, three iterations are needed
|
|
2046
|
+
// 1. First attempt to complete the transaction.
|
|
2047
|
+
// 2. Not enough capacity for the change cell.
|
|
2048
|
+
// 3. Fee increased by the change cell.
|
|
2049
|
+
// ===
|
|
2002
2050
|
while (true) {
|
|
2003
|
-
|
|
2004
|
-
const collected = await (async () => {
|
|
2051
|
+
collected += await (async () => {
|
|
2005
2052
|
if (!(options?.shouldAddInputs ?? true)) {
|
|
2006
|
-
const fee =
|
|
2007
|
-
(await tx.getFee(from.client)) - leastFee - leastExtraCapacity;
|
|
2008
|
-
if (fee < Zero) {
|
|
2009
|
-
throw new ErrorTransactionInsufficientCapacity(-fee);
|
|
2010
|
-
}
|
|
2011
2053
|
return 0;
|
|
2012
2054
|
}
|
|
2013
2055
|
|
|
2014
2056
|
try {
|
|
2015
|
-
return await
|
|
2057
|
+
return await this.completeInputsByCapacity(
|
|
2016
2058
|
from,
|
|
2017
2059
|
leastFee + leastExtraCapacity,
|
|
2018
2060
|
filter,
|
|
@@ -2031,21 +2073,33 @@ export class Transaction extends mol.Entity.Base<
|
|
|
2031
2073
|
}
|
|
2032
2074
|
})();
|
|
2033
2075
|
|
|
2034
|
-
await from.
|
|
2076
|
+
const fee = await this.getFee(from.client);
|
|
2077
|
+
if (fee < leastFee + leastExtraCapacity) {
|
|
2078
|
+
// Not enough capacity are collected, it should only happens when shouldAddInputs is false
|
|
2079
|
+
throw new ErrorTransactionInsufficientCapacity(
|
|
2080
|
+
leastFee + leastExtraCapacity - fee,
|
|
2081
|
+
{ isForChange: leastExtraCapacity !== Zero },
|
|
2082
|
+
);
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2085
|
+
await from.prepareTransaction(this);
|
|
2035
2086
|
if (leastFee === Zero) {
|
|
2036
2087
|
// The initial fee is calculated based on prepared transaction
|
|
2037
|
-
|
|
2088
|
+
// This should only happens during the first iteration
|
|
2089
|
+
leastFee = this.estimateFee(feeRate);
|
|
2038
2090
|
}
|
|
2039
|
-
const fee = await tx.getFee(from.client);
|
|
2040
2091
|
// The extra capacity paid the fee without a change
|
|
2092
|
+
// leastExtraCapacity should be 0 here, otherwise we should failed in the previous check
|
|
2093
|
+
// So this only happens in the first iteration
|
|
2041
2094
|
if (fee === leastFee) {
|
|
2042
|
-
this.copy(tx);
|
|
2043
2095
|
return [collected, false];
|
|
2044
2096
|
}
|
|
2045
2097
|
|
|
2098
|
+
// Invoke the change function on a transaction multiple times may cause problems, so we clone it
|
|
2099
|
+
const tx = this.clone();
|
|
2046
2100
|
const needed = numFrom(await Promise.resolve(change(tx, fee - leastFee)));
|
|
2047
|
-
// No enough extra capacity to create new cells for change
|
|
2048
2101
|
if (needed > Zero) {
|
|
2102
|
+
// No enough extra capacity to create new cells for change, collect inputs again
|
|
2049
2103
|
leastExtraCapacity = needed;
|
|
2050
2104
|
continue;
|
|
2051
2105
|
}
|
|
@@ -2073,6 +2127,38 @@ export class Transaction extends mol.Entity.Base<
|
|
|
2073
2127
|
}
|
|
2074
2128
|
}
|
|
2075
2129
|
|
|
2130
|
+
/**
|
|
2131
|
+
* Completes the transaction fee by adding inputs and creating a change output with the specified lock script.
|
|
2132
|
+
* This is a convenience method that automatically creates a change cell with the provided lock script
|
|
2133
|
+
* when there's excess capacity after paying the transaction fee.
|
|
2134
|
+
*
|
|
2135
|
+
* @param from - The signer to complete inputs from and prepare the transaction.
|
|
2136
|
+
* @param change - The lock script for the change output cell.
|
|
2137
|
+
* @param feeRate - Optional fee rate in shannons per 1000 bytes. If not provided, it will be fetched from the client.
|
|
2138
|
+
* @param filter - Optional filter for selecting cells when adding inputs.
|
|
2139
|
+
* @param options - Optional configuration object.
|
|
2140
|
+
* @param options.feeRateBlockRange - Block range for fee rate calculation when feeRate is not provided.
|
|
2141
|
+
* @param options.maxFeeRate - Maximum allowed fee rate.
|
|
2142
|
+
* @param options.shouldAddInputs - Whether to add inputs automatically. Defaults to true.
|
|
2143
|
+
* @returns A promise that resolves to a tuple containing:
|
|
2144
|
+
* - The number of inputs added during the process
|
|
2145
|
+
* - A boolean indicating whether change outputs were created (true) or fee was paid without change (false)
|
|
2146
|
+
*
|
|
2147
|
+
* @example
|
|
2148
|
+
* ```typescript
|
|
2149
|
+
* const changeScript = Script.from({
|
|
2150
|
+
* codeHash: "0x...",
|
|
2151
|
+
* hashType: "type",
|
|
2152
|
+
* args: "0x..."
|
|
2153
|
+
* });
|
|
2154
|
+
*
|
|
2155
|
+
* const [addedInputs, hasChange] = await tx.completeFeeChangeToLock(
|
|
2156
|
+
* signer,
|
|
2157
|
+
* changeScript,
|
|
2158
|
+
* 1000n // 1000 shannons per 1000 bytes
|
|
2159
|
+
* );
|
|
2160
|
+
* ```
|
|
2161
|
+
*/
|
|
2076
2162
|
completeFeeChangeToLock(
|
|
2077
2163
|
from: Signer,
|
|
2078
2164
|
change: ScriptLike,
|
|
@@ -2104,6 +2190,33 @@ export class Transaction extends mol.Entity.Base<
|
|
|
2104
2190
|
);
|
|
2105
2191
|
}
|
|
2106
2192
|
|
|
2193
|
+
/**
|
|
2194
|
+
* Completes the transaction fee using the signer's recommended address for change.
|
|
2195
|
+
* This is a convenience method that automatically uses the signer's recommended
|
|
2196
|
+
* address as the change destination, making it easier to complete transactions
|
|
2197
|
+
* without manually specifying a change address.
|
|
2198
|
+
*
|
|
2199
|
+
* @param from - The signer to complete inputs from and prepare the transaction.
|
|
2200
|
+
* @param feeRate - Optional fee rate in shannons per 1000 bytes. If not provided, it will be fetched from the client.
|
|
2201
|
+
* @param filter - Optional filter for selecting cells when adding inputs.
|
|
2202
|
+
* @param options - Optional configuration object.
|
|
2203
|
+
* @param options.feeRateBlockRange - Block range for fee rate calculation when feeRate is not provided.
|
|
2204
|
+
* @param options.maxFeeRate - Maximum allowed fee rate.
|
|
2205
|
+
* @param options.shouldAddInputs - Whether to add inputs automatically. Defaults to true.
|
|
2206
|
+
* @returns A promise that resolves to a tuple containing:
|
|
2207
|
+
* - The number of inputs added during the process
|
|
2208
|
+
* - A boolean indicating whether change outputs were created (true) or fee was paid without change (false)
|
|
2209
|
+
*
|
|
2210
|
+
* @example
|
|
2211
|
+
* ```typescript
|
|
2212
|
+
* const [addedInputs, hasChange] = await tx.completeFeeBy(
|
|
2213
|
+
* signer,
|
|
2214
|
+
* 1000n // 1000 shannons per 1000 bytes
|
|
2215
|
+
* );
|
|
2216
|
+
*
|
|
2217
|
+
* // Change will automatically go to signer's recommended address
|
|
2218
|
+
* ```
|
|
2219
|
+
*/
|
|
2107
2220
|
async completeFeeBy(
|
|
2108
2221
|
from: Signer,
|
|
2109
2222
|
feeRate?: NumLike,
|
|
@@ -2119,6 +2232,35 @@ export class Transaction extends mol.Entity.Base<
|
|
|
2119
2232
|
return this.completeFeeChangeToLock(from, script, feeRate, filter, options);
|
|
2120
2233
|
}
|
|
2121
2234
|
|
|
2235
|
+
/**
|
|
2236
|
+
* Completes the transaction fee by adding excess capacity to an existing output.
|
|
2237
|
+
* Instead of creating a new change output, this method adds any excess capacity
|
|
2238
|
+
* to the specified existing output in the transaction.
|
|
2239
|
+
*
|
|
2240
|
+
* @param from - The signer to complete inputs from and prepare the transaction.
|
|
2241
|
+
* @param index - The index of the existing output to add excess capacity to.
|
|
2242
|
+
* @param feeRate - Optional fee rate in shannons per 1000 bytes. If not provided, it will be fetched from the client.
|
|
2243
|
+
* @param filter - Optional filter for selecting cells when adding inputs.
|
|
2244
|
+
* @param options - Optional configuration object.
|
|
2245
|
+
* @param options.feeRateBlockRange - Block range for fee rate calculation when feeRate is not provided.
|
|
2246
|
+
* @param options.maxFeeRate - Maximum allowed fee rate.
|
|
2247
|
+
* @param options.shouldAddInputs - Whether to add inputs automatically. Defaults to true.
|
|
2248
|
+
* @returns A promise that resolves to a tuple containing:
|
|
2249
|
+
* - The number of inputs added during the process
|
|
2250
|
+
* - A boolean indicating whether change was applied (true) or fee was paid without change (false)
|
|
2251
|
+
*
|
|
2252
|
+
* @throws {Error} When the specified output index doesn't exist.
|
|
2253
|
+
*
|
|
2254
|
+
* @example
|
|
2255
|
+
* ```typescript
|
|
2256
|
+
* // Add excess capacity to the first output (index 0)
|
|
2257
|
+
* const [addedInputs, hasChange] = await tx.completeFeeChangeToOutput(
|
|
2258
|
+
* signer,
|
|
2259
|
+
* 0, // Output index
|
|
2260
|
+
* 1000n // 1000 shannons per 1000 bytes
|
|
2261
|
+
* );
|
|
2262
|
+
* ```
|
|
2263
|
+
*/
|
|
2122
2264
|
completeFeeChangeToOutput(
|
|
2123
2265
|
from: Signer,
|
|
2124
2266
|
index: NumLike,
|
|
@@ -2148,7 +2290,26 @@ export class Transaction extends mol.Entity.Base<
|
|
|
2148
2290
|
}
|
|
2149
2291
|
|
|
2150
2292
|
/**
|
|
2151
|
-
* Calculate Nervos DAO profit between two blocks
|
|
2293
|
+
* Calculate Nervos DAO profit between two blocks.
|
|
2294
|
+
* This function computes the profit earned from a Nervos DAO deposit
|
|
2295
|
+
* based on the capacity and the time period between deposit and withdrawal.
|
|
2296
|
+
*
|
|
2297
|
+
* @param profitableCapacity - The capacity that earns profit (total capacity minus occupied capacity).
|
|
2298
|
+
* @param depositHeaderLike - The block header when the DAO deposit was made.
|
|
2299
|
+
* @param withdrawHeaderLike - The block header when the DAO withdrawal is made.
|
|
2300
|
+
* @returns The profit amount in CKB (capacity units).
|
|
2301
|
+
*
|
|
2302
|
+
* @example
|
|
2303
|
+
* ```typescript
|
|
2304
|
+
* const profit = calcDaoProfit(
|
|
2305
|
+
* ccc.fixedPointFrom(100), // 100 CKB profitable capacity
|
|
2306
|
+
* depositHeader,
|
|
2307
|
+
* withdrawHeader
|
|
2308
|
+
* );
|
|
2309
|
+
* console.log(`Profit: ${profit} shannons`);
|
|
2310
|
+
* ```
|
|
2311
|
+
*
|
|
2312
|
+
* @see {@link https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md | Nervos DAO RFC}
|
|
2152
2313
|
*/
|
|
2153
2314
|
export function calcDaoProfit(
|
|
2154
2315
|
profitableCapacity: NumLike,
|
|
@@ -2167,8 +2328,26 @@ export function calcDaoProfit(
|
|
|
2167
2328
|
}
|
|
2168
2329
|
|
|
2169
2330
|
/**
|
|
2170
|
-
* Calculate claimable epoch for Nervos DAO withdrawal
|
|
2171
|
-
*
|
|
2331
|
+
* Calculate claimable epoch for Nervos DAO withdrawal.
|
|
2332
|
+
* This function determines the earliest epoch when a Nervos DAO withdrawal
|
|
2333
|
+
* can be claimed based on the deposit and withdrawal epochs.
|
|
2334
|
+
*
|
|
2335
|
+
* @param depositHeader - The block header when the DAO deposit was made.
|
|
2336
|
+
* @param withdrawHeader - The block header when the DAO withdrawal was initiated.
|
|
2337
|
+
* @returns The epoch when the withdrawal can be claimed, represented as [number, index, length].
|
|
2338
|
+
*
|
|
2339
|
+
* @example
|
|
2340
|
+
* ```typescript
|
|
2341
|
+
* const claimEpoch = calcDaoClaimEpoch(depositHeader, withdrawHeader);
|
|
2342
|
+
* console.log(`Can claim at epoch: ${claimEpoch[0]}, index: ${claimEpoch[1]}, length: ${claimEpoch[2]}`);
|
|
2343
|
+
* ```
|
|
2344
|
+
*
|
|
2345
|
+
* @remarks
|
|
2346
|
+
* The Nervos DAO has a minimum lock period of 180 epochs (~30 days).
|
|
2347
|
+
* This function calculates the exact epoch when the withdrawal becomes claimable
|
|
2348
|
+
* based on the deposit epoch and withdrawal epoch timing.
|
|
2349
|
+
*
|
|
2350
|
+
* @see {@link https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md | Nervos DAO RFC}
|
|
2172
2351
|
*/
|
|
2173
2352
|
export function calcDaoClaimEpoch(
|
|
2174
2353
|
depositHeader: ClientBlockHeaderLike,
|
|
@@ -120,10 +120,80 @@ export const MAINNET_SCRIPTS: Record<KnownScript, ScriptInfoLike | undefined> =
|
|
|
120
120
|
cellDep: {
|
|
121
121
|
outPoint: {
|
|
122
122
|
txHash:
|
|
123
|
-
"
|
|
123
|
+
"0xbdc3153fcb7c256d007be758c0b50c8da60b5ac0e44ae9e11d3dcc5b73d2b347",
|
|
124
124
|
index: 0,
|
|
125
125
|
},
|
|
126
|
-
depType: "
|
|
126
|
+
depType: "code",
|
|
127
|
+
},
|
|
128
|
+
type: {
|
|
129
|
+
codeHash:
|
|
130
|
+
"0x00000000000000000000000000000000000000000000000000545950455f4944",
|
|
131
|
+
hashType: "type",
|
|
132
|
+
args: "0xd00c84f0ec8fd441c38bc3f87a371f547190f2fcff88e642bc5bf54b9e318323",
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
cellDep: {
|
|
137
|
+
outPoint: {
|
|
138
|
+
txHash:
|
|
139
|
+
"0xbac0f6f0a177a03974870df47ab0c3e761c5dd19e9ca165358d0b72bd7f433f5",
|
|
140
|
+
index: 1,
|
|
141
|
+
},
|
|
142
|
+
depType: "code",
|
|
143
|
+
},
|
|
144
|
+
type: {
|
|
145
|
+
codeHash:
|
|
146
|
+
"0x00000000000000000000000000000000000000000000000000545950455f4944",
|
|
147
|
+
hashType: "type",
|
|
148
|
+
args: "0xaede124e8eca8ea4a0029d5f0999299a3f9a4090c6fd6f83d88c41707cc8aa75",
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
cellDep: {
|
|
153
|
+
outPoint: {
|
|
154
|
+
txHash:
|
|
155
|
+
"0xbac0f6f0a177a03974870df47ab0c3e761c5dd19e9ca165358d0b72bd7f433f5",
|
|
156
|
+
index: 2,
|
|
157
|
+
},
|
|
158
|
+
depType: "code",
|
|
159
|
+
},
|
|
160
|
+
type: {
|
|
161
|
+
codeHash:
|
|
162
|
+
"0x00000000000000000000000000000000000000000000000000545950455f4944",
|
|
163
|
+
hashType: "type",
|
|
164
|
+
args: "0x2409a9568c2ae67e73e12b1f0193c9a44c782ec1c73627b3c92ad2d0fb6128ed",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
cellDep: {
|
|
169
|
+
outPoint: {
|
|
170
|
+
txHash:
|
|
171
|
+
"0xbac0f6f0a177a03974870df47ab0c3e761c5dd19e9ca165358d0b72bd7f433f5",
|
|
172
|
+
index: 3,
|
|
173
|
+
},
|
|
174
|
+
depType: "code",
|
|
175
|
+
},
|
|
176
|
+
type: {
|
|
177
|
+
codeHash:
|
|
178
|
+
"0x00000000000000000000000000000000000000000000000000545950455f4944",
|
|
179
|
+
hashType: "type",
|
|
180
|
+
args: "0xaf96ed62f5997c4a4814058545bc958124bada97fcd42955756e3c078da64523",
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
cellDep: {
|
|
185
|
+
outPoint: {
|
|
186
|
+
txHash:
|
|
187
|
+
"0xbac0f6f0a177a03974870df47ab0c3e761c5dd19e9ca165358d0b72bd7f433f5",
|
|
188
|
+
index: 4,
|
|
189
|
+
},
|
|
190
|
+
depType: "code",
|
|
191
|
+
},
|
|
192
|
+
type: {
|
|
193
|
+
codeHash:
|
|
194
|
+
"0x00000000000000000000000000000000000000000000000000545950455f4944",
|
|
195
|
+
hashType: "type",
|
|
196
|
+
args: "0xfa9f0c3599dc23b9489a11dfedb11489c7ad342982848495856966ed23a6bc7d",
|
|
127
197
|
},
|
|
128
198
|
},
|
|
129
199
|
],
|
|
@@ -387,72 +457,4 @@ export const MAINNET_SCRIPTS: Record<KnownScript, ScriptInfoLike | undefined> =
|
|
|
387
457
|
},
|
|
388
458
|
],
|
|
389
459
|
},
|
|
390
|
-
[KnownScript.RgbppLock]: {
|
|
391
|
-
codeHash:
|
|
392
|
-
"0xbc6c568a1a0d0a09f6844dc9d74ddb4343c32143ff25f727c59edf4fb72d6936",
|
|
393
|
-
hashType: "type",
|
|
394
|
-
cellDeps: [
|
|
395
|
-
{
|
|
396
|
-
cellDep: {
|
|
397
|
-
outPoint: {
|
|
398
|
-
txHash:
|
|
399
|
-
"0x04c5c3e69f1aa6ee27fb9de3d15a81704e387ab3b453965adbe0b6ca343c6f41",
|
|
400
|
-
index: 0,
|
|
401
|
-
},
|
|
402
|
-
depType: "code",
|
|
403
|
-
},
|
|
404
|
-
type: {
|
|
405
|
-
codeHash:
|
|
406
|
-
"0x00000000000000000000000000000000000000000000000000545950455f4944",
|
|
407
|
-
hashType: "type",
|
|
408
|
-
args: "0x68ad3d9e0bb9ea841a5d1fcd600137bd3f45401e759e353121f26cd0d981452f",
|
|
409
|
-
},
|
|
410
|
-
},
|
|
411
|
-
// Rgbpp lock config cell dep
|
|
412
|
-
{
|
|
413
|
-
cellDep: {
|
|
414
|
-
outPoint: {
|
|
415
|
-
txHash:
|
|
416
|
-
"0x04c5c3e69f1aa6ee27fb9de3d15a81704e387ab3b453965adbe0b6ca343c6f41",
|
|
417
|
-
index: 1,
|
|
418
|
-
},
|
|
419
|
-
depType: "code",
|
|
420
|
-
},
|
|
421
|
-
},
|
|
422
|
-
],
|
|
423
|
-
},
|
|
424
|
-
[KnownScript.BtcTimeLock]: {
|
|
425
|
-
codeHash:
|
|
426
|
-
"0x70d64497a075bd651e98ac030455ea200637ee325a12ad08aff03f1a117e5a62",
|
|
427
|
-
hashType: "type",
|
|
428
|
-
cellDeps: [
|
|
429
|
-
{
|
|
430
|
-
cellDep: {
|
|
431
|
-
outPoint: {
|
|
432
|
-
txHash:
|
|
433
|
-
"0x6257bf4297ee75fcebe2654d8c5f8d93bc9fc1b3dc62b8cef54ffe166162e996",
|
|
434
|
-
index: 0,
|
|
435
|
-
},
|
|
436
|
-
depType: "code",
|
|
437
|
-
},
|
|
438
|
-
type: {
|
|
439
|
-
codeHash:
|
|
440
|
-
"0x00000000000000000000000000000000000000000000000000545950455f4944",
|
|
441
|
-
hashType: "type",
|
|
442
|
-
args: "0x44b8253ae18e913a2845b0d548eaf6b3ba1099ed26835888932a754194028a8a",
|
|
443
|
-
},
|
|
444
|
-
},
|
|
445
|
-
// btc time lock config cell dep
|
|
446
|
-
{
|
|
447
|
-
cellDep: {
|
|
448
|
-
outPoint: {
|
|
449
|
-
txHash:
|
|
450
|
-
"0x6257bf4297ee75fcebe2654d8c5f8d93bc9fc1b3dc62b8cef54ffe166162e996",
|
|
451
|
-
index: 1,
|
|
452
|
-
},
|
|
453
|
-
depType: "code",
|
|
454
|
-
},
|
|
455
|
-
},
|
|
456
|
-
],
|
|
457
|
-
},
|
|
458
460
|
});
|