2

How to obtain a stable encoding of emitted events from a smart contract?

I have a smart contract which emits events when these events are later fetched from the chain:

#[ink(event)]
pub struct MissionReady {
    #[ink(topic)]
    owner: AccountId,
    #[ink(topic)]
    operator: AccountId,
}
#[ink(event)]
/// Mission finished successfully
pub struct MissionAccomplished {}
#[ink(event)]
/// Mission is deployed on the target fleet as claimed by the network operator
pub struct MissionDeployed {}

they are scale encoded respectively as

[0, 178, 156, 14, 4, 92, 150, 156, 193, 7, 172, 3, 90, 148, 68, 118, 151, 188, 182, 196 ...]
[1]
[2]

which looks like they are stored in an enum internally.

This encoding changes if the order in the source code is changed.

The generated metadata in the .contract file can be read with a shell command like this:

   cat ./target/ink/$i/$i.contract  | jq '{"V3"}[]."spec"."events"'

But this will only detect changes, not prevent them, is there a way to fixate the encoding to keep the binary interface while recompiling the contract?

1
  • what do you mean "scale-encoded events"? What exactly do you scale-encode in the ABI? May 4 at 12:11

1 Answer 1

2

Currently there is no way to make the encoding independent of the ordering. As you say a internal enum is generated with the event structs as variants and they are just added in the order they appear.

However there is currently a WIP to update the event definition syntax, to make events independent of contract definitions. See https://github.com/paritytech/ink/pull/1243.

This will require events to be defined as enums, and they would maintain their encoding regardless of the order they are defined in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.