@@ -229,8 +229,12 @@ export const CellOutputVec = mol.vector(CellOutput);
229
229
/**
230
230
* @public
231
231
*/
232
- export type CellLike = {
233
- outPoint : OutPointLike ;
232
+ export type CellLike = (
233
+ | {
234
+ outPoint : OutPointLike ;
235
+ }
236
+ | { previousOutput : OutPointLike }
237
+ ) & {
234
238
cellOutput : CellOutputLike ;
235
239
outputData : HexLike ;
236
240
} ;
@@ -265,7 +269,7 @@ export class Cell {
265
269
}
266
270
267
271
return new Cell (
268
- OutPoint . from ( cell . outPoint ) ,
272
+ OutPoint . from ( "outPoint" in cell ? cell . outPoint : cell . previousOutput ) ,
269
273
CellOutput . from ( cell . cellOutput ) ,
270
274
hexFrom ( cell . outputData ) ,
271
275
) ;
@@ -450,8 +454,12 @@ export class Since extends mol.Entity.Base<SinceLike, Since>() {
450
454
/**
451
455
* @public
452
456
*/
453
- export type CellInputLike = {
454
- previousOutput : OutPointLike ;
457
+ export type CellInputLike = (
458
+ | {
459
+ previousOutput : OutPointLike ;
460
+ }
461
+ | { outPoint : OutPointLike }
462
+ ) & {
455
463
since ?: SinceLike | NumLike | null ;
456
464
cellOutput ?: CellOutputLike | null ;
457
465
outputData ?: HexLike | null ;
@@ -465,10 +473,7 @@ export type CellInputLike = {
465
473
since : Since ,
466
474
previousOutput : OutPoint ,
467
475
} )
468
- . mapIn ( ( encodable : CellInputLike ) => ( {
469
- ...encodable ,
470
- since : encodable . since ?? 0 ,
471
- } ) ) ,
476
+ . mapIn ( ( encodable : CellInputLike ) => CellInput . from ( encodable ) ) ,
472
477
)
473
478
export class CellInput extends mol . Entity . Base < CellInputLike , CellInput > ( ) {
474
479
/**
@@ -509,7 +514,11 @@ export class CellInput extends mol.Entity.Base<CellInputLike, CellInput>() {
509
514
}
510
515
511
516
return new CellInput (
512
- OutPoint . from ( cellInput . previousOutput ) ,
517
+ OutPoint . from (
518
+ "previousOutput" in cellInput
519
+ ? cellInput . previousOutput
520
+ : cellInput . outPoint ,
521
+ ) ,
513
522
Since . from ( cellInput . since ?? 0 ) . toNum ( ) ,
514
523
apply ( CellOutput . from , cellInput . cellOutput ) ,
515
524
apply ( hexFrom , cellInput . outputData ) ,
@@ -1230,6 +1239,58 @@ export class Transaction extends mol.Entity.Base<
1230
1239
this . outputsData [ index ] = hexFrom ( witness ) ;
1231
1240
}
1232
1241
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
+ }
1233
1294
/**
1234
1295
* Add output
1235
1296
*
@@ -1245,7 +1306,7 @@ export class Transaction extends mol.Entity.Base<
1245
1306
outputLike : Omit < CellOutputLike , "capacity" > &
1246
1307
Partial < Pick < CellOutputLike , "capacity" > > ,
1247
1308
outputData : HexLike = "0x" ,
1248
- ) : void {
1309
+ ) : number {
1249
1310
const output = CellOutput . from ( {
1250
1311
...outputLike ,
1251
1312
capacity : outputLike . capacity ?? 0 ,
@@ -1255,8 +1316,10 @@ export class Transaction extends mol.Entity.Base<
1255
1316
output . occupiedSize + bytesFrom ( outputData ) . length ,
1256
1317
) ;
1257
1318
}
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 ;
1260
1323
}
1261
1324
1262
1325
/**
@@ -1415,15 +1478,7 @@ export class Transaction extends mol.Entity.Base<
1415
1478
acc = next ;
1416
1479
}
1417
1480
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 ) ) ;
1427
1482
if ( fulfilled ) {
1428
1483
return {
1429
1484
addedCount : collectedCells . length ,
@@ -1543,13 +1598,7 @@ export class Transaction extends mol.Entity.Base<
1543
1598
continue ;
1544
1599
}
1545
1600
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 ) ;
1553
1602
return 1 ;
1554
1603
}
1555
1604
0 commit comments