Skip to content

Commit 090bee8

Browse files
authored
Merge pull request #532 from algorandfoundation/fix/catch-app-state-decode-error
fix: add try catch to handle app state decode error
2 parents 9c8e45e + 6e74500 commit 090bee8

File tree

1 file changed

+56
-51
lines changed
  • src/features/applications/mappers

1 file changed

+56
-51
lines changed

src/features/applications/mappers/index.ts

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -128,63 +128,68 @@ const asApplicationState = (state: modelsv2.TealKeyValue, type: 'local' | 'globa
128128
return getRawApplicationState(state)
129129
}
130130

131-
// Check for local/global keys first
132-
for (const [keyName, storageKey] of Object.entries(appSpec.state.keys[type])) {
133-
if (storageKey.key === key) {
134-
return {
135-
key: {
136-
name: keyName,
137-
type: DecodedAbiStorageKeyType.Key,
138-
...asDecodedAbiStorageValue(appSpec, storageKey.keyType, base64ToBytes(key)),
139-
},
140-
value: tealValueToAbiStorageValue(appSpec, storageKey.valueType, value),
141-
} satisfies DecodedApplicationState
142-
}
143-
}
144-
145-
// Check for local/global maps with prefix
146-
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
147-
if (!storageMap.prefix) {
148-
continue
149-
}
150-
const keyBytes = base64ToBytes(key)
151-
152-
const prefixBytes = base64ToBytes(storageMap.prefix)
153-
if (uint8ArrayStartsWith(keyBytes, prefixBytes)) {
154-
const keyValueBytes = keyBytes.subarray(prefixBytes.length)
155-
156-
return {
157-
key: {
158-
name: keyName,
159-
type: DecodedAbiStorageKeyType.MapKey,
160-
prefix: base64ToUtf8(storageMap.prefix),
161-
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
162-
},
163-
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
164-
} satisfies DecodedApplicationState
131+
try {
132+
// Check for local/global keys first
133+
for (const [keyName, storageKey] of Object.entries(appSpec.state.keys[type])) {
134+
if (storageKey.key === key) {
135+
return {
136+
key: {
137+
name: keyName,
138+
type: DecodedAbiStorageKeyType.Key,
139+
...asDecodedAbiStorageValue(appSpec, storageKey.keyType, base64ToBytes(key)),
140+
},
141+
value: tealValueToAbiStorageValue(appSpec, storageKey.valueType, value),
142+
} satisfies DecodedApplicationState
143+
}
165144
}
166-
}
167145

168-
// Check for local/global maps without prefix
169-
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
170-
if (storageMap.prefix) {
171-
continue
146+
// Check for local/global maps with prefix
147+
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
148+
if (!storageMap.prefix) {
149+
continue
150+
}
151+
const keyBytes = base64ToBytes(key)
152+
153+
const prefixBytes = base64ToBytes(storageMap.prefix)
154+
if (uint8ArrayStartsWith(keyBytes, prefixBytes)) {
155+
const keyValueBytes = keyBytes.subarray(prefixBytes.length)
156+
157+
return {
158+
key: {
159+
name: keyName,
160+
type: DecodedAbiStorageKeyType.MapKey,
161+
prefix: base64ToUtf8(storageMap.prefix),
162+
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
163+
},
164+
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
165+
} satisfies DecodedApplicationState
166+
}
172167
}
173168

174-
try {
175-
const keyValueBytes = base64ToBytes(key)
169+
// Check for local/global maps without prefix
170+
for (const [keyName, storageMap] of Object.entries(appSpec.state.maps[type])) {
171+
if (storageMap.prefix) {
172+
continue
173+
}
176174

177-
return {
178-
key: {
179-
name: keyName,
180-
type: DecodedAbiStorageKeyType.MapKey,
181-
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
182-
},
183-
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
184-
} satisfies DecodedApplicationState
185-
} catch {
186-
// Do nothing
175+
try {
176+
const keyValueBytes = base64ToBytes(key)
177+
178+
return {
179+
key: {
180+
name: keyName,
181+
type: DecodedAbiStorageKeyType.MapKey,
182+
...asDecodedAbiStorageValue(appSpec, storageMap.keyType, keyValueBytes),
183+
},
184+
value: tealValueToAbiStorageValue(appSpec, storageMap.valueType, value),
185+
} satisfies DecodedApplicationState
186+
} catch {
187+
// Do nothing
188+
}
187189
}
190+
} catch (e: unknown) {
191+
// eslint-disable-next-line no-console
192+
console.warn('Failed to decode application state', e)
188193
}
189194

190195
// The default case

0 commit comments

Comments
 (0)