2024-03-19 08:39:32 +00:00
|
|
|
import std/unittest
|
|
|
|
import pkg/questionable/results
|
2024-03-20 10:24:53 +00:00
|
|
|
import pkg/contractabi
|
2024-03-19 08:39:32 +00:00
|
|
|
import pkg/ethers/errors
|
|
|
|
|
|
|
|
suite "Decoding of custom errors":
|
|
|
|
|
2024-03-20 10:24:53 +00:00
|
|
|
type
|
|
|
|
SimpleError = object of SolidityError
|
|
|
|
ErrorWithArguments = object of SolidityError
|
|
|
|
arguments: tuple[one: UInt256, two: bool]
|
2024-03-19 08:39:32 +00:00
|
|
|
|
|
|
|
test "decodes a simple error":
|
|
|
|
let decoded = SimpleError.decode(@[0xc2'u8, 0xbb, 0x94, 0x7c])
|
|
|
|
check decoded is ?!(ref SimpleError)
|
|
|
|
check decoded.isSuccess
|
|
|
|
check (!decoded) != nil
|
|
|
|
|
2024-03-20 10:24:53 +00:00
|
|
|
test "decodes error with arguments":
|
|
|
|
let expected = (ref ErrorWithArguments)(arguments: (1.u256, true))
|
|
|
|
let encoded = AbiEncoder.encode(expected)
|
|
|
|
let decoded = ErrorWithArguments.decode(encoded)
|
|
|
|
check decoded.isSuccess
|
|
|
|
check (!decoded).arguments.one == 1.u256
|
|
|
|
check (!decoded).arguments.two == true
|
|
|
|
|
2024-03-19 08:39:32 +00:00
|
|
|
test "returns failure when decoding fails":
|
|
|
|
let invalid = @[0xc2'u8, 0xbb, 0x94, 0x0] # last byte is wrong
|
|
|
|
let decoded = SimpleError.decode(invalid)
|
|
|
|
check decoded.isFailure
|
|
|
|
|
|
|
|
test "returns failure when data is less than 4 bytes":
|
|
|
|
let invalid = @[0xc2'u8, 0xbb, 0x94]
|
|
|
|
let decoded = SimpleError.decode(invalid)
|
|
|
|
check decoded.isFailure
|
|
|
|
|
2024-03-20 10:51:03 +00:00
|
|
|
test "returns failure when there are trailing bytes":
|
|
|
|
let invalid = @[0xc2'u8, 0xbb, 0x94, 0x7c, 0x0] # one byte too many
|
|
|
|
let decoded = SimpleError.decode(invalid)
|
|
|
|
check decoded.isFailure
|
|
|
|
|
|
|
|
test "returns failure when there are trailing bytes after arguments":
|
|
|
|
let error = (ref ErrorWithArguments)(arguments: (1.u256, true))
|
|
|
|
let encoded = AbiEncoder.encode(error)
|
|
|
|
let invalid = encoded & @[0x0'u8] # one byte too many
|
|
|
|
let decoded = ErrorWithArguments.decode(invalid)
|
|
|
|
check decoded.isFailure
|
|
|
|
|
2024-03-19 08:39:32 +00:00
|
|
|
test "decoding only works for SolidityErrors":
|
|
|
|
type InvalidError = ref object of CatchableError
|
|
|
|
const works = compiles:
|
|
|
|
InvalidError.decode(@[0x1'u8, 0x2, 0x3, 0x4])
|
|
|
|
check not works
|
|
|
|
|