Skip to content

Add more information to SimulationOutput #78

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 2 commits into
base: feat/mt-burn-withdraw-info
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
16 changes: 9 additions & 7 deletions core/src/engine/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::Result;
use crate::{
Deadline,
intents::{
Expand All @@ -18,49 +19,50 @@ pub trait Inspector {
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_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_intent_executed(&mut self, signer_id: &AccountIdRef, hash: CryptoHash);
}
2 changes: 1 addition & 1 deletion core/src/intents/token_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl ExecutableIntent for TokenDiff {

engine
.inspector
.on_token_diff(signer_id, &self, &fees_collected, intent_hash);
.on_token_diff(signer_id, &self, &fees_collected, intent_hash)?;

// deposit fees to collector
if !fees_collected.is_empty() {
Expand Down
16 changes: 9 additions & 7 deletions core/src/intents/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ 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);
engine
.inspector
.on_transfer(sender_id, &self, intent_hash)?;
engine
.state
.internal_sub_balance(sender_id, self.tokens.clone())?;
Expand All @@ -59,7 +61,7 @@ impl ExecutableIntent for Transfer {
}

#[near(serializers = [borsh, json])]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
/// Withdraw given FT tokens from the intents contract to a given external account id (external being outside of intents).
pub struct FtWithdraw {
pub token: AccountId,
Expand Down Expand Up @@ -95,7 +97,7 @@ impl ExecutableIntent for FtWithdraw {
{
engine
.inspector
.on_ft_withdraw(owner_id, &self, intent_hash);
.on_ft_withdraw(owner_id, &self, intent_hash)?;
engine.state.ft_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -137,7 +139,7 @@ impl ExecutableIntent for NftWithdraw {
{
engine
.inspector
.on_nft_withdraw(owner_id, &self, intent_hash);
.on_nft_withdraw(owner_id, &self, intent_hash)?;
engine.state.nft_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -182,7 +184,7 @@ impl ExecutableIntent for MtWithdraw {
{
engine
.inspector
.on_mt_withdraw(owner_id, &self, intent_hash);
.on_mt_withdraw(owner_id, &self, intent_hash)?;
engine.state.mt_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -212,7 +214,7 @@ impl ExecutableIntent for NativeWithdraw {
{
engine
.inspector
.on_native_withdraw(owner_id, &self, intent_hash);
.on_native_withdraw(owner_id, &self, intent_hash)?;
engine.state.native_withdraw(owner_id, self)
}
}
Expand Down Expand Up @@ -250,7 +252,7 @@ impl ExecutableIntent for StorageDeposit {
{
engine
.inspector
.on_storage_deposit(owner_id, &self, intent_hash);
.on_storage_deposit(owner_id, &self, intent_hash)?;
engine.state.storage_deposit(owner_id, self)
}
}
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod accounts;
mod deadline;
pub mod engine;
mod error;
pub mod error;
Copy link
Collaborator

Choose a reason for hiding this comment

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

error::* is already re-exported below

pub mod events;
pub mod fees;
pub mod intents;
Expand Down
7 changes: 7 additions & 0 deletions core/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,16 @@ pub enum ParseTokenIdError {

#[near(serializers = [borsh, json])]
#[autoimpl(Deref using self.0)]
#[autoimpl(DerefMut using self.0)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

DerefMut was not implemented here on purpose: all mutations happen on Amounts level to enforce using .checked_...() operations

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Amounts<T = BTreeMap<TokenId, u128>>(T);

impl<T> From<T> for Amounts<T> {
fn from(m: T) -> Self {
Amounts(m)
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
Amounts(m)
Self::new(m)

}
}

impl<T> Amounts<T> {
#[inline]
pub const fn new(map: T) -> Self {
Expand Down
29 changes: 22 additions & 7 deletions defuse/src/contract/intents/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use defuse_core::error::Result;
use defuse_core::{
Deadline,
accounts::AccountEvent,
Expand Down Expand Up @@ -29,7 +30,7 @@ impl Inspector for ExecuteInspector {
sender_id: &AccountIdRef,
transfer: &Transfer,
intent_hash: CryptoHash,
) {
) -> Result<()> {
DefuseEvent::Transfer(
[IntentEvent::new(
AccountEvent::new(sender_id, Cow::Borrowed(transfer)),
Expand All @@ -39,6 +40,8 @@ impl Inspector for ExecuteInspector {
.into(),
)
.emit();

Ok(())
}

#[inline]
Expand All @@ -48,7 +51,7 @@ impl Inspector for ExecuteInspector {
token_diff: &TokenDiff,
fees_collected: &Amounts,
intent_hash: CryptoHash,
) {
) -> Result<()> {
DefuseEvent::TokenDiff(
[IntentEvent::new(
AccountEvent::new(
Expand All @@ -64,14 +67,16 @@ impl Inspector for ExecuteInspector {
.into(),
)
.emit();

Ok(())
}

fn on_ft_withdraw(
&mut self,
owner_id: &AccountIdRef,
ft_withdraw: &FtWithdraw,
intent_hash: CryptoHash,
) {
) -> Result<()> {
DefuseEvent::FtWithdraw(
[IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(ft_withdraw)),
Expand All @@ -81,14 +86,16 @@ impl Inspector for ExecuteInspector {
.into(),
)
.emit();

Ok(())
}

fn on_nft_withdraw(
&mut self,
owner_id: &AccountIdRef,
nft_withdraw: &NftWithdraw,
intent_hash: CryptoHash,
) {
) -> Result<()> {
DefuseEvent::NftWithdraw(
[IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(nft_withdraw)),
Expand All @@ -98,14 +105,16 @@ impl Inspector for ExecuteInspector {
.into(),
)
.emit();

Ok(())
}

fn on_mt_withdraw(
&mut self,
owner_id: &AccountIdRef,
mt_withdraw: &MtWithdraw,
intent_hash: CryptoHash,
) {
) -> Result<()> {
DefuseEvent::MtWithdraw(
[IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(mt_withdraw)),
Expand All @@ -115,14 +124,16 @@ impl Inspector for ExecuteInspector {
.into(),
)
.emit();

Ok(())
}

fn on_native_withdraw(
&mut self,
owner_id: &AccountIdRef,
native_withdraw: &NativeWithdraw,
intent_hash: CryptoHash,
) {
) -> Result<()> {
DefuseEvent::NativeWithdraw(
[IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(native_withdraw)),
Expand All @@ -132,14 +143,16 @@ impl Inspector for ExecuteInspector {
.into(),
)
.emit();

Ok(())
}

fn on_storage_deposit(
&mut self,
owner_id: &AccountIdRef,
storage_deposit: &StorageDeposit,
intent_hash: CryptoHash,
) {
) -> Result<()> {
DefuseEvent::StorageDeposit(
[IntentEvent::new(
AccountEvent::new(owner_id, Cow::Borrowed(storage_deposit)),
Expand All @@ -149,6 +162,8 @@ impl Inspector for ExecuteInspector {
.into(),
)
.emit();

Ok(())
}

#[inline]
Expand Down
6 changes: 5 additions & 1 deletion 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::default();
let mut inspector = SimulateInspector::new(self.wnear_id.clone());
let engine = Engine::new(self.cached(), &mut inspector);

let invariant_violated = match engine.execute_signed_intents(signed) {
Expand All @@ -50,6 +50,10 @@ impl Intents for Contract {
min_deadline: inspector.min_deadline,
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,
}
}
}
Loading
Loading