1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::H256;
use ft_storage_io::{FTStorageAction, FTStorageEvent};
use gstd::{msg, prelude::*, ActorId};

#[derive(Debug, Encode, Decode, TypeInfo, Clone)]
pub enum InstructionState {
    ScheduledRun,
    ScheduledAbort,
    RunWithError,
    Finished,
}

#[derive(Debug, Encode, Decode, TypeInfo, Clone)]
pub struct Instruction {
    pub state: InstructionState,
    pub address: ActorId,
    pub transaction: FTStorageAction,
    pub compensation: Option<FTStorageAction>,
}

impl Instruction {
    /// Create a new instruction from a given transaction and a compensation
    pub fn new(
        address: ActorId,
        transaction: FTStorageAction,
        compensation: Option<FTStorageAction>,
    ) -> Self {
        Instruction {
            state: InstructionState::ScheduledRun,
            address,
            transaction,
            compensation,
        }
    }

    pub async fn start(&mut self) -> Result<(), ()> {
        match self.state {
            InstructionState::ScheduledRun => {
                let result = msg::send_for_reply_as::<_, FTStorageEvent>(
                    self.address,
                    self.transaction,
                    0,
                    0,
                )
                .expect("Error in sending a message in instruction")
                .await;
                match result {
                    Ok(FTStorageEvent::Ok) => {
                        self.state = InstructionState::ScheduledAbort;
                        Ok(())
                    }
                    _ => {
                        self.state = InstructionState::RunWithError;
                        Err(())
                    }
                }
            }
            InstructionState::RunWithError => Err(()),
            _ => Ok(()),
        }
    }

    pub async fn abort(&mut self) -> Result<(), ()> {
        match self.state {
            InstructionState::ScheduledAbort => {
                let result = msg::send_for_reply_as::<_, FTStorageEvent>(
                    self.address,
                    self.compensation
                        .expect("No compensation for that instruction"),
                    0,
                    0,
                )
                .expect("Error in sending a compensation message in instruction")
                .await;
                match result {
                    Ok(FTStorageEvent::Ok) => {
                        self.state = InstructionState::Finished;
                        Ok(())
                    }
                    _ => Err(()),
                }
            }
            InstructionState::Finished => Ok(()),
            _ => Err(()),
        }
    }
}

pub fn create_decrease_instruction(
    transaction_hash: H256,
    msg_source: &ActorId,
    sender_storage: &ActorId,
    sender: &ActorId,
    amount: u128,
) -> Instruction {
    Instruction::new(
        *sender_storage,
        FTStorageAction::DecreaseBalance {
            transaction_hash,
            msg_source: *msg_source,
            account: *sender,
            amount,
        },
        Some(FTStorageAction::IncreaseBalance {
            transaction_hash,
            account: *sender,
            amount,
        }),
    )
}

pub fn create_increase_instruction(
    transaction_hash: H256,
    recipient_storage: &ActorId,
    recipient: &ActorId,
    amount: u128,
) -> Instruction {
    Instruction::new(
        *recipient_storage,
        FTStorageAction::IncreaseBalance {
            transaction_hash,
            account: *recipient,
            amount,
        },
        None,
    )
}