//! The TopicsBuilder implementation is copied from ink-env since it is no longer exported by ink-env-3.0.0. use crate::PinkEnvironment; use ink::env::{ hash::{Blake2x256, CryptoHash, HashOutput}, topics::TopicsBuilderBackend, Environment, Topics, }; use ink::primitives::Clear; pub fn topics_for(event: impl Topics + scale::Encode) -> Vec { event.topics::(TopicsBuilder::::new().into()) } struct TopicsBuilder { topics: Vec<::Hash>, } impl TopicsBuilder where E: Environment, { fn new() -> Self { Self { topics: Vec::new() } } } impl TopicsBuilderBackend for TopicsBuilder where E: Environment, { type Output = Vec<::Hash>; fn expect(&mut self, _expected_topics: usize) {} fn push_topic(&mut self, topic_value: &T) where T: scale::Encode, { let encoded = topic_value.encode(); let len_encoded = encoded.len(); let mut result = ::Hash::CLEAR_HASH; let len_result = result.as_ref().len(); if len_encoded <= len_result { result.as_mut()[..len_encoded].copy_from_slice(&encoded[..]); } else { let mut hash_output = ::Type::default(); ::hash(&encoded[..], &mut hash_output); let copy_len = core::cmp::min(hash_output.len(), len_result); result.as_mut()[0..copy_len].copy_from_slice(&hash_output[0..copy_len]); } self.topics.push(result); } fn output(self) -> Self::Output { self.topics } }