Skip to main content

defuse/contract/tokens/nep141/
deposit.rs

1use defuse_core::token_id::{TokenId, nep141::Nep141TokenId};
2use near_contract_standards::fungible_token::receiver::FungibleTokenReceiver;
3use near_plugins::{Pausable, pause};
4use near_sdk::{AccountId, FunctionError, PromiseOrValue, env, json_types::U128, near, require};
5
6use crate::{
7    contract::{Contract, ContractExt},
8    intents::{Intents, ext_intents},
9    tokens::{DepositAction, DepositMessage},
10};
11
12#[near]
13impl FungibleTokenReceiver for Contract {
14    /// Deposit fungible tokens.
15    ///
16    /// `msg` contains [`AccountId`] of the internal recipient.
17    /// Empty `msg` means deposit to `sender_id`
18    #[pause]
19    fn ft_on_transfer(
20        &mut self,
21        sender_id: AccountId,
22        amount: U128,
23        msg: String,
24    ) -> PromiseOrValue<U128> {
25        require!(amount.0 > 0, "zero amount");
26
27        let token_id = TokenId::Nep141(Nep141TokenId::new(env::predecessor_account_id()));
28
29        let DepositMessage {
30            receiver_id,
31            action,
32        } = if msg.is_empty() {
33            DepositMessage::new(sender_id)
34        } else {
35            msg.parse().unwrap_or_else(|e| panic!("{e}"))
36        };
37
38        self.deposit(receiver_id, [(token_id, amount.0)], Some("deposit"))
39            .unwrap_or_else(|err| err.panic());
40
41        let Some(action) = action else {
42            return PromiseOrValue::Value(0.into());
43        };
44
45        match action {
46            // DepositAction::Notify(notify) => Self::notify_on_transfer(
47            //     sender_id.clone(),
48            //     vec![sender_id],
49            //     receiver_id.clone(),
50            //     vec![token_id.to_string()],
51            //     vec![amount],
52            //     notify,
53            // )
54            // .unwrap_or_else(|err| err.panic())
55            // .then(
56            //     Self::ext(env::current_account_id())
57            //         .with_static_gas(Self::mt_resolve_deposit_gas(1))
58            //         .with_unused_gas_weight(0)
59            //         .ft_resolve_deposit(receiver_id, env::predecessor_account_id(), amount),
60            // )
61            // .into(),
62            DepositAction::Execute(execute) => {
63                if !execute.execute_intents.is_empty() {
64                    if execute.refund_if_fails {
65                        self.execute_intents(execute.execute_intents);
66                    } else {
67                        ext_intents::ext(env::current_account_id())
68                            .execute_intents(execute.execute_intents)
69                            .detach();
70                    }
71                }
72                PromiseOrValue::Value(0.into())
73            }
74        }
75    }
76}
77
78#[near]
79impl Contract {
80    #[private]
81    #[allow(clippy::needless_pass_by_value)]
82    pub fn ft_resolve_deposit(
83        &mut self,
84        receiver_id: AccountId,
85        contract_id: AccountId,
86        #[allow(unused_mut)] mut amount: U128,
87    ) -> PromiseOrValue<U128> {
88        self.resolve_deposit_internal(
89            &receiver_id,
90            [(Nep141TokenId::new(contract_id).into(), &mut amount.0)],
91        );
92        PromiseOrValue::Value(amount)
93    }
94}