@@ -158,21 +158,73 @@ export class ApplicationInnerTxn extends ApplicationTransaction implements itxn.
158158 }
159159}
160160
161- export const createInnerTxn = ( fields : InnerTxnFields ) : InnerTxn => {
161+ export const createInnerTxn = < TFields extends InnerTxnFields , T extends InnerTxn > ( fields : TFields ) : T => {
162162 switch ( fields . type ) {
163163 case TransactionType . Payment :
164- return new PaymentInnerTxn ( fields )
164+ return new PaymentInnerTxn ( fields ) as T
165165 case TransactionType . AssetConfig :
166- return new AssetConfigInnerTxn ( fields )
166+ return new AssetConfigInnerTxn ( fields ) as T
167167 case TransactionType . AssetTransfer :
168- return new AssetTransferInnerTxn ( fields as itxn . AssetTransferFields )
168+ return new AssetTransferInnerTxn ( fields as itxn . AssetTransferFields ) as T
169169 case TransactionType . AssetFreeze :
170- return new AssetFreezeInnerTxn ( fields as itxn . AssetFreezeFields )
170+ return new AssetFreezeInnerTxn ( fields as itxn . AssetFreezeFields ) as T
171171 case TransactionType . ApplicationCall :
172- return new ApplicationInnerTxn ( fields )
172+ return new ApplicationInnerTxn ( fields ) as unknown as T
173173 case TransactionType . KeyRegistration :
174- return new KeyRegistrationInnerTxn ( fields )
174+ return new KeyRegistrationInnerTxn ( fields ) as T
175175 default :
176- internal . errors . internalError ( `Invalid inner transaction type: ${ fields . type } ` )
176+ throw new internal . errors . InternalError ( `Invalid inner transaction type: ${ fields . type } ` )
177+ }
178+ }
179+
180+ export function submitGroup < TFields extends itxn . InnerTxnList > ( ...transactionFields : TFields ) : itxn . TxnFor < TFields > {
181+ return transactionFields . map ( ( f : ( typeof transactionFields ) [ number ] ) => f . submit ( ) ) as itxn . TxnFor < TFields >
182+ }
183+ export function payment ( fields : itxn . PaymentFields ) : itxn . PaymentItxnParams {
184+ ensureItxnGroupBegin ( )
185+ return new ItxnParams < itxn . PaymentFields , itxn . PaymentInnerTxn > ( fields , TransactionType . Payment )
186+ }
187+ export function keyRegistration ( fields : itxn . KeyRegistrationFields ) : itxn . KeyRegistrationItxnParams {
188+ ensureItxnGroupBegin ( )
189+ return new ItxnParams < itxn . KeyRegistrationFields , itxn . KeyRegistrationInnerTxn > ( fields , TransactionType . KeyRegistration )
190+ }
191+ export function assetConfig ( fields : itxn . AssetConfigFields ) : itxn . AssetConfigItxnParams {
192+ ensureItxnGroupBegin ( )
193+ return new ItxnParams < itxn . AssetConfigFields , itxn . AssetConfigInnerTxn > ( fields , TransactionType . AssetConfig )
194+ }
195+ export function assetTransfer ( fields : itxn . AssetTransferFields ) : itxn . AssetTransferItxnParams {
196+ ensureItxnGroupBegin ( )
197+ return new ItxnParams < itxn . AssetTransferFields , itxn . AssetTransferInnerTxn > ( fields , TransactionType . AssetTransfer )
198+ }
199+ export function assetFreeze ( fields : itxn . AssetFreezeFields ) : itxn . AssetFreezeItxnParams {
200+ ensureItxnGroupBegin ( )
201+ return new ItxnParams < itxn . AssetFreezeFields , itxn . AssetFreezeInnerTxn > ( fields , TransactionType . AssetFreeze )
202+ }
203+ export function applicationCall ( fields : itxn . ApplicationCallFields ) : itxn . ApplicationCallItxnParams {
204+ ensureItxnGroupBegin ( )
205+ return new ItxnParams < itxn . ApplicationCallFields , itxn . ApplicationInnerTxn > ( fields , TransactionType . ApplicationCall )
206+ }
207+
208+ export class ItxnParams < TFields extends InnerTxnFields , TTransaction extends InnerTxn > {
209+ #fields: TFields & { type : TransactionType }
210+ constructor ( fields : TFields , type : TransactionType ) {
211+ this . #fields = { ...fields , type }
212+ }
213+ submit ( ) : TTransaction {
214+ return createInnerTxn < InnerTxnFields , TTransaction > ( this . #fields)
215+ }
216+
217+ set ( p : Partial < TFields > ) {
218+ Object . assign ( this . #fields, p )
219+ }
220+
221+ copy ( ) {
222+ return new ItxnParams < TFields , TTransaction > ( this . #fields, this . #fields. type )
223+ }
224+ }
225+
226+ const ensureItxnGroupBegin = ( ) => {
227+ if ( ! lazyContext . activeGroup . constructingItxnGroup . length ) {
228+ lazyContext . activeGroup . beginInnerTransactionGroup ( )
177229 }
178230}
0 commit comments