Skip to content

Commit

Permalink
Trying to adapt to new transcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Dec 7, 2023
1 parent 8699a75 commit fce046b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
38 changes: 33 additions & 5 deletions drink/src/session/record.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::rc::Rc;

use contract_transcode::{ContractMessageTranscoder, Value};
use parity_scale_codec::{Decode, Encode};
use parity_scale_codec::Decode;

use crate::{
errors::MessageResult,
Expand Down Expand Up @@ -185,6 +185,9 @@ impl<R: frame_system::Config> EventBatch<R> {
impl EventBatch<MinimalRuntime> {
/// Returns all the contract events that were emitted during the contract interaction.
///
/// **WARNING**: This method will return all the events that were emitted by ANY contract. If your
/// call triggered multiple contracts, you will have to filter the events yourself.
///
/// We have to match against static enum variant, and thus (at least for now) we support only
/// `MinimalRuntime`.
pub fn contract_events(&self) -> Vec<&[u8]> {
Expand All @@ -200,16 +203,41 @@ impl EventBatch<MinimalRuntime> {
}

/// The same as `contract_events`, but decodes the events using the given transcoder.
///
/// **WARNING**: This method will try to decode all the events that were emitted by ANY
/// contract. This means that some contract events might either fail to decode or be decoded
/// incorrectly (to some rubbish). In the former case, they will be skipped, but with the latter
/// case, you will have to filter the events yourself.
///
/// **WARNING 2**: This method will ignore anonymous events.
pub fn contract_events_decoded(
&self,
transcoder: &Rc<ContractMessageTranscoder>,
) -> Vec<Value> {
let signature_topics = transcoder
.metadata()
.spec()
.events()
.iter()
.filter_map(|event| event.signature_topic())
.map(|sig| sig.as_bytes().try_into().unwrap())
.collect::<Vec<[u8; 32]>>();

println!("signature_topics: {:?}", signature_topics);

self.contract_events()
.into_iter()
.map(|data| {
transcoder
.decode_contract_event(&mut data.encode().as_slice())
.expect("Failed to decode contract event")
.filter_map(|mut data| {
println!("data: {:?}", data);
for signature_topic in &signature_topics {
match transcoder.decode_contract_event(&signature_topic.into(), &mut data) {
Ok(decoded) => return Some(decoded),
Err(err) => {
println!("err: {:?}", err);
}
}
}
None
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/contract-events/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version = "0.1.0"
path = "lib.rs"

[dependencies]
ink = { version = "=4.2.1", default-features = false, features = ["ink-debug"] }
ink = { version = "=5.0.0-rc", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
Expand Down

0 comments on commit fce046b

Please sign in to comment.