t3rn_sdk_primitives/
xc.rs

1use crate::{Debug, MAX_SELECTION_NAME_LENGTH};
2use codec::{Decode, Encode, MaxEncodedLen};
3
4#[derive(Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, Debug)]
5pub enum Chain<AccountId, Balance, Hash>
6where
7    Hash: Encode + Decode,
8    AccountId: Encode + Decode,
9    Balance: Encode + Decode,
10{
11    Kusama(Operation<AccountId, Balance, Hash>),
12    Polkadot(Operation<AccountId, Balance, Hash>),
13    Karura(Operation<AccountId, Balance, Hash>),
14    T3rn(Operation<AccountId, Balance, Hash>),
15}
16
17impl<AccountId, Balance, Hash> Chain<AccountId, Balance, Hash>
18where
19    Hash: Encode + Decode,
20    AccountId: Encode + Decode,
21    Balance: Encode + Decode,
22{
23    // Could just be deref
24    pub fn get_operation(self) -> Operation<AccountId, Balance, Hash> {
25        match self {
26            Chain::Kusama(op) => op,
27            Chain::Polkadot(op) => op,
28            Chain::Karura(op) => op,
29            Chain::T3rn(op) => op,
30        }
31    }
32}
33
34// TODO: these aren't used anymore, create a ticket to reinstate the target id
35impl<AccountId, Balance, Hash> AsRef<[u8; MAX_SELECTION_NAME_LENGTH]>
36    for Chain<AccountId, Balance, Hash>
37where
38    Hash: Encode + Decode,
39    AccountId: Encode + Decode,
40    Balance: Encode + Decode,
41{
42    fn as_ref(&self) -> &[u8; MAX_SELECTION_NAME_LENGTH] {
43        match self {
44            Chain::Kusama(_) => b"kusm",
45            Chain::Polkadot(_) => b"polk",
46            Chain::Karura(_) => b"karu",
47            Chain::T3rn(_) => b"t3rn",
48        }
49    }
50}
51
52#[derive(Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, Debug)]
53pub enum Operation<AccountId, Balance, Hash>
54where
55    Hash: Encode + Decode,
56    AccountId: Encode + Decode,
57    Balance: Encode + Decode,
58{
59    Transfer {
60        caller: AccountId,
61        to: AccountId,
62        amount: Balance,
63    },
64    TransferMulti {
65        caller: AccountId,
66        to: AccountId,
67        amount: Balance,
68        asset: Hash,
69    },
70    AddLiquidity {
71        caller: AccountId,
72        to: AccountId,
73        asset_left: Hash,
74        asset_right: Hash,
75        liquidity_token: Hash,
76        amount_left: Balance,
77        amount_right: Balance,
78        amount_liquidity_token: Balance,
79    },
80    Swap {
81        caller: AccountId,
82        to: AccountId,
83        amount_from: Balance,
84        amount_to: Balance,
85        asset_from: Hash,
86        asset_to: Hash,
87    },
88    Call {
89        caller: AccountId,
90        call: VM,
91    },
92}
93
94// TODO[https://github.com/t3rn/3vm/issues/91]: Calls to different VMs, these are dummy for now.
95/// A representation of the type of a call that the VM can handle.
96#[derive(Encode, Decode, MaxEncodedLen, Clone, PartialEq, Eq, Debug)]
97pub enum VM {
98    Evm,
99    Wasm,
100    Pallet,
101    Volatile,
102    System,
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108    use scale_info::prelude::vec;
109
110    #[test]
111    fn transfer_selector_works() {
112        let caller = [5_u8; 2];
113        let to = [6_u8; 2];
114        let balance = 100_u32;
115        let selector = Chain::<_, _, u32>::Kusama(Operation::Transfer {
116            caller,
117            to,
118            amount: balance,
119        });
120        assert_eq!(
121            selector.clone().encode(),
122            [
123                // 107, 117, 115, 109, // chain
124                0, 0, 5, 5, 6, 6, 100, 0, 0, 0
125            ]
126        );
127    }
128
129    #[test]
130    fn transfer_multi_selector_works() {
131        let caller = [5_u8; 2];
132        let to = [6_u8; 2];
133        let asset = [7_u8; 2];
134        let balance = 100_u32;
135        let selector = Chain::Kusama(Operation::TransferMulti {
136            caller,
137            to,
138            amount: balance,
139            asset,
140        });
141        assert_eq!(
142            selector.encode(),
143            [
144                // 107, 117, 115, 109, // chain
145                0, 1, 5, 5, 6, 6, 100, 0, 0, 0, 7, 7
146            ]
147        );
148    }
149
150    #[test]
151    fn add_liquidity_selector_works() {
152        let caller = [5_u8; 2];
153        let to = [6_u8; 2];
154        let asset_left = [7_u8; 2];
155        let asset_right = [8_u8; 2];
156        let liquidity_token = [9_u8; 2];
157        let amount_left = 100_u32;
158        let amount_right = 200_u32;
159        let amount_liquidity_token = 300_u32;
160        let selector = Chain::Kusama(Operation::AddLiquidity {
161            caller,
162            to,
163            asset_left,
164            asset_right,
165            liquidity_token,
166            amount_left,
167            amount_right,
168            amount_liquidity_token,
169        });
170
171        assert_eq!(
172            selector.encode(),
173            vec![
174                // 4, 107, 117, 115, 109, // chain
175                0, 2, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 100, 0, 0, 0, 200, 0, 0, 0, 44, 1, 0, 0
176            ]
177        );
178    }
179
180    #[test]
181    fn test_swap_selector_works() {
182        let caller = [5_u8; 2];
183        let to = [6_u8; 2];
184        let asset_from = [7_u8; 2];
185        let asset_to = [8_u8; 2];
186        let amount_from = 100_u32;
187        let amount_to = 200_u32;
188
189        let selector = Chain::Kusama(Operation::Swap {
190            caller,
191            to,
192            amount_from,
193            amount_to,
194            asset_from,
195            asset_to,
196        });
197        assert_eq!(
198            selector.encode(),
199            vec![
200                // 4, 107, 117, 115, 109, // chain
201                0, 3, 5, 5, 6, 6, 100, 0, 0, 0, 200, 0, 0, 0, 7, 7, 8, 8
202            ]
203        );
204    }
205
206    #[test]
207    fn test_evm_call_works() {
208        let call = VM::Evm;
209        let caller = [5_u8; 2];
210        let selector = Chain::<_, u32, u32>::Kusama(Operation::Call { caller, call });
211        assert_eq!(
212            selector.encode(),
213            vec![
214                // 4, 107, 117, 115, 109, // Chain
215                0, 4, 5, 5, 0
216            ]
217        );
218    }
219}