|
| 1 | +#![cfg(test)] |
| 2 | + |
| 3 | +use crate::polkadot_test_net::*; |
| 4 | +use frame_support::assert_ok; |
| 5 | +use hex_literal::hex; |
| 6 | +use hydradx_runtime::evm::uniswap_v3_trade_executor::UniswapV3; |
| 7 | +use hydradx_runtime::{AssetId, Currencies, Parameters, Router, Runtime, RuntimeEvent, RuntimeOrigin}; |
| 8 | +use hydradx_traits::router::{PoolType, Trade}; |
| 9 | +use orml_traits::MultiCurrency; |
| 10 | +use pallet_broadcast::types::Filler; |
| 11 | +use pallet_route_executor::TradeExecution; |
| 12 | +use primitives::{Balance, EvmAddress}; |
| 13 | +use sp_core::H160; |
| 14 | + |
| 15 | +pub const PATH_TO_SNAPSHOT: &str = "uniswap-snapshot/SNAPSHOT"; |
| 16 | + |
| 17 | +const UNISWAP_V3_FACTORY: EvmAddress = H160(hex!("0000000000000000000000000000000000000000")); |
| 18 | +const UNISWAP_V3_SWAP_ROUTER: EvmAddress = H160(hex!("0000000000000000000000000000000000000000")); |
| 19 | +const UNISWAP_V3_QUOTER: EvmAddress = H160(hex!("0000000000000000000000000000000000000000")); |
| 20 | + |
| 21 | +const ASSET_IN: AssetId = 0; |
| 22 | +const ASSET_OUT: AssetId = 20; |
| 23 | +const FEE_TIER: u32 = 3000; |
| 24 | +const SELL_AMOUNT: Balance = 1_000_000_000_000; |
| 25 | + |
| 26 | +fn with_uniswap_v3(execution: impl FnOnce()) { |
| 27 | + TestNet::reset(); |
| 28 | + hydra_live_ext(PATH_TO_SNAPSHOT).execute_with(|| { |
| 29 | + assert_ok!(Parameters::set_uniswap_v3_addresses( |
| 30 | + RuntimeOrigin::root(), |
| 31 | + UNISWAP_V3_FACTORY, |
| 32 | + UNISWAP_V3_SWAP_ROUTER, |
| 33 | + UNISWAP_V3_QUOTER, |
| 34 | + )); |
| 35 | + execution(); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +fn uniswap_route() -> Vec<Trade<AssetId>> { |
| 40 | + vec![Trade { |
| 41 | + pool: PoolType::UniswapV3(FEE_TIER), |
| 42 | + asset_in: ASSET_IN, |
| 43 | + asset_out: ASSET_OUT, |
| 44 | + }] |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +#[ignore = "requires uniswap-snapshot/SNAPSHOT and deployment constants; see uniswap-snapshot/README.md"] |
| 49 | +fn calculate_out_given_in_should_return_positive_quote_when_pool_has_liquidity() { |
| 50 | + with_uniswap_v3(|| { |
| 51 | + let amount_out = |
| 52 | + UniswapV3::calculate_out_given_in(PoolType::UniswapV3(FEE_TIER), ASSET_IN, ASSET_OUT, SELL_AMOUNT) |
| 53 | + .expect("quote should succeed"); |
| 54 | + assert!(amount_out > 0); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +#[ignore = "requires uniswap-snapshot/SNAPSHOT and deployment constants; see uniswap-snapshot/README.md"] |
| 60 | +fn calculate_in_given_out_should_exceed_output_when_pool_charges_fee() { |
| 61 | + with_uniswap_v3(|| { |
| 62 | + let amount_out = SELL_AMOUNT / 2; |
| 63 | + let amount_in = |
| 64 | + UniswapV3::calculate_in_given_out(PoolType::UniswapV3(FEE_TIER), ASSET_IN, ASSET_OUT, amount_out) |
| 65 | + .expect("quote should succeed"); |
| 66 | + assert!(amount_in > amount_out); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +#[ignore = "requires uniswap-snapshot/SNAPSHOT and deployment constants; see uniswap-snapshot/README.md"] |
| 72 | +fn router_sell_should_increase_output_balance_when_routed_through_uniswap_v3() { |
| 73 | + with_uniswap_v3(|| { |
| 74 | + let before = Currencies::free_balance(ASSET_OUT, &ALICE.into()); |
| 75 | + assert_ok!(Router::sell( |
| 76 | + RuntimeOrigin::signed(ALICE.into()), |
| 77 | + ASSET_IN, |
| 78 | + ASSET_OUT, |
| 79 | + SELL_AMOUNT, |
| 80 | + 0, |
| 81 | + uniswap_route().try_into().unwrap(), |
| 82 | + )); |
| 83 | + let after = Currencies::free_balance(ASSET_OUT, &ALICE.into()); |
| 84 | + assert!(after > before); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +#[test] |
| 89 | +#[ignore = "requires uniswap-snapshot/SNAPSHOT and deployment constants; see uniswap-snapshot/README.md"] |
| 90 | +fn router_buy_should_deliver_exact_output_when_routed_through_uniswap_v3() { |
| 91 | + with_uniswap_v3(|| { |
| 92 | + let buy_amount = SELL_AMOUNT / 2; |
| 93 | + let before = Currencies::free_balance(ASSET_OUT, &ALICE.into()); |
| 94 | + assert_ok!(Router::buy( |
| 95 | + RuntimeOrigin::signed(ALICE.into()), |
| 96 | + ASSET_IN, |
| 97 | + ASSET_OUT, |
| 98 | + buy_amount, |
| 99 | + u128::MAX, |
| 100 | + uniswap_route().try_into().unwrap(), |
| 101 | + )); |
| 102 | + let after = Currencies::free_balance(ASSET_OUT, &ALICE.into()); |
| 103 | + assert_eq!(after - before, buy_amount); |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +#[test] |
| 108 | +#[ignore = "requires uniswap-snapshot/SNAPSHOT and deployment constants; see uniswap-snapshot/README.md"] |
| 109 | +fn router_sell_should_emit_uniswap_v3_filler_event() { |
| 110 | + with_uniswap_v3(|| { |
| 111 | + assert_ok!(Router::sell( |
| 112 | + RuntimeOrigin::signed(ALICE.into()), |
| 113 | + ASSET_IN, |
| 114 | + ASSET_OUT, |
| 115 | + SELL_AMOUNT, |
| 116 | + 0, |
| 117 | + uniswap_route().try_into().unwrap(), |
| 118 | + )); |
| 119 | + let emitted = frame_system::Pallet::<Runtime>::events().into_iter().any(|record| { |
| 120 | + matches!( |
| 121 | + record.event, |
| 122 | + RuntimeEvent::Broadcast(pallet_broadcast::Event::Swapped3 { |
| 123 | + filler_type: Filler::UniswapV3, |
| 124 | + .. |
| 125 | + }) |
| 126 | + ) |
| 127 | + }); |
| 128 | + assert!(emitted); |
| 129 | + }); |
| 130 | +} |
0 commit comments