Use `solidityType()` to check indexed event parameter

This commit is contained in:
Mark Spanbroek 2022-09-20 15:09:18 +02:00 committed by Eric Mastro
parent fc3cc9c577
commit d001ee8e01
1 changed files with 17 additions and 27 deletions

View File

@ -1,25 +1,10 @@
import std/macros import std/macros
import std/typetraits
import pkg/contractabi import pkg/contractabi
import ./basics import ./basics
import ./provider import ./provider
type type
Event* = object of RootObj Event* = object of RootObj
ValueType = uint8 | uint16 | uint32 | uint64 | UInt256 | UInt128 |
int8 | int16 | int32 | int64 | Int256 | Int128 |
bool | Address
SmallByteArray = array[ 1, byte] | array[ 2, byte] | array[ 3, byte] |
array[ 4, byte] | array[ 5, byte] | array[ 6, byte] |
array[ 7, byte] | array[ 8, byte] | array[ 9, byte] |
array[10, byte] | array[11, byte] | array[12, byte] |
array[13, byte] | array[14, byte] | array[15, byte] |
array[16, byte] | array[17, byte] | array[18, byte] |
array[19, byte] | array[20, byte] | array[21, byte] |
array[22, byte] | array[23, byte] | array[24, byte] |
array[25, byte] | array[26, byte] | array[27, byte] |
array[28, byte] | array[29, byte] | array[30, byte] |
array[31, byte] | array[32, byte]
push: {.upraises: [].} push: {.upraises: [].}
@ -34,17 +19,22 @@ func decode*[E: Event](decoder: var AbiDecoder, _: type E): ?!E =
decoder.finishTuple() decoder.finishTuple()
success event success event
func isSupported(T: type): bool = func fitsInIndexedField(T: type): bool {.compileTime.} =
var supported = false const supportedTypes = [
# nim 1.2.x fails distinctBase checks on non-distinct types at compile time, "uint8", "uint16", "uint32", "uint64", "uint256", "uint128",
# so we must separate with `when` "int8", "int16", "int32", "int64", "int256", "int128",
when T is distinct: "bool", "address",
supported = T.distinctBase is ValueType or "bytes1", "bytes2", "bytes3", "bytes4",
T.distinctBase is SmallByteArray "bytes5", "bytes6", "bytes7", "bytes8",
else: "bytes9", "bytes10", "bytes11", "bytes12",
supported = T is ValueType or "bytes13", "bytes14", "bytes15", "bytes16",
T is SmallByteArray "bytes17", "bytes18", "bytes19", "bytes20",
return supported "bytes21", "bytes22", "bytes23", "bytes24",
"bytes25", "bytes26", "bytes27", "bytes28",
"bytes29", "bytes30", "bytes31", "bytes32"
]
solidityType(T) in supportedTypes
func decode*[E: Event](_: type E, data: seq[byte], topics: seq[Topic]): ?!E = func decode*[E: Event](_: type E, data: seq[byte], topics: seq[Topic]): ?!E =
var event = ?Abidecoder.decode(data, E) var event = ?Abidecoder.decode(data, E)
@ -53,7 +43,7 @@ func decode*[E: Event](_: type E, data: seq[byte], topics: seq[Topic]): ?!E =
if field.hasCustomPragma(indexed): if field.hasCustomPragma(indexed):
if i >= topics.len: if i >= topics.len:
return failure "indexed event parameter not found" return failure "indexed event parameter not found"
if typeof(field).isSupported: when typeof(field).fitsInIndexedField:
field = ?AbiDecoder.decode(@(topics[i]), typeof(field)) field = ?AbiDecoder.decode(@(topics[i]), typeof(field))
inc i inc i
success event success event