Skip to main content

defuse_core/intents/
imt.rs

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