1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::Config;
use common::{balance, AssetInfoProvider, Balance, PriceVariant};
use frame_support::pallet_prelude::*;
use frame_support::sp_runtime::traits::Zero;
use frame_support::traits::Time;
use frame_system::pallet_prelude::*;
use order_book::DataLayer;
use order_book::{MomentOf, OrderBook, OrderBookId};
use order_book::{OrderPrice, OrderVolume};
use sp_std::prelude::*;

#[derive(Encode, Decode, Clone, PartialEq, Eq, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct OrderBookFillSettings<Moment> {
    /// Best (highest) price for placed buy orders
    pub best_bid_price: order_book::types::OrderPrice,
    /// Best (lowest) price for placed sell orders
    pub best_ask_price: order_book::types::OrderPrice,
    /// Lifespan of inserted orders, max by default
    pub lifespan: Option<Moment>,
}

/// Does not create an order book if it already exists
pub fn create_multiple_empty_unchecked<T: Config>(
    who: &T::AccountId,
    order_book_ids: Vec<OrderBookId<T::AssetId, T::DEXId>>,
) -> Result<(), DispatchError> {
    let to_create_ids: Vec<_> = order_book_ids
        .into_iter()
        .filter(|id| !<order_book::OrderBooks<T>>::contains_key(id))
        .collect();
    for order_book_id in &to_create_ids {
        if !trading_pair::Pallet::<T>::is_trading_pair_enabled(
            &order_book_id.dex_id,
            &order_book_id.quote.into(),
            &order_book_id.base.into(),
        )? {
            trading_pair::Pallet::<T>::register_pair(
                order_book_id.dex_id,
                order_book_id.quote.into(),
                order_book_id.base.into(),
            )?;
        }
        if <T as Config>::AssetInfoProvider::is_non_divisible(&order_book_id.base)
            && <T as Config>::AssetInfoProvider::total_balance(&order_book_id.base, &who)?
                == Balance::zero()
        {
            assets::Pallet::<T>::mint_unchecked(&order_book_id.base, &who, 1)?;
        }
        order_book::Pallet::<T>::verify_create_orderbook_params(who, &order_book_id)?;
    }

    for order_book_id in to_create_ids {
        order_book::Pallet::<T>::create_orderbook_unchecked(&order_book_id)?;
    }
    Ok(())
}

/// Place orders into the orderbook.
///
/// In fill settings, `best_bid_price` should be at least 3 price steps from the
/// lowest accepted price, and `best_ask_price` - at least 3 steps below
/// maximum price.
pub fn fill_multiple_empty_unchecked<T: Config>(
    bids_owner: T::AccountId,
    asks_owner: T::AccountId,
    fill_settings: Vec<(
        OrderBookId<T::AssetId, T::DEXId>,
        OrderBookFillSettings<MomentOf<T>>,
    )>,
) -> Result<(), DispatchError> {
    let now = <T as order_book::Config>::Time::now();

    // Prices are specified as price steps from the specified best ask price.
    // Amounts are added to min_lot and aligned with lot(amount) step.

    // (price_steps_from_best_bid, amount)
    let buy_orders_steps = [
        (0, OrderVolume::divisible(balance!(168.5))),
        (1, OrderVolume::divisible(balance!(95.2))),
        (1, OrderVolume::divisible(balance!(44.7))),
        (3, OrderVolume::divisible(balance!(56.4))),
        (3, OrderVolume::divisible(balance!(89.9))),
        (3, OrderVolume::divisible(balance!(115))),
    ];

    // (price_steps_from_best_ask, amount)
    let sell_orders_steps = [
        (0, OrderVolume::divisible(balance!(176.3))),
        (1, OrderVolume::divisible(balance!(85.4))),
        (1, OrderVolume::divisible(balance!(93.2))),
        (3, OrderVolume::divisible(balance!(36.6))),
        (3, OrderVolume::divisible(balance!(205.5))),
        (3, OrderVolume::divisible(balance!(13.7))),
    ];

    let mut data = order_book::cache_data_layer::CacheDataLayer::<T>::new();

    for (order_book_id, settings) in fill_settings {
        fill_order_book(
            &mut data,
            order_book_id,
            asks_owner.clone(),
            bids_owner.clone(),
            buy_orders_steps.into_iter(),
            sell_orders_steps.into_iter(),
            settings,
            now,
        )?;
    }
    data.commit();
    Ok(())
}

/// Fill a single order book.
fn fill_order_book<T: Config>(
    data: &mut impl DataLayer<T>,
    book_id: OrderBookId<T::AssetId, T::DEXId>,
    asks_owner: T::AccountId,
    bids_owner: T::AccountId,
    buy_orders_steps: impl Iterator<Item = (u128, OrderVolume)>,
    sell_orders_steps: impl Iterator<Item = (u128, OrderVolume)>,
    settings: OrderBookFillSettings<MomentOf<T>>,
    now: MomentOf<T>,
) -> Result<(), DispatchError> {
    let current_block = frame_system::Pallet::<T>::block_number();
    let lifespan = settings
        .lifespan
        .unwrap_or(<T as order_book::Config>::MAX_ORDER_LIFESPAN);
    let mut order_book = <order_book::OrderBooks<T>>::get(book_id)
        .ok_or(crate::Error::<T>::CannotFillUnknownOrderBook)?;

    // Convert price steps and best price to actual prices
    let buy_orders: Vec<_> = buy_orders_steps
        .map(|(price_steps, base)| {
            (
                settings.best_bid_price
                    - OrderPrice::divisible(price_steps * (*order_book.tick_size.balance())),
                order_book.align_amount(base + order_book.step_lot_size),
            )
        })
        .collect();
    let sell_orders: Vec<_> = sell_orders_steps
        .map(|(price_steps, base)| {
            (
                settings.best_ask_price
                    + OrderPrice::divisible(price_steps * (*order_book.tick_size.balance())),
                order_book.align_amount(base + order_book.step_lot_size),
            )
        })
        .collect();
    // Total amount of quote asset to be locked from `bids_owner`
    let buy_quote_locked: Balance = buy_orders
        .iter()
        .map(|(quote, base)| *(*quote * (*base)).balance())
        .sum();
    let sell_base_locked: Balance = sell_orders.iter().map(|(_, base)| *base.balance()).sum();

    // mint required amount to make this extrinsic self-sufficient
    assets::Pallet::<T>::mint_unchecked(&book_id.quote, &bids_owner, buy_quote_locked)?;
    assets::Pallet::<T>::mint_unchecked(&book_id.base, &asks_owner, sell_base_locked)?;

    // place buy orders
    place_multiple_orders(
        data,
        &mut order_book,
        bids_owner.clone(),
        PriceVariant::Buy,
        buy_orders.into_iter(),
        now,
        lifespan,
        current_block,
    )?;

    // place sell orders
    place_multiple_orders(
        data,
        &mut order_book,
        asks_owner.clone(),
        PriceVariant::Sell,
        sell_orders.into_iter(),
        now,
        lifespan,
        current_block,
    )?;

    <order_book::OrderBooks<T>>::set(book_id, Some(order_book));
    Ok(())
}

fn place_multiple_orders<T: Config>(
    data: &mut impl DataLayer<T>,
    book: &mut OrderBook<T>,
    owner: T::AccountId,
    side: PriceVariant,
    orders: impl Iterator<Item = (OrderPrice, OrderVolume)>,
    time: MomentOf<T>,
    lifespan: MomentOf<T>,
    current_block: BlockNumberFor<T>,
) -> Result<(), DispatchError> {
    for (price, amount) in orders {
        let order_id = book.next_order_id();
        let order = order_book::LimitOrder::<T>::new(
            order_id,
            owner.clone(),
            side,
            price,
            amount,
            time,
            lifespan,
            current_block,
        );
        book.place_limit_order(order, data)?;
    }
    Ok(())
}