Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix of Hybrid Router after asset system merge #1309

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 8 additions & 27 deletions primitives/src/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ impl<AI, BA, BN, M, A> Market<AI, BA, BN, M, A> {

for i in 0..categories {
match self.scoring_rule {
ScoringRule::Orderbook => {
assets.push(MarketAssetClass::<MI>::CategoricalOutcome(market_id, i))
}
ScoringRule::Lmsr => {
ScoringRule::AmmCdaHybrid => {
assets.push(MarketAssetClass::<MI>::CategoricalOutcome(market_id, i))
}
ScoringRule::Parimutuel => {
Expand Down Expand Up @@ -182,10 +179,7 @@ impl<AI, BA, BN, M, A> Market<AI, BA, BN, M, A> {
) -> Option<MarketAssetClass<MI>> {
match outcome_report {
OutcomeReport::Categorical(idx) => match self.scoring_rule {
ScoringRule::Orderbook => {
Some(MarketAssetClass::<MI>::CategoricalOutcome(market_id, *idx))
}
ScoringRule::Lmsr => {
ScoringRule::AmmCdaHybrid => {
sea212 marked this conversation as resolved.
Show resolved Hide resolved
Some(MarketAssetClass::<MI>::CategoricalOutcome(market_id, *idx))
}
ScoringRule::Parimutuel => {
Expand Down Expand Up @@ -521,15 +515,9 @@ mod tests {

#[test_case(
MarketType::Categorical(2),
ScoringRule::Lmsr,
ScoringRule::AmmCdaHybrid,
vec![MarketAsset::CategoricalOutcome(0, 0), MarketAsset::CategoricalOutcome(0, 1)];
"categorical_market_lmsr"
)]
#[test_case(
MarketType::Categorical(2),
ScoringRule::Orderbook,
vec![MarketAsset::CategoricalOutcome(0, 0), MarketAsset::CategoricalOutcome(0, 1)];
"categorical_market_orderbook"
"categorical_market_amm_cda_hybrid"
)]
#[test_case(
MarketType::Categorical(2),
Expand All @@ -539,7 +527,7 @@ mod tests {
)]
#[test_case(
MarketType::Scalar(12..=34),
ScoringRule::Lmsr,
ScoringRule::AmmCdaHybrid,
vec![MarketAsset::ScalarOutcome(0, ScalarPosition::Long), MarketAsset::ScalarOutcome(0, ScalarPosition::Short)];
"scalar_market"
)]
Expand Down Expand Up @@ -575,17 +563,10 @@ mod tests {

#[test_case(
MarketType::Categorical(2),
ScoringRule::Lmsr,
OutcomeReport::Categorical(2),
Some(MarketAsset::CategoricalOutcome(0, 2));
"categorical_market_lmsr"
)]
#[test_case(
MarketType::Categorical(2),
ScoringRule::Orderbook,
ScoringRule::AmmCdaHybrid,
OutcomeReport::Categorical(2),
Some(MarketAsset::CategoricalOutcome(0, 2));
"categorical_market_orderbook"
"categorical_market_amm_cda_hybrid"
)]
#[test_case(
MarketType::Categorical(2),
Expand All @@ -596,7 +577,7 @@ mod tests {
)]
#[test_case(
MarketType::Scalar(12..=34),
ScoringRule::Lmsr,
ScoringRule::AmmCdaHybrid,
OutcomeReport::Scalar(2),
None;
"scalar_market"
Expand Down
7 changes: 7 additions & 0 deletions zrml/hybrid-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ zrml-market-commons = { workspace = true }
orml-asset-registry = { workspace = true, optional = true }
orml-currencies = { workspace = true, optional = true }
orml-tokens = { workspace = true, optional = true }
pallet-assets = { workspace = true, optional = true }
pallet-balances = { workspace = true, optional = true }
pallet-randomness-collective-flip = { workspace = true, optional = true }
pallet-timestamp = { workspace = true, optional = true }
Expand All @@ -22,6 +23,7 @@ serde = { workspace = true, optional = true }
sp-io = { workspace = true, optional = true }
xcm = { workspace = true, optional = true }
xcm-builder = { workspace = true, optional = true }
zrml-asset-router = { workspace = true, optional = true }
zrml-authorized = { workspace = true, optional = true }
zrml-court = { workspace = true, optional = true }
zrml-global-disputes = { workspace = true, optional = true }
Expand All @@ -42,6 +44,7 @@ mock = [
"orml-asset-registry/default",
"orml-currencies/default",
"orml-tokens/default",
"pallet-assets/default",
"pallet-balances/default",
"pallet-randomness-collective-flip/default",
"pallet-timestamp/default",
Expand All @@ -51,6 +54,8 @@ mock = [
"sp-io/default",
"xcm/default",
"zeitgeist-primitives/mock",
"zrml-asset-router/default",
"zrml-asset-router/std",
sea212 marked this conversation as resolved.
Show resolved Hide resolved
"zrml-market-commons/default",
"zrml-neo-swaps/default",
"zrml-orderbook/default",
Expand All @@ -70,7 +75,9 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"pallet-assets?/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"zrml-prediction-markets/runtime-benchmarks",
]
std = [
"frame-benchmarking?/std",
Expand Down
35 changes: 21 additions & 14 deletions zrml/hybrid-router/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ use zeitgeist_primitives::{
constants::{base_multiples::*, CENT},
math::fixed::{BaseProvider, FixedDiv, ZeitgeistBase},
traits::{CompleteSetOperationsApi, DeployPoolApi, HybridRouterOrderbookApi},
types::{Asset, Market, MarketCreation, MarketPeriod, MarketStatus, MarketType, ScoringRule},
types::{
Asset, BaseAssetClass, Market, MarketCreation, MarketPeriod, MarketStatus, MarketType,
ScoringRule,
},
};
use zrml_market_commons::MarketCommonsPalletApi;

Expand All @@ -65,7 +68,11 @@ fn create_spot_prices<T: Config>(asset_count: u16) -> Vec<BalanceOf<T>> {
amounts
}

fn create_market<T>(caller: T::AccountId, base_asset: AssetOf<T>, asset_count: u16) -> MarketIdOf<T>
fn create_market<T>(
caller: T::AccountId,
base_asset: BaseAssetClass,
asset_count: u16,
) -> MarketIdOf<T>
where
T: Config,
{
Expand Down Expand Up @@ -93,16 +100,16 @@ where

fn create_market_and_deploy_pool<T: Config>(
caller: T::AccountId,
base_asset: AssetOf<T>,
base_asset: BaseAssetClass,
asset_count: u16,
amount: BalanceOf<T>,
) -> MarketIdOf<T>
where
T: Config,
{
let market_id = create_market::<T>(caller.clone(), base_asset, asset_count);
let total_cost = amount + T::AssetManager::minimum_balance(base_asset);
assert_ok!(T::AssetManager::deposit(base_asset, &caller, total_cost));
let total_cost = amount + T::AssetManager::minimum_balance(base_asset.into());
assert_ok!(T::AssetManager::deposit(base_asset.into(), &caller, total_cost));
assert_ok_with_transaction!(T::CompleteSetOperations::buy_complete_set(
caller.clone(),
market_id,
Expand All @@ -125,7 +132,7 @@ mod benchmarks {
#[benchmark]
fn buy(n: Linear<2, 16>, o: Linear<0, 10>) {
let buyer: T::AccountId = whitelisted_caller();
let base_asset = Asset::Ztg;
let base_asset = BaseAssetClass::Ztg;
let asset_count = n.try_into().unwrap();
let market_id = create_market_and_deploy_pool::<T>(
buyer.clone(),
Expand All @@ -136,7 +143,7 @@ mod benchmarks {

let asset = Asset::CategoricalOutcome(market_id, 0u16);
let amount_in = _1000.saturated_into();
assert_ok!(T::AssetManager::deposit(base_asset, &buyer, amount_in));
assert_ok!(T::AssetManager::deposit(base_asset.into(), &buyer, amount_in));

let spot_prices = create_spot_prices::<T>(asset_count);
let first_spot_price = spot_prices[0];
Expand All @@ -145,7 +152,7 @@ mod benchmarks {
let orders = (0u128..o as u128).collect::<Vec<_>>();
let maker_asset = asset;
let maker_amount = _20.saturated_into();
let taker_asset = base_asset;
let taker_asset: AssetOf<T> = base_asset.into();
let taker_amount: BalanceOf<T> = _11.saturated_into();
assert!(taker_amount.bdiv_floor(maker_amount).unwrap() > first_spot_price);
for (i, order_id) in orders.iter().enumerate() {
Expand Down Expand Up @@ -179,14 +186,14 @@ mod benchmarks {
let buyer_limit_order = T::OrderBook::order(o as u128).unwrap();
assert_eq!(buyer_limit_order.market_id, market_id);
assert_eq!(buyer_limit_order.maker, buyer);
assert_eq!(buyer_limit_order.maker_asset, base_asset);
assert_eq!(buyer_limit_order.maker_asset, base_asset.into());
assert_eq!(buyer_limit_order.taker_asset, asset);
}

#[benchmark]
fn sell(n: Linear<2, 10>, o: Linear<0, 10>) {
let seller: T::AccountId = whitelisted_caller();
let base_asset = Asset::Ztg;
let base_asset = BaseAssetClass::Ztg;
let asset_count = n.try_into().unwrap();
let market_id = create_market_and_deploy_pool::<T>(
seller.clone(),
Expand All @@ -201,15 +208,15 @@ mod benchmarks {
// seller base asset amount needs to exist,
// otherwise repatriate_reserved_named from order book fails
// with DeadAccount for base asset repatriate to seller beneficiary
let min_balance = T::AssetManager::minimum_balance(base_asset);
assert_ok!(T::AssetManager::deposit(base_asset, &seller, min_balance));
let min_balance = T::AssetManager::minimum_balance(base_asset.into());
assert_ok!(T::AssetManager::deposit(base_asset.into(), &seller, min_balance));

let spot_prices = create_spot_prices::<T>(asset_count);
let first_spot_price = spot_prices[0];

let min_price = _1_100.saturated_into();
let orders = (0u128..o as u128).collect::<Vec<_>>();
let maker_asset = base_asset;
let maker_asset: AssetOf<T> = base_asset.into();
let maker_amount: BalanceOf<T> = _9.saturated_into();
let taker_asset = asset;
let taker_amount = _100.saturated_into();
Expand Down Expand Up @@ -247,7 +254,7 @@ mod benchmarks {
assert_eq!(seller_limit_order.market_id, market_id);
assert_eq!(seller_limit_order.maker, seller);
assert_eq!(seller_limit_order.maker_asset, asset);
assert_eq!(seller_limit_order.taker_asset, base_asset);
assert_eq!(seller_limit_order.taker_asset, base_asset.into());
}

impl_benchmark_test_suite!(
Expand Down
18 changes: 9 additions & 9 deletions zrml/hybrid-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mod pallet {
},
orderbook::{Order, OrderId},
traits::{HybridRouterAmmApi, HybridRouterOrderbookApi},
types::{Asset, Market, MarketType, ScalarPosition},
types::{Asset, BaseAsset, BaseAssetClass, Market, MarketType, ScalarPosition},
sea212 marked this conversation as resolved.
Show resolved Hide resolved
};
use zrml_market_commons::MarketCommonsPalletApi;

Expand Down Expand Up @@ -140,7 +140,7 @@ mod pallet {
<<T as Config>::MarketCommons as MarketCommonsPalletApi>::MarketId;
pub(crate) type MomentOf<T> = <<T as Config>::MarketCommons as MarketCommonsPalletApi>::Moment;
pub(crate) type MarketOf<T> =
Market<AccountIdOf<T>, BalanceOf<T>, BlockNumberFor<T>, MomentOf<T>, Asset<MarketIdOf<T>>>;
Market<AccountIdOf<T>, BalanceOf<T>, BlockNumberFor<T>, MomentOf<T>, BaseAsset>;
pub(crate) type AmmTradeOf<T> = AmmTrade<BalanceOf<T>>;
pub(crate) type OrderTradeOf<T> = OrderbookTrade<AccountIdOf<T>, BalanceOf<T>>;

Expand Down Expand Up @@ -455,7 +455,7 @@ mod pallet {
mut remaining: BalanceOf<T>,
who: &AccountIdOf<T>,
market_id: MarketIdOf<T>,
base_asset: AssetOf<T>,
base_asset: BaseAssetClass,
sea212 marked this conversation as resolved.
Show resolved Hide resolved
asset: AssetOf<T>,
price_limit: BalanceOf<T>,
) -> Result<OrderAmmTradesInfo<T>, DispatchError> {
Expand All @@ -471,7 +471,7 @@ mod pallet {
Err(_) => continue,
};

let order_price = order.price(base_asset)?;
let order_price = order.price(base_asset.into())?;

match tx_type {
TxType::Buy => {
Expand Down Expand Up @@ -619,12 +619,12 @@ mod pallet {
);
ensure!(orders.len() as u32 <= T::MaxOrders::get(), Error::<T>::MaxOrdersExceeded);
let market = T::MarketCommons::market(&market_id)?;
let assets = Self::outcome_assets(market_id, &market);
let assets = market.outcome_assets(market_id);
ensure!(asset_count as usize == assets.len(), Error::<T>::AssetCountMismatch);

let (asset_in, asset_out) = match tx_type {
TxType::Buy => (market.base_asset, asset),
TxType::Sell => (asset, market.base_asset),
TxType::Buy => (market.base_asset.into(), asset),
TxType::Sell => (asset, market.base_asset.into()),
};
T::AssetManager::ensure_can_withdraw(asset_in, &who, amount_in)?;

Expand Down Expand Up @@ -665,7 +665,7 @@ mod pallet {
if !remaining.is_zero() {
let (maker_asset, maker_amount, taker_asset, taker_amount) = match tx_type {
TxType::Buy => {
let maker_asset = market.base_asset;
let maker_asset = market.base_asset.into();
let maker_amount = remaining;
let taker_asset = asset;
let taker_amount = remaining.bdiv_ceil(price_limit)?;
Expand All @@ -674,7 +674,7 @@ mod pallet {
TxType::Sell => {
let maker_asset = asset;
let maker_amount = remaining;
let taker_asset = market.base_asset;
let taker_asset = market.base_asset.into();
let taker_amount = price_limit.bmul_floor(remaining)?;
(maker_asset, maker_amount, taker_asset, taker_amount)
}
Expand Down
Loading
Loading