nim-ethers/ethers/errors.nim
Mark Spanbroek 6b57e56a39 abi decoding of simple custom errors
Only supports errors without arguments for now
2024-05-21 13:19:24 +02:00

18 lines
518 B
Nim

import pkg/contractabi/selector
import ./basics
type SolidityError* = object of EthersError
{.push raises:[].}
template errors*(types) {.pragma.}
func decode*[E: SolidityError](_: type E, data: seq[byte]): ?!(ref E) =
const name = $E
const selector = selector(name, typeof(()))
if data.len < 4:
return failure "unable to decode " & name & ": signature too short"
if selector.toArray[0..<4] != data[0..<4]:
return failure "unable to decode " & name & ": signature doesn't match"
success (ref E)()