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