@@ -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)
473478export 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
0 commit comments