11import { getApplicationResultAtom } from '@/features/applications/data'
22import { dataStore } from '@/features/common/data/data-store'
3+ import { asAlgosdkTransactions } from '@/features/transaction-wizard/mappers'
34import { BuildableTransactionType , BuildMethodCallTransactionResult , BuildTransactionResult } from '@/features/transaction-wizard/models'
45import { asError } from '@/utils/error'
56import { AppClient } from '@algorandfoundation/algokit-utils/types/app-client'
@@ -8,12 +9,7 @@ import algosdk from 'algosdk'
89type URLTokenBaseHTTPError = {
910 name : 'URLTokenBaseHTTPError'
1011 response : {
11- body : {
12- data : {
13- [ 'app-index' ] : number
14- [ 'group-index' ] : number
15- }
16- }
12+ text : string
1713 }
1814}
1915
@@ -23,26 +19,49 @@ type SimulateError = Error & {
2319
2420// eslint-disable-next-line @typescript-eslint/no-explicit-any
2521const isURLTokenBaseHTTPError = ( e : any ) : e is URLTokenBaseHTTPError => {
26- return (
27- e . name === 'URLTokenBaseHTTPError' &&
28- e . response ?. body ?. data &&
29- e . response . body . data [ 'app-index' ] !== undefined &&
30- e . response . body . data [ 'group-index' ] !== undefined
31- )
22+ return e . name === 'URLTokenBaseHTTPError' && e . response ?. text
3223}
3324
3425// eslint-disable-next-line @typescript-eslint/no-explicit-any
3526const isSimulateError = ( e : any ) : e is SimulateError => {
3627 return e . simulateResponse !== undefined
3728}
3829
30+ /**
31+ * The supplied transactions array may contain transactions passed as ABI method call args, which become group transactions.
32+ * This function flattens any such args into the transaction group and finds the transaction by group index.
33+ */
34+ const findTransactionInGroup = async ( transactions : BuildTransactionResult [ ] , groupIndex : number ) => {
35+ const indexMap : number [ ] = [ ]
36+
37+ for ( const [ i , transaction ] of transactions . entries ( ) ) {
38+ // Flattens out transactions passed as ABI method call args
39+ const txns = await asAlgosdkTransactions ( transaction )
40+ txns . forEach ( ( _ ) => {
41+ indexMap . push ( i )
42+ } )
43+ }
44+
45+ const index = indexMap [ groupIndex ]
46+ if ( index === undefined ) {
47+ return undefined
48+ }
49+ return transactions [ index ]
50+ }
51+
3952export const parseCallAbiMethodError = async ( e : unknown , transactions : BuildTransactionResult [ ] ) : Promise < Error > => {
4053 if ( ! isURLTokenBaseHTTPError ( e ) ) {
4154 return asError ( e )
4255 }
4356
44- const groupIndex = e . response . body . data [ 'group-index' ]
45- const transaction = transactions [ groupIndex ]
57+ const errorBody = JSON . parse ( e . response . text )
58+ const groupIndex =
59+ errorBody && errorBody . data && errorBody . data [ 'group-index' ] !== undefined ? ( errorBody . data [ 'group-index' ] as number ) : undefined
60+ if ( groupIndex === undefined ) {
61+ return asError ( e )
62+ }
63+
64+ const transaction = await findTransactionInGroup ( transactions , groupIndex )
4665 if ( ! transaction || transaction . type !== BuildableTransactionType . MethodCall ) {
4766 return asError ( e )
4867 }
@@ -62,7 +81,7 @@ export const parseSimulateAbiMethodError = async (e: unknown, transactions: Buil
6281
6382 // When there are multiple errors, the failedAt array will only have one element
6483 const groupIndex = Number ( e . simulateResponse . txnGroups [ 0 ] . failedAt [ 0 ] )
65- const transaction = transactions [ groupIndex ]
84+ const transaction = await findTransactionInGroup ( transactions , groupIndex )
6685 if ( ! transaction || transaction . type !== BuildableTransactionType . MethodCall ) {
6786 return asError ( e )
6887 }
@@ -91,7 +110,7 @@ const parseErrorForTransaction = async (e: unknown, groupIndex: number, transact
91110 return asError ( e )
92111 }
93112
94- return new Error ( `Error in transaction ${ groupIndex + 1 } : ${ tealErrorMessage } ` )
113+ return new Error ( `Error in transaction at index ${ groupIndex } : ${ tealErrorMessage } ` )
95114}
96115
97116const extractErrorMessage = ( errorString : string ) : string | undefined => {
0 commit comments