Skip to content

Commit 4930635

Browse files
authored
feat(ACI): expose events decoding through Contract ACI stamp (#971)
* feat(ACI)L: Fid order of events when decoding TODO: Check with core about order thingy from EVENTS docs * fix(Test): Fix contract test * fix(Test): Fix event parsing tests * chore(build): Update node version to 5.5.4
1 parent 1940a15 commit 4930635

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
NODE_TAG=v5.5.3
1+
NODE_TAG=v5.5.4
22
COMPILER_TAG=v4.2.0

es/contract/aci/transformation.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,31 @@ export function decodeEvents (events, options = { schema: [] }) {
3636
return events.map(l => {
3737
const [eName, ...eParams] = l.topics
3838
const hexHash = toBytes(eName, true).toString('hex')
39-
const { schema, isHasNonIndexed } = options.schema
39+
const { schema } = options.schema
4040
.reduce(
4141
(acc, el) => {
4242
if (hash(el.name).toString('hex') === hexHash) {
4343
l.name = el.name
4444
return {
45-
schema: el.types.filter(e => e !== SOPHIA_TYPES.string),
46-
isHasNonIndexed: el.types.includes(SOPHIA_TYPES.string),
45+
schema: el.types,
4746
name: el.name
4847
}
4948
}
5049
return acc
5150
},
52-
{ schema: [], isHasNonIndexed: true }
51+
{ schema: [] }
5352
)
53+
const { decoded } = schema.reduce((acc, el) => {
54+
if (el === SOPHIA_TYPES.string) {
55+
return { decoded: [...acc.decoded, transformEvent(l.data, el)], params: acc.params }
56+
}
57+
const [event, ...tail] = acc.params
58+
return { decoded: [...acc.decoded, transformEvent(event, el)], params: tail }
59+
}, { decoded: [], params: eParams })
60+
5461
return {
5562
...l,
56-
decoded: [
57-
...isHasNonIndexed ? [decode(l.data).toString('utf-8')] : [],
58-
...eParams.map((event, i) => transformEvent(event, schema[i]))
59-
]
63+
decoded
6064
}
6165
})
6266
}
@@ -77,6 +81,8 @@ export function transformEvent (event, type) {
7781
return toBytes(event, true).toString('hex')
7882
case SOPHIA_TYPES.address:
7983
return addressFromDecimal(event).split('_')[1]
84+
case SOPHIA_TYPES.string:
85+
return decode(event).toString('utf-8')
8086
default:
8187
return toBytes(event, true)
8288
}

test/integration/contract.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ contract StateContract =
6363
record state = { value: string, key: number, testOption: option(string) }
6464
record yesEr = { t: number}
6565
66-
datatype event = TheFirstEvent(int) | AnotherEvent(string, address) | AnotherEvent2(string, bool, int)
66+
datatype event = TheFirstEvent(int) | AnotherEvent(string, address) | AnotherEvent2(bool, string, int)
6767
datatype dateUnit = Year | Month | Day
6868
datatype one_or_both('a, 'b) = Left('a) | Right('b) | Both('a, 'b)
6969
@@ -111,7 +111,7 @@ contract StateContract =
111111
stateful entrypoint emitEvents() : unit =
112112
Chain.event(TheFirstEvent(42))
113113
Chain.event(AnotherEvent("This is not indexed", Contract.address))
114-
Chain.event(AnotherEvent2("This is not indexed", true, 1))
114+
Chain.event(AnotherEvent2(true, "This is not indexed", 1))
115115
`
116116
const aensDelegationContract = `
117117
contract DelegateTest =
@@ -547,7 +547,7 @@ describe('Contract', function () {
547547
decodedEventsWithoutACI = decodeEvents(log, { schema: events })
548548
})
549549
const events = [
550-
{ name: 'AnotherEvent2', types: [SOPHIA_TYPES.string, SOPHIA_TYPES.bool, SOPHIA_TYPES.int] },
550+
{ name: 'AnotherEvent2', types: [SOPHIA_TYPES.bool, SOPHIA_TYPES.string, SOPHIA_TYPES.int] },
551551
{ name: 'AnotherEvent', types: [SOPHIA_TYPES.string, SOPHIA_TYPES.address] },
552552
{ name: 'TheFirstEvent', types: [SOPHIA_TYPES.int] }
553553
]

0 commit comments

Comments
 (0)