From e3d5ce512235e778de0bd285ebccdec926b27102 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 13 Apr 2022 10:05:41 +0200 Subject: [PATCH] Fix decoding of events with dynamic arguments Such as byte sequences or strings. --- ethers.nimble | 2 +- ethers/events.nim | 2 ++ testmodule/testEvents.nim | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ethers.nimble b/ethers.nimble index f50f88c..003ec0a 100644 --- a/ethers.nimble +++ b/ethers.nimble @@ -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" diff --git a/ethers/events.nim b/ethers/events.nim index 7159cb0..23f9058 100644 --- a/ethers/events.nim +++ b/ethers/events.nim @@ -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 = diff --git a/testmodule/testEvents.nim b/testmodule/testEvents.nim index 3d07e3c..3636cdf 100644 --- a/testmodule/testEvents.nim +++ b/testmodule/testEvents.nim @@ -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]