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:
@@ -24,6 +24,8 @@ sp-runtime = { git = "https://github.com/serai-dex/substrate", default-features
|
||||
|
||||
pallet-transaction-payment = { 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 }
|
||||
coins-primitives = { package = "serai-coins-primitives", path = "../primitives", default-features = false }
|
||||
|
||||
@@ -38,6 +40,8 @@ std = [
|
||||
|
||||
"pallet-transaction-payment/std",
|
||||
|
||||
"dex-primitives/std",
|
||||
|
||||
"serai-primitives/std",
|
||||
"coins-primitives/std",
|
||||
]
|
||||
|
||||
@@ -14,12 +14,14 @@ pub mod pallet {
|
||||
|
||||
use pallet_transaction_payment::{Config as TpConfig, OnChargeTransaction};
|
||||
|
||||
use dex_primitives::{Currency, Coins as CoinsTrait};
|
||||
|
||||
use serai_primitives::*;
|
||||
pub use coins_primitives as primitives;
|
||||
use primitives::*;
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config<AccountId = Public> + TpConfig {
|
||||
pub trait Config: frame_system::Config<AccountId = Public> {
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
}
|
||||
|
||||
@@ -72,12 +74,13 @@ pub mod pallet {
|
||||
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
// initialize the supply of the coins
|
||||
for c in COINS.iter() {
|
||||
// TODO: Don't use COINS yet GenesisConfig so we can safely expand COINS
|
||||
for c in &COINS {
|
||||
Supply::<T>::set(c, 0);
|
||||
}
|
||||
|
||||
// initialize the genesis accounts
|
||||
for (account, balance) in self.accounts.iter() {
|
||||
for (account, balance) in &self.accounts {
|
||||
Pallet::<T>::mint(*account, *balance).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -214,7 +217,75 @@ pub mod pallet {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> OnChargeTransaction<T> for Pallet<T> {
|
||||
impl<T: Config> Currency<T::AccountId> for Pallet<T> {
|
||||
type Balance = SubstrateAmount;
|
||||
|
||||
fn balance(of: &Public) -> Self::Balance {
|
||||
Self::balance(*of, Coin::Serai).0
|
||||
}
|
||||
|
||||
/// TODO: make sure of coin precision here.
|
||||
fn minimum_balance() -> Self::Balance {
|
||||
1
|
||||
}
|
||||
|
||||
fn transfer(
|
||||
from: &Public,
|
||||
to: &Public,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
let balance = Balance { coin: Coin::Serai, amount: Amount(amount) };
|
||||
Self::transfer_internal(*from, *to, balance)?;
|
||||
Ok(amount)
|
||||
}
|
||||
|
||||
fn mint(to: &Public, amount: Self::Balance) -> Result<Self::Balance, DispatchError> {
|
||||
Self::mint(*to, Balance { coin: Coin::native(), amount: Amount(amount) })?;
|
||||
Ok(amount)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Have DEX implement for Coins, not Coins implement for Coins
|
||||
impl<T: Config> CoinsTrait<T::AccountId> for Pallet<T> {
|
||||
type Balance = SubstrateAmount;
|
||||
type CoinId = Coin;
|
||||
|
||||
// TODO: Swap the order of these arguments
|
||||
fn balance(coin: Self::CoinId, of: &Public) -> Self::Balance {
|
||||
Self::balance(*of, coin).0
|
||||
}
|
||||
|
||||
fn minimum_balance(_: Self::CoinId) -> Self::Balance {
|
||||
1
|
||||
}
|
||||
|
||||
// TODO: Move coin next to amount
|
||||
fn transfer(
|
||||
coin: Self::CoinId,
|
||||
from: &Public,
|
||||
to: &Public,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
let balance = Balance { coin, amount: Amount(amount) };
|
||||
Self::transfer_internal(*from, *to, balance)?;
|
||||
Ok(amount)
|
||||
}
|
||||
|
||||
// TODO: Move coin next to amount
|
||||
fn mint(
|
||||
coin: Self::CoinId,
|
||||
to: &Public,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
Self::mint(*to, Balance { coin, amount: Amount(amount) })?;
|
||||
Ok(amount)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> OnChargeTransaction<T> for Pallet<T>
|
||||
where
|
||||
T: TpConfig,
|
||||
{
|
||||
type Balance = SubstrateAmount;
|
||||
type LiquidityInfo = Option<SubstrateAmount>;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ use scale_info::TypeInfo;
|
||||
|
||||
use serai_primitives::{Balance, SeraiAddress, ExternalAddress, Data, system_address};
|
||||
|
||||
pub const FEE_ACCOUNT: SeraiAddress = system_address(b"FeeAccount");
|
||||
pub const FEE_ACCOUNT: SeraiAddress = system_address(b"Coins-fees");
|
||||
|
||||
#[derive(
|
||||
Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Encode, Decode, MaxEncodedLen, TypeInfo,
|
||||
@@ -46,6 +46,6 @@ fn address() {
|
||||
use sp_runtime::traits::TrailingZeroInput;
|
||||
assert_eq!(
|
||||
FEE_ACCOUNT,
|
||||
SeraiAddress::decode(&mut TrailingZeroInput::new(b"FeeAccount")).unwrap()
|
||||
SeraiAddress::decode(&mut TrailingZeroInput::new(b"Coins-fees")).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user