defuse/tokens/
mod.rs

1pub mod nep141;
2pub mod nep171;
3pub mod nep245;
4
5use core::{
6    fmt::{self, Debug, Display},
7    str::FromStr,
8};
9
10use defuse_core::payload::multi::MultiPayload;
11use defuse_near_utils::UnwrapOrPanicError;
12use near_account_id::ParseAccountError;
13use near_sdk::{AccountId, near, serde_json};
14use thiserror::Error as ThisError;
15
16#[near(serializers = [json])]
17#[derive(Debug, Clone)]
18pub struct DepositMessage {
19    pub receiver_id: AccountId,
20
21    #[serde(default, skip_serializing_if = "Vec::is_empty")]
22    pub execute_intents: Vec<MultiPayload>,
23
24    #[serde(default, skip_serializing_if = "::core::ops::Not::not")]
25    pub refund_if_fails: bool,
26}
27
28impl DepositMessage {
29    #[must_use]
30    #[inline]
31    pub const fn new(receiver_id: AccountId) -> Self {
32        Self {
33            receiver_id,
34            execute_intents: Vec::new(),
35            refund_if_fails: false,
36        }
37    }
38
39    #[must_use]
40    #[inline]
41    pub fn with_execute_intents(mut self, intents: impl IntoIterator<Item = MultiPayload>) -> Self {
42        self.execute_intents.extend(intents);
43        self
44    }
45
46    #[must_use]
47    #[inline]
48    pub const fn with_refund_if_fails(mut self) -> Self {
49        self.refund_if_fails = true;
50        self
51    }
52}
53
54impl Display for DepositMessage {
55    #[inline]
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        if self.execute_intents.is_empty() {
58            f.write_str(self.receiver_id.as_str())
59        } else {
60            f.write_str(&serde_json::to_string(self).unwrap_or_panic_display())
61        }
62    }
63}
64
65impl FromStr for DepositMessage {
66    type Err = ParseDepositMessageError;
67
68    #[inline]
69    fn from_str(s: &str) -> Result<Self, Self::Err> {
70        if s.starts_with('{') {
71            serde_json::from_str(s).map_err(Into::into)
72        } else {
73            s.parse().map(Self::new).map_err(Into::into)
74        }
75    }
76}
77
78#[derive(Debug, ThisError)]
79pub enum ParseDepositMessageError {
80    #[error(transparent)]
81    Account(#[from] ParseAccountError),
82    #[error("JSON: {0}")]
83    JSON(#[from] serde_json::Error),
84}