2023-11-05 20:02:34 +03:00
|
|
|
// This file was originally:
|
|
|
|
|
|
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
// It has been forked into a crate distributed under the AGPL 3.0.
|
|
|
|
|
// Please check the current distribution for up-to-date copyright and licensing information.
|
|
|
|
|
|
|
|
|
|
//! # Serai Dex pallet
|
|
|
|
|
//!
|
|
|
|
|
//! Serai Dex pallet based on the [Uniswap V2](https://github.com/Uniswap/v2-core) logic.
|
|
|
|
|
//!
|
|
|
|
|
//! ## Overview
|
|
|
|
|
//!
|
|
|
|
|
//! This pallet allows you to:
|
|
|
|
|
//!
|
|
|
|
|
//! - [create a liquidity pool](`Pallet::create_pool()`) for 2 coins
|
|
|
|
|
//! - [provide the liquidity](`Pallet::add_liquidity()`) and receive back an LP token
|
|
|
|
|
//! - [exchange the LP token back to coins](`Pallet::remove_liquidity()`)
|
|
|
|
|
//! - [swap a specific amount of coins for another](`Pallet::swap_exact_tokens_for_tokens()`) if
|
|
|
|
|
//! there is a pool created, or
|
|
|
|
|
//! - [swap some coins for a specific amount of
|
|
|
|
|
//! another](`Pallet::swap_tokens_for_exact_tokens()`).
|
|
|
|
|
//! - [query for an exchange price](`DexApi::quote_price_exact_tokens_for_tokens`) via
|
|
|
|
|
//! a runtime call endpoint
|
|
|
|
|
//! - [query the size of a liquidity pool](`DexApi::get_reserves`) via a runtime api
|
|
|
|
|
//! endpoint.
|
|
|
|
|
//!
|
|
|
|
|
//! The `quote_price_exact_tokens_for_tokens` and `quote_price_tokens_for_exact_tokens` functions
|
|
|
|
|
//! both take a path parameter of the route to take. If you want to swap from native coin to
|
|
|
|
|
//! non-native coin 1, you would pass in a path of `[DOT, 1]` or `[1, DOT]`. If you want to swap
|
|
|
|
|
//! from non-native coin 1 to non-native coin 2, you would pass in a path of `[1, DOT, 2]`.
|
|
|
|
|
//!
|
|
|
|
|
//! (For an example of configuring this pallet to use `MultiLocation` as an coin id, see the
|
|
|
|
|
//! cumulus repo).
|
|
|
|
|
//!
|
|
|
|
|
//! Here is an example `state_call` that asks for a quote of a pool of native versus coin 1:
|
|
|
|
|
//!
|
|
|
|
|
//! ```text
|
|
|
|
|
//! curl -sS -H "Content-Type: application/json" -d \
|
|
|
|
|
//! '{
|
|
|
|
|
//! "id": 1,
|
|
|
|
|
//! "jsonrpc": "2.0",
|
|
|
|
|
//! "method": "state_call",
|
|
|
|
|
//! "params": [
|
|
|
|
|
//! "DexApi_quote_price_tokens_for_exact_tokens",
|
|
|
|
|
//! "0x0101000000000000000000000011000000000000000000"
|
|
|
|
|
//! ]
|
|
|
|
|
//! }' \
|
|
|
|
|
//! http://localhost:9933/
|
|
|
|
|
//! ```
|
|
|
|
|
//! (This can be run against the kitchen sync node in the `node` folder of this repo.)
|
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
2023-11-12 14:37:31 +03:00
|
|
|
use frame_support::traits::DefensiveOption;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
#[cfg(feature = "runtime-benchmarks")]
|
|
|
|
|
mod benchmarking;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
mod types;
|
2023-11-05 20:02:34 +03:00
|
|
|
pub mod weights;
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod mock;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
use frame_support::ensure;
|
2023-11-05 20:02:34 +03:00
|
|
|
use frame_system::{
|
|
|
|
|
pallet_prelude::{BlockNumberFor, OriginFor},
|
2023-11-12 14:37:31 +03:00
|
|
|
ensure_signed,
|
2023-11-05 20:02:34 +03:00
|
|
|
};
|
2023-11-12 14:37:31 +03:00
|
|
|
|
2023-11-05 20:02:34 +03:00
|
|
|
pub use pallet::*;
|
2023-11-12 14:37:31 +03:00
|
|
|
|
|
|
|
|
use sp_runtime::{traits::TrailingZeroInput, DispatchError};
|
|
|
|
|
|
2023-12-05 16:52:50 +03:00
|
|
|
use serai_primitives::{NetworkId, Coin, SubstrateAmount};
|
2023-11-12 14:37:31 +03:00
|
|
|
|
2023-11-05 20:02:34 +03:00
|
|
|
use sp_std::prelude::*;
|
2023-11-12 14:37:31 +03:00
|
|
|
pub use types::*;
|
2023-11-05 20:02:34 +03:00
|
|
|
pub use weights::WeightInfo;
|
|
|
|
|
|
2023-12-17 00:01:41 -05:00
|
|
|
// TODO: Investigate why Substrate generates these
|
|
|
|
|
#[allow(clippy::cast_possible_truncation, clippy::no_effect_underscore_binding)]
|
2023-11-05 20:02:34 +03:00
|
|
|
#[frame_support::pallet]
|
|
|
|
|
pub mod pallet {
|
|
|
|
|
use super::*;
|
|
|
|
|
use frame_support::{pallet_prelude::*, BoundedBTreeSet};
|
2023-11-12 14:37:31 +03:00
|
|
|
|
|
|
|
|
use sp_core::sr25519::Public;
|
|
|
|
|
use sp_runtime::traits::IntegerSquareRoot;
|
|
|
|
|
|
|
|
|
|
use coins_pallet::{Pallet as CoinsPallet, Config as CoinsConfig};
|
|
|
|
|
|
2024-02-19 20:50:04 -05:00
|
|
|
use serai_primitives::{Coin, Amount, Balance, SubstrateAmount, reverse_lexicographic_order};
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
/// Pool ID.
|
|
|
|
|
///
|
|
|
|
|
/// The pool's `AccountId` is derived from this type. Any changes to the type may necessitate a
|
|
|
|
|
/// migration.
|
2023-11-12 14:37:31 +03:00
|
|
|
pub type PoolId = Coin;
|
|
|
|
|
|
|
|
|
|
/// LiquidityTokens Pallet as an instance of coins pallet.
|
|
|
|
|
pub type LiquidityTokens<T> = coins_pallet::Pallet<T, coins_pallet::Instance1>;
|
|
|
|
|
|
|
|
|
|
/// A type used for amount conversions.
|
|
|
|
|
pub type HigherPrecisionBalance = u128;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
#[pallet::pallet]
|
|
|
|
|
pub struct Pallet<T>(_);
|
|
|
|
|
|
|
|
|
|
#[pallet::config]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub trait Config:
|
|
|
|
|
frame_system::Config<AccountId = Public>
|
|
|
|
|
+ CoinsConfig
|
|
|
|
|
+ coins_pallet::Config<coins_pallet::Instance1>
|
|
|
|
|
{
|
2023-11-05 20:02:34 +03:00
|
|
|
/// Overarching event type.
|
|
|
|
|
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
|
|
|
|
|
|
|
|
|
/// A % the liquidity providers will take of every swap. Represents 10ths of a percent.
|
|
|
|
|
#[pallet::constant]
|
|
|
|
|
type LPFee: Get<u32>;
|
|
|
|
|
|
|
|
|
|
/// The minimum LP token amount that could be minted. Ameliorates rounding errors.
|
|
|
|
|
#[pallet::constant]
|
2023-11-12 14:37:31 +03:00
|
|
|
type MintMinLiquidity: Get<SubstrateAmount>;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
/// The max number of hops in a swap.
|
|
|
|
|
#[pallet::constant]
|
|
|
|
|
type MaxSwapPathLength: Get<u32>;
|
|
|
|
|
|
2024-02-19 20:50:04 -05:00
|
|
|
/// Last N number of blocks that oracle keeps track of the prices.
|
|
|
|
|
#[pallet::constant]
|
|
|
|
|
type MedianPriceWindowLength: Get<u16>;
|
|
|
|
|
|
2023-11-05 20:02:34 +03:00
|
|
|
/// Weight information for extrinsics in this pallet.
|
|
|
|
|
type WeightInfo: WeightInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 07:47:23 -05:00
|
|
|
/// Map from `PoolId` to `()`. This establishes whether a pool has been officially
|
2023-11-05 20:02:34 +03:00
|
|
|
/// created rather than people sending tokens directly to a pool's public account.
|
|
|
|
|
#[pallet::storage]
|
2023-12-11 07:47:23 -05:00
|
|
|
pub type Pools<T: Config> = StorageMap<_, Blake2_128Concat, PoolId, (), OptionQuery>;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-12-05 16:52:50 +03:00
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::getter(fn spot_price_for_block)]
|
|
|
|
|
pub type SpotPriceForBlock<T: Config> =
|
2024-02-19 20:50:04 -05:00
|
|
|
StorageDoubleMap<_, Identity, BlockNumberFor<T>, Identity, Coin, Amount, OptionQuery>;
|
2023-12-05 16:52:50 +03:00
|
|
|
|
2024-02-19 20:50:04 -05:00
|
|
|
/// Moving window of prices from each block.
|
2023-12-05 16:52:50 +03:00
|
|
|
///
|
2024-02-19 20:50:04 -05:00
|
|
|
/// The [u8; 8] key is the amount's big endian bytes, and u16 is the amount of inclusions in this
|
|
|
|
|
/// multi-set. Since the underlying map is lexicographically sorted, this map stores amounts from
|
|
|
|
|
/// low to high.
|
2023-12-05 16:52:50 +03:00
|
|
|
#[pallet::storage]
|
2024-02-19 20:50:04 -05:00
|
|
|
pub type SpotPrices<T: Config> =
|
2023-12-05 16:52:50 +03:00
|
|
|
StorageDoubleMap<_, Identity, Coin, Identity, [u8; 8], u16, OptionQuery>;
|
2024-02-19 20:50:04 -05:00
|
|
|
|
|
|
|
|
// SpotPrices, yet with keys stored in reverse lexicographic order.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
pub type ReverseSpotPrices<T: Config> =
|
|
|
|
|
StorageDoubleMap<_, Identity, Coin, Identity, [u8; 8], (), OptionQuery>;
|
|
|
|
|
|
|
|
|
|
/// Current length of the `SpotPrices` map.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
pub type SpotPricesLength<T: Config> = StorageMap<_, Identity, Coin, u16, OptionQuery>;
|
|
|
|
|
|
|
|
|
|
/// Current position of the median within the `SpotPrices` map;
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
pub type CurrentMedianPosition<T: Config> = StorageMap<_, Identity, Coin, u16, OptionQuery>;
|
|
|
|
|
|
|
|
|
|
/// Current median price of the prices in the `SpotPrices` map at any given time.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::getter(fn median_price)]
|
|
|
|
|
pub type MedianPrice<T: Config> = StorageMap<_, Identity, Coin, Amount, OptionQuery>;
|
|
|
|
|
|
|
|
|
|
/// The price used for evaluating economic security, which is the highest observed median price.
|
|
|
|
|
#[pallet::storage]
|
|
|
|
|
#[pallet::getter(fn security_oracle_value)]
|
|
|
|
|
pub type SecurityOracleValue<T: Config> = StorageMap<_, Identity, Coin, Amount, OptionQuery>;
|
|
|
|
|
|
2023-12-05 16:52:50 +03:00
|
|
|
impl<T: Config> Pallet<T> {
|
2024-02-19 20:50:04 -05:00
|
|
|
fn restore_median(
|
|
|
|
|
coin: Coin,
|
|
|
|
|
mut current_median_pos: u16,
|
|
|
|
|
mut current_median: Amount,
|
|
|
|
|
length: u16,
|
|
|
|
|
) {
|
|
|
|
|
// 1 -> 0 (the only value)
|
|
|
|
|
// 2 -> 1 (the higher element), 4 -> 2 (the higher element)
|
|
|
|
|
// 3 -> 1 (the true median)
|
|
|
|
|
let target_median_pos = length / 2;
|
|
|
|
|
while current_median_pos < target_median_pos {
|
|
|
|
|
// Get the amount of presences for the current element
|
|
|
|
|
let key = current_median.0.to_be_bytes();
|
|
|
|
|
let presences = SpotPrices::<T>::get(coin, key).unwrap();
|
|
|
|
|
// > is correct, not >=.
|
|
|
|
|
// Consider:
|
|
|
|
|
// - length = 1, current_median_pos = 0, presences = 1, target_median_pos = 0
|
|
|
|
|
// - length = 2, current_median_pos = 0, presences = 2, target_median_pos = 1
|
|
|
|
|
// - length = 2, current_median_pos = 0, presences = 1, target_median_pos = 1
|
|
|
|
|
if (current_median_pos + presences) > target_median_pos {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
current_median_pos += presences;
|
|
|
|
|
|
|
|
|
|
let key = SpotPrices::<T>::hashed_key_for(coin, key);
|
|
|
|
|
let next_price = SpotPrices::<T>::iter_key_prefix_from(coin, key).next().unwrap();
|
|
|
|
|
current_median = Amount(u64::from_be_bytes(next_price));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while current_median_pos > target_median_pos {
|
|
|
|
|
// Get the next element
|
|
|
|
|
let key = reverse_lexicographic_order(current_median.0.to_be_bytes());
|
|
|
|
|
let key = ReverseSpotPrices::<T>::hashed_key_for(coin, key);
|
|
|
|
|
let next_price = ReverseSpotPrices::<T>::iter_key_prefix_from(coin, key).next().unwrap();
|
|
|
|
|
let next_price = reverse_lexicographic_order(next_price);
|
|
|
|
|
current_median = Amount(u64::from_be_bytes(next_price));
|
|
|
|
|
|
|
|
|
|
// Get its amount of presences
|
|
|
|
|
let presences = SpotPrices::<T>::get(coin, current_median.0.to_be_bytes()).unwrap();
|
|
|
|
|
// Adjust from next_value_first_pos to this_value_first_pos by substracting this value's
|
|
|
|
|
// amount of times present
|
|
|
|
|
current_median_pos -= presences;
|
|
|
|
|
|
|
|
|
|
if current_median_pos <= target_median_pos {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrentMedianPosition::<T>::set(coin, Some(current_median_pos));
|
|
|
|
|
MedianPrice::<T>::set(coin, Some(current_median));
|
2023-12-05 16:52:50 +03:00
|
|
|
}
|
|
|
|
|
|
2024-02-19 20:50:04 -05:00
|
|
|
pub(crate) fn insert_into_median(coin: Coin, amount: Amount) {
|
|
|
|
|
let new_quantity_of_presences =
|
|
|
|
|
SpotPrices::<T>::get(coin, amount.0.to_be_bytes()).unwrap_or(0) + 1;
|
|
|
|
|
SpotPrices::<T>::set(coin, amount.0.to_be_bytes(), Some(new_quantity_of_presences));
|
|
|
|
|
if new_quantity_of_presences == 1 {
|
|
|
|
|
ReverseSpotPrices::<T>::set(
|
|
|
|
|
coin,
|
|
|
|
|
reverse_lexicographic_order(amount.0.to_be_bytes()),
|
|
|
|
|
Some(()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let new_length = SpotPricesLength::<T>::get(coin).unwrap_or(0) + 1;
|
|
|
|
|
SpotPricesLength::<T>::set(coin, Some(new_length));
|
|
|
|
|
|
|
|
|
|
let Some(current_median) = MedianPrice::<T>::get(coin) else {
|
|
|
|
|
MedianPrice::<T>::set(coin, Some(amount));
|
|
|
|
|
CurrentMedianPosition::<T>::set(coin, Some(0));
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut current_median_pos = CurrentMedianPosition::<T>::get(coin).unwrap();
|
|
|
|
|
// If this is being inserted before the current median, the current median's position has
|
|
|
|
|
// increased
|
|
|
|
|
if amount < current_median {
|
|
|
|
|
current_median_pos += 1;
|
|
|
|
|
}
|
|
|
|
|
Self::restore_median(coin, current_median_pos, current_median, new_length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn remove_from_median(coin: Coin, amount: Amount) {
|
|
|
|
|
let mut current_median = MedianPrice::<T>::get(coin).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut current_median_pos = CurrentMedianPosition::<T>::get(coin).unwrap();
|
|
|
|
|
if amount < current_median {
|
|
|
|
|
current_median_pos -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let new_quantity_of_presences =
|
|
|
|
|
SpotPrices::<T>::get(coin, amount.0.to_be_bytes()).unwrap() - 1;
|
|
|
|
|
if new_quantity_of_presences == 0 {
|
|
|
|
|
let normal_key = amount.0.to_be_bytes();
|
|
|
|
|
SpotPrices::<T>::remove(coin, normal_key);
|
|
|
|
|
ReverseSpotPrices::<T>::remove(coin, reverse_lexicographic_order(amount.0.to_be_bytes()));
|
|
|
|
|
|
|
|
|
|
// If we've removed the current item at this position, update to the item now at this
|
|
|
|
|
// position
|
|
|
|
|
if amount == current_median {
|
|
|
|
|
let key = SpotPrices::<T>::hashed_key_for(coin, normal_key);
|
|
|
|
|
current_median = Amount(u64::from_be_bytes(
|
|
|
|
|
SpotPrices::<T>::iter_key_prefix_from(coin, key).next().unwrap(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
SpotPrices::<T>::set(coin, amount.0.to_be_bytes(), Some(new_quantity_of_presences));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let new_length = SpotPricesLength::<T>::get(coin).unwrap() - 1;
|
|
|
|
|
SpotPricesLength::<T>::set(coin, Some(new_length));
|
|
|
|
|
|
|
|
|
|
Self::restore_median(coin, current_median_pos, current_median, new_length);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-05 16:52:50 +03:00
|
|
|
|
2023-11-05 20:02:34 +03:00
|
|
|
// Pallet's events.
|
|
|
|
|
#[pallet::event]
|
|
|
|
|
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
|
|
|
|
pub enum Event<T: Config> {
|
2023-12-05 16:52:50 +03:00
|
|
|
/// A successful call of the `CreatePool` extrinsic will create this event.
|
2023-11-05 20:02:34 +03:00
|
|
|
PoolCreated {
|
|
|
|
|
/// The pool id associated with the pool. Note that the order of the coins may not be
|
|
|
|
|
/// the same as the order specified in the create pool extrinsic.
|
2023-11-12 14:37:31 +03:00
|
|
|
pool_id: PoolId,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// The account ID of the pool.
|
|
|
|
|
pool_account: T::AccountId,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// A successful call of the `AddLiquidity` extrinsic will create this event.
|
|
|
|
|
LiquidityAdded {
|
|
|
|
|
/// The account that the liquidity was taken from.
|
|
|
|
|
who: T::AccountId,
|
|
|
|
|
/// The account that the liquidity tokens were minted to.
|
|
|
|
|
mint_to: T::AccountId,
|
|
|
|
|
/// The pool id of the pool that the liquidity was added to.
|
2023-11-12 14:37:31 +03:00
|
|
|
pool_id: PoolId,
|
|
|
|
|
/// The amount of the coin that was added to the pool.
|
|
|
|
|
coin_amount: SubstrateAmount,
|
|
|
|
|
/// The amount of the SRI that was added to the pool.
|
|
|
|
|
sri_amount: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// The amount of lp tokens that were minted of that id.
|
2023-11-12 14:37:31 +03:00
|
|
|
lp_token_minted: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/// A successful call of the `RemoveLiquidity` extrinsic will create this event.
|
|
|
|
|
LiquidityRemoved {
|
|
|
|
|
/// The account that the liquidity tokens were burned from.
|
|
|
|
|
who: T::AccountId,
|
|
|
|
|
/// The account that the coins were transferred to.
|
|
|
|
|
withdraw_to: T::AccountId,
|
|
|
|
|
/// The pool id that the liquidity was removed from.
|
2023-11-12 14:37:31 +03:00
|
|
|
pool_id: PoolId,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// The amount of the first coin that was removed from the pool.
|
2023-11-12 14:37:31 +03:00
|
|
|
coin_amount: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// The amount of the second coin that was removed from the pool.
|
2023-11-12 14:37:31 +03:00
|
|
|
sri_amount: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// The amount of lp tokens that were burned of that id.
|
2023-11-12 14:37:31 +03:00
|
|
|
lp_token_burned: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
},
|
2023-12-06 09:53:06 -05:00
|
|
|
|
2023-11-05 20:02:34 +03:00
|
|
|
/// Coins have been converted from one to another. Both `SwapExactTokenForToken`
|
|
|
|
|
/// and `SwapTokenForExactToken` will generate this event.
|
|
|
|
|
SwapExecuted {
|
|
|
|
|
/// Which account was the instigator of the swap.
|
|
|
|
|
who: T::AccountId,
|
|
|
|
|
/// The account that the coins were transferred to.
|
|
|
|
|
send_to: T::AccountId,
|
|
|
|
|
/// The route of coin ids that the swap went through.
|
2023-11-12 14:37:31 +03:00
|
|
|
/// E.g. A -> SRI -> B
|
|
|
|
|
path: BoundedVec<Coin, T::MaxSwapPathLength>,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// The amount of the first coin that was swapped.
|
2023-11-12 14:37:31 +03:00
|
|
|
amount_in: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// The amount of the second coin that was received.
|
2023-11-12 14:37:31 +03:00
|
|
|
amount_out: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::genesis_config]
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
|
|
|
|
|
pub struct GenesisConfig<T: Config> {
|
|
|
|
|
/// Pools to create at launch.
|
2023-11-12 14:37:31 +03:00
|
|
|
pub pools: Vec<Coin>,
|
|
|
|
|
/// field just to have T.
|
|
|
|
|
pub _ignore: PhantomData<T>,
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> Default for GenesisConfig<T> {
|
|
|
|
|
fn default() -> Self {
|
2023-11-12 14:37:31 +03:00
|
|
|
GenesisConfig { pools: Default::default(), _ignore: Default::default() }
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::genesis_build]
|
|
|
|
|
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
|
|
|
|
fn build(&self) {
|
2023-12-05 16:52:50 +03:00
|
|
|
// create the pools
|
2023-11-05 20:02:34 +03:00
|
|
|
for coin in &self.pools {
|
2023-11-12 14:37:31 +03:00
|
|
|
Pallet::<T>::create_pool(*coin).unwrap();
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::error]
|
|
|
|
|
pub enum Error<T> {
|
|
|
|
|
/// Provided coins are equal.
|
|
|
|
|
EqualCoins,
|
|
|
|
|
/// Pool already exists.
|
|
|
|
|
PoolExists,
|
|
|
|
|
/// Desired amount can't be zero.
|
|
|
|
|
WrongDesiredAmount,
|
|
|
|
|
/// Provided amount should be greater than or equal to the existential deposit/coin's
|
2023-11-12 14:37:31 +03:00
|
|
|
/// minimum amount.
|
|
|
|
|
CoinAmountLessThanMinimum,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// Provided amount should be greater than or equal to the existential deposit/coin's
|
2023-11-12 14:37:31 +03:00
|
|
|
/// minimum amount.
|
|
|
|
|
SriAmountLessThanMinimum,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// Reserve needs to always be greater than or equal to the existential deposit/coin's
|
2023-11-12 14:37:31 +03:00
|
|
|
/// minimum amount.
|
|
|
|
|
ReserveLeftLessThanMinimum,
|
2023-11-05 20:02:34 +03:00
|
|
|
/// Desired amount can't be equal to the pool reserve.
|
|
|
|
|
AmountOutTooHigh,
|
|
|
|
|
/// The pool doesn't exist.
|
|
|
|
|
PoolNotFound,
|
|
|
|
|
/// An overflow happened.
|
|
|
|
|
Overflow,
|
2023-11-12 14:37:31 +03:00
|
|
|
/// The minimum amount requirement for the first token in the pair wasn't met.
|
2023-11-05 20:02:34 +03:00
|
|
|
CoinOneDepositDidNotMeetMinimum,
|
2023-11-12 14:37:31 +03:00
|
|
|
/// The minimum amount requirement for the second token in the pair wasn't met.
|
2023-11-05 20:02:34 +03:00
|
|
|
CoinTwoDepositDidNotMeetMinimum,
|
2023-11-12 14:37:31 +03:00
|
|
|
/// The minimum amount requirement for the first token in the pair wasn't met.
|
2023-11-05 20:02:34 +03:00
|
|
|
CoinOneWithdrawalDidNotMeetMinimum,
|
2023-11-12 14:37:31 +03:00
|
|
|
/// The minimum amount requirement for the second token in the pair wasn't met.
|
2023-11-05 20:02:34 +03:00
|
|
|
CoinTwoWithdrawalDidNotMeetMinimum,
|
|
|
|
|
/// Optimal calculated amount is less than desired.
|
|
|
|
|
OptimalAmountLessThanDesired,
|
|
|
|
|
/// Insufficient liquidity minted.
|
|
|
|
|
InsufficientLiquidityMinted,
|
|
|
|
|
/// Requested liquidity can't be zero.
|
|
|
|
|
ZeroLiquidity,
|
|
|
|
|
/// Amount can't be zero.
|
|
|
|
|
ZeroAmount,
|
|
|
|
|
/// Calculated amount out is less than provided minimum amount.
|
|
|
|
|
ProvidedMinimumNotSufficientForSwap,
|
|
|
|
|
/// Provided maximum amount is not sufficient for swap.
|
|
|
|
|
ProvidedMaximumNotSufficientForSwap,
|
|
|
|
|
/// The provided path must consists of 2 coins at least.
|
|
|
|
|
InvalidPath,
|
|
|
|
|
/// It was not possible to calculate path data.
|
|
|
|
|
PathError,
|
|
|
|
|
/// The provided path must consists of unique coins.
|
|
|
|
|
NonUniquePath,
|
|
|
|
|
/// Unable to find an element in an array/vec that should have one-to-one correspondence
|
|
|
|
|
/// with another. For example, an array of coins constituting a `path` should have a
|
|
|
|
|
/// corresponding array of `amounts` along the path.
|
|
|
|
|
CorrespondenceError,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::hooks]
|
|
|
|
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
|
2023-12-05 16:52:50 +03:00
|
|
|
fn on_finalize(n: BlockNumberFor<T>) {
|
|
|
|
|
// we run this on on_finalize because we want to use the last price of the block for a coin.
|
|
|
|
|
// This prevents the exploit where a malicious block proposer spikes the price in either
|
|
|
|
|
// direction, then includes a swap in the other direction (ensuring they don't get arbitraged
|
|
|
|
|
// against)
|
|
|
|
|
// Since they'll have to leave the spike present at the end of the block, making the next
|
|
|
|
|
// block the one to include any arbitrage transactions (which there's no guarantee they'll
|
|
|
|
|
// produce), this cannot be done in a way without significant risk
|
|
|
|
|
for coin in Pools::<T>::iter_keys() {
|
|
|
|
|
// insert the new price to our oracle window
|
|
|
|
|
// The spot price for 1 coin, in atomic units, to SRI is used
|
2023-12-05 12:29:36 -05:00
|
|
|
let sri_per_coin =
|
|
|
|
|
if let Ok((sri_balance, coin_balance)) = Self::get_reserves(&Coin::native(), &coin) {
|
|
|
|
|
// We use 1 coin to handle rounding errors which may occur with atomic units
|
|
|
|
|
// If we used atomic units, any coin whose atomic unit is worth less than SRI's atomic
|
|
|
|
|
// unit would cause a 'price' of 0
|
|
|
|
|
// If the decimals aren't large enough to provide sufficient buffer, use 10,000
|
|
|
|
|
let coin_decimals = coin.decimals().max(5);
|
|
|
|
|
let accuracy_increase =
|
|
|
|
|
HigherPrecisionBalance::from(SubstrateAmount::pow(10, coin_decimals));
|
|
|
|
|
u64::try_from(
|
|
|
|
|
accuracy_increase * HigherPrecisionBalance::from(sri_balance) /
|
|
|
|
|
HigherPrecisionBalance::from(coin_balance),
|
|
|
|
|
)
|
|
|
|
|
.unwrap_or(u64::MAX)
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
};
|
2023-12-05 16:52:50 +03:00
|
|
|
|
2024-02-19 20:50:04 -05:00
|
|
|
let sri_per_coin = Amount(sri_per_coin);
|
|
|
|
|
SpotPriceForBlock::<T>::set(n, coin, Some(sri_per_coin));
|
|
|
|
|
Self::insert_into_median(coin, sri_per_coin);
|
|
|
|
|
if SpotPricesLength::<T>::get(coin).unwrap() > T::MedianPriceWindowLength::get() {
|
|
|
|
|
let old = n - T::MedianPriceWindowLength::get().into();
|
|
|
|
|
let old_price = SpotPriceForBlock::<T>::get(old, coin).unwrap();
|
|
|
|
|
SpotPriceForBlock::<T>::remove(old, coin);
|
|
|
|
|
Self::remove_from_median(coin, old_price);
|
2023-12-05 16:52:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update the oracle value
|
2024-02-19 20:50:04 -05:00
|
|
|
let median = Self::median_price(coin).unwrap_or(Amount(0));
|
|
|
|
|
let oracle_value = Self::security_oracle_value(coin).unwrap_or(Amount(0));
|
|
|
|
|
if median > oracle_value {
|
|
|
|
|
SecurityOracleValue::<T>::set(coin, Some(median));
|
2023-12-05 16:52:50 +03:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> Pallet<T> {
|
|
|
|
|
/// Creates an empty liquidity pool and an associated new `lp_token` coin
|
|
|
|
|
/// (the id of which is returned in the `Event::PoolCreated` event).
|
|
|
|
|
///
|
|
|
|
|
/// Once a pool is created, someone may [`Pallet::add_liquidity`] to it.
|
2023-11-12 14:37:31 +03:00
|
|
|
pub(crate) fn create_pool(coin: Coin) -> DispatchResult {
|
|
|
|
|
ensure!(coin != Coin::Serai, Error::<T>::EqualCoins);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
// prepare pool_id
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap();
|
|
|
|
|
ensure!(!Pools::<T>::contains_key(pool_id), Error::<T>::PoolExists);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
2023-11-05 20:02:34 +03:00
|
|
|
frame_system::Pallet::<T>::inc_providers(&pool_account);
|
|
|
|
|
|
2023-12-11 07:47:23 -05:00
|
|
|
Pools::<T>::insert(pool_id, ());
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-12-11 07:47:23 -05:00
|
|
|
Self::deposit_event(Event::PoolCreated { pool_id, pool_account });
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-12-05 16:52:50 +03:00
|
|
|
|
|
|
|
|
/// A hook to be called whenever a network's session is rotated.
|
|
|
|
|
pub fn on_new_session(network: NetworkId) {
|
|
|
|
|
// reset the oracle value
|
|
|
|
|
for coin in network.coins() {
|
2024-02-19 20:50:04 -05:00
|
|
|
SecurityOracleValue::<T>::set(*coin, Self::median_price(coin));
|
2023-12-05 16:52:50 +03:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pallet's callable functions.
|
|
|
|
|
#[pallet::call]
|
|
|
|
|
impl<T: Config> Pallet<T> {
|
|
|
|
|
/// Provide liquidity into the pool of `coin1` and `coin2`.
|
|
|
|
|
/// NOTE: an optimal amount of coin1 and coin2 will be calculated and
|
|
|
|
|
/// might be different than the provided `amount1_desired`/`amount2_desired`
|
|
|
|
|
/// thus you should provide the min amount you're happy to provide.
|
|
|
|
|
/// Params `amount1_min`/`amount2_min` represent that.
|
|
|
|
|
/// `mint_to` will be sent the liquidity tokens that represent this share of the pool.
|
|
|
|
|
///
|
|
|
|
|
/// Once liquidity is added, someone may successfully call
|
|
|
|
|
/// [`Pallet::swap_exact_tokens_for_tokens`] successfully.
|
|
|
|
|
#[pallet::call_index(0)]
|
|
|
|
|
#[pallet::weight(T::WeightInfo::add_liquidity())]
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
|
pub fn add_liquidity(
|
|
|
|
|
origin: OriginFor<T>,
|
2023-11-12 14:37:31 +03:00
|
|
|
coin: Coin,
|
|
|
|
|
coin_desired: SubstrateAmount,
|
|
|
|
|
sri_desired: SubstrateAmount,
|
|
|
|
|
coin_min: SubstrateAmount,
|
|
|
|
|
sri_min: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
mint_to: T::AccountId,
|
|
|
|
|
) -> DispatchResult {
|
|
|
|
|
let sender = ensure_signed(origin)?;
|
2023-11-12 14:37:31 +03:00
|
|
|
ensure!((sri_desired > 0) && (coin_desired > 0), Error::<T>::WrongDesiredAmount);
|
|
|
|
|
ensure!(coin != Coin::Serai, Error::<T>::EqualCoins);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap();
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-12-11 07:47:23 -05:00
|
|
|
Pools::<T>::get(pool_id).as_ref().ok_or(Error::<T>::PoolNotFound)?;
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let sri_reserve = Self::get_balance(&pool_account, Coin::Serai);
|
|
|
|
|
let coin_reserve = Self::get_balance(&pool_account, coin);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let sri_amount: SubstrateAmount;
|
|
|
|
|
let coin_amount: SubstrateAmount;
|
|
|
|
|
if (sri_reserve == 0) || (coin_reserve == 0) {
|
|
|
|
|
sri_amount = sri_desired;
|
|
|
|
|
coin_amount = coin_desired;
|
2023-11-05 20:02:34 +03:00
|
|
|
} else {
|
2023-11-12 14:37:31 +03:00
|
|
|
let coin_optimal = Self::quote(sri_desired, sri_reserve, coin_reserve)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
if coin_optimal <= coin_desired {
|
|
|
|
|
ensure!(coin_optimal >= coin_min, Error::<T>::CoinTwoDepositDidNotMeetMinimum);
|
|
|
|
|
sri_amount = sri_desired;
|
|
|
|
|
coin_amount = coin_optimal;
|
2023-11-05 20:02:34 +03:00
|
|
|
} else {
|
2023-11-12 14:37:31 +03:00
|
|
|
let sri_optimal = Self::quote(coin_desired, coin_reserve, sri_reserve)?;
|
|
|
|
|
ensure!(sri_optimal <= sri_desired, Error::<T>::OptimalAmountLessThanDesired);
|
|
|
|
|
ensure!(sri_optimal >= sri_min, Error::<T>::CoinOneDepositDidNotMeetMinimum);
|
|
|
|
|
sri_amount = sri_optimal;
|
|
|
|
|
coin_amount = coin_desired;
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
ensure!(sri_amount.saturating_add(sri_reserve) >= 1, Error::<T>::SriAmountLessThanMinimum);
|
|
|
|
|
ensure!(coin_amount.saturating_add(coin_reserve) >= 1, Error::<T>::CoinAmountLessThanMinimum);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::transfer(
|
|
|
|
|
&sender,
|
|
|
|
|
&pool_account,
|
|
|
|
|
Balance { coin: Coin::Serai, amount: Amount(sri_amount) },
|
|
|
|
|
)?;
|
|
|
|
|
Self::transfer(&sender, &pool_account, Balance { coin, amount: Amount(coin_amount) })?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let total_supply = LiquidityTokens::<T>::supply(coin);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let lp_token_amount: SubstrateAmount;
|
|
|
|
|
if total_supply == 0 {
|
|
|
|
|
lp_token_amount = Self::calc_lp_amount_for_zero_supply(sri_amount, coin_amount)?;
|
|
|
|
|
LiquidityTokens::<T>::mint(
|
|
|
|
|
pool_account,
|
|
|
|
|
Balance { coin, amount: Amount(T::MintMinLiquidity::get()) },
|
|
|
|
|
)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
} else {
|
2023-11-12 14:37:31 +03:00
|
|
|
let side1 = Self::mul_div(sri_amount, total_supply, sri_reserve)?;
|
|
|
|
|
let side2 = Self::mul_div(coin_amount, total_supply, coin_reserve)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
lp_token_amount = side1.min(side2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ensure!(
|
|
|
|
|
lp_token_amount > T::MintMinLiquidity::get(),
|
|
|
|
|
Error::<T>::InsufficientLiquidityMinted
|
|
|
|
|
);
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
LiquidityTokens::<T>::mint(mint_to, Balance { coin, amount: Amount(lp_token_amount) })?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
Self::deposit_event(Event::LiquidityAdded {
|
|
|
|
|
who: sender,
|
|
|
|
|
mint_to,
|
|
|
|
|
pool_id,
|
2023-11-12 14:37:31 +03:00
|
|
|
coin_amount,
|
|
|
|
|
sri_amount,
|
2023-11-05 20:02:34 +03:00
|
|
|
lp_token_minted: lp_token_amount,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Allows you to remove liquidity by providing the `lp_token_burn` tokens that will be
|
|
|
|
|
/// burned in the process. With the usage of `amount1_min_receive`/`amount2_min_receive`
|
|
|
|
|
/// it's possible to control the min amount of returned tokens you're happy with.
|
|
|
|
|
#[pallet::call_index(1)]
|
|
|
|
|
#[pallet::weight(T::WeightInfo::remove_liquidity())]
|
|
|
|
|
pub fn remove_liquidity(
|
|
|
|
|
origin: OriginFor<T>,
|
2023-11-12 14:37:31 +03:00
|
|
|
coin: Coin,
|
|
|
|
|
lp_token_burn: SubstrateAmount,
|
|
|
|
|
coin_min_receive: SubstrateAmount,
|
|
|
|
|
sri_min_receive: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
withdraw_to: T::AccountId,
|
|
|
|
|
) -> DispatchResult {
|
2023-11-12 14:37:31 +03:00
|
|
|
let sender = ensure_signed(origin.clone())?;
|
|
|
|
|
ensure!(coin != Coin::Serai, Error::<T>::EqualCoins);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_id = Self::get_pool_id(coin, Coin::Serai).unwrap();
|
|
|
|
|
ensure!(lp_token_burn > 0, Error::<T>::ZeroLiquidity);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-12-11 07:47:23 -05:00
|
|
|
Pools::<T>::get(pool_id).as_ref().ok_or(Error::<T>::PoolNotFound)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
|
|
|
|
let sri_reserve = Self::get_balance(&pool_account, Coin::Serai);
|
|
|
|
|
let coin_reserve = Self::get_balance(&pool_account, coin);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let total_supply = LiquidityTokens::<T>::supply(coin);
|
2023-11-05 20:02:34 +03:00
|
|
|
let lp_redeem_amount = lp_token_burn;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let sri_amount = Self::mul_div(lp_redeem_amount, sri_reserve, total_supply)?;
|
|
|
|
|
let coin_amount = Self::mul_div(lp_redeem_amount, coin_reserve, total_supply)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
ensure!(
|
2023-11-12 14:37:31 +03:00
|
|
|
(sri_amount != 0) && (sri_amount >= sri_min_receive),
|
2023-11-05 20:02:34 +03:00
|
|
|
Error::<T>::CoinOneWithdrawalDidNotMeetMinimum
|
|
|
|
|
);
|
|
|
|
|
ensure!(
|
2023-11-12 14:37:31 +03:00
|
|
|
(coin_amount != 0) && (coin_amount >= coin_min_receive),
|
2023-11-05 20:02:34 +03:00
|
|
|
Error::<T>::CoinTwoWithdrawalDidNotMeetMinimum
|
|
|
|
|
);
|
2023-11-12 14:37:31 +03:00
|
|
|
let sri_reserve_left = sri_reserve.saturating_sub(sri_amount);
|
|
|
|
|
let coin_reserve_left = coin_reserve.saturating_sub(coin_amount);
|
|
|
|
|
|
|
|
|
|
ensure!(sri_reserve_left >= 1, Error::<T>::ReserveLeftLessThanMinimum);
|
|
|
|
|
ensure!(coin_reserve_left >= 1, Error::<T>::ReserveLeftLessThanMinimum);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
// burn the provided lp token amount that includes the fee
|
2023-11-12 14:37:31 +03:00
|
|
|
LiquidityTokens::<T>::burn(origin, Balance { coin, amount: Amount(lp_token_burn) })?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::transfer(
|
|
|
|
|
&pool_account,
|
|
|
|
|
&withdraw_to,
|
|
|
|
|
Balance { coin: Coin::Serai, amount: Amount(sri_amount) },
|
|
|
|
|
)?;
|
|
|
|
|
Self::transfer(&pool_account, &withdraw_to, Balance { coin, amount: Amount(coin_amount) })?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
Self::deposit_event(Event::LiquidityRemoved {
|
|
|
|
|
who: sender,
|
|
|
|
|
withdraw_to,
|
|
|
|
|
pool_id,
|
2023-11-12 14:37:31 +03:00
|
|
|
coin_amount,
|
|
|
|
|
sri_amount,
|
2023-11-05 20:02:34 +03:00
|
|
|
lp_token_burned: lp_token_burn,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap the exact amount of `coin1` into `coin2`.
|
|
|
|
|
/// `amount_out_min` param allows you to specify the min amount of the `coin2`
|
|
|
|
|
/// you're happy to receive.
|
|
|
|
|
///
|
|
|
|
|
/// [`DexApi::quote_price_exact_tokens_for_tokens`] runtime call can be called
|
|
|
|
|
/// for a quote.
|
|
|
|
|
#[pallet::call_index(2)]
|
|
|
|
|
#[pallet::weight(T::WeightInfo::swap_exact_tokens_for_tokens())]
|
|
|
|
|
pub fn swap_exact_tokens_for_tokens(
|
|
|
|
|
origin: OriginFor<T>,
|
2023-11-12 14:37:31 +03:00
|
|
|
path: BoundedVec<Coin, T::MaxSwapPathLength>,
|
|
|
|
|
amount_in: SubstrateAmount,
|
|
|
|
|
amount_out_min: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
send_to: T::AccountId,
|
|
|
|
|
) -> DispatchResult {
|
|
|
|
|
let sender = ensure_signed(origin)?;
|
|
|
|
|
Self::do_swap_exact_tokens_for_tokens(
|
|
|
|
|
sender,
|
|
|
|
|
path,
|
|
|
|
|
amount_in,
|
|
|
|
|
Some(amount_out_min),
|
|
|
|
|
send_to,
|
|
|
|
|
)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap any amount of `coin1` to get the exact amount of `coin2`.
|
|
|
|
|
/// `amount_in_max` param allows to specify the max amount of the `coin1`
|
|
|
|
|
/// you're happy to provide.
|
|
|
|
|
///
|
|
|
|
|
/// [`DexApi::quote_price_tokens_for_exact_tokens`] runtime call can be called
|
|
|
|
|
/// for a quote.
|
|
|
|
|
#[pallet::call_index(3)]
|
|
|
|
|
#[pallet::weight(T::WeightInfo::swap_tokens_for_exact_tokens())]
|
|
|
|
|
pub fn swap_tokens_for_exact_tokens(
|
|
|
|
|
origin: OriginFor<T>,
|
2023-11-12 14:37:31 +03:00
|
|
|
path: BoundedVec<Coin, T::MaxSwapPathLength>,
|
|
|
|
|
amount_out: SubstrateAmount,
|
|
|
|
|
amount_in_max: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
send_to: T::AccountId,
|
|
|
|
|
) -> DispatchResult {
|
|
|
|
|
let sender = ensure_signed(origin)?;
|
|
|
|
|
Self::do_swap_tokens_for_exact_tokens(
|
|
|
|
|
sender,
|
|
|
|
|
path,
|
|
|
|
|
amount_out,
|
|
|
|
|
Some(amount_in_max),
|
|
|
|
|
send_to,
|
|
|
|
|
)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> Pallet<T> {
|
|
|
|
|
/// Swap exactly `amount_in` of coin `path[0]` for coin `path[1]`.
|
|
|
|
|
/// If an `amount_out_min` is specified, it will return an error if it is unable to acquire
|
|
|
|
|
/// the amount desired.
|
|
|
|
|
///
|
|
|
|
|
/// Withdraws the `path[0]` coin from `sender`, deposits the `path[1]` coin to `send_to`.
|
|
|
|
|
///
|
|
|
|
|
/// If successful, returns the amount of `path[1]` acquired for the `amount_in`.
|
|
|
|
|
pub fn do_swap_exact_tokens_for_tokens(
|
|
|
|
|
sender: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
path: BoundedVec<Coin, T::MaxSwapPathLength>,
|
|
|
|
|
amount_in: SubstrateAmount,
|
|
|
|
|
amount_out_min: Option<SubstrateAmount>,
|
2023-11-05 20:02:34 +03:00
|
|
|
send_to: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Result<SubstrateAmount, DispatchError> {
|
|
|
|
|
ensure!(amount_in > 0, Error::<T>::ZeroAmount);
|
2023-11-05 20:02:34 +03:00
|
|
|
if let Some(amount_out_min) = amount_out_min {
|
2023-11-12 14:37:31 +03:00
|
|
|
ensure!(amount_out_min > 0, Error::<T>::ZeroAmount);
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self::validate_swap_path(&path)?;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let amounts = Self::get_amounts_out(amount_in, &path)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
let amount_out =
|
|
|
|
|
*amounts.last().defensive_ok_or("get_amounts_out() returned an empty result")?;
|
|
|
|
|
|
|
|
|
|
if let Some(amount_out_min) = amount_out_min {
|
|
|
|
|
ensure!(amount_out >= amount_out_min, Error::<T>::ProvidedMinimumNotSufficientForSwap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self::do_swap(sender, &amounts, path, send_to)?;
|
|
|
|
|
Ok(amount_out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Take the `path[0]` coin and swap some amount for `amount_out` of the `path[1]`. If an
|
|
|
|
|
/// `amount_in_max` is specified, it will return an error if acquiring `amount_out` would be
|
|
|
|
|
/// too costly.
|
|
|
|
|
///
|
|
|
|
|
/// Withdraws `path[0]` coin from `sender`, deposits the `path[1]` coin to `send_to`,
|
|
|
|
|
///
|
|
|
|
|
/// If successful returns the amount of the `path[0]` taken to provide `path[1]`.
|
|
|
|
|
pub fn do_swap_tokens_for_exact_tokens(
|
|
|
|
|
sender: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
path: BoundedVec<Coin, T::MaxSwapPathLength>,
|
|
|
|
|
amount_out: SubstrateAmount,
|
|
|
|
|
amount_in_max: Option<SubstrateAmount>,
|
2023-11-05 20:02:34 +03:00
|
|
|
send_to: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Result<SubstrateAmount, DispatchError> {
|
|
|
|
|
ensure!(amount_out > 0, Error::<T>::ZeroAmount);
|
2023-11-05 20:02:34 +03:00
|
|
|
if let Some(amount_in_max) = amount_in_max {
|
2023-11-12 14:37:31 +03:00
|
|
|
ensure!(amount_in_max > 0, Error::<T>::ZeroAmount);
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self::validate_swap_path(&path)?;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let amounts = Self::get_amounts_in(amount_out, &path)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
let amount_in =
|
|
|
|
|
*amounts.first().defensive_ok_or("get_amounts_in() returned an empty result")?;
|
|
|
|
|
|
|
|
|
|
if let Some(amount_in_max) = amount_in_max {
|
|
|
|
|
ensure!(amount_in <= amount_in_max, Error::<T>::ProvidedMaximumNotSufficientForSwap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Self::do_swap(sender, &amounts, path, send_to)?;
|
|
|
|
|
Ok(amount_in)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Transfer an `amount` of `coin_id`.
|
|
|
|
|
fn transfer(
|
|
|
|
|
from: &T::AccountId,
|
|
|
|
|
to: &T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
balance: Balance,
|
|
|
|
|
) -> Result<Amount, DispatchError> {
|
|
|
|
|
CoinsPallet::<T>::transfer_internal(*from, *to, balance)?;
|
|
|
|
|
Ok(balance.amount)
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
/// Convert a `HigherPrecisionBalance` type to an `SubstrateAmount`.
|
2023-11-05 20:02:34 +03:00
|
|
|
pub(crate) fn convert_hpb_to_coin_balance(
|
2023-11-12 14:37:31 +03:00
|
|
|
amount: HigherPrecisionBalance,
|
|
|
|
|
) -> Result<SubstrateAmount, Error<T>> {
|
2023-11-05 20:02:34 +03:00
|
|
|
amount.try_into().map_err(|_| Error::<T>::Overflow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap coins along a `path`, depositing in `send_to`.
|
|
|
|
|
pub(crate) fn do_swap(
|
|
|
|
|
sender: T::AccountId,
|
2023-12-05 10:34:20 -05:00
|
|
|
amounts: &[SubstrateAmount],
|
2023-11-12 14:37:31 +03:00
|
|
|
path: BoundedVec<Coin, T::MaxSwapPathLength>,
|
2023-11-05 20:02:34 +03:00
|
|
|
send_to: T::AccountId,
|
|
|
|
|
) -> Result<(), DispatchError> {
|
|
|
|
|
ensure!(amounts.len() > 1, Error::<T>::CorrespondenceError);
|
|
|
|
|
if let Some([coin1, coin2]) = &path.get(0 .. 2) {
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_id = Self::get_pool_id(*coin1, *coin2)?;
|
|
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
2023-11-05 20:02:34 +03:00
|
|
|
// amounts should always contain a corresponding element to path.
|
|
|
|
|
let first_amount = amounts.first().ok_or(Error::<T>::CorrespondenceError)?;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::transfer(
|
|
|
|
|
&sender,
|
|
|
|
|
&pool_account,
|
|
|
|
|
Balance { coin: *coin1, amount: Amount(*first_amount) },
|
|
|
|
|
)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
let mut i = 0;
|
2023-12-16 20:54:24 -05:00
|
|
|
let path_len = u32::try_from(path.len()).unwrap();
|
2023-11-12 14:37:31 +03:00
|
|
|
#[allow(clippy::explicit_counter_loop)]
|
2023-11-05 20:02:34 +03:00
|
|
|
for coins_pair in path.windows(2) {
|
|
|
|
|
if let [coin1, coin2] = coins_pair {
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_id = Self::get_pool_id(*coin1, *coin2)?;
|
|
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
let amount_out =
|
|
|
|
|
amounts.get((i + 1) as usize).ok_or(Error::<T>::CorrespondenceError)?;
|
|
|
|
|
|
|
|
|
|
let to = if i < path_len - 2 {
|
|
|
|
|
let coin3 = path.get((i + 2) as usize).ok_or(Error::<T>::PathError)?;
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::get_pool_account(Self::get_pool_id(*coin2, *coin3)?)
|
2023-11-05 20:02:34 +03:00
|
|
|
} else {
|
2023-11-12 14:37:31 +03:00
|
|
|
send_to
|
2023-11-05 20:02:34 +03:00
|
|
|
};
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let reserve = Self::get_balance(&pool_account, *coin2);
|
2023-11-05 20:02:34 +03:00
|
|
|
let reserve_left = reserve.saturating_sub(*amount_out);
|
2023-11-12 14:37:31 +03:00
|
|
|
ensure!(reserve_left >= 1, Error::<T>::ReserveLeftLessThanMinimum);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::transfer(
|
|
|
|
|
&pool_account,
|
|
|
|
|
&to,
|
|
|
|
|
Balance { coin: *coin2, amount: Amount(*amount_out) },
|
|
|
|
|
)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
2023-11-12 14:37:31 +03:00
|
|
|
i += 1;
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
Self::deposit_event(Event::SwapExecuted {
|
|
|
|
|
who: sender,
|
|
|
|
|
send_to,
|
|
|
|
|
path,
|
|
|
|
|
amount_in: *first_amount,
|
|
|
|
|
amount_out: *amounts.last().expect("Always has more than 1 element"),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::<T>::InvalidPath.into());
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The account ID of the pool.
|
|
|
|
|
///
|
|
|
|
|
/// This actually does computation. If you need to keep using it, then make sure you cache
|
|
|
|
|
/// the value and only call this once.
|
2023-11-12 14:37:31 +03:00
|
|
|
pub fn get_pool_account(pool_id: PoolId) -> T::AccountId {
|
|
|
|
|
let encoded_pool_id = sp_io::hashing::blake2_256(&Encode::encode(&pool_id)[..]);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
Decode::decode(&mut TrailingZeroInput::new(encoded_pool_id.as_ref()))
|
|
|
|
|
.expect("infinite length input; no invalid inputs for type; qed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the `owner`'s balance of `coin`, which could be the chain's native coin or another
|
2023-11-12 14:37:31 +03:00
|
|
|
/// fungible. Returns a value in the form of an `Amount`.
|
|
|
|
|
fn get_balance(owner: &T::AccountId, coin: Coin) -> SubstrateAmount {
|
|
|
|
|
CoinsPallet::<T>::balance(*owner, coin).0
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a pool id constructed from 2 coins.
|
|
|
|
|
/// We expect deterministic order, so (coin1, coin2) or (coin2, coin1) returns the same
|
2023-11-12 14:37:31 +03:00
|
|
|
/// result. Coins have to be different and one of them should be Coin::Serai.
|
|
|
|
|
pub fn get_pool_id(coin1: Coin, coin2: Coin) -> Result<PoolId, Error<T>> {
|
|
|
|
|
ensure!((coin1 == Coin::Serai) || (coin2 == Coin::Serai), Error::<T>::PoolNotFound);
|
|
|
|
|
ensure!(coin1 != coin2, Error::<T>::EqualCoins);
|
|
|
|
|
if coin1 == Coin::Serai {
|
|
|
|
|
Ok(coin2)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(coin1)
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the balance of each coin in the pool.
|
|
|
|
|
/// The tuple result is in the order requested (not necessarily the same as pool order).
|
|
|
|
|
pub fn get_reserves(
|
2023-11-12 14:37:31 +03:00
|
|
|
coin1: &Coin,
|
|
|
|
|
coin2: &Coin,
|
|
|
|
|
) -> Result<(SubstrateAmount, SubstrateAmount), Error<T>> {
|
|
|
|
|
let pool_id = Self::get_pool_id(*coin1, *coin2)?;
|
|
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let balance1 = Self::get_balance(&pool_account, *coin1);
|
|
|
|
|
let balance2 = Self::get_balance(&pool_account, *coin2);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
if (balance1 == 0) || (balance2 == 0) {
|
2023-11-05 20:02:34 +03:00
|
|
|
Err(Error::<T>::PoolNotFound)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok((balance1, balance2))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Leading to an amount at the end of a `path`, get the required amounts in.
|
|
|
|
|
pub(crate) fn get_amounts_in(
|
2023-11-12 14:37:31 +03:00
|
|
|
amount_out: SubstrateAmount,
|
|
|
|
|
path: &BoundedVec<Coin, T::MaxSwapPathLength>,
|
|
|
|
|
) -> Result<Vec<SubstrateAmount>, DispatchError> {
|
|
|
|
|
let mut amounts: Vec<SubstrateAmount> = vec![amount_out];
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
for coins_pair in path.windows(2).rev() {
|
|
|
|
|
if let [coin1, coin2] = coins_pair {
|
|
|
|
|
let (reserve_in, reserve_out) = Self::get_reserves(coin1, coin2)?;
|
|
|
|
|
let prev_amount = amounts.last().expect("Always has at least one element");
|
2023-11-12 14:37:31 +03:00
|
|
|
let amount_in = Self::get_amount_in(*prev_amount, reserve_in, reserve_out)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
amounts.push(amount_in);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amounts.reverse();
|
|
|
|
|
Ok(amounts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Following an amount into a `path`, get the corresponding amounts out.
|
|
|
|
|
pub(crate) fn get_amounts_out(
|
2023-11-12 14:37:31 +03:00
|
|
|
amount_in: SubstrateAmount,
|
|
|
|
|
path: &BoundedVec<Coin, T::MaxSwapPathLength>,
|
|
|
|
|
) -> Result<Vec<SubstrateAmount>, DispatchError> {
|
|
|
|
|
let mut amounts: Vec<SubstrateAmount> = vec![amount_in];
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
for coins_pair in path.windows(2) {
|
|
|
|
|
if let [coin1, coin2] = coins_pair {
|
|
|
|
|
let (reserve_in, reserve_out) = Self::get_reserves(coin1, coin2)?;
|
|
|
|
|
let prev_amount = amounts.last().expect("Always has at least one element");
|
2023-11-12 14:37:31 +03:00
|
|
|
let amount_out = Self::get_amount_out(*prev_amount, reserve_in, reserve_out)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
amounts.push(amount_out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(amounts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Used by the RPC service to provide current prices.
|
|
|
|
|
pub fn quote_price_exact_tokens_for_tokens(
|
2023-11-12 14:37:31 +03:00
|
|
|
coin1: Coin,
|
|
|
|
|
coin2: Coin,
|
|
|
|
|
amount: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
include_fee: bool,
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Option<SubstrateAmount> {
|
|
|
|
|
let pool_id = Self::get_pool_id(coin1, coin2).ok()?;
|
|
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let balance1 = Self::get_balance(&pool_account, coin1);
|
|
|
|
|
let balance2 = Self::get_balance(&pool_account, coin2);
|
|
|
|
|
if balance1 != 0 {
|
2023-11-05 20:02:34 +03:00
|
|
|
if include_fee {
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::get_amount_out(amount, balance1, balance2).ok()
|
2023-11-05 20:02:34 +03:00
|
|
|
} else {
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::quote(amount, balance1, balance2).ok()
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Used by the RPC service to provide current prices.
|
|
|
|
|
pub fn quote_price_tokens_for_exact_tokens(
|
2023-11-12 14:37:31 +03:00
|
|
|
coin1: Coin,
|
|
|
|
|
coin2: Coin,
|
|
|
|
|
amount: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
include_fee: bool,
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Option<SubstrateAmount> {
|
|
|
|
|
let pool_id = Self::get_pool_id(coin1, coin2).ok()?;
|
|
|
|
|
let pool_account = Self::get_pool_account(pool_id);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let balance1 = Self::get_balance(&pool_account, coin1);
|
|
|
|
|
let balance2 = Self::get_balance(&pool_account, coin2);
|
|
|
|
|
if balance1 != 0 {
|
2023-11-05 20:02:34 +03:00
|
|
|
if include_fee {
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::get_amount_in(amount, balance1, balance2).ok()
|
2023-11-05 20:02:34 +03:00
|
|
|
} else {
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::quote(amount, balance2, balance1).ok()
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Calculates the optimal amount from the reserves.
|
|
|
|
|
pub fn quote(
|
2023-11-12 14:37:31 +03:00
|
|
|
amount: SubstrateAmount,
|
|
|
|
|
reserve1: SubstrateAmount,
|
|
|
|
|
reserve2: SubstrateAmount,
|
|
|
|
|
) -> Result<SubstrateAmount, Error<T>> {
|
2023-11-05 20:02:34 +03:00
|
|
|
// amount * reserve2 / reserve1
|
|
|
|
|
Self::mul_div(amount, reserve2, reserve1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn calc_lp_amount_for_zero_supply(
|
2023-11-12 14:37:31 +03:00
|
|
|
amount1: SubstrateAmount,
|
|
|
|
|
amount2: SubstrateAmount,
|
|
|
|
|
) -> Result<SubstrateAmount, Error<T>> {
|
|
|
|
|
let amount1 = HigherPrecisionBalance::from(amount1);
|
|
|
|
|
let amount2 = HigherPrecisionBalance::from(amount2);
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
let result = amount1
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_mul(amount2)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?
|
|
|
|
|
.integer_sqrt()
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_sub(T::MintMinLiquidity::get().into())
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::InsufficientLiquidityMinted)?;
|
|
|
|
|
|
|
|
|
|
result.try_into().map_err(|_| Error::<T>::Overflow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mul_div(
|
2023-11-12 14:37:31 +03:00
|
|
|
a: SubstrateAmount,
|
|
|
|
|
b: SubstrateAmount,
|
|
|
|
|
c: SubstrateAmount,
|
|
|
|
|
) -> Result<SubstrateAmount, Error<T>> {
|
|
|
|
|
let a = HigherPrecisionBalance::from(a);
|
|
|
|
|
let b = HigherPrecisionBalance::from(b);
|
|
|
|
|
let c = HigherPrecisionBalance::from(c);
|
|
|
|
|
|
|
|
|
|
let result =
|
|
|
|
|
a.checked_mul(b).ok_or(Error::<T>::Overflow)?.checked_div(c).ok_or(Error::<T>::Overflow)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
result.try_into().map_err(|_| Error::<T>::Overflow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Calculates amount out.
|
|
|
|
|
///
|
|
|
|
|
/// Given an input amount of an coin and pair reserves, returns the maximum output amount
|
|
|
|
|
/// of the other coin.
|
|
|
|
|
pub fn get_amount_out(
|
2023-11-12 14:37:31 +03:00
|
|
|
amount_in: SubstrateAmount,
|
|
|
|
|
reserve_in: SubstrateAmount,
|
|
|
|
|
reserve_out: SubstrateAmount,
|
|
|
|
|
) -> Result<SubstrateAmount, Error<T>> {
|
|
|
|
|
let amount_in = HigherPrecisionBalance::from(amount_in);
|
|
|
|
|
let reserve_in = HigherPrecisionBalance::from(reserve_in);
|
|
|
|
|
let reserve_out = HigherPrecisionBalance::from(reserve_out);
|
|
|
|
|
|
|
|
|
|
if (reserve_in == 0) || (reserve_out == 0) {
|
2023-11-05 20:02:34 +03:00
|
|
|
return Err(Error::<T>::ZeroLiquidity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let amount_in_with_fee = amount_in
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_mul(
|
|
|
|
|
HigherPrecisionBalance::from(1000u32) - HigherPrecisionBalance::from(T::LPFee::get()),
|
|
|
|
|
)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let numerator = amount_in_with_fee.checked_mul(reserve_out).ok_or(Error::<T>::Overflow)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
let denominator = reserve_in
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_mul(1000u32.into())
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_add(amount_in_with_fee)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let result = numerator.checked_div(denominator).ok_or(Error::<T>::Overflow)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
result.try_into().map_err(|_| Error::<T>::Overflow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Calculates amount in.
|
|
|
|
|
///
|
|
|
|
|
/// Given an output amount of an coin and pair reserves, returns a required input amount
|
|
|
|
|
/// of the other coin.
|
|
|
|
|
pub fn get_amount_in(
|
2023-11-12 14:37:31 +03:00
|
|
|
amount_out: SubstrateAmount,
|
|
|
|
|
reserve_in: SubstrateAmount,
|
|
|
|
|
reserve_out: SubstrateAmount,
|
|
|
|
|
) -> Result<SubstrateAmount, Error<T>> {
|
|
|
|
|
let amount_out = HigherPrecisionBalance::from(amount_out);
|
|
|
|
|
let reserve_in = HigherPrecisionBalance::from(reserve_in);
|
|
|
|
|
let reserve_out = HigherPrecisionBalance::from(reserve_out);
|
|
|
|
|
|
|
|
|
|
if (reserve_in == 0) || (reserve_out == 0) {
|
2023-11-05 20:02:34 +03:00
|
|
|
Err(Error::<T>::ZeroLiquidity)?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if amount_out >= reserve_out {
|
|
|
|
|
Err(Error::<T>::AmountOutTooHigh)?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let numerator = reserve_in
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_mul(amount_out)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_mul(1000u32.into())
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?;
|
|
|
|
|
|
|
|
|
|
let denominator = reserve_out
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_sub(amount_out)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_mul(
|
|
|
|
|
HigherPrecisionBalance::from(1000u32) - HigherPrecisionBalance::from(T::LPFee::get()),
|
|
|
|
|
)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?;
|
|
|
|
|
|
|
|
|
|
let result = numerator
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_div(denominator)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?
|
2023-11-12 14:37:31 +03:00
|
|
|
.checked_add(1)
|
2023-11-05 20:02:34 +03:00
|
|
|
.ok_or(Error::<T>::Overflow)?;
|
|
|
|
|
|
|
|
|
|
result.try_into().map_err(|_| Error::<T>::Overflow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ensure that a path is valid.
|
|
|
|
|
fn validate_swap_path(
|
2023-11-12 14:37:31 +03:00
|
|
|
path: &BoundedVec<Coin, T::MaxSwapPathLength>,
|
2023-11-05 20:02:34 +03:00
|
|
|
) -> Result<(), DispatchError> {
|
|
|
|
|
ensure!(path.len() >= 2, Error::<T>::InvalidPath);
|
|
|
|
|
|
|
|
|
|
// validate all the pools in the path are unique
|
2023-11-12 14:37:31 +03:00
|
|
|
let mut pools = BoundedBTreeSet::<PoolId, T::MaxSwapPathLength>::new();
|
2023-11-05 20:02:34 +03:00
|
|
|
for coins_pair in path.windows(2) {
|
|
|
|
|
if let [coin1, coin2] = coins_pair {
|
2023-11-12 14:37:31 +03:00
|
|
|
let pool_id = Self::get_pool_id(*coin1, *coin2)?;
|
2023-11-05 20:02:34 +03:00
|
|
|
let new_element = pools.try_insert(pool_id).map_err(|_| Error::<T>::Overflow)?;
|
|
|
|
|
if !new_element {
|
|
|
|
|
return Err(Error::<T>::NonUniquePath.into());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
impl<T: Config> Swap<T::AccountId, HigherPrecisionBalance, Coin> for Pallet<T> {
|
2023-11-05 20:02:34 +03:00
|
|
|
fn swap_exact_tokens_for_tokens(
|
|
|
|
|
sender: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
path: Vec<Coin>,
|
|
|
|
|
amount_in: HigherPrecisionBalance,
|
|
|
|
|
amount_out_min: Option<HigherPrecisionBalance>,
|
2023-11-05 20:02:34 +03:00
|
|
|
send_to: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Result<HigherPrecisionBalance, DispatchError> {
|
2023-11-05 20:02:34 +03:00
|
|
|
let path = path.try_into().map_err(|_| Error::<T>::PathError)?;
|
|
|
|
|
let amount_out_min = amount_out_min.map(Self::convert_hpb_to_coin_balance).transpose()?;
|
|
|
|
|
let amount_out = Self::do_swap_exact_tokens_for_tokens(
|
|
|
|
|
sender,
|
|
|
|
|
path,
|
|
|
|
|
Self::convert_hpb_to_coin_balance(amount_in)?,
|
|
|
|
|
amount_out_min,
|
|
|
|
|
send_to,
|
|
|
|
|
)?;
|
|
|
|
|
Ok(amount_out.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn swap_tokens_for_exact_tokens(
|
|
|
|
|
sender: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
path: Vec<Coin>,
|
|
|
|
|
amount_out: HigherPrecisionBalance,
|
|
|
|
|
amount_in_max: Option<HigherPrecisionBalance>,
|
2023-11-05 20:02:34 +03:00
|
|
|
send_to: T::AccountId,
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Result<HigherPrecisionBalance, DispatchError> {
|
2023-11-05 20:02:34 +03:00
|
|
|
let path = path.try_into().map_err(|_| Error::<T>::PathError)?;
|
|
|
|
|
let amount_in_max = amount_in_max.map(Self::convert_hpb_to_coin_balance).transpose()?;
|
|
|
|
|
let amount_in = Self::do_swap_tokens_for_exact_tokens(
|
|
|
|
|
sender,
|
|
|
|
|
path,
|
|
|
|
|
Self::convert_hpb_to_coin_balance(amount_out)?,
|
|
|
|
|
amount_in_max,
|
|
|
|
|
send_to,
|
|
|
|
|
)?;
|
|
|
|
|
Ok(amount_in.into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sp_api::decl_runtime_apis! {
|
|
|
|
|
/// This runtime api allows people to query the size of the liquidity pools
|
|
|
|
|
/// and quote prices for swaps.
|
2023-11-12 14:37:31 +03:00
|
|
|
pub trait DexApi {
|
2023-11-05 20:02:34 +03:00
|
|
|
/// Provides a quote for [`Pallet::swap_tokens_for_exact_tokens`].
|
|
|
|
|
///
|
|
|
|
|
/// Note that the price may have changed by the time the transaction is executed.
|
|
|
|
|
/// (Use `amount_in_max` to control slippage.)
|
|
|
|
|
fn quote_price_tokens_for_exact_tokens(
|
2023-11-12 14:37:31 +03:00
|
|
|
coin1: Coin,
|
|
|
|
|
coin2: Coin,
|
|
|
|
|
amount: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
include_fee: bool
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Option<SubstrateAmount>;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
/// Provides a quote for [`Pallet::swap_exact_tokens_for_tokens`].
|
|
|
|
|
///
|
|
|
|
|
/// Note that the price may have changed by the time the transaction is executed.
|
|
|
|
|
/// (Use `amount_out_min` to control slippage.)
|
|
|
|
|
fn quote_price_exact_tokens_for_tokens(
|
2023-11-12 14:37:31 +03:00
|
|
|
coin1: Coin,
|
|
|
|
|
coin2: Coin,
|
|
|
|
|
amount: SubstrateAmount,
|
2023-11-05 20:02:34 +03:00
|
|
|
include_fee: bool
|
2023-11-12 14:37:31 +03:00
|
|
|
) -> Option<SubstrateAmount>;
|
2023-11-05 20:02:34 +03:00
|
|
|
|
|
|
|
|
/// Returns the size of the liquidity pool for the given coin pair.
|
2023-11-12 14:37:31 +03:00
|
|
|
fn get_reserves(coin1: Coin, coin2: Coin) -> Option<(SubstrateAmount, SubstrateAmount)>;
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sp_core::generate_feature_enabled_macro!(
|
|
|
|
|
runtime_benchmarks_enabled,
|
|
|
|
|
feature = "runtime-benchmarks",
|
|
|
|
|
$
|
|
|
|
|
);
|