Skip to main content

defuse_core/intents/
imt.rs

1use borsh::{BorshDeserialize, BorshSerialize};
2use near_sdk::{AccountId, AccountIdRef, CryptoHash};
3use serde::{Deserialize, Serialize};
4use serde_with::{DisplayFromStr, serde_as};
5use std::{borrow::Cow, collections::BTreeMap};
6
7use crate::{
8    Result,
9    accounts::AccountEvent,
10    amounts::Amounts,
11    engine::{Engine, Inspector, State},
12    events::DefuseEvent,
13    intents::{ExecutableIntent, MaybeIntentEvent, tokens::NotifyOnTransfer},
14    tokens::imt::{ImtMintEvent, ImtTokens},
15};
16
17#[serde_as]
18#[cfg_attr(feature = "abi", derive(::schemars::JsonSchema, ::borsh::BorshSchema))]
19#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
20/// Mint a set of tokens from the signer to a specified account id, within the intents contract.
21pub struct ImtMint {
22    /// Receiver of the minted tokens
23    pub receiver_id: AccountId,
24
25    /// The `token_ids` will be wrapped to bind the token ID to the
26    /// minter authority (i.e. signer of this intent).
27    /// The final string representation of the token will be as follows:
28    /// `imt:<minter_id>:<token_id>`
29    #[serde_as(as = "Amounts<BTreeMap<_, DisplayFromStr>>")]
30    pub tokens: ImtTokens,
31
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub memo: Option<String>,
34
35    /// Optionally notify `receiver_id` via `mt_on_transfer()`
36    ///
37    /// NOTE: `min_gas` is adjusted with following values:
38    /// * minimum: 5TGas
39    /// * default: 30TGas
40    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
41    pub notification: Option<NotifyOnTransfer>,
42}
43
44impl ExecutableIntent for ImtMint {
45    #[inline]
46    fn execute_intent<S, I>(
47        self,
48        signer_id: &AccountIdRef,
49        engine: &mut Engine<S, I>,
50        intent_hash: CryptoHash,
51    ) -> Result<()>
52    where
53        S: State,
54        I: Inspector,
55    {
56        engine
57            .inspector
58            .on_event(DefuseEvent::ImtMint(Cow::Borrowed(
59                [MaybeIntentEvent::new_intent(
60                    AccountEvent::new(signer_id, ImtMintEvent::from(&self)),
61                    intent_hash,
62                )]
63                .as_slice(),
64            )));
65
66        engine.state.imt_mint(
67            signer_id,
68            self.receiver_id,
69            self.tokens,
70            self.memo,
71            self.notification,
72        )
73    }
74}
75
76#[serde_as]
77#[cfg_attr(feature = "abi", derive(::schemars::JsonSchema, ::borsh::BorshSchema))]
78#[derive(Debug, Clone, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
79/// Burn a set of imt tokens, within the intents contract.
80pub struct ImtBurn {
81    // The minter authority of the imt tokens
82    pub minter_id: AccountId,
83
84    /// The `token_ids` will be wrapped to bind the token ID to the
85    /// minter authority. The final string representation of the
86    /// token will be as follows:
87    /// `imt:<minter_id>:<token_id>`
88    #[serde_as(as = "Amounts<BTreeMap<_, DisplayFromStr>>")]
89    pub tokens: ImtTokens,
90
91    #[serde(default, skip_serializing_if = "Option::is_none")]
92    pub memo: Option<String>,
93}
94
95impl ExecutableIntent for ImtBurn {
96    #[inline]
97    fn execute_intent<S, I>(
98        self,
99        signer_id: &AccountIdRef,
100        engine: &mut Engine<S, I>,
101        intent_hash: CryptoHash,
102    ) -> Result<()>
103    where
104        S: State,
105        I: Inspector,
106    {
107        engine
108            .inspector
109            .on_event(DefuseEvent::ImtBurn(Cow::Borrowed(
110                [MaybeIntentEvent::new_intent(
111                    AccountEvent::new(signer_id, Cow::Borrowed(&self)),
112                    intent_hash,
113                )]
114                .as_slice(),
115            )));
116
117        engine
118            .state
119            .imt_burn(signer_id, &self.minter_id, self.tokens, self.memo)
120    }
121}