Skip to main content

fedimint_walletv2_client/
receive_sm.rs

1use fedimint_client_module::DynGlobalClientContext;
2use fedimint_client_module::sm::{ClientSMDatabaseTransaction, State, StateTransition};
3use fedimint_core::TransactionId;
4use fedimint_core::core::OperationId;
5use fedimint_core::encoding::{Decodable, Encodable};
6
7use crate::WalletClientContext;
8use crate::events::{ReceivePaymentStatus, ReceivePaymentUpdateEvent};
9
10#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
11pub struct ReceiveStateMachine {
12    pub common: ReceiveSMCommon,
13    pub state: ReceiveSMState,
14}
15
16impl ReceiveStateMachine {
17    pub fn update(&self, state: ReceiveSMState) -> Self {
18        Self {
19            common: self.common.clone(),
20            state,
21        }
22    }
23}
24
25#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
26pub struct ReceiveSMCommon {
27    pub operation_id: OperationId,
28    pub txid: TransactionId,
29    pub value: bitcoin::Amount,
30    pub fee: bitcoin::Amount,
31}
32
33#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
34pub enum ReceiveSMState {
35    Funding,
36    Success,
37    Aborted(String),
38}
39
40impl State for ReceiveStateMachine {
41    type ModuleContext = WalletClientContext;
42
43    fn transitions(
44        &self,
45        context: &Self::ModuleContext,
46        global_context: &DynGlobalClientContext,
47    ) -> Vec<StateTransition<Self>> {
48        let ctx = context.clone();
49        let global_context = global_context.clone();
50
51        match &self.state {
52            ReceiveSMState::Funding => {
53                vec![StateTransition::new(
54                    Self::await_funding(global_context.clone(), self.common.txid),
55                    move |dbtx, result, old_state| {
56                        Box::pin(Self::transition_funding(
57                            ctx.clone(),
58                            global_context.clone(),
59                            dbtx,
60                            result,
61                            old_state,
62                        ))
63                    },
64                )]
65            }
66            ReceiveSMState::Success | ReceiveSMState::Aborted(_) => {
67                vec![]
68            }
69        }
70    }
71
72    fn operation_id(&self) -> OperationId {
73        self.common.operation_id
74    }
75}
76
77impl ReceiveStateMachine {
78    async fn await_funding(
79        global_context: DynGlobalClientContext,
80        txid: TransactionId,
81    ) -> Result<(), String> {
82        global_context.await_tx_accepted(txid).await
83    }
84
85    async fn transition_funding(
86        context: WalletClientContext,
87        global_context: DynGlobalClientContext,
88        dbtx: &mut ClientSMDatabaseTransaction<'_, '_>,
89        result: Result<(), String>,
90        old_state: ReceiveStateMachine,
91    ) -> ReceiveStateMachine {
92        let account = global_context.account(dbtx).await;
93
94        match result {
95            Ok(()) => {
96                context
97                    .client_ctx
98                    .log_event(
99                        &mut dbtx.module_tx(),
100                        ReceivePaymentUpdateEvent {
101                            operation_id: old_state.common.operation_id,
102                            status: ReceivePaymentStatus::Success,
103                            account,
104                        },
105                    )
106                    .await;
107
108                old_state.update(ReceiveSMState::Success)
109            }
110            Err(error) => {
111                context
112                    .client_ctx
113                    .log_event(
114                        &mut dbtx.module_tx(),
115                        ReceivePaymentUpdateEvent {
116                            operation_id: old_state.common.operation_id,
117                            status: ReceivePaymentStatus::Aborted,
118                            account,
119                        },
120                    )
121                    .await;
122
123                old_state.update(ReceiveSMState::Aborted(error))
124            }
125        }
126    }
127}