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