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