Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions core/src/engine/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::Deadline;
use crate::events::DefuseEvent;
use crate::{Deadline, events::Event};
use impl_tools::autoimpl;
use near_sdk::{AccountIdRef, CryptoHash};

#[autoimpl(for <T: trait + ?Sized> &mut T, Box<T>)]
pub trait Inspector {
fn emit_event(&mut self, event: DefuseEvent<'_>);
fn on_event(&mut self, event: Event<'_>);

fn on_deadline(&mut self, deadline: Deadline);

Expand Down
19 changes: 19 additions & 0 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@ use crate::{
tokens::{FtWithdraw, MtWithdraw, NativeWithdraw, NftWithdraw, StorageDeposit, Transfer},
},
};
use defuse_nep245::MtEvent;
use derive_more::derive::From;
use near_sdk::{near, serde::Deserialize};
use std::borrow::Cow;

#[must_use = "make sure to `.emit()` this event"]
#[near(serializers = [json])]
#[derive(Debug, Clone, From)]
#[serde(untagged)]
pub enum Event<'a> {
Dip4(DefuseEvent<'a>),
Nep245(MtEvent<'a>),
}

impl Event<'_> {
pub fn emit(&self) {
match self {
Self::Dip4(event) => event.emit(),
Self::Nep245(event) => event.emit(),
}
}
}

#[must_use = "make sure to `.emit()` this event"]
#[near(event_json(standard = "dip4"))]
#[derive(Debug, Clone, Deserialize, From)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/intents/token_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl ExecutableIntent for TokenDiff {
intent_hash,
)]));

engine.inspector.emit_event(event);
engine.inspector.on_event(event.into());

// deposit fees to collector
if !fees_collected.is_empty() {
Expand Down
12 changes: 6 additions & 6 deletions core/src/intents/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ExecutableIntent for Transfer {
intent_hash,
)]));

engine.inspector.emit_event(event);
engine.inspector.on_event(event.into());

engine
.state
Expand Down Expand Up @@ -106,7 +106,7 @@ impl ExecutableIntent for FtWithdraw {
intent_hash,
)]));

engine.inspector.emit_event(event);
engine.inspector.on_event(event.into());

engine.state.ft_withdraw(owner_id, self)
}
Expand Down Expand Up @@ -152,7 +152,7 @@ impl ExecutableIntent for NftWithdraw {
intent_hash,
)]));

engine.inspector.emit_event(event);
engine.inspector.on_event(event.into());

engine.state.nft_withdraw(owner_id, self)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ impl ExecutableIntent for MtWithdraw {
intent_hash,
)]));

engine.inspector.emit_event(event);
engine.inspector.on_event(event.into());

engine.state.mt_withdraw(owner_id, self)
}
Expand Down Expand Up @@ -236,7 +236,7 @@ impl ExecutableIntent for NativeWithdraw {
intent_hash,
)]));

engine.inspector.emit_event(event);
engine.inspector.on_event(event.into());

engine.state.native_withdraw(owner_id, self)
}
Expand Down Expand Up @@ -278,7 +278,7 @@ impl ExecutableIntent for StorageDeposit {
intent_hash,
)]));

engine.inspector.emit_event(event);
engine.inspector.on_event(event.into());

engine.state.storage_deposit(owner_id, self)
}
Expand Down
8 changes: 6 additions & 2 deletions defuse/src/contract/intents/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use defuse_core::{
Deadline, accounts::AccountEvent, engine::Inspector, events::DefuseEvent, intents::IntentEvent,
Deadline,
accounts::AccountEvent,
engine::Inspector,
events::{DefuseEvent, Event},
intents::IntentEvent,
};
use near_sdk::{AccountIdRef, CryptoHash};
use std::borrow::Cow;
Expand All @@ -11,7 +15,7 @@ pub struct ExecuteInspector {

impl Inspector for ExecuteInspector {
#[inline]
fn emit_event(&mut self, event: DefuseEvent<'_>) {
fn on_event(&mut self, event: Event<'_>) {
event.emit();
}

Expand Down
5 changes: 1 addition & 4 deletions defuse/src/contract/intents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Intents for Contract {
#[pause(name = "intents")]
#[inline]
fn simulate_intents(&self, signed: Vec<MultiPayload>) -> SimulationOutput {
let mut inspector = SimulateInspector::new(self.wnear_id.clone());
let mut inspector = SimulateInspector::default();
let engine = Engine::new(self.cached(), &mut inspector);

let invariant_violated = match engine.execute_signed_intents(signed) {
Expand All @@ -51,9 +51,6 @@ impl Intents for Contract {
invariant_violated,
state: StateOutput { fee: self.fee() },
balance_diff: inspector.balance_diff,
ft_withdrawals: inspector.ft_withdrawals,
nft_withdrawals: inspector.nft_withdrawals,
mt_withdrawals: inspector.mt_withdrawals,
}
}
}
27 changes: 7 additions & 20 deletions defuse/src/contract/intents/simulate.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use defuse_core::events::DefuseEvent;
use defuse_core::events::Event;
use defuse_core::{
Deadline,
accounts::AccountEvent,
engine::Inspector,
intents::{
IntentEvent,
token_diff::TokenDeltas,
tokens::{FtWithdraw, MtWithdraw, NftWithdraw},
},
intents::{IntentEvent, token_diff::TokenDeltas},
};
use defuse_near_utils::UnwrapOrPanicError;
use near_sdk::{AccountId, AccountIdRef, CryptoHash, serde_json};
Expand All @@ -17,34 +13,25 @@ pub struct SimulateInspector {
pub intents_executed: Vec<IntentEvent<AccountEvent<'static, ()>>>,
pub min_deadline: Deadline,
pub balance_diff: HashMap<AccountId, TokenDeltas>,
#[allow(dead_code)] // FIXME: remove
pub wnear_id: AccountId,
pub ft_withdrawals: Option<Vec<FtWithdraw>>,
pub nft_withdrawals: Option<Vec<NftWithdraw>>,
pub mt_withdrawals: Option<Vec<MtWithdraw>>,
pub events_emitted: Vec<String>,
pub events_emitted: Vec<serde_json::Value>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub events_emitted: Vec<serde_json::Value>,
pub events: Vec<serde_json::Value>,

}

impl SimulateInspector {
pub fn new(wnear_id: AccountId) -> Self {
impl Default for SimulateInspector {
fn default() -> Self {
Self {
intents_executed: Vec::new(),
min_deadline: Deadline::MAX,
balance_diff: HashMap::default(),
wnear_id,
ft_withdrawals: None,
nft_withdrawals: None,
mt_withdrawals: None,
events_emitted: Vec::new(),
}
}
}

impl Inspector for SimulateInspector {
#[inline]
fn emit_event(&mut self, event: DefuseEvent<'_>) {
fn on_event(&mut self, event: Event<'_>) {
self.events_emitted
.push(serde_json::to_string(&event).unwrap_or_panic_display());
.push(serde_json::to_value(&event).unwrap_or_panic_display());
}

#[inline]
Expand Down
11 changes: 1 addition & 10 deletions defuse/src/intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use defuse_core::{
accounts::AccountEvent,
engine::deltas::InvariantViolated,
fees::Pips,
intents::{
IntentEvent,
token_diff::TokenDeltas,
tokens::{FtWithdraw, MtWithdraw, NftWithdraw},
},
intents::{IntentEvent, token_diff::TokenDeltas},
payload::multi::MultiPayload,
};

Expand Down Expand Up @@ -53,11 +49,6 @@ pub struct SimulationOutput {

/// All changes in balances after simulating the intent
pub balance_diff: HashMap<AccountId, TokenDeltas>,

/// Explicit withdrawal requests
pub ft_withdrawals: Option<Vec<FtWithdraw>>,
pub nft_withdrawals: Option<Vec<NftWithdraw>>,
pub mt_withdrawals: Option<Vec<MtWithdraw>>,
}

impl SimulationOutput {
Expand Down
3 changes: 0 additions & 3 deletions tests/src/tests/defuse/intents/ft_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ async fn test_ft_withdraw_intent_msg(
.collect::<BTreeMap<_, _>>()
)
);
assert_eq!(sim_out.ft_withdrawals, Some(vec![withdraw_intent]));
assert!(sim_out.nft_withdrawals.is_none());
assert!(sim_out.mt_withdrawals.is_none());

env.defuse.execute_intents(intents).await.unwrap();

Expand Down
Loading