From ed1b91e4ec0bb65ae5ac8d43b47813028891213e Mon Sep 17 00:00:00 2001 From: Hoang Dinh Date: Thu, 27 Nov 2025 09:22:27 +1000 Subject: [PATCH] fix: add try catch to handle app state decode error --- src/features/applications/mappers/index.ts | 107 +++++++++++---------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/src/features/applications/mappers/index.ts b/src/features/applications/mappers/index.ts index 3ea86dd3e..04b14429d 100644 --- a/src/features/applications/mappers/index.ts +++ b/src/features/applications/mappers/index.ts @@ -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