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: patching algosdkv3 compatibility issues #53

Merged
merged 2 commits into from
Jan 6, 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
64 changes: 8 additions & 56 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"package": "vsce package"
},
"dependencies": {
"algosdk": "^2.9.0",
"algosdk": "^3.0.0",
"avm-debug-adapter": "^0.3.0",
"lodash": "^4.17.21"
},
Expand Down
71 changes: 67 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,76 @@ import * as vscode from 'vscode'
import { MAX_FILES_TO_SHOW, NO_WORKSPACE_ERROR_MESSAGE } from './constants'
import { workspaceFileAccessor } from './fileAccessor'

// NOTE: Changes to 'address' or 'toUint' fields below must be propagated to other algokit repos using similar parsing logic
// Such as: https://github.com/algorandfoundation/algokit-subscriber-ts/pull/102#discussion_r1888287708
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
function parseAlgosdkV2SimulateResponse(obj: any): any {
if (obj === null || typeof obj !== 'object') return obj

const addressFields = new Set(['snd', 'close', 'aclose', 'rekey', 'rcv', 'arcv', 'fadd', 'asnd', 'm', 'r', 'f', 'c'])
const toUintFields = new Set(['gh', 'apaa'])

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const processValue = (key: string, value: any): any => {
if (typeof value === 'string') {
if (addressFields.has(key)) {
try {
return algosdk.encodeAddress(algosdk.base64ToBytes(value))
} catch {
return value
}
}
if (toUintFields.has(key)) {
return algosdk.base64ToBytes(value)
}
} else if (Array.isArray(value)) {
if (toUintFields.has(key)) {
return value.map((item) => (typeof item === 'string' ? algosdk.base64ToBytes(item) : item))
}
return value.map((item) => parseAlgosdkV2SimulateResponse(item))
} else if (typeof value === 'object' && value !== null) {
return parseAlgosdkV2SimulateResponse(value)
}
return value
}

return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, processValue(key, value)]))
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function objectToMapRecursive(obj: any): any {
if (obj === null || typeof obj !== 'object' || obj instanceof Uint8Array) {
return obj
}

if (Array.isArray(obj)) {
return obj.map(objectToMapRecursive)
}

return new Map(Object.entries(obj).map(([key, value]) => [key, objectToMapRecursive(value)]))
}

function tryParseAlgosdkV2SimulateResponse(rawSimulateTrace: object): algosdk.modelsv2.SimulateResponse {
const algosdkV2Response = parseAlgosdkV2SimulateResponse(rawSimulateTrace)

if (algosdkV2Response.version !== 2) {
throw new Error(`Unsupported simulate response version: ${algosdkV2Response.version}`)
}

return algosdk.modelsv2.SimulateResponse.fromEncodingData(objectToMapRecursive(algosdkV2Response))
}

export async function getSimulateTrace(filePath: string): Promise<algosdk.modelsv2.SimulateResponse | null> {
const traceFileContent = await readFileAsJson<Record<string, unknown>>(filePath)
if (!traceFileContent) {
vscode.window.showErrorMessage(`Could not open the simulate trace file at path "${filePath}".`)
return null
}
return algosdk.modelsv2.SimulateResponse.from_obj_for_encoding(traceFileContent)
try {
return algosdk.decodeJSON(JSON.stringify(traceFileContent), algosdk.modelsv2.SimulateResponse)
} catch (e) {
return tryParseAlgosdkV2SimulateResponse(traceFileContent)
}
}

export function getUniqueHashes(simulateTrace: algosdk.modelsv2.SimulateResponse): string[] {
Expand Down Expand Up @@ -52,12 +115,12 @@ export function getSourceMapQuickPickItems(
const { txn, lsig } = txnResult.txn

if (hash === bytesToBase64(approvalProgramHash) || hash === bytesToBase64(clearStateProgramHash)) {
const title = txn.apid
? `Select source maps for Application with ID: ${txn.apid}, hash: ${hash}`
const title = txn.applicationCall?.appIndex
? `Select source maps for Application with ID: ${txn.applicationCall.appIndex}, hash: ${hash}`
: `Select source map for Application with hash: ${hash}`
identifiers[hash] = { hash, title }
} else if (hash === bytesToBase64(logicSigHash)) {
let lsigBytes: Uint8Array | undefined = lsig?.l
let lsigBytes: Uint8Array | undefined = lsig?.logic
if (typeof lsigBytes === 'string') {
lsigBytes = new Uint8Array(Buffer.from(lsigBytes, 'base64'))
}
Expand Down
Loading