Skip to content

Commit

Permalink
handle decode error (#570)
Browse files Browse the repository at this point in the history
* handle decode error

* fmt
  • Loading branch information
xlc committed Nov 29, 2023
1 parent a755f09 commit d2015a2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/core/src/utils/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { hexToU8a, u8aToHex } from '@polkadot/util'
import _ from 'lodash'

import { decodeWellKnownKey } from './well-known-keys.js'
import { defaultLogger } from '../logger.js'

const logger = defaultLogger.child({ module: 'decoder' })

const _CACHE: Record<string, LRUCache<HexString, StorageEntry>> = {}

Expand Down Expand Up @@ -78,12 +81,22 @@ export const decodeKeyValue = (
const { storage, decodedKey } = decodeKey(meta, block, key)

if (!storage || !decodedKey) {
logger.warn({ key, value }, 'Failed to decode storage key')
return undefined
}

const decodeValue = () => {
if (!value) return null
return meta.registry.createType(decodedKey.outputType, hexToU8a(value))[toHuman ? 'toHuman' : 'toJSON']()
try {
return meta.registry.createType(decodedKey.outputType, hexToU8a(value))[toHuman ? 'toHuman' : 'toJSON']()
} catch (error) {
logger.warn(error, 'Failed to decode storage value')
logger.warn(
{ key, value, section: storage.section, method: storage.method, args: decodedKey.args },
'Failed to decode storage value',
)
return undefined
}
}

return {
Expand Down

0 comments on commit d2015a2

Please sign in to comment.