Skip to content

Commit

Permalink
Merge pull request #385 from algorandfoundation/hotfix/fix-algosdk3-b…
Browse files Browse the repository at this point in the history
…ehaviour-differences

fix: resolve some algosdk@3 behaviour differences
  • Loading branch information
neilcampbell authored Feb 3, 2025
2 parents 59052b7 + 80b9a99 commit 68f0281
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"postinstall": "patch-package"
},
"dependencies": {
"@algorandfoundation/algokit-subscriber": "3.0.1",
"@algorandfoundation/algokit-subscriber": "^3.0.2",
"@algorandfoundation/algokit-utils": "^8.1.0",
"@auth0/auth0-react": "^2.2.4",
"@blockshake/defly-connect": "^1.2.1",
Expand Down Expand Up @@ -197,4 +197,4 @@
"overrides": {
"ws@>7.0.0 <7.5.9": "7.5.10"
}
}
}
51 changes: 35 additions & 16 deletions src/features/abi-methods/utils/parse-errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getApplicationResultAtom } from '@/features/applications/data'
import { dataStore } from '@/features/common/data/data-store'
import { asAlgosdkTransactions } from '@/features/transaction-wizard/mappers'
import { BuildableTransactionType, BuildMethodCallTransactionResult, BuildTransactionResult } from '@/features/transaction-wizard/models'
import { asError } from '@/utils/error'
import { AppClient } from '@algorandfoundation/algokit-utils/types/app-client'
Expand All @@ -8,12 +9,7 @@ import algosdk from 'algosdk'
type URLTokenBaseHTTPError = {
name: 'URLTokenBaseHTTPError'
response: {
body: {
data: {
['app-index']: number
['group-index']: number
}
}
text: string
}
}

Expand All @@ -23,26 +19,49 @@ type SimulateError = Error & {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isURLTokenBaseHTTPError = (e: any): e is URLTokenBaseHTTPError => {
return (
e.name === 'URLTokenBaseHTTPError' &&
e.response?.body?.data &&
e.response.body.data['app-index'] !== undefined &&
e.response.body.data['group-index'] !== undefined
)
return e.name === 'URLTokenBaseHTTPError' && e.response?.text
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isSimulateError = (e: any): e is SimulateError => {
return e.simulateResponse !== undefined
}

/**
* The supplied transactions array may contain transactions passed as ABI method call args, which become group transactions.
* This function flattens any such args into the transaction group and finds the transaction by group index.
*/
const findTransactionInGroup = async (transactions: BuildTransactionResult[], groupIndex: number) => {
const indexMap: number[] = []

for (const [i, transaction] of transactions.entries()) {
// Flattens out transactions passed as ABI method call args
const txns = await asAlgosdkTransactions(transaction)
txns.forEach((_) => {
indexMap.push(i)
})
}

const index = indexMap[groupIndex]
if (index === undefined) {
return undefined
}
return transactions[index]
}

export const parseCallAbiMethodError = async (e: unknown, transactions: BuildTransactionResult[]): Promise<Error> => {
if (!isURLTokenBaseHTTPError(e)) {
return asError(e)
}

const groupIndex = e.response.body.data['group-index']
const transaction = transactions[groupIndex]
const errorBody = JSON.parse(e.response.text)
const groupIndex =
errorBody && errorBody.data && errorBody.data['group-index'] !== undefined ? (errorBody.data['group-index'] as number) : undefined
if (groupIndex === undefined) {
return asError(e)
}

const transaction = await findTransactionInGroup(transactions, groupIndex)
if (!transaction || transaction.type !== BuildableTransactionType.MethodCall) {
return asError(e)
}
Expand All @@ -62,7 +81,7 @@ export const parseSimulateAbiMethodError = async (e: unknown, transactions: Buil

// When there are multiple errors, the failedAt array will only have one element
const groupIndex = Number(e.simulateResponse.txnGroups[0].failedAt[0])
const transaction = transactions[groupIndex]
const transaction = await findTransactionInGroup(transactions, groupIndex)
if (!transaction || transaction.type !== BuildableTransactionType.MethodCall) {
return asError(e)
}
Expand Down Expand Up @@ -91,7 +110,7 @@ const parseErrorForTransaction = async (e: unknown, groupIndex: number, transact
return asError(e)
}

return new Error(`Error in transaction ${groupIndex + 1}: ${tealErrorMessage}`)
return new Error(`Error in transaction at index ${groupIndex}: ${tealErrorMessage}`)
}

const extractErrorMessage = (errorString: string): string | undefined => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export function transformSearchParamsTransactions(searchParamTransactions: BaseS
} catch (error) {
if (error instanceof z.ZodError) {
const badPaths = error.errors.map((e) => e.path.join('-'))
errors.push(`Error in transaction ${index} in the following fields: ${badPaths.join(', ')}`)
errors.push(`Error in transaction at index ${index} in the following fields: ${badPaths.join(', ')}`)
continue
}
if (error instanceof Error) {
errors.push(`Error in transaction ${index}: ${error.message}`)
errors.push(`Error in transaction at index ${index}: ${error.message}`)
}
}
}
Expand Down

0 comments on commit 68f0281

Please sign in to comment.