defuse_core/intents/
imt.rs1use 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)]
18pub struct ImtMint {
20 pub receiver_id: AccountId,
22
23 #[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 #[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)]
77pub struct ImtBurn {
79 pub minter_id: AccountId,
81
82 #[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}