Skip to content

Commit

Permalink
adjust intent generator to not use spot price
Browse files Browse the repository at this point in the history
  • Loading branch information
enthusiastmartin committed Nov 1, 2024
1 parent be39307 commit 2022311
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
29 changes: 22 additions & 7 deletions integration-tests/src/ice/generator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use hydra_dx_math::omnipool::types::AssetReserveState;
use pallet_ice::traits::{OmnipoolAssetInfo, OmnipoolInfo};
use pallet_ice::types::{Intent, Swap, SwapType};
use primitives::{AccountId, AssetId, Moment};
use primitives::{AccountId, AssetId, Balance, Moment};
use rand::Rng;
use sp_core::crypto::AccountId32;
use sp_runtime::{FixedPointNumber, FixedU128};

fn to_asset_reserve_state(s: &OmnipoolAssetInfo<AssetId>) -> AssetReserveState<Balance> {
AssetReserveState {
reserve: s.reserve,
hub_reserve: s.hub_reserve,
..Default::default()
}
}

pub(crate) fn generate_random_intents(
c: u32,
data: Vec<OmnipoolAssetInfo<AssetId>>,
Expand All @@ -18,12 +27,18 @@ pub(crate) fn generate_random_intents(
let reserve_in = data[idx_in].reserve;
let reserve_out = data[idx_out].reserve;
let amount_in = rng.gen_range(1..reserve_in / 4);
let lrna_in = FixedU128::from_rational(amount_in, reserve_in)
.checked_mul_int(data[idx_in].hub_reserve)
.unwrap();
let amount_out = FixedU128::from_rational(reserve_out, data[idx_out].hub_reserve)
.checked_mul_int(lrna_in)
.unwrap();

let s_in = to_asset_reserve_state(&data[idx_in]);
let s_out = to_asset_reserve_state(&data[idx_out]);
let r = hydra_dx_math::omnipool::calculate_sell_state_changes(
&s_in,
&s_out,
amount_in,
data[idx_out].fee,
data[idx_in].hub_fee,
0,
);
let amount_out = *r.unwrap().asset_out.delta_reserve;
return (data[idx_in].asset_id, data[idx_out].asset_id, amount_in, amount_out);
}
};
Expand Down
76 changes: 74 additions & 2 deletions integration-tests/src/ice/omni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn execute_solution_should_work_with_multiple_intents() {
hydra_live_ext(PATH_TO_SNAPSHOT).execute_with(|| {
let deadline: Moment = Timestamp::now() + 43_200_000;
let intents = generate_random_intents(
2,
3,
OmnipoolDataProvider::<hydradx_runtime::Runtime>::assets(None),
deadline,
);
Expand All @@ -192,7 +192,6 @@ fn execute_solution_should_work_with_multiple_intents() {
));
}
let intents = submit_intents(intents);
dbg!(&intents);
let resolved = solve_intents_with::<OmniSolverWithOmnipool>(intents).unwrap();
dbg!(&resolved);

Expand Down Expand Up @@ -240,3 +239,76 @@ fn solve_should_not_return_solution_when_intent_at_exact_spot_price() {
assert_eq!(resolved.len(), 0);
});
}

#[test]
fn execute_solution_should_work_when_transfer_are_below_existential_deposit() {
hydra_live_ext(PATH_TO_SNAPSHOT).execute_with(|| {
let deadline: Moment = Timestamp::now() + 43_200_000;
let intents: Vec<Intent<AccountId, AssetId>> = vec![
Intent {
who: ALICE.into(),
swap: Swap {
asset_in: 5,
asset_out: 8,
amount_in: 4821630410495467,
amount_out: 4300252617313999658,
swap_type: SwapType::ExactIn,
},
deadline: 43200000,
partial: true,
on_success: None,
on_failure: None,
},
Intent {
who: BOB.into(),
swap: Swap {
asset_in: 100,
asset_out: 14,
amount_in: 81565235644454869738270,
amount_out: 380462588393307031,
swap_type: SwapType::ExactIn,
},
deadline: 43200000,
partial: true,
on_success: None,
on_failure: None,
},
Intent {
who: CHARLIE.into(),
swap: Swap {
asset_in: 31,
asset_out: 5,
amount_in: 2503466695997857626345467,
amount_out: 9807415192088,
swap_type: SwapType::ExactIn,
},
deadline: 43200000,
partial: true,
on_success: None,
on_failure: None,
},
];
for intent in intents.iter() {
assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
intent.who.clone().into(),
intent.swap.asset_in,
intent.swap.amount_in as i128,
));
}
let intents = submit_intents(intents);
let resolved = solve_intents_with::<OmniSolverWithOmnipool>(intents).unwrap();
dbg!(&resolved);

let (trades, score) =
pallet_ice::Pallet::<hydradx_runtime::Runtime>::calculate_trades_and_score(&resolved.to_vec()).unwrap();

assert_ok!(ICE::submit_solution(
RuntimeOrigin::signed(BOB.into()),
resolved,
BoundedTrades::try_from(trades).unwrap(),
score,
System::current_block_number()
));
});
}

0 comments on commit 2022311

Please sign in to comment.