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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::{storage::BoundedVec, Box, Debug, MAX_CALL_LEN};
use codec::{Decode, Encode, MaxEncodedLen};
use t3rn_types::sfx::Insurance;

#[derive(Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, Debug)]
pub enum Chain<AccountId, Balance, Hash>
where
    Hash: Encode + Decode,
    AccountId: Encode + Decode,
    Balance: Encode + Decode,
{
    Kusama(Operation<AccountId, Balance, Hash>),
    Polkadot(Operation<AccountId, Balance, Hash>),
    Karura(Operation<AccountId, Balance, Hash>),
    T3rn(Operation<AccountId, Balance, Hash>),
}

impl<AccountId, Balance, Hash> Chain<AccountId, Balance, Hash>
where
    Hash: Encode + Decode,
    AccountId: Encode + Decode,
    Balance: Encode + Decode,
{
    // Could just be deref
    pub fn get_operation(self) -> Operation<AccountId, Balance, Hash> {
        match self {
            Chain::Kusama(op) => op,
            Chain::Polkadot(op) => op,
            Chain::Karura(op) => op,
            Chain::T3rn(op) => op,
        }
    }
}

#[derive(Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, Debug)]
pub struct Call<AccountId, Balance>
where
    AccountId: Encode + Decode,
    Balance: Encode + Decode,
{
    pub caller: AccountId,
    pub call: VM<AccountId, Balance>,
    pub data: BoundedVec<u8, MAX_CALL_LEN>,
}

#[derive(Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, Debug)]
pub enum Operation<AccountId, Balance, Hash>
where
    Hash: Encode + Decode,
    AccountId: Encode + Decode,
    Balance: Encode + Decode,
{
    Transfer {
        caller: AccountId,
        to: AccountId,
        amount: Balance,
        insurance: Option<Insurance<Balance>>,
    },
    TransferMulti {
        asset: Hash,
        caller: AccountId,
        to: AccountId,
        amount: Balance,
        insurance: Option<Insurance<Balance>>,
    },
    AddLiquidity {
        caller: AccountId,
        to: AccountId,
        asset_left: Hash,
        asset_right: Hash,
        liquidity_token: Hash,
        amount_left: Balance,
        amount_right: Balance,
        amount_liquidity_token: Balance,
        insurance: Option<Insurance<Balance>>,
    },
    Swap {
        caller: AccountId,
        to: AccountId,
        amount_from: Balance,
        amount_to: Balance,
        asset_from: Hash,
        asset_to: Hash,
        insurance: Option<Insurance<Balance>>,
    },
    Call(Box<Call<AccountId, Balance>>),
    Data {
        index: Hash,
    },
}

/// A representation of the type of a call that the VM can handle.
#[derive(Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, Debug)]
pub enum VM<AccountId, Balance>
where
    AccountId: Encode + Decode,
    Balance: Encode + Decode,
{
    Evm {
        dest: AccountId,
        value: Balance,
    },
    Wasm {
        dest: AccountId,
        value: Balance,
        gas_limit: Balance,
        storage_limit: Option<Balance>,
    },
}

#[cfg(test)]
mod tests {
    use super::*;
    use scale_info::prelude::vec;

    #[test]
    fn transfer_selector_works() {
        let caller = [5_u8; 2];
        let to = [6_u8; 2];
        let balance = 100_u32;
        let selector = Chain::<_, _, u32>::Kusama(Operation::Transfer {
            caller,
            to,
            amount: balance,
            insurance: None,
        });
        assert_eq!(
            selector.clone().encode(),
            [0, 0, 5, 5, 6, 6, 100, 0, 0, 0, 0]
        );
    }

    #[test]
    fn transfer_multi_selector_works() {
        let caller = [5_u8; 2];
        let to = [6_u8; 2];
        let asset = [7_u8; 2];
        let balance = 100_u32;
        let selector = Chain::Kusama(Operation::TransferMulti {
            caller,
            to,
            amount: balance,
            asset,
            insurance: None,
        });
        assert_eq!(selector.encode(), [0, 1, 7, 7, 5, 5, 6, 6, 100, 0, 0, 0, 0]);
    }

    #[test]
    fn add_liquidity_selector_works() {
        let caller = [5_u8; 2];
        let to = [6_u8; 2];
        let asset_left = [7_u8; 2];
        let asset_right = [8_u8; 2];
        let liquidity_token = [9_u8; 2];
        let amount_left = 100_u32;
        let amount_right = 200_u32;
        let amount_liquidity_token = 300_u32;
        let selector = Chain::Kusama(Operation::AddLiquidity {
            caller,
            to,
            asset_left,
            asset_right,
            liquidity_token,
            amount_left,
            amount_right,
            amount_liquidity_token,
            insurance: None,
        });

        assert_eq!(
            selector.encode(),
            vec![0, 2, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 100, 0, 0, 0, 200, 0, 0, 0, 44, 1, 0, 0, 0]
        );
    }

    #[test]
    fn test_swap_selector_works() {
        let caller = [5_u8; 2];
        let to = [6_u8; 2];
        let asset_from = [7_u8; 2];
        let asset_to = [8_u8; 2];
        let amount_from = 100_u32;
        let amount_to = 200_u32;

        let selector = Chain::Kusama(Operation::Swap {
            caller,
            to,
            amount_from,
            amount_to,
            asset_from,
            asset_to,
            insurance: None,
        });
        assert_eq!(
            selector.encode(),
            vec![0, 3, 5, 5, 6, 6, 100, 0, 0, 0, 200, 0, 0, 0, 7, 7, 8, 8, 0]
        );
    }

    #[test]
    fn test_evm_call_works() {
        let dest = [6_u8; 2];
        let value = 100_u32;
        let call = VM::Evm { dest, value };
        let caller = [5_u8; 2];
        let selector = Chain::<_, u32, u32>::Kusama(Operation::Call(Box::new(Call {
            caller,
            call,
            data: BoundedVec::default(),
        })));
        assert_eq!(
            selector.encode(),
            vec![0, 4, 5, 5, 0, 6, 6, 100, 0, 0, 0, 0]
        );
    }
}