defuse_core/engine/state/
mod.rs

1pub mod cached;
2pub mod deltas;
3
4use crate::{
5    Nonce, NoncePrefix, Result, Salt,
6    amounts::Amounts,
7    fees::Pips,
8    intents::{
9        auth::AuthCall,
10        tokens::{
11            FtWithdraw, MtWithdraw, NativeWithdraw, NftWithdraw, NotifyOnTransfer, StorageDeposit,
12        },
13    },
14    token_id::{TokenId, nep141::Nep141TokenId},
15};
16use cached::CachedState;
17use defuse_crypto::PublicKey;
18use impl_tools::autoimpl;
19use near_sdk::{AccountId, AccountIdRef};
20use std::borrow::Cow;
21
22#[autoimpl(for<T: trait + ?Sized> &T, &mut T, Box<T>)]
23pub trait StateView {
24    fn verifying_contract(&self) -> Cow<'_, AccountIdRef>;
25    fn wnear_id(&self) -> Cow<'_, AccountIdRef>;
26    fn wnear_token_id(&self) -> TokenId {
27        Nep141TokenId::new(self.wnear_id().into_owned()).into()
28    }
29
30    fn fee(&self) -> Pips;
31    fn fee_collector(&self) -> Cow<'_, AccountIdRef>;
32
33    #[must_use]
34    fn has_public_key(&self, account_id: &AccountIdRef, public_key: &PublicKey) -> bool;
35    fn iter_public_keys(&self, account_id: &AccountIdRef) -> impl Iterator<Item = PublicKey> + '_;
36
37    #[must_use]
38    fn is_nonce_used(&self, account_id: &AccountIdRef, nonce: Nonce) -> bool;
39
40    #[must_use]
41    fn balance_of(&self, account_id: &AccountIdRef, token_id: &TokenId) -> u128;
42
43    fn is_account_locked(&self, account_id: &AccountIdRef) -> bool;
44
45    /// Returns whether authentication by `PREDECESSOR_ID` is enabled.
46    fn is_auth_by_predecessor_id_enabled(&self, account_id: &AccountIdRef) -> bool;
47
48    /// Returns whether salt in nonce is valid
49    fn is_valid_salt(&self, salt: Salt) -> bool;
50
51    #[inline]
52    fn cached(self) -> CachedState<Self>
53    where
54        Self: Sized,
55    {
56        CachedState::new(self)
57    }
58}
59
60#[autoimpl(for<T: trait + ?Sized> &mut T, Box<T>)]
61pub trait State: StateView {
62    fn add_public_key(&mut self, account_id: AccountId, public_key: PublicKey) -> Result<()>;
63
64    fn remove_public_key(&mut self, account_id: AccountId, public_key: PublicKey) -> Result<()>;
65
66    fn commit_nonce(&mut self, account_id: AccountId, nonce: Nonce) -> Result<()>;
67
68    fn cleanup_nonce_by_prefix(
69        &mut self,
70        account_id: &AccountIdRef,
71        prefix: NoncePrefix,
72    ) -> Result<bool>;
73
74    fn internal_add_balance(
75        &mut self,
76        owner_id: AccountId,
77        tokens: impl IntoIterator<Item = (TokenId, u128)>,
78    ) -> Result<()>;
79
80    fn internal_sub_balance(
81        &mut self,
82        owner_id: &AccountIdRef,
83        tokens: impl IntoIterator<Item = (TokenId, u128)>,
84    ) -> Result<()>;
85
86    fn internal_apply_deltas(
87        &mut self,
88        owner_id: &AccountIdRef,
89        tokens: impl IntoIterator<Item = (TokenId, i128)>,
90    ) -> Result<()> {
91        for (token_id, delta) in tokens {
92            let tokens = [(token_id, delta.unsigned_abs())];
93            if delta.is_negative() {
94                self.internal_sub_balance(owner_id, tokens)?;
95            } else {
96                self.internal_add_balance(owner_id.to_owned(), tokens)?;
97            }
98        }
99        Ok(())
100    }
101
102    fn ft_withdraw(&mut self, owner_id: &AccountIdRef, withdraw: FtWithdraw) -> Result<()>;
103
104    fn nft_withdraw(&mut self, owner_id: &AccountIdRef, withdraw: NftWithdraw) -> Result<()>;
105
106    fn mt_withdraw(&mut self, owner_id: &AccountIdRef, withdraw: MtWithdraw) -> Result<()>;
107
108    fn native_withdraw(&mut self, owner_id: &AccountIdRef, withdraw: NativeWithdraw) -> Result<()>;
109
110    fn notify_on_transfer(
111        &self,
112        sender_id: &AccountIdRef,
113        receiver_id: AccountId,
114        tokens: Amounts,
115        notification: NotifyOnTransfer,
116    );
117
118    fn storage_deposit(
119        &mut self,
120        owner_id: &AccountIdRef,
121        storage_deposit: StorageDeposit,
122    ) -> Result<()>;
123
124    /// Sets whether authentication by `PREDECESSOR_ID` is enabled.
125    /// Returns whether authentication by `PREDECESSOR_ID` was enabled
126    /// before.
127    fn set_auth_by_predecessor_id(&mut self, account_id: AccountId, enable: bool) -> Result<bool>;
128
129    fn auth_call(&mut self, signer_id: &AccountIdRef, auth_call: AuthCall) -> Result<()>;
130}