Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve some algosdk@3 behaviour differences #385

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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