Decode indexed event parameters bytes1 up to bytes32
This commit is contained in:
parent
fc8af1117c
commit
c49a9cbae9
|
@ -8,6 +8,17 @@ type
|
||||||
ValueType = uint8 | uint16 | uint32 | uint64 | UInt256 | UInt128 |
|
ValueType = uint8 | uint16 | uint32 | uint64 | UInt256 | UInt128 |
|
||||||
int8 | int16 | int32 | int64 | Int256 | Int128 |
|
int8 | int16 | int32 | int64 | Int256 | Int128 |
|
||||||
bool | Address
|
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: [].}
|
||||||
|
|
||||||
|
@ -27,7 +38,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) is ValueType:
|
if typeof(field) is ValueType or typeof(field) is SmallByteArray:
|
||||||
field = ?AbiDecoder.decode(@(topics[i]), typeof(field))
|
field = ?AbiDecoder.decode(@(topics[i]), typeof(field))
|
||||||
inc i
|
inc i
|
||||||
success event
|
success event
|
||||||
|
|
|
@ -14,12 +14,14 @@ suite "Events":
|
||||||
b {.indexed.}: Address
|
b {.indexed.}: Address
|
||||||
c: Address
|
c: Address
|
||||||
d {.indexed.}: UInt256
|
d {.indexed.}: UInt256
|
||||||
|
e {.indexed.}: array[32, byte]
|
||||||
ComplexIndexedEvent = object of Event
|
ComplexIndexedEvent = object of Event
|
||||||
a {.indexed.}: array[42, UInt256]
|
a {.indexed.}: array[42, UInt256]
|
||||||
b {.indexed.}: seq[UInt256]
|
b {.indexed.}: seq[UInt256]
|
||||||
c {.indexed.}: string
|
c {.indexed.}: string
|
||||||
d {.indexed.}: seq[byte]
|
d {.indexed.}: seq[byte]
|
||||||
e {.indexed.}: (Address, UInt256)
|
e {.indexed.}: (Address, UInt256)
|
||||||
|
f {.indexed.}: array[33, byte]
|
||||||
|
|
||||||
proc example(_: type SimpleEvent): SimpleEvent =
|
proc example(_: type SimpleEvent): SimpleEvent =
|
||||||
SimpleEvent(
|
SimpleEvent(
|
||||||
|
@ -32,7 +34,8 @@ suite "Events":
|
||||||
a: UInt256.example,
|
a: UInt256.example,
|
||||||
b: Address.example,
|
b: Address.example,
|
||||||
c: Address.example,
|
c: Address.example,
|
||||||
d: UInt256.example
|
d: UInt256.example,
|
||||||
|
e: array[32, byte].example
|
||||||
)
|
)
|
||||||
|
|
||||||
func encode[T](_: type Topic, value: T): Topic =
|
func encode[T](_: type Topic, value: T): Topic =
|
||||||
|
@ -50,6 +53,7 @@ suite "Events":
|
||||||
topics.add Topic.default
|
topics.add Topic.default
|
||||||
topics.add Topic.encode(event.b)
|
topics.add Topic.encode(event.b)
|
||||||
topics.add Topic.encode(event.d)
|
topics.add Topic.encode(event.d)
|
||||||
|
topics.add Topic.encode(event.e)
|
||||||
let data = AbiEncoder.encode( (event.a, event.c) )
|
let data = AbiEncoder.encode( (event.a, event.c) )
|
||||||
check IndexedEvent.decode(data, topics) == success event
|
check IndexedEvent.decode(data, topics) == success event
|
||||||
|
|
||||||
|
@ -73,6 +77,7 @@ suite "Events":
|
||||||
Topic.example,
|
Topic.example,
|
||||||
Topic.example,
|
Topic.example,
|
||||||
Topic.example,
|
Topic.example,
|
||||||
|
Topic.example,
|
||||||
Topic.example
|
Topic.example
|
||||||
]
|
]
|
||||||
check ComplexIndexedEvent.decode(@[], topics) == success ComplexIndexedEvent()
|
check ComplexIndexedEvent.decode(@[], topics) == success ComplexIndexedEvent()
|
||||||
|
|
Loading…
Reference in New Issue