Fix decoding of events with dynamic arguments

Such as byte sequences or strings.
This commit is contained in:
Mark Spanbroek 2022-04-13 10:05:41 +02:00 committed by markspanbroek
parent 073924d3f5
commit e3d5ce5122
3 changed files with 17 additions and 1 deletions

View File

@ -4,7 +4,7 @@ description = "library for interacting with Ethereum"
license = "MIT"
requires "chronos >= 3.0.0 & < 4.0.0"
requires "contractabi >= 0.4.2 & < 0.5.0"
requires "contractabi >= 0.4.4 & < 0.5.0"
requires "questionable >= 0.10.2 & < 0.11.0"
requires "upraises >= 0.1.0 & < 0.2.0"
requires "json_rpc"

View File

@ -26,9 +26,11 @@ template indexed* {.pragma.}
func decode*[E: Event](decoder: var AbiDecoder, _: type E): ?!E =
var event: E
decoder.startTuple()
for field in event.fields:
if not field.hasCustomPragma(indexed):
field = ?decoder.read(typeof(field))
decoder.finishTuple()
success event
func decode*[E: Event](_: type E, data: seq[byte], topics: seq[Topic]): ?!E =

View File

@ -9,6 +9,9 @@ suite "Events":
SimpleEvent = object of Event
a: UInt256
b: Address
DynamicSizeEvent = object of Event
a: array[32, byte]
b: seq[byte]
IndexedEvent = object of Event
a: UInt256
b {.indexed.}: Address
@ -29,6 +32,12 @@ suite "Events":
b: Address.example
)
proc example(_: type DynamicSizeEvent): DynamicSizeEvent =
DynamicSizeEvent(
a: array[32, byte].example,
b: seq[byte].example
)
proc example(_: type IndexedEvent): IndexedEvent =
IndexedEvent(
a: UInt256.example,
@ -47,6 +56,11 @@ suite "Events":
let data = AbiEncoder.encode( (event.a, event.b) )
check SimpleEvent.decode(data, @[]) == success event
test "decodes dynamically sized fields":
let event = DynamicSizeEvent.example
let data = AbiEncoder.encode( (event.a, event.b) )
check DynamicSizeEvent.decode(data, @[]) == success event
test "decodes indexed fields":
let event = IndexedEvent.example
var topics: seq[Topic]