1- import { Account , Application , arc4 , Asset , bytes , gtxn , internal , uint64 } from '@algorandfoundation/algo-ts'
1+ import { Account , Application , arc4 , Asset , bytes , internal , TransactionType , uint64 } from '@algorandfoundation/algo-ts'
22import { lazyContext } from '../context-helpers/internal-context'
33import { asNumber , asUint64 , asUint64Cls } from '../util'
4+ import {
5+ ApplicationTransaction ,
6+ AssetConfigTransaction ,
7+ AssetFreezeTransaction ,
8+ AssetTransferTransaction ,
9+ KeyRegistrationTransaction ,
10+ PaymentTransaction ,
11+ Transaction ,
12+ } from './transactions'
413
5- const getTransaction = < T extends gtxn . Transaction > ( t : internal . primitives . StubUint64Compat ) : T => {
6- const transactions = lazyContext . activeGroup . transactions
7- const index = asNumber ( t )
8- if ( index >= transactions . length ) {
9- throw new internal . errors . InternalError ( 'invalid group index' )
14+ export const getApplicationTransaction = ( index ?: internal . primitives . StubUint64Compat ) : ApplicationTransaction => {
15+ return getTransactionImpl ( { type : TransactionType . ApplicationCall , index } ) as ApplicationTransaction
16+ }
17+ export const getAssetConfigTransaction = ( index ?: internal . primitives . StubUint64Compat ) : AssetConfigTransaction => {
18+ return getTransactionImpl ( { type : TransactionType . AssetConfig , index } ) as AssetConfigTransaction
19+ }
20+ export const getAssetTransferTransaction = ( index ?: internal . primitives . StubUint64Compat ) : AssetTransferTransaction => {
21+ return getTransactionImpl ( { type : TransactionType . AssetTransfer , index } ) as AssetTransferTransaction
22+ }
23+ export const getAssetFreezeTransaction = ( index ?: internal . primitives . StubUint64Compat ) : AssetFreezeTransaction => {
24+ return getTransactionImpl ( { type : TransactionType . AssetFreeze , index } ) as AssetFreezeTransaction
25+ }
26+ export const getKeyRegistrationTransaction = ( index ?: internal . primitives . StubUint64Compat ) : KeyRegistrationTransaction => {
27+ return getTransactionImpl ( { type : TransactionType . KeyRegistration , index } ) as KeyRegistrationTransaction
28+ }
29+ export const getPaymentTransaction = ( index ?: internal . primitives . StubUint64Compat ) : PaymentTransaction => {
30+ return getTransactionImpl ( { type : TransactionType . Payment , index } ) as PaymentTransaction
31+ }
32+ export const getTransaction = ( index ?: internal . primitives . StubUint64Compat ) : Transaction => {
33+ return getTransactionImpl ( { index } )
34+ }
35+ const getTransactionImpl = ( { type, index } : { type ?: TransactionType ; index ?: internal . primitives . StubUint64Compat } ) => {
36+ const i = index !== undefined ? asNumber ( index ) : undefined
37+ if ( i !== undefined && i >= lazyContext . activeGroup . transactions . length ) {
38+ throw new internal . errors . InternalError ( 'Invalid group index' )
39+ }
40+ const transaction = i !== undefined ? lazyContext . activeGroup . transactions [ i ] : lazyContext . activeGroup . activeTransaction
41+ if ( type === undefined ) {
42+ return transaction
43+ }
44+ if ( transaction . type !== type ) {
45+ throw new internal . errors . InternalError ( `Invalid transaction type: ${ transaction . type } ` )
46+ }
47+ switch ( type ) {
48+ case TransactionType . ApplicationCall :
49+ return transaction as ApplicationTransaction
50+ case TransactionType . Payment :
51+ return transaction as PaymentTransaction
52+ case TransactionType . AssetConfig :
53+ return transaction as AssetConfigTransaction
54+ case TransactionType . AssetTransfer :
55+ return transaction as AssetTransferTransaction
56+ case TransactionType . AssetFreeze :
57+ return transaction as AssetFreezeTransaction
58+ case TransactionType . KeyRegistration :
59+ return transaction as KeyRegistrationTransaction
60+ default :
61+ throw new internal . errors . InternalError ( `Invalid transaction type: ${ type } ` )
1062 }
11- return transactions [ index ] as T
1263}
64+
1365export const GTxn : internal . opTypes . GTxnType = {
1466 sender ( t : internal . primitives . StubUint64Compat ) : Account {
1567 return getTransaction ( t ) . sender
@@ -33,28 +85,28 @@ export const GTxn: internal.opTypes.GTxnType = {
3385 return getTransaction ( t ) . lease
3486 } ,
3587 receiver ( t : internal . primitives . StubUint64Compat ) : Account {
36- return getTransaction < gtxn . PaymentTxn > ( t ) . receiver
88+ return getPaymentTransaction ( t ) . receiver
3789 } ,
3890 amount ( t : uint64 ) : uint64 {
39- return getTransaction < gtxn . PaymentTxn > ( t ) . amount
91+ return getPaymentTransaction ( t ) . amount
4092 } ,
4193 closeRemainderTo ( t : internal . primitives . StubUint64Compat ) : Account {
42- return getTransaction < gtxn . PaymentTxn > ( t ) . closeRemainderTo
94+ return getPaymentTransaction ( t ) . closeRemainderTo
4395 } ,
4496 votePk ( t : internal . primitives . StubUint64Compat ) : bytes {
45- return getTransaction < gtxn . KeyRegistrationTxn > ( t ) . voteKey
97+ return getKeyRegistrationTransaction ( t ) . voteKey
4698 } ,
4799 selectionPk ( t : internal . primitives . StubUint64Compat ) : bytes {
48- return getTransaction < gtxn . KeyRegistrationTxn > ( t ) . selectionKey
100+ return getKeyRegistrationTransaction ( t ) . selectionKey
49101 } ,
50102 voteFirst ( t : internal . primitives . StubUint64Compat ) : uint64 {
51- return getTransaction < gtxn . KeyRegistrationTxn > ( t ) . voteFirst
103+ return getKeyRegistrationTransaction ( t ) . voteFirst
52104 } ,
53105 voteLast ( t : internal . primitives . StubUint64Compat ) : uint64 {
54- return getTransaction < gtxn . KeyRegistrationTxn > ( t ) . voteLast
106+ return getKeyRegistrationTransaction ( t ) . voteLast
55107 } ,
56108 voteKeyDilution ( t : internal . primitives . StubUint64Compat ) : uint64 {
57- return getTransaction < gtxn . KeyRegistrationTxn > ( t ) . voteKeyDilution
109+ return getKeyRegistrationTransaction ( t ) . voteKeyDilution
58110 } ,
59111 type ( t : internal . primitives . StubUint64Compat ) : bytes {
60112 return asUint64Cls ( getTransaction ( t ) . type ) . toBytes ( ) . asAlgoTs ( )
@@ -63,19 +115,19 @@ export const GTxn: internal.opTypes.GTxnType = {
63115 return asUint64 ( getTransaction ( t ) . type )
64116 } ,
65117 xferAsset ( t : internal . primitives . StubUint64Compat ) : Asset {
66- return getTransaction < gtxn . AssetTransferTxn > ( t ) . xferAsset
118+ return getAssetTransferTransaction ( t ) . xferAsset
67119 } ,
68120 assetAmount ( t : internal . primitives . StubUint64Compat ) : uint64 {
69- return getTransaction < gtxn . AssetTransferTxn > ( t ) . assetAmount
121+ return getAssetTransferTransaction ( t ) . assetAmount
70122 } ,
71123 assetSender ( t : internal . primitives . StubUint64Compat ) : Account {
72- return getTransaction < gtxn . AssetTransferTxn > ( t ) . assetSender
124+ return getAssetTransferTransaction ( t ) . assetSender
73125 } ,
74126 assetReceiver ( t : internal . primitives . StubUint64Compat ) : Account {
75- return getTransaction < gtxn . AssetTransferTxn > ( t ) . assetReceiver
127+ return getAssetTransferTransaction ( t ) . assetReceiver
76128 } ,
77129 assetCloseTo ( t : internal . primitives . StubUint64Compat ) : Account {
78- return getTransaction < gtxn . AssetTransferTxn > ( t ) . assetCloseTo
130+ return getAssetTransferTransaction ( t ) . assetCloseTo
79131 } ,
80132 groupIndex ( t : internal . primitives . StubUint64Compat ) : uint64 {
81133 return getTransaction ( t ) . groupIndex
@@ -84,136 +136,136 @@ export const GTxn: internal.opTypes.GTxnType = {
84136 return getTransaction ( t ) . txnId
85137 } ,
86138 applicationId ( t : internal . primitives . StubUint64Compat ) : Application {
87- return getTransaction < gtxn . ApplicationTxn > ( t ) . appId
139+ return getApplicationTransaction ( t ) . appId
88140 } ,
89141 onCompletion ( t : internal . primitives . StubUint64Compat ) : uint64 {
90- const onCompletionStr = getTransaction < gtxn . ApplicationTxn > ( t ) . onCompletion
142+ const onCompletionStr = getApplicationTransaction ( t ) . onCompletion
91143 return asUint64 ( arc4 . OnCompleteAction [ onCompletionStr ] )
92144 } ,
93145 applicationArgs ( a : internal . primitives . StubUint64Compat , b : internal . primitives . StubUint64Compat ) : bytes {
94- return getTransaction < gtxn . ApplicationTxn > ( a ) . appArgs ( asUint64 ( b ) )
146+ return getApplicationTransaction ( a ) . appArgs ( asUint64 ( b ) )
95147 } ,
96148 numAppArgs ( t : internal . primitives . StubUint64Compat ) : uint64 {
97- return getTransaction < gtxn . ApplicationTxn > ( t ) . numAppArgs
149+ return getApplicationTransaction ( t ) . numAppArgs
98150 } ,
99151 accounts ( a : internal . primitives . StubUint64Compat , b : internal . primitives . StubUint64Compat ) : Account {
100- return getTransaction < gtxn . ApplicationTxn > ( a ) . accounts ( asUint64 ( b ) )
152+ return getApplicationTransaction ( a ) . accounts ( asUint64 ( b ) )
101153 } ,
102154 numAccounts ( t : internal . primitives . StubUint64Compat ) : uint64 {
103- return getTransaction < gtxn . ApplicationTxn > ( t ) . numAccounts
155+ return getApplicationTransaction ( t ) . numAccounts
104156 } ,
105157 approvalProgram ( t : internal . primitives . StubUint64Compat ) : bytes {
106- return getTransaction < gtxn . ApplicationTxn > ( t ) . approvalProgram
158+ return getApplicationTransaction ( t ) . approvalProgram
107159 } ,
108160 clearStateProgram ( t : internal . primitives . StubUint64Compat ) : bytes {
109- return getTransaction < gtxn . ApplicationTxn > ( t ) . clearStateProgram
161+ return getApplicationTransaction ( t ) . clearStateProgram
110162 } ,
111163 rekeyTo ( t : internal . primitives . StubUint64Compat ) : Account {
112164 return getTransaction ( t ) . rekeyTo
113165 } ,
114166 configAsset ( t : internal . primitives . StubUint64Compat ) : Asset {
115- return getTransaction < gtxn . AssetConfigTxn > ( t ) . configAsset
167+ return getAssetConfigTransaction ( t ) . configAsset
116168 } ,
117169 configAssetTotal ( t : internal . primitives . StubUint64Compat ) : uint64 {
118- return getTransaction < gtxn . AssetConfigTxn > ( t ) . total
170+ return getAssetConfigTransaction ( t ) . total
119171 } ,
120172 configAssetDecimals ( t : internal . primitives . StubUint64Compat ) : uint64 {
121- return getTransaction < gtxn . AssetConfigTxn > ( t ) . decimals
173+ return getAssetConfigTransaction ( t ) . decimals
122174 } ,
123175 configAssetDefaultFrozen ( t : internal . primitives . StubUint64Compat ) : boolean {
124- return getTransaction < gtxn . AssetConfigTxn > ( t ) . defaultFrozen
176+ return getAssetConfigTransaction ( t ) . defaultFrozen
125177 } ,
126178 configAssetUnitName ( t : internal . primitives . StubUint64Compat ) : bytes {
127- return getTransaction < gtxn . AssetConfigTxn > ( t ) . unitName
179+ return getAssetConfigTransaction ( t ) . unitName
128180 } ,
129181 configAssetName ( t : internal . primitives . StubUint64Compat ) : bytes {
130- return getTransaction < gtxn . AssetConfigTxn > ( t ) . assetName
182+ return getAssetConfigTransaction ( t ) . assetName
131183 } ,
132184 configAssetUrl ( t : internal . primitives . StubUint64Compat ) : bytes {
133- return getTransaction < gtxn . AssetConfigTxn > ( t ) . url
185+ return getAssetConfigTransaction ( t ) . url
134186 } ,
135187 configAssetMetadataHash ( t : internal . primitives . StubUint64Compat ) : bytes {
136- return getTransaction < gtxn . AssetConfigTxn > ( t ) . metadataHash
188+ return getAssetConfigTransaction ( t ) . metadataHash
137189 } ,
138190 configAssetManager ( t : internal . primitives . StubUint64Compat ) : Account {
139- return getTransaction < gtxn . AssetConfigTxn > ( t ) . manager
191+ return getAssetConfigTransaction ( t ) . manager
140192 } ,
141193 configAssetReserve ( t : internal . primitives . StubUint64Compat ) : Account {
142- return getTransaction < gtxn . AssetConfigTxn > ( t ) . reserve
194+ return getAssetConfigTransaction ( t ) . reserve
143195 } ,
144196 configAssetFreeze ( t : internal . primitives . StubUint64Compat ) : Account {
145- return getTransaction < gtxn . AssetConfigTxn > ( t ) . freeze
197+ return getAssetConfigTransaction ( t ) . freeze
146198 } ,
147199 configAssetClawback ( t : internal . primitives . StubUint64Compat ) : Account {
148- return getTransaction < gtxn . AssetConfigTxn > ( t ) . clawback
200+ return getAssetConfigTransaction ( t ) . clawback
149201 } ,
150202 freezeAsset ( t : internal . primitives . StubUint64Compat ) : Asset {
151- return getTransaction < gtxn . AssetFreezeTxn > ( t ) . freezeAsset
203+ return getAssetFreezeTransaction ( t ) . freezeAsset
152204 } ,
153205 freezeAssetAccount ( t : internal . primitives . StubUint64Compat ) : Account {
154- return getTransaction < gtxn . AssetFreezeTxn > ( t ) . freezeAccount
206+ return getAssetFreezeTransaction ( t ) . freezeAccount
155207 } ,
156208 freezeAssetFrozen ( t : internal . primitives . StubUint64Compat ) : boolean {
157- return getTransaction < gtxn . AssetFreezeTxn > ( t ) . frozen
209+ return getAssetFreezeTransaction ( t ) . frozen
158210 } ,
159211 assets ( a : internal . primitives . StubUint64Compat , b : internal . primitives . StubUint64Compat ) : Asset {
160- return getTransaction < gtxn . ApplicationTxn > ( a ) . assets ( asUint64 ( b ) )
212+ return getApplicationTransaction ( a ) . assets ( asUint64 ( b ) )
161213 } ,
162214 numAssets ( t : internal . primitives . StubUint64Compat ) : uint64 {
163- return getTransaction < gtxn . ApplicationTxn > ( t ) . numAssets
215+ return getApplicationTransaction ( t ) . numAssets
164216 } ,
165217 applications ( a : internal . primitives . StubUint64Compat , b : internal . primitives . StubUint64Compat ) : Application {
166- return getTransaction < gtxn . ApplicationTxn > ( a ) . apps ( asUint64 ( b ) )
218+ return getApplicationTransaction ( a ) . apps ( asUint64 ( b ) )
167219 } ,
168220 numApplications ( t : internal . primitives . StubUint64Compat ) : uint64 {
169- return getTransaction < gtxn . ApplicationTxn > ( t ) . numApps
221+ return getApplicationTransaction ( t ) . numApps
170222 } ,
171223 globalNumUint ( t : internal . primitives . StubUint64Compat ) : uint64 {
172- return getTransaction < gtxn . ApplicationTxn > ( t ) . globalNumUint
224+ return getApplicationTransaction ( t ) . globalNumUint
173225 } ,
174226 globalNumByteSlice ( t : internal . primitives . StubUint64Compat ) : uint64 {
175- return getTransaction < gtxn . ApplicationTxn > ( t ) . globalNumBytes
227+ return getApplicationTransaction ( t ) . globalNumBytes
176228 } ,
177229 localNumUint ( t : internal . primitives . StubUint64Compat ) : uint64 {
178- return getTransaction < gtxn . ApplicationTxn > ( t ) . localNumUint
230+ return getApplicationTransaction ( t ) . localNumUint
179231 } ,
180232 localNumByteSlice ( t : internal . primitives . StubUint64Compat ) : uint64 {
181- return getTransaction < gtxn . ApplicationTxn > ( t ) . localNumBytes
233+ return getApplicationTransaction ( t ) . localNumBytes
182234 } ,
183235 extraProgramPages ( t : internal . primitives . StubUint64Compat ) : uint64 {
184- return getTransaction < gtxn . ApplicationTxn > ( t ) . extraProgramPages
236+ return getApplicationTransaction ( t ) . extraProgramPages
185237 } ,
186238 nonparticipation ( t : internal . primitives . StubUint64Compat ) : boolean {
187- return getTransaction < gtxn . KeyRegistrationTxn > ( t ) . nonparticipation
239+ return getKeyRegistrationTransaction ( t ) . nonparticipation
188240 } ,
189241 logs ( a : internal . primitives . StubUint64Compat , b : internal . primitives . StubUint64Compat ) : bytes {
190- return getTransaction < gtxn . ApplicationTxn > ( a ) . logs ( asUint64 ( b ) )
242+ return getApplicationTransaction ( a ) . logs ( asUint64 ( b ) )
191243 } ,
192244 numLogs ( t : internal . primitives . StubUint64Compat ) : uint64 {
193- return getTransaction < gtxn . ApplicationTxn > ( t ) . numLogs
245+ return getApplicationTransaction ( t ) . numLogs
194246 } ,
195247 createdAssetId ( t : internal . primitives . StubUint64Compat ) : Asset {
196- return getTransaction < gtxn . AssetConfigTxn > ( t ) . createdAsset
248+ return getAssetConfigTransaction ( t ) . createdAsset
197249 } ,
198250 createdApplicationId ( t : internal . primitives . StubUint64Compat ) : Application {
199- return getTransaction < gtxn . ApplicationTxn > ( t ) . createdApp
251+ return getApplicationTransaction ( t ) . createdApp
200252 } ,
201253 lastLog ( t : internal . primitives . StubUint64Compat ) : bytes {
202- return getTransaction < gtxn . ApplicationTxn > ( t ) . lastLog
254+ return getApplicationTransaction ( t ) . lastLog
203255 } ,
204256 stateProofPk ( t : internal . primitives . StubUint64Compat ) : bytes {
205- return getTransaction < gtxn . KeyRegistrationTxn > ( t ) . stateProofKey
257+ return getKeyRegistrationTransaction ( t ) . stateProofKey
206258 } ,
207259 approvalProgramPages ( a : internal . primitives . StubUint64Compat , b : internal . primitives . StubUint64Compat ) : bytes {
208- return getTransaction < gtxn . ApplicationTxn > ( a ) . approvalProgramPages ( asUint64 ( b ) )
260+ return getApplicationTransaction ( a ) . approvalProgramPages ( asUint64 ( b ) )
209261 } ,
210262 numApprovalProgramPages ( t : internal . primitives . StubUint64Compat ) : uint64 {
211- return getTransaction < gtxn . ApplicationTxn > ( t ) . numApprovalProgramPages
263+ return getApplicationTransaction ( t ) . numApprovalProgramPages
212264 } ,
213265 clearStateProgramPages ( a : internal . primitives . StubUint64Compat , b : internal . primitives . StubUint64Compat ) : bytes {
214- return getTransaction < gtxn . ApplicationTxn > ( a ) . clearStateProgramPages ( asUint64 ( b ) )
266+ return getApplicationTransaction ( a ) . clearStateProgramPages ( asUint64 ( b ) )
215267 } ,
216268 numClearStateProgramPages ( t : internal . primitives . StubUint64Compat ) : uint64 {
217- return getTransaction < gtxn . ApplicationTxn > ( t ) . numClearStateProgramPages
269+ return getApplicationTransaction ( t ) . numClearStateProgramPages
218270 } ,
219271}
0 commit comments