Skip to content
Open
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
107 changes: 56 additions & 51 deletions src/features/applications/mappers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,63 +128,68 @@ const asApplicationState = (state: modelsv2.TealKeyValue, type: 'local' | 'globa
return getRawApplicationState(state)
}

// Check for local/global keys first
for (const [keyName, storageKey] of Object.entries(appSpec.state.keys[type])) {
if (storageKey.key === key) {
return {
key: {
name: keyName,
type: DecodedAbiStorageKeyType.Key,
...asDecodedAbiStorageValue(appSpec, storageKey.keyType, base64ToBytes(key)),
},
value: tealValueToAbiStorageValue(appSpec, storageKey.valueType, value),
} satisfies DecodedApplicationState
}
}

// Check for local/global maps with prefix
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
if (!storageMap.prefix) {
continue
}
const keyBytes = base64ToBytes(key)

const prefixBytes = base64ToBytes(storageMap.prefix)
if (uint8ArrayStartsWith(keyBytes, prefixBytes)) {
const keyValueBytes = keyBytes.subarray(prefixBytes.length)

return {
key: {
name: keyName,
type: DecodedAbiStorageKeyType.MapKey,
prefix: base64ToUtf8(storageMap.prefix),
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
},
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
} satisfies DecodedApplicationState
try {
// Check for local/global keys first
for (const [keyName, storageKey] of Object.entries(appSpec.state.keys[type])) {
if (storageKey.key === key) {
return {
key: {
name: keyName,
type: DecodedAbiStorageKeyType.Key,
...asDecodedAbiStorageValue(appSpec, storageKey.keyType, base64ToBytes(key)),
},
value: tealValueToAbiStorageValue(appSpec, storageKey.valueType, value),
} satisfies DecodedApplicationState
}
}
}

// Check for local/global maps without prefix
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
if (storageMap.prefix) {
continue
// Check for local/global maps with prefix
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
if (!storageMap.prefix) {
continue
}
const keyBytes = base64ToBytes(key)

const prefixBytes = base64ToBytes(storageMap.prefix)
if (uint8ArrayStartsWith(keyBytes, prefixBytes)) {
const keyValueBytes = keyBytes.subarray(prefixBytes.length)

return {
key: {
name: keyName,
type: DecodedAbiStorageKeyType.MapKey,
prefix: base64ToUtf8(storageMap.prefix),
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
},
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
} satisfies DecodedApplicationState
}
}

try {
const keyValueBytes = base64ToBytes(key)
// Check for local/global maps without prefix
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
if (storageMap.prefix) {
continue
}

return {
key: {
name: keyName,
type: DecodedAbiStorageKeyType.MapKey,
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
},
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
} satisfies DecodedApplicationState
} catch {
// Do nothing
try {
const keyValueBytes = base64ToBytes(key)

return {
key: {
name: keyName,
type: DecodedAbiStorageKeyType.MapKey,
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
},
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
} satisfies DecodedApplicationState
} catch {
// Do nothing
}
}
} catch (e: unknown) {
// eslint-disable-next-line no-console
console.warn('Failed to decode application state', e)
}

// The default case
Expand Down