Skip to content

Commit 6f10589

Browse files
committed
feat: Transaction utils
Transaction.getInput Transaction.addInput Transaction.getOutput
1 parent a3476f8 commit 6f10589

File tree

6 files changed

+91
-58
lines changed

6 files changed

+91
-58
lines changed

.changeset/rude-numbers-deny.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ckb-ccc/core": minor
3+
"@ckb-ccc/spore": patch
4+
---
5+
6+
feat(core): Transaction utils

packages/core/src/ckb/transaction.ts

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,12 @@ export const CellOutputVec = mol.vector(CellOutput);
229229
/**
230230
* @public
231231
*/
232-
export type CellLike = {
233-
outPoint: OutPointLike;
232+
export type CellLike = (
233+
| {
234+
outPoint: OutPointLike;
235+
}
236+
| { previousOutput: OutPointLike }
237+
) & {
234238
cellOutput: CellOutputLike;
235239
outputData: HexLike;
236240
};
@@ -265,7 +269,7 @@ export class Cell {
265269
}
266270

267271
return new Cell(
268-
OutPoint.from(cell.outPoint),
272+
OutPoint.from("outPoint" in cell ? cell.outPoint : cell.previousOutput),
269273
CellOutput.from(cell.cellOutput),
270274
hexFrom(cell.outputData),
271275
);
@@ -450,8 +454,12 @@ export class Since extends mol.Entity.Base<SinceLike, Since>() {
450454
/**
451455
* @public
452456
*/
453-
export type CellInputLike = {
454-
previousOutput: OutPointLike;
457+
export type CellInputLike = (
458+
| {
459+
previousOutput: OutPointLike;
460+
}
461+
| { outPoint: OutPointLike }
462+
) & {
455463
since?: SinceLike | NumLike | null;
456464
cellOutput?: CellOutputLike | null;
457465
outputData?: HexLike | null;
@@ -465,10 +473,7 @@ export type CellInputLike = {
465473
since: Since,
466474
previousOutput: OutPoint,
467475
})
468-
.mapIn((encodable: CellInputLike) => ({
469-
...encodable,
470-
since: encodable.since ?? 0,
471-
})),
476+
.mapIn((encodable: CellInputLike) => CellInput.from(encodable)),
472477
)
473478
export class CellInput extends mol.Entity.Base<CellInputLike, CellInput>() {
474479
/**
@@ -509,7 +514,11 @@ export class CellInput extends mol.Entity.Base<CellInputLike, CellInput>() {
509514
}
510515

511516
return new CellInput(
512-
OutPoint.from(cellInput.previousOutput),
517+
OutPoint.from(
518+
"previousOutput" in cellInput
519+
? cellInput.previousOutput
520+
: cellInput.outPoint,
521+
),
513522
Since.from(cellInput.since ?? 0).toNum(),
514523
apply(CellOutput.from, cellInput.cellOutput),
515524
apply(hexFrom, cellInput.outputData),
@@ -1230,6 +1239,58 @@ export class Transaction extends mol.Entity.Base<
12301239
this.outputsData[index] = hexFrom(witness);
12311240
}
12321241

1242+
/**
1243+
* get input
1244+
*
1245+
* @param index - The cell input index
1246+
*
1247+
* @example
1248+
* ```typescript
1249+
* await tx.getInput(0);
1250+
* ```
1251+
*/
1252+
getInput(index: NumLike): CellInput | undefined {
1253+
return this.inputs[Number(numFrom(index))];
1254+
}
1255+
/**
1256+
* add input
1257+
*
1258+
* @param inputLike - The cell input.
1259+
*
1260+
* @example
1261+
* ```typescript
1262+
* await tx.addInput({ });
1263+
* ```
1264+
*/
1265+
addInput(inputLike: CellInputLike): number {
1266+
return this.inputs.push(CellInput.from(inputLike));
1267+
}
1268+
1269+
/**
1270+
* get output
1271+
*
1272+
* @param index - The cell output index
1273+
*
1274+
* @example
1275+
* ```typescript
1276+
* await tx.getOutput(0);
1277+
* ```
1278+
*/
1279+
getOutput(index: NumLike):
1280+
| {
1281+
cellOutput: CellOutput;
1282+
outputData: Hex;
1283+
}
1284+
| undefined {
1285+
const i = Number(numFrom(index));
1286+
if (i >= this.outputs.length) {
1287+
return;
1288+
}
1289+
return {
1290+
cellOutput: this.outputs[i],
1291+
outputData: this.outputsData[i] ?? "0x",
1292+
};
1293+
}
12331294
/**
12341295
* Add output
12351296
*
@@ -1245,7 +1306,7 @@ export class Transaction extends mol.Entity.Base<
12451306
outputLike: Omit<CellOutputLike, "capacity"> &
12461307
Partial<Pick<CellOutputLike, "capacity">>,
12471308
outputData: HexLike = "0x",
1248-
): void {
1309+
): number {
12491310
const output = CellOutput.from({
12501311
...outputLike,
12511312
capacity: outputLike.capacity ?? 0,
@@ -1255,8 +1316,10 @@ export class Transaction extends mol.Entity.Base<
12551316
output.occupiedSize + bytesFrom(outputData).length,
12561317
);
12571318
}
1258-
const i = this.outputs.push(output) - 1;
1259-
this.setOutputDataAt(i, outputData);
1319+
const len = this.outputs.push(output);
1320+
this.setOutputDataAt(len - 1, outputData);
1321+
1322+
return len;
12601323
}
12611324

12621325
/**
@@ -1415,15 +1478,7 @@ export class Transaction extends mol.Entity.Base<
14151478
acc = next;
14161479
}
14171480

1418-
this.inputs.push(
1419-
...collectedCells.map(({ outPoint, outputData, cellOutput }) =>
1420-
CellInput.from({
1421-
previousOutput: outPoint,
1422-
outputData,
1423-
cellOutput,
1424-
}),
1425-
),
1426-
);
1481+
collectedCells.forEach((cell) => this.addInput(cell));
14271482
if (fulfilled) {
14281483
return {
14291484
addedCount: collectedCells.length,
@@ -1543,13 +1598,7 @@ export class Transaction extends mol.Entity.Base<
15431598
continue;
15441599
}
15451600

1546-
this.inputs.push(
1547-
CellInput.from({
1548-
previousOutput: cell.outPoint,
1549-
outputData: cell.outputData,
1550-
cellOutput: cell.cellOutput,
1551-
}),
1552-
);
1601+
this.addInput(cell);
15531602
return 1;
15541603
}
15551604

packages/core/src/client/client.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,14 @@ export abstract class Client {
119119
if (!transaction) {
120120
return;
121121
}
122-
123-
const index = Number(numFrom(outPoint.index));
124-
if (index >= transaction.transaction.outputs.length) {
122+
const output = transaction.transaction.getOutput(outPoint.index);
123+
if (!output) {
125124
return;
126125
}
127126

128127
const cell = Cell.from({
129128
outPoint,
130-
cellOutput: transaction.transaction.outputs[index],
131-
outputData: transaction.transaction.outputsData[index] ?? "0x",
129+
...output,
132130
});
133131
await this.cache.recordCells(cell);
134132
return cell;

packages/spore/src/cluster/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,7 @@ export async function transferSporeCluster(params: {
157157
// build cluster cell
158158
const { cell: cluster, scriptInfo } = await assertCluster(signer.client, id);
159159

160-
tx.inputs.push(
161-
ccc.CellInput.from({
162-
previousOutput: cluster.outPoint,
163-
...cluster,
164-
}),
165-
);
160+
tx.addInput(cluster);
166161
tx.addOutput(
167162
{
168163
lock: to,

packages/spore/src/spore/advanced.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ export async function prepareCluster(
5050
return;
5151
}
5252

53-
tx.inputs.push(
54-
ccc.CellInput.from({
55-
previousOutput: cluster.outPoint,
56-
...cluster,
57-
}),
58-
);
53+
tx.addInput(cluster);
5954
tx.addOutput(cluster.cellOutput, cluster.outputData);
6055
await tx.addCellDepInfos(signer.client, scriptInfo.cellDeps);
6156
// note: add cluster as cellDep, which will be used in Spore contract

packages/spore/src/spore/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,7 @@ export async function transferSpore(params: {
175175

176176
const { cell: sporeCell, scriptInfo } = await assertSpore(signer.client, id);
177177
await tx.addCellDepInfos(signer.client, scriptInfo.cellDeps);
178-
tx.inputs.push(
179-
ccc.CellInput.from({
180-
previousOutput: sporeCell.outPoint,
181-
...sporeCell,
182-
}),
183-
);
178+
tx.addInput(sporeCell);
184179
tx.addOutput(
185180
{
186181
lock: to,
@@ -230,12 +225,7 @@ export async function meltSpore(params: {
230225
// build spore cell
231226
const { cell: sporeCell, scriptInfo } = await assertSpore(signer.client, id);
232227
await tx.addCellDepInfos(signer.client, scriptInfo.cellDeps);
233-
tx.inputs.push(
234-
ccc.CellInput.from({
235-
previousOutput: sporeCell.outPoint,
236-
...sporeCell,
237-
}),
238-
);
228+
tx.addInput(sporeCell);
239229

240230
const actions = scriptInfo.cobuild
241231
? [assembleMeltSporeAction(sporeCell.cellOutput, scriptInfoHash)]

0 commit comments

Comments
 (0)