Skip to content

Post events at the execution level and horde them in the inspector #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: feat/more-in-sim2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
62 changes: 3 additions & 59 deletions core/src/engine/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,12 @@
use crate::error::Result;
use crate::{
Deadline,
intents::{
token_diff::TokenDiff,
tokens::{FtWithdraw, MtWithdraw, NativeWithdraw, NftWithdraw, StorageDeposit, Transfer},
},
tokens::Amounts,
};
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 on_deadline(&mut self, deadline: Deadline);

fn on_transfer(
&mut self,
sender_id: &AccountIdRef,
transfer: &Transfer,
intent_hash: CryptoHash,
) -> Result<()>;

fn on_token_diff(
&mut self,
owner_id: &AccountIdRef,
token_diff: &TokenDiff,
fees_collected: &Amounts,
intent_hash: CryptoHash,
) -> Result<()>;

fn on_ft_withdraw(
&mut self,
owner_id: &AccountIdRef,
ft_withdraw: &FtWithdraw,
intent_hash: CryptoHash,
) -> Result<()>;
fn on_event(&mut self, event: Event<'_>);

fn on_nft_withdraw(
&mut self,
owner_id: &AccountIdRef,
nft_withdraw: &NftWithdraw,
intent_hash: CryptoHash,
) -> Result<()>;

fn on_mt_withdraw(
&mut self,
owner_id: &AccountIdRef,
mt_withdraw: &MtWithdraw,
intent_hash: CryptoHash,
) -> Result<()>;

fn on_native_withdraw(
&mut self,
owner_id: &AccountIdRef,
native_withdraw: &NativeWithdraw,
intent_hash: CryptoHash,
) -> Result<()>;

fn on_storage_deposit(
&mut self,
owner_id: &AccountIdRef,
storage_deposit: &StorageDeposit,
intent_hash: CryptoHash,
) -> Result<()>;
fn on_deadline(&mut self, deadline: Deadline);

fn on_intent_executed(&mut self, signer_id: &AccountIdRef, hash: CryptoHash);
}
27 changes: 22 additions & 5 deletions core/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use std::borrow::Cow;

use derive_more::derive::From;
use near_sdk::{near, serde::Deserialize};

use crate::{
accounts::{AccountEvent, PublicKeyEvent},
fees::{FeeChangedEvent, FeeCollectorChangedEvent},
Expand All @@ -12,6 +7,28 @@ 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"))]
Expand Down
19 changes: 15 additions & 4 deletions core/src/intents/token_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use serde_with::{DisplayFromStr, serde_as};

use crate::{
DefuseError, Result,
accounts::AccountEvent,
engine::{Engine, Inspector, State, StateView},
events::DefuseEvent,
fees::Pips,
tokens::{Amounts, TokenId},
};

use super::ExecutableIntent;
use super::{ExecutableIntent, IntentEvent};

pub type TokenDeltas = Amounts<BTreeMap<TokenId, i128>>;

Expand Down Expand Up @@ -84,9 +86,18 @@ impl ExecutableIntent for TokenDiff {
}
}

engine
.inspector
.on_token_diff(signer_id, &self, &fees_collected, intent_hash)?;
let event = DefuseEvent::TokenDiff(Cow::Owned(vec![IntentEvent::new(
AccountEvent::new(
signer_id,
TokenDiffEvent {
diff: Cow::Borrowed(&self),
fees_collected: fees_collected.clone(),
},
),
intent_hash,
)]));

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

// deposit fees to collector
if !fees_collected.is_empty() {
Expand Down
67 changes: 47 additions & 20 deletions core/src/intents/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use std::collections::BTreeMap;
use std::{borrow::Cow, collections::BTreeMap};

use near_contract_standards::non_fungible_token;
use near_sdk::{AccountId, AccountIdRef, CryptoHash, NearToken, json_types::U128, near};
use serde_with::{DisplayFromStr, serde_as};

use crate::{
DefuseError, Result,
accounts::AccountEvent,
engine::{Engine, Inspector, State},
events::DefuseEvent,
tokens::Amounts,
};

use super::ExecutableIntent;
use super::{ExecutableIntent, IntentEvent};

#[cfg_attr(
all(feature = "abi", not(target_arch = "wasm32")),
Expand Down Expand Up @@ -47,9 +49,13 @@ impl ExecutableIntent for Transfer {
if sender_id == self.receiver_id || self.tokens.is_empty() {
return Err(DefuseError::InvalidIntent);
}
engine
.inspector
.on_transfer(sender_id, &self, intent_hash)?;
let event = DefuseEvent::Transfer(Cow::Owned(vec![IntentEvent::new(
AccountEvent::new(sender_id, Cow::Borrowed(&self)),
intent_hash,
)]));

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

engine
.state
.internal_sub_balance(sender_id, self.tokens.clone())?;
Expand Down Expand Up @@ -95,9 +101,13 @@ impl ExecutableIntent for FtWithdraw {
S: State,
I: Inspector,
{
engine
.inspector
.on_ft_withdraw(owner_id, &self, intent_hash)?;
let event = DefuseEvent::FtWithdraw(Cow::Owned(vec![IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(&self)),
intent_hash,
)]));

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

engine.state.ft_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -137,9 +147,13 @@ impl ExecutableIntent for NftWithdraw {
S: State,
I: Inspector,
{
engine
.inspector
.on_nft_withdraw(owner_id, &self, intent_hash)?;
let event = DefuseEvent::NftWithdraw(Cow::Owned(vec![IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(&self)),
intent_hash,
)]));

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

engine.state.nft_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -170,6 +184,7 @@ pub struct MtWithdraw {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub storage_deposit: Option<NearToken>,
}

impl ExecutableIntent for MtWithdraw {
#[inline]
fn execute_intent<S, I>(
Expand All @@ -182,9 +197,13 @@ impl ExecutableIntent for MtWithdraw {
S: State,
I: Inspector,
{
engine
.inspector
.on_mt_withdraw(owner_id, &self, intent_hash)?;
let event = DefuseEvent::MtWithdraw(Cow::Owned(vec![IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(&self)),
intent_hash,
)]));

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

engine.state.mt_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -212,9 +231,13 @@ impl ExecutableIntent for NativeWithdraw {
S: State,
I: Inspector,
{
engine
.inspector
.on_native_withdraw(owner_id, &self, intent_hash)?;
let event = DefuseEvent::NativeWithdraw(Cow::Owned(vec![IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(&self)),
intent_hash,
)]));

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

engine.state.native_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -250,9 +273,13 @@ impl ExecutableIntent for StorageDeposit {
S: State,
I: Inspector,
{
engine
.inspector
.on_storage_deposit(owner_id, &self, intent_hash)?;
let event = DefuseEvent::StorageDeposit(Cow::Owned(vec![IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(&self)),
intent_hash,
)]));

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

engine.state.storage_deposit(owner_id, self)
}
}
Loading
Loading