2024-03-19 08:39:32 +00:00
|
|
|
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)()
|
2024-03-19 14:27:51 +00:00
|
|
|
|
|
|
|
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
|