|
| 1 | +use primitives::Transaction; |
| 2 | + |
1 | 3 | use crate::SwapperProvider; |
2 | 4 | use crate::across::AcrossCrossChain; |
3 | 5 | use crate::thorchain::ThorchainCrossChain; |
4 | | -use primitives::Chain; |
5 | 6 |
|
6 | 7 | pub trait CrossChainProvider: Send + Sync { |
7 | 8 | fn provider(&self) -> SwapperProvider; |
8 | | - fn is_swap(&self, chain: &Chain, to_address: Option<&str>, memo: Option<&str>) -> bool; |
| 9 | + fn is_swap(&self, transaction: &Transaction) -> bool; |
9 | 10 | } |
10 | 11 |
|
11 | 12 | const PROVIDERS: [&dyn CrossChainProvider; 2] = [&ThorchainCrossChain, &AcrossCrossChain]; |
12 | 13 |
|
13 | | -pub fn swap_provider(chain: &Chain, to_address: Option<&str>, memo: Option<&str>) -> Option<SwapperProvider> { |
14 | | - PROVIDERS.iter().find(|p| p.is_swap(chain, to_address, memo)).map(|p| p.provider()) |
| 14 | +pub fn swap_provider(transaction: &Transaction) -> Option<SwapperProvider> { |
| 15 | + PROVIDERS.iter().find(|p| p.is_swap(transaction)).map(|p| p.provider()) |
15 | 16 | } |
16 | 17 |
|
17 | | -pub fn is_cross_chain_swap(chain: &Chain, to_address: &str, memo: Option<&str>) -> bool { |
18 | | - swap_provider(chain, Some(to_address), memo).is_some() |
| 18 | +pub fn is_cross_chain_swap(transaction: &Transaction) -> bool { |
| 19 | + swap_provider(transaction).is_some() |
19 | 20 | } |
20 | 21 |
|
21 | 22 | #[cfg(test)] |
22 | 23 | mod tests { |
23 | 24 | use super::*; |
| 25 | + use primitives::Chain; |
24 | 26 |
|
25 | 27 | #[test] |
26 | 28 | fn test_thorchain_swap_detected() { |
27 | | - let memo = "=:ETH.USDT:0x858734a6353C9921a78fB3c937c8E20Ba6f36902:1635978e6/1/0"; |
28 | | - assert_eq!( |
29 | | - swap_provider(&Chain::Ethereum, Some("0x0000000000000000000000000000000000000000"), Some(memo)), |
30 | | - Some(SwapperProvider::Thorchain), |
31 | | - ); |
| 29 | + let transaction = Transaction { |
| 30 | + memo: Some("=:ETH.USDT:0x858734a6353C9921a78fB3c937c8E20Ba6f36902:1635978e6/1/0".to_string()), |
| 31 | + ..Transaction::mock() |
| 32 | + }; |
| 33 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Thorchain)); |
32 | 34 | } |
33 | 35 |
|
34 | 36 | #[test] |
35 | 37 | fn test_thorchain_non_swap_memo() { |
36 | | - assert!(!is_cross_chain_swap( |
37 | | - &Chain::Ethereum, |
38 | | - "0x0000000000000000000000000000000000000000", |
39 | | - Some("ADD:ETH.ETH:0x123"), |
40 | | - )); |
| 38 | + assert!(!is_cross_chain_swap(&Transaction { |
| 39 | + memo: Some("ADD:ETH.ETH:0x123".to_string()), |
| 40 | + ..Transaction::mock() |
| 41 | + })); |
41 | 42 | } |
42 | 43 |
|
43 | 44 | #[test] |
44 | 45 | fn test_no_memo() { |
45 | | - assert!(swap_provider(&Chain::Ethereum, Some("0x0000000000000000000000000000000000000000"), None).is_none()); |
| 46 | + assert!(swap_provider(&Transaction::mock()).is_none()); |
46 | 47 | } |
47 | 48 |
|
48 | 49 | #[test] |
49 | 50 | fn test_across_swap_detected() { |
50 | | - assert_eq!( |
51 | | - swap_provider(&Chain::Ethereum, Some("0x5c7BCd6E7De5423a257D81B442095A1a6ced35C5"), None), |
52 | | - Some(SwapperProvider::Across), |
53 | | - ); |
| 51 | + let transaction = Transaction { |
| 52 | + to: "0x5c7BCd6E7De5423a257D81B442095A1a6ced35C5".to_string(), |
| 53 | + ..Transaction::mock() |
| 54 | + }; |
| 55 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Across)); |
54 | 56 | } |
55 | 57 |
|
56 | 58 | #[test] |
57 | 59 | fn test_across_swap_case_insensitive() { |
58 | | - assert!(is_cross_chain_swap(&Chain::Ethereum, "0x5c7bcd6e7de5423a257d81b442095a1a6ced35c5", None)); |
| 60 | + let transaction = Transaction { |
| 61 | + to: "0x5c7bcd6e7de5423a257d81b442095a1a6ced35c5".to_string(), |
| 62 | + ..Transaction::mock() |
| 63 | + }; |
| 64 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Across)); |
59 | 65 | } |
60 | 66 |
|
61 | 67 | #[test] |
62 | 68 | fn test_across_unsupported_chain() { |
63 | | - assert!(!is_cross_chain_swap(&Chain::Fantom, "0x5c7BCd6E7De5423a257D81B442095A1a6ced35C5", None)); |
| 69 | + let transaction = Transaction { |
| 70 | + asset_id: Chain::Fantom.as_asset_id(), |
| 71 | + to: "0x5c7BCd6E7De5423a257D81B442095A1a6ced35C5".to_string(), |
| 72 | + ..Transaction::mock() |
| 73 | + }; |
| 74 | + assert!(swap_provider(&transaction).is_none()); |
64 | 75 | } |
65 | 76 |
|
66 | 77 | #[test] |
67 | 78 | fn test_across_arbitrum() { |
68 | | - assert_eq!( |
69 | | - swap_provider(&Chain::Arbitrum, Some("0xe35e9842fceaca96570b734083f4a58e8f7c5f2a"), None), |
70 | | - Some(SwapperProvider::Across), |
71 | | - ); |
| 79 | + let transaction = Transaction { |
| 80 | + asset_id: Chain::Arbitrum.as_asset_id(), |
| 81 | + to: "0xe35e9842fceaca96570b734083f4a58e8f7c5f2a".to_string(), |
| 82 | + ..Transaction::mock() |
| 83 | + }; |
| 84 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Across)); |
72 | 85 | } |
73 | 86 |
|
74 | 87 | #[test] |
75 | 88 | fn test_thorchain_takes_priority_over_across() { |
76 | | - let memo = "=:ETH.USDT:0x858734a6353C9921a78fB3c937c8E20Ba6f36902:1635978e6/1/0"; |
77 | | - assert_eq!( |
78 | | - swap_provider(&Chain::Ethereum, Some("0x5c7BCd6E7De5423a257D81B442095A1a6ced35C5"), Some(memo)), |
79 | | - Some(SwapperProvider::Thorchain), |
80 | | - ); |
| 89 | + let transaction = Transaction { |
| 90 | + to: "0x5c7BCd6E7De5423a257D81B442095A1a6ced35C5".to_string(), |
| 91 | + memo: Some("=:ETH.USDT:0x858734a6353C9921a78fB3c937c8E20Ba6f36902:1635978e6/1/0".to_string()), |
| 92 | + ..Transaction::mock() |
| 93 | + }; |
| 94 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Thorchain)); |
81 | 95 | } |
82 | 96 |
|
83 | 97 | #[test] |
84 | 98 | fn test_non_evm_chains_no_panic() { |
85 | | - assert!(!is_cross_chain_swap(&Chain::Bitcoin, "bc1qaddress", None)); |
86 | | - assert!(!is_cross_chain_swap(&Chain::Solana, "So1111111111111111111111111111111111111111", None)); |
87 | | - assert!(!is_cross_chain_swap(&Chain::Ton, "EQAddress", None)); |
88 | | - assert!(!is_cross_chain_swap(&Chain::Cosmos, "cosmos1address", None)); |
89 | | - assert!(!is_cross_chain_swap(&Chain::Sui, "0xaddress", None)); |
| 99 | + let btc = Transaction { |
| 100 | + asset_id: Chain::Bitcoin.as_asset_id(), |
| 101 | + to: "bc1qaddress".to_string(), |
| 102 | + ..Transaction::mock() |
| 103 | + }; |
| 104 | + let sol = Transaction { |
| 105 | + asset_id: Chain::Solana.as_asset_id(), |
| 106 | + to: "So1111111111111111111111111111111111111111".to_string(), |
| 107 | + ..Transaction::mock() |
| 108 | + }; |
| 109 | + assert!(swap_provider(&btc).is_none()); |
| 110 | + assert!(swap_provider(&sol).is_none()); |
90 | 111 | } |
91 | 112 |
|
92 | 113 | #[test] |
93 | 114 | fn test_thorchain_swap_no_to_address() { |
94 | | - let memo = "=:s:0xBA4D1d35bCe0e8F28E5a3403e7a0b996c5d50AC4:0/1/0:g1:50"; |
95 | | - assert_eq!(swap_provider(&Chain::Litecoin, None, Some(memo)), Some(SwapperProvider::Thorchain)); |
| 115 | + let transaction = Transaction { |
| 116 | + asset_id: Chain::Litecoin.as_asset_id(), |
| 117 | + to: String::new(), |
| 118 | + memo: Some("=:s:0xBA4D1d35bCe0e8F28E5a3403e7a0b996c5d50AC4:0/1/0:g1:50".to_string()), |
| 119 | + ..Transaction::mock() |
| 120 | + }; |
| 121 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Thorchain)); |
96 | 122 | } |
97 | 123 |
|
98 | 124 | #[test] |
99 | 125 | fn test_thorchain_evm_router_detected() { |
100 | | - assert_eq!( |
101 | | - swap_provider(&Chain::Ethereum, Some("0xD37BbE5744D730a1d98d8DC97c42F0Ca46aD7146"), None), |
102 | | - Some(SwapperProvider::Thorchain), |
103 | | - ); |
| 126 | + let transaction = Transaction { |
| 127 | + to: "0xD37BbE5744D730a1d98d8DC97c42F0Ca46aD7146".to_string(), |
| 128 | + ..Transaction::mock() |
| 129 | + }; |
| 130 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Thorchain)); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_thorchain_evm_data_detected() { |
| 135 | + let data = "0x3d3a623a626331713965797870616730777875386a74756b7a747a6b636876637a65793039616134397632326c353a302f312f303a67313a3530"; |
| 136 | + let transaction = Transaction { |
| 137 | + to: "0xdfb89f7b854b79fdac99ddeb55921349ca649def".to_string(), |
| 138 | + data: Some(data.to_string()), |
| 139 | + ..Transaction::mock() |
| 140 | + }; |
| 141 | + assert_eq!(swap_provider(&transaction), Some(SwapperProvider::Thorchain)); |
104 | 142 | } |
105 | 143 | } |
0 commit comments