2022-02-01 14:49:36 +00:00
|
|
|
import std/macros
|
|
|
|
import pkg/contractabi
|
|
|
|
import ./basics
|
2022-02-02 15:56:37 +00:00
|
|
|
import ./provider
|
2022-02-01 14:49:36 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Event* = object of RootObj
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
|
|
|
|
|
|
|
template indexed* {.pragma.}
|
|
|
|
|
2022-02-02 16:18:09 +00:00
|
|
|
func decode*[E: Event](decoder: var AbiDecoder, _: type E): ?!E =
|
2022-02-01 14:49:36 +00:00
|
|
|
var event: E
|
2022-04-13 08:05:41 +00:00
|
|
|
decoder.startTuple()
|
2022-02-01 14:49:36 +00:00
|
|
|
for field in event.fields:
|
|
|
|
if not field.hasCustomPragma(indexed):
|
|
|
|
field = ?decoder.read(typeof(field))
|
2022-04-13 08:05:41 +00:00
|
|
|
decoder.finishTuple()
|
2022-02-01 14:49:36 +00:00
|
|
|
success event
|
|
|
|
|
2022-09-20 13:09:18 +00:00
|
|
|
func fitsInIndexedField(T: type): bool {.compileTime.} =
|
|
|
|
const supportedTypes = [
|
|
|
|
"uint8", "uint16", "uint32", "uint64", "uint256", "uint128",
|
|
|
|
"int8", "int16", "int32", "int64", "int256", "int128",
|
|
|
|
"bool", "address",
|
|
|
|
"bytes1", "bytes2", "bytes3", "bytes4",
|
|
|
|
"bytes5", "bytes6", "bytes7", "bytes8",
|
|
|
|
"bytes9", "bytes10", "bytes11", "bytes12",
|
|
|
|
"bytes13", "bytes14", "bytes15", "bytes16",
|
|
|
|
"bytes17", "bytes18", "bytes19", "bytes20",
|
|
|
|
"bytes21", "bytes22", "bytes23", "bytes24",
|
|
|
|
"bytes25", "bytes26", "bytes27", "bytes28",
|
|
|
|
"bytes29", "bytes30", "bytes31", "bytes32"
|
|
|
|
]
|
|
|
|
|
|
|
|
solidityType(T) in supportedTypes
|
2022-08-19 06:08:52 +00:00
|
|
|
|
2022-02-01 14:49:36 +00:00
|
|
|
func decode*[E: Event](_: type E, data: seq[byte], topics: seq[Topic]): ?!E =
|
2023-03-09 09:58:54 +00:00
|
|
|
var event = ?AbiDecoder.decode(data, E)
|
2022-02-01 14:49:36 +00:00
|
|
|
var i = 1
|
|
|
|
for field in event.fields:
|
|
|
|
if field.hasCustomPragma(indexed):
|
|
|
|
if i >= topics.len:
|
|
|
|
return failure "indexed event parameter not found"
|
2022-09-20 13:09:18 +00:00
|
|
|
when typeof(field).fitsInIndexedField:
|
2022-08-19 06:08:52 +00:00
|
|
|
field = ?AbiDecoder.decode(@(topics[i]), typeof(field))
|
2022-02-01 14:49:36 +00:00
|
|
|
inc i
|
|
|
|
success event
|