mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Add Dex pallet (#407)
* Move pallet-asset-conversion * update licensing * initial integration * Integrate Currency & Assets types * integrate liquidity tokens * fmt * integrate dex pallet tests * fmt * compilation error fixes * integrate dex benchmarks * fmt * cargo clippy * replace all occurrences of "asset" with "coin" * add the actual add liq/swap logic to in-instructions * add client side & tests * fix deny * Lint and changes - Renames InInstruction::AddLiquidity to InInstruction::SwapAndAddLiquidity - Makes create_pool an internal function - Makes dex-pallet exclusively create pools against a native coin - Removes various fees - Adds new crates to GH workflow * Fix rebase artifacts * Correct other rebase artifact * Correct CI specification for liquidity-tokens * Correct primitives' test to the standardized pallet account scheme --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
46
substrate/liquidity-tokens/pallet/Cargo.toml
Normal file
46
substrate/liquidity-tokens/pallet/Cargo.toml
Normal file
@@ -0,0 +1,46 @@
|
||||
[package]
|
||||
name = "serai-liquidity-tokens-pallet"
|
||||
version = "0.1.0"
|
||||
description = "liquidity tokens pallet for Serai"
|
||||
license = "AGPL-3.0-only"
|
||||
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/liquidity-tokens/pallet"
|
||||
authors = ["Akil Demir <aeg_asd@hotmail.com>"]
|
||||
edition = "2021"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[dependencies]
|
||||
parity-scale-codec = { version = "3", default-features = false, features = ["derive"] }
|
||||
scale-info = { version = "2", default-features = false, features = ["derive"] }
|
||||
|
||||
frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
frame-support = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
dex-primitives = { package = "serai-dex-primitives", path = "../../dex/primitives", default-features = false }
|
||||
|
||||
serai-primitives = { path = "../../primitives", default-features = false }
|
||||
|
||||
[features]
|
||||
std = [
|
||||
"frame-system/std",
|
||||
"frame-support/std",
|
||||
|
||||
"sp-core/std",
|
||||
"sp-std/std",
|
||||
|
||||
"dex-primitives/std",
|
||||
|
||||
"serai-primitives/std",
|
||||
]
|
||||
|
||||
runtime-benchmarks = [
|
||||
"frame-system/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
]
|
||||
|
||||
default = ["std"]
|
||||
15
substrate/liquidity-tokens/pallet/LICENSE
Normal file
15
substrate/liquidity-tokens/pallet/LICENSE
Normal file
@@ -0,0 +1,15 @@
|
||||
AGPL-3.0-only license
|
||||
|
||||
Copyright (c) 2023 Luke Parker
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License Version 3 as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
152
substrate/liquidity-tokens/pallet/src/lib.rs
Normal file
152
substrate/liquidity-tokens/pallet/src/lib.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use sp_core::sr25519::Public;
|
||||
use sp_std::vec::Vec;
|
||||
use frame_support::pallet_prelude::*;
|
||||
|
||||
use dex_primitives::LiquidityTokens;
|
||||
use serai_primitives::*;
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config<AccountId = Public> {
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
AmountOverflowed,
|
||||
NotEnoughCoins,
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
LtMint { to: Public, token: u32, amount: Amount },
|
||||
LtBurn { from: Public, token: u32, amount: Amount },
|
||||
}
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
/// The amount of coins each account has.
|
||||
// Identity is used as the second key's hasher due to it being a non-manipulatable fixed-space
|
||||
// ID.
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn balances)]
|
||||
pub type Balances<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
Public,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
SubstrateAmount,
|
||||
OptionQuery,
|
||||
>;
|
||||
|
||||
/// The total supply of each coin.
|
||||
// We use Identity type here again due to reasons stated in the Balances Storage.
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn supply)]
|
||||
pub type Supply<T: Config> = StorageMap<_, Blake2_128Concat, u32, SubstrateAmount, ValueQuery>;
|
||||
|
||||
// TODO: apis: supply, mint, burn, transfer
|
||||
impl<T: Config> Pallet<T> {
|
||||
/// Returns the balance of a given account for `token`.
|
||||
pub fn balance(token: u32, of: T::AccountId) -> SubstrateAmount {
|
||||
Self::balances(of, token).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Mint `balance` to the given account.
|
||||
///
|
||||
/// Errors if any amount overflows.
|
||||
pub fn mint_into(token: u32, to: Public, amount: SubstrateAmount) -> Result<(), Error<T>> {
|
||||
let balance = Self::balances(to, token).unwrap_or(0);
|
||||
|
||||
// update the balance
|
||||
let new_amount = balance.checked_add(amount).ok_or(Error::<T>::AmountOverflowed)?;
|
||||
|
||||
// save
|
||||
Balances::<T>::set(to, token, Some(new_amount));
|
||||
|
||||
// update the supply
|
||||
let new_supply =
|
||||
Self::supply(token).checked_add(amount).ok_or(Error::<T>::AmountOverflowed)?;
|
||||
Supply::<T>::set(token, new_supply);
|
||||
|
||||
Self::deposit_event(Event::LtMint { to, token, amount: Amount(amount) });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Burn `balance` from the specified account.
|
||||
pub fn burn_from(token: u32, from: Public, amount: SubstrateAmount) -> Result<(), Error<T>> {
|
||||
let balance = Self::balances(from, token);
|
||||
if balance.is_none() {
|
||||
Err(Error::<T>::NotEnoughCoins)?;
|
||||
}
|
||||
|
||||
// update the balance
|
||||
let new_amount = balance.unwrap().checked_sub(amount).ok_or(Error::<T>::NotEnoughCoins)?;
|
||||
|
||||
// save
|
||||
if new_amount == 0 {
|
||||
Balances::<T>::remove(from, token);
|
||||
} else {
|
||||
Balances::<T>::set(from, token, Some(new_amount));
|
||||
}
|
||||
|
||||
// update the supply
|
||||
let new_supply = Self::supply(token).checked_sub(amount).unwrap();
|
||||
if new_supply == 0 {
|
||||
Supply::<T>::remove(token);
|
||||
} else {
|
||||
Supply::<T>::set(token, new_supply);
|
||||
}
|
||||
|
||||
Self::deposit_event(Event::LtBurn { from, token, amount: Amount(amount) });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn total_issuance(token: u32) -> SubstrateAmount {
|
||||
Supply::<T>::get(token)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> LiquidityTokens<T::AccountId> for Pallet<T> {
|
||||
type Balance = SubstrateAmount;
|
||||
type CoinId = u32;
|
||||
|
||||
fn mint_into(
|
||||
token: Self::CoinId,
|
||||
to: &Public,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
Self::mint_into(token, *to, amount)?;
|
||||
Ok(amount)
|
||||
}
|
||||
|
||||
fn burn_from(
|
||||
token: Self::CoinId,
|
||||
from: &Public,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
Self::burn_from(token, *from, amount)?;
|
||||
Ok(amount)
|
||||
}
|
||||
|
||||
fn total_issuance(token: Self::CoinId) -> Self::Balance {
|
||||
Self::total_issuance(token)
|
||||
}
|
||||
|
||||
fn coin_ids() -> Vec<Self::CoinId> {
|
||||
Supply::<T>::iter_keys().collect::<Vec<Self::CoinId>>()
|
||||
}
|
||||
|
||||
fn balance(token: Self::CoinId, of: &Public) -> Self::Balance {
|
||||
Self::balance(token, *of)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub use pallet::*;
|
||||
Reference in New Issue
Block a user