nim-ethers/ethers/events.nim

47 lines
1.8 KiB
Nim
Raw Normal View History

2022-02-01 14:49:36 +00:00
import std/macros
import pkg/contractabi
import ./basics
import ./provider
2022-02-01 14:49:36 +00:00
type
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]
2022-02-01 14:49:36 +00:00
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
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))
decoder.finishTuple()
2022-02-01 14:49:36 +00:00
success event
func decode*[E: Event](_: type E, data: seq[byte], topics: seq[Topic]): ?!E =
var event = ?Abidecoder.decode(data, E)
var i = 1
for field in event.fields:
if field.hasCustomPragma(indexed):
if i >= topics.len:
return failure "indexed event parameter not found"
if typeof(field) is ValueType or typeof(field) is SmallByteArray:
2022-02-01 14:49:36 +00:00
field = ?AbiDecoder.decode(@(topics[i]), typeof(field))
inc i
success event