mirror of
https://github.com/status-im/nim-ethers.git
synced 2025-01-12 16:44:23 +00:00
74f15fca9c
Currently only errors without arguments
29 lines
815 B
Nim
29 lines
815 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)()
|
|
|
|
template convertCustomErrors*[ErrorTypes: tuple](body: untyped): untyped =
|
|
try:
|
|
body
|
|
except ProviderError as error:
|
|
block:
|
|
if data =? error.data:
|
|
for e in ErrorTypes.default.fields:
|
|
if error =? typeof(e).decode(data):
|
|
raise error
|
|
raise error
|