mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Restore the coins pallet to the runtime
This commit is contained in:
@@ -22,30 +22,26 @@ workspace = true
|
|||||||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
|
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
|
||||||
scale-info = { version = "2", default-features = false, features = ["derive"] }
|
scale-info = { version = "2", default-features = false, features = ["derive"] }
|
||||||
|
|
||||||
frame-system = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
|
||||||
frame-support = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
|
||||||
|
|
||||||
sp-core = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
sp-core = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
||||||
sp-std = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
sp-std = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
||||||
sp-runtime = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
sp-runtime = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
||||||
|
|
||||||
pallet-transaction-payment = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
frame-system = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
||||||
|
frame-support = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
||||||
|
|
||||||
serai-primitives = { path = "../primitives", default-features = false }
|
serai-primitives = { path = "../primitives", default-features = false, features = ["serde", "non_canonical_scale_derivations"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
sp-io = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false, features = ["std"] }
|
sp-io = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false, features = ["std"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
std = [
|
std = [
|
||||||
"frame-system/std",
|
|
||||||
"frame-support/std",
|
|
||||||
|
|
||||||
"sp-core/std",
|
"sp-core/std",
|
||||||
"sp-std/std",
|
"sp-std/std",
|
||||||
"sp-runtime/std",
|
"sp-runtime/std",
|
||||||
|
|
||||||
"pallet-transaction-payment/std",
|
"frame-system/std",
|
||||||
|
"frame-support/std",
|
||||||
|
|
||||||
"serai-primitives/std",
|
"serai-primitives/std",
|
||||||
]
|
]
|
||||||
@@ -53,8 +49,6 @@ std = [
|
|||||||
try-runtime = [
|
try-runtime = [
|
||||||
"frame-system/try-runtime",
|
"frame-system/try-runtime",
|
||||||
"frame-support/try-runtime",
|
"frame-support/try-runtime",
|
||||||
|
|
||||||
"sp-runtime/try-runtime",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
runtime-benchmarks = [
|
runtime-benchmarks = [
|
||||||
|
|||||||
3
substrate/coins/README.md
Normal file
3
substrate/coins/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Coins Pallet
|
||||||
|
|
||||||
|
Pallet implementing the necessary coins logic for the Serai protocol.
|
||||||
@@ -1,109 +1,150 @@
|
|||||||
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||||
|
#![doc = include_str!("../README.md")]
|
||||||
|
#![deny(missing_docs)]
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod mock;
|
mod mock;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
use serai_primitives::{Balance, Coin, ExternalBalance, SubstrateAmount};
|
use serai_primitives::balance::ExternalBalance;
|
||||||
|
|
||||||
|
/// The decider for if a mint is allowed or not.
|
||||||
pub trait AllowMint {
|
pub trait AllowMint {
|
||||||
|
/// Whether or not the mint of the specified coins is allowed.
|
||||||
fn is_allowed(balance: &ExternalBalance) -> bool;
|
fn is_allowed(balance: &ExternalBalance) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AllowMint for () {
|
/// An `AllowMint` implementor which always returns true.
|
||||||
|
pub struct AlwaysAllowMint;
|
||||||
|
impl AllowMint for AlwaysAllowMint {
|
||||||
fn is_allowed(_: &ExternalBalance) -> bool {
|
fn is_allowed(_: &ExternalBalance) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Investigate why Substrate generates this
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
#[allow(unreachable_patterns, clippy::cast_possible_truncation)]
|
|
||||||
#[frame_support::pallet]
|
#[frame_support::pallet]
|
||||||
pub mod pallet {
|
mod pallet {
|
||||||
use super::*;
|
use core::any::TypeId;
|
||||||
use sp_std::{vec::Vec, any::TypeId};
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
use sp_core::sr25519::Public;
|
use sp_core::sr25519::Public;
|
||||||
use sp_runtime::{
|
|
||||||
traits::{DispatchInfoOf, PostDispatchInfoOf},
|
|
||||||
transaction_validity::{TransactionValidityError, InvalidTransaction},
|
|
||||||
};
|
|
||||||
|
|
||||||
use frame_system::pallet_prelude::*;
|
use frame_system::pallet_prelude::*;
|
||||||
use frame_support::pallet_prelude::*;
|
use frame_support::pallet_prelude::*;
|
||||||
|
|
||||||
use pallet_transaction_payment::{Config as TpConfig, OnChargeTransaction};
|
use serai_primitives::{coin::*, balance::*, instructions::OutInstructionWithBalance};
|
||||||
|
|
||||||
use serai_primitives::*;
|
use super::*;
|
||||||
pub use coins_primitives as primitives;
|
|
||||||
use primitives::*;
|
|
||||||
|
|
||||||
type LiquidityTokensInstance = crate::Instance1;
|
/// The instance used to represent coins on the Serai network.
|
||||||
|
///
|
||||||
|
/// This would either be SRI itself or the sriXYZ coins swappable via pools.
|
||||||
|
pub struct CoinsInstance;
|
||||||
|
/// The instance used to represent liquidity tokens on the Serai network.
|
||||||
|
///
|
||||||
|
/// Coin::XYZ would be considered as the liquidity token for the Coin::SRI - Coin::XYZ pool.
|
||||||
|
pub struct LiquidityTokensInstance;
|
||||||
|
|
||||||
|
/// The configuration of this pallet.
|
||||||
#[pallet::config]
|
#[pallet::config]
|
||||||
pub trait Config<I: 'static = ()>: frame_system::Config<AccountId = Public> {
|
pub trait Config<I: 'static = ()>: frame_system::Config<AccountId = Public> {
|
||||||
|
/// The event type.
|
||||||
type RuntimeEvent: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
type RuntimeEvent: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||||
|
/// What decides if mints are allowed.
|
||||||
type AllowMint: AllowMint;
|
type AllowMint: AllowMint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The genesis state to use for this pallet.
|
||||||
#[pallet::genesis_config]
|
#[pallet::genesis_config]
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
|
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
|
||||||
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
||||||
|
/// The balances to initiate the state with.
|
||||||
|
///
|
||||||
|
/// This is useful for test networks where it's desirable to have some coins available
|
||||||
|
/// immediately.
|
||||||
pub accounts: Vec<(T::AccountId, Balance)>,
|
pub accounts: Vec<(T::AccountId, Balance)>,
|
||||||
pub _ignore: PhantomData<I>,
|
/// PhantomData to bind `I`.
|
||||||
|
pub _instance: PhantomData<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
GenesisConfig { accounts: Default::default(), _ignore: Default::default() }
|
GenesisConfig { accounts: Default::default(), _instance: Default::default() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An error incurred.
|
||||||
#[pallet::error]
|
#[pallet::error]
|
||||||
pub enum Error<T, I = ()> {
|
pub enum Error<T, I = ()> {
|
||||||
AmountOverflowed,
|
/// The mint wasn't allowed.
|
||||||
NotEnoughCoins,
|
|
||||||
BurnWithInstructionNotAllowed,
|
|
||||||
MintNotAllowed,
|
MintNotAllowed,
|
||||||
|
/// The amount an account had overflowed.
|
||||||
|
AmountOverflowed,
|
||||||
|
/// The account didn't have enough coins for this operation.
|
||||||
|
NotEnoughCoins,
|
||||||
|
/// An Instruction was specified with a Burn when that's unsupported.
|
||||||
|
BurnWithInstructionNotAllowed,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An event emitted.
|
||||||
#[pallet::event]
|
#[pallet::event]
|
||||||
#[pallet::generate_deposit(fn deposit_event)]
|
#[pallet::generate_deposit(fn deposit_event)]
|
||||||
pub enum Event<T: Config<I>, I: 'static = ()> {
|
pub enum Event<T: Config<I>, I: 'static = ()> {
|
||||||
Mint { to: Public, balance: Balance },
|
/// Coins were minted.
|
||||||
Burn { from: Public, balance: Balance },
|
Mint {
|
||||||
BurnWithInstruction { from: Public, instruction: OutInstructionWithBalance },
|
/// The account minted to.
|
||||||
Transfer { from: Public, to: Public, balance: Balance },
|
to: Public,
|
||||||
|
/// The balance minted.
|
||||||
|
balance: Balance,
|
||||||
|
},
|
||||||
|
/// Coins were transferred.
|
||||||
|
Transfer {
|
||||||
|
/// The account transferred from.
|
||||||
|
from: Public,
|
||||||
|
/// The account transferred to.
|
||||||
|
to: Public,
|
||||||
|
/// The balance transferred.
|
||||||
|
balance: Balance,
|
||||||
|
},
|
||||||
|
/// Coins were burnt.
|
||||||
|
Burn {
|
||||||
|
/// The account burnt from.
|
||||||
|
from: Public,
|
||||||
|
/// The balance burnt.
|
||||||
|
balance: Balance,
|
||||||
|
},
|
||||||
|
/// Coins were burnt with an instruction.
|
||||||
|
BurnWithInstruction {
|
||||||
|
/// The account burnt from.
|
||||||
|
from: Public,
|
||||||
|
/// The instruction, and associated balance.
|
||||||
|
instruction: OutInstructionWithBalance,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The Pallet struct.
|
||||||
#[pallet::pallet]
|
#[pallet::pallet]
|
||||||
pub struct Pallet<T, I = ()>(_);
|
pub struct Pallet<T, I = ()>(_);
|
||||||
|
|
||||||
/// The amount of coins each account has.
|
/// 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
|
// Identity is used as the second key's hasher due to Coin being a small, fixed-space ID.
|
||||||
// ID.
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
#[pallet::getter(fn balances)]
|
type Balances<T: Config<I>, I: 'static = ()> =
|
||||||
pub type Balances<T: Config<I>, I: 'static = ()> =
|
StorageDoubleMap<_, Blake2_128Concat, Public, Identity, Coin, Amount, ValueQuery>;
|
||||||
StorageDoubleMap<_, Blake2_128Concat, Public, Identity, Coin, SubstrateAmount, ValueQuery>;
|
|
||||||
|
|
||||||
/// The total supply of each coin.
|
/// The total supply of each coin.
|
||||||
// We use Identity type here again due to reasons stated in the Balances Storage.
|
|
||||||
#[pallet::storage]
|
#[pallet::storage]
|
||||||
#[pallet::getter(fn supply)]
|
type Supply<T: Config<I>, I: 'static = ()> = StorageMap<_, Identity, Coin, Amount, ValueQuery>;
|
||||||
pub type Supply<T: Config<I>, I: 'static = ()> =
|
|
||||||
StorageMap<_, Identity, Coin, SubstrateAmount, ValueQuery>;
|
|
||||||
|
|
||||||
#[pallet::genesis_build]
|
#[pallet::genesis_build]
|
||||||
impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
|
impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
|
||||||
fn build(&self) {
|
fn build(&self) {
|
||||||
// initialize the supply of the coins
|
|
||||||
// TODO: Don't use COINS yet GenesisConfig so we can safely expand COINS
|
|
||||||
for c in &COINS {
|
|
||||||
Supply::<T, I>::set(c, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize the genesis accounts
|
// initialize the genesis accounts
|
||||||
for (account, balance) in &self.accounts {
|
for (account, balance) in &self.accounts {
|
||||||
Pallet::<T, I>::mint(*account, *balance).unwrap();
|
Pallet::<T, I>::mint(*account, *balance).unwrap();
|
||||||
@@ -111,36 +152,29 @@ pub mod pallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pallet::hooks]
|
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||||
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
|
/// Returns the balance of `coin` for the specified account.
|
||||||
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
|
pub fn balance(
|
||||||
// burn the fees collected previous block
|
of: impl scale::EncodeLike<Public>,
|
||||||
let coin = Coin::Serai;
|
coin: impl scale::EncodeLike<Coin>,
|
||||||
let amount = Self::balance(FEE_ACCOUNT.into(), coin);
|
) -> Amount {
|
||||||
// we can unwrap, we are not burning more then what we have
|
Balances::<T, I>::get(of, coin)
|
||||||
// If this errors, it'll halt the runtime however (due to being called at the start of every
|
|
||||||
// block), requiring extra care when reviewing
|
|
||||||
Self::burn_internal(FEE_ACCOUNT.into(), Balance { coin, amount }).unwrap();
|
|
||||||
Weight::zero() // TODO
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
/// Returns the supply of `coin`.
|
||||||
/// Returns the balance of a given account for `coin`.
|
pub fn supply(coin: impl scale::EncodeLike<Coin>) -> Amount {
|
||||||
pub fn balance(of: Public, coin: Coin) -> Amount {
|
Supply::<T, I>::get(coin)
|
||||||
Amount(Self::balances(of, coin))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decrease_balance_internal(from: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
fn decrease_balance_internal(from: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
||||||
let coin = &balance.coin;
|
let coin = &balance.coin;
|
||||||
|
|
||||||
// sub amount from account
|
// sub amount from account
|
||||||
let new_amount = Self::balances(from, coin)
|
let new_amount =
|
||||||
.checked_sub(balance.amount.0)
|
(Self::balance(from, coin) - balance.amount).ok_or(Error::<T, I>::NotEnoughCoins)?;
|
||||||
.ok_or(Error::<T, I>::NotEnoughCoins)?;
|
|
||||||
|
|
||||||
// save
|
// save
|
||||||
if new_amount == 0 {
|
if new_amount == Amount(0) {
|
||||||
Balances::<T, I>::remove(from, coin);
|
Balances::<T, I>::remove(from, coin);
|
||||||
} else {
|
} else {
|
||||||
Balances::<T, I>::set(from, coin, new_amount);
|
Balances::<T, I>::set(from, coin, new_amount);
|
||||||
@@ -152,9 +186,8 @@ pub mod pallet {
|
|||||||
let coin = &balance.coin;
|
let coin = &balance.coin;
|
||||||
|
|
||||||
// add amount to account
|
// add amount to account
|
||||||
let new_amount = Self::balances(to, coin)
|
let new_amount =
|
||||||
.checked_add(balance.amount.0)
|
(Self::balance(to, coin) + balance.amount).ok_or(Error::<T, I>::AmountOverflowed)?;
|
||||||
.ok_or(Error::<T, I>::AmountOverflowed)?;
|
|
||||||
|
|
||||||
// save
|
// save
|
||||||
Balances::<T, I>::set(to, coin, new_amount);
|
Balances::<T, I>::set(to, coin, new_amount);
|
||||||
@@ -165,21 +198,22 @@ pub mod pallet {
|
|||||||
///
|
///
|
||||||
/// Errors if any amount overflows.
|
/// Errors if any amount overflows.
|
||||||
pub fn mint(to: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
pub fn mint(to: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
||||||
// If the coin isn't Serai, which we're always allowed to mint, and the mint isn't explicitly
|
|
||||||
// allowed, error
|
|
||||||
if !ExternalCoin::try_from(balance.coin)
|
|
||||||
.map(|coin| T::AllowMint::is_allowed(&ExternalBalance { coin, amount: balance.amount }))
|
|
||||||
.unwrap_or(true)
|
|
||||||
{
|
{
|
||||||
|
// If this is an external coin, check if we can mint it
|
||||||
|
let external_balance = ExternalBalance::try_from(balance);
|
||||||
|
let can_mint_external = external_balance.as_ref().map(T::AllowMint::is_allowed);
|
||||||
|
// If it was native to the Serai network, we can always mint it
|
||||||
|
let can_mint = can_mint_external.unwrap_or(true);
|
||||||
|
if !can_mint {
|
||||||
Err(Error::<T, I>::MintNotAllowed)?;
|
Err(Error::<T, I>::MintNotAllowed)?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// update the balance
|
// update the balance
|
||||||
Self::increase_balance_internal(to, balance)?;
|
Self::increase_balance_internal(to, balance)?;
|
||||||
|
|
||||||
// update the supply
|
// update the supply
|
||||||
let new_supply = Self::supply(balance.coin)
|
let new_supply = (Supply::<T, I>::get(balance.coin) + balance.amount)
|
||||||
.checked_add(balance.amount.0)
|
|
||||||
.ok_or(Error::<T, I>::AmountOverflowed)?;
|
.ok_or(Error::<T, I>::AmountOverflowed)?;
|
||||||
Supply::<T, I>::set(balance.coin, new_supply);
|
Supply::<T, I>::set(balance.coin, new_supply);
|
||||||
|
|
||||||
@@ -189,27 +223,25 @@ pub mod pallet {
|
|||||||
|
|
||||||
/// Burn `balance` from the specified account.
|
/// Burn `balance` from the specified account.
|
||||||
fn burn_internal(from: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
fn burn_internal(from: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
||||||
// don't waste time if amount == 0
|
|
||||||
if balance.amount.0 == 0 {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// update the balance
|
// update the balance
|
||||||
Self::decrease_balance_internal(from, balance)?;
|
Self::decrease_balance_internal(from, balance)?;
|
||||||
|
|
||||||
// update the supply
|
// update the supply
|
||||||
let new_supply = Self::supply(balance.coin).checked_sub(balance.amount.0).unwrap();
|
Supply::<T, I>::mutate(balance.coin, |supply| {
|
||||||
Supply::<T, I>::set(balance.coin, new_supply);
|
// We can unwrap here as we're burning an amount legitimately in the system (per successful
|
||||||
|
// decrease), so the supply must be greater than this value
|
||||||
|
let new_supply = (*supply - balance.amount).unwrap();
|
||||||
|
*supply = new_supply;
|
||||||
|
});
|
||||||
|
|
||||||
|
// We don't emit the event here, but rather at the call-site, due to being unsure if we
|
||||||
|
// should emit `Burn` or `BurnWithInstruction`
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transfer `balance` from `from` to `to`.
|
/// Transfer `balance` from `from` to `to`.
|
||||||
pub fn transfer_internal(
|
fn transfer_internal(from: Public, to: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
||||||
from: Public,
|
|
||||||
to: Public,
|
|
||||||
balance: Balance,
|
|
||||||
) -> Result<(), Error<T, I>> {
|
|
||||||
// update balances of accounts
|
// update balances of accounts
|
||||||
Self::decrease_balance_internal(from, balance)?;
|
Self::decrease_balance_internal(from, balance)?;
|
||||||
Self::increase_balance_internal(to, balance)?;
|
Self::increase_balance_internal(to, balance)?;
|
||||||
@@ -220,6 +252,7 @@ pub mod pallet {
|
|||||||
|
|
||||||
#[pallet::call]
|
#[pallet::call]
|
||||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||||
|
/// Transfer `balance` from the signer to `to`.
|
||||||
#[pallet::call_index(0)]
|
#[pallet::call_index(0)]
|
||||||
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
||||||
pub fn transfer(origin: OriginFor<T>, to: Public, balance: Balance) -> DispatchResult {
|
pub fn transfer(origin: OriginFor<T>, to: Public, balance: Balance) -> DispatchResult {
|
||||||
@@ -228,7 +261,7 @@ pub mod pallet {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Burn `balance` from the caller.
|
/// Burn `balance` from the signer.
|
||||||
#[pallet::call_index(1)]
|
#[pallet::call_index(1)]
|
||||||
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
||||||
pub fn burn(origin: OriginFor<T>, balance: Balance) -> DispatchResult {
|
pub fn burn(origin: OriginFor<T>, balance: Balance) -> DispatchResult {
|
||||||
@@ -238,14 +271,15 @@ pub mod pallet {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Burn `balance` with `OutInstructionWithBalance` from the caller.
|
/// Burn `balance` from the signer with `instruction`.
|
||||||
#[pallet::call_index(2)]
|
#[pallet::call_index(2)]
|
||||||
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
||||||
pub fn burn_with_instruction(
|
pub fn burn_with_instruction(
|
||||||
origin: OriginFor<T>,
|
origin: OriginFor<T>,
|
||||||
instruction: OutInstructionWithBalance,
|
instruction: OutInstructionWithBalance,
|
||||||
) -> DispatchResult {
|
) -> DispatchResult {
|
||||||
if TypeId::of::<I>() == TypeId::of::<LiquidityTokensInstance>() {
|
// Only allow specifying an instruction if this is Coins, not LiquidityTokens
|
||||||
|
if TypeId::of::<I>() != TypeId::of::<CoinsInstance>() {
|
||||||
Err(Error::<T, I>::BurnWithInstructionNotAllowed)?;
|
Err(Error::<T, I>::BurnWithInstructionNotAllowed)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,49 +289,6 @@ pub mod pallet {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Config> OnChargeTransaction<T> for Pallet<T>
|
|
||||||
where
|
|
||||||
T: TpConfig,
|
|
||||||
{
|
|
||||||
type Balance = SubstrateAmount;
|
|
||||||
type LiquidityInfo = Option<SubstrateAmount>;
|
|
||||||
|
|
||||||
fn withdraw_fee(
|
|
||||||
who: &Public,
|
|
||||||
_call: &T::RuntimeCall,
|
|
||||||
_dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
|
|
||||||
fee: Self::Balance,
|
|
||||||
_tip: Self::Balance,
|
|
||||||
) -> Result<Self::LiquidityInfo, TransactionValidityError> {
|
|
||||||
if fee == 0 {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
let balance = Balance { coin: Coin::Serai, amount: Amount(fee) };
|
|
||||||
match Self::transfer_internal(*who, FEE_ACCOUNT.into(), balance) {
|
|
||||||
Err(_) => Err(InvalidTransaction::Payment)?,
|
|
||||||
Ok(()) => Ok(Some(fee)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn correct_and_deposit_fee(
|
|
||||||
who: &Public,
|
|
||||||
_dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
|
|
||||||
_post_info: &PostDispatchInfoOf<T::RuntimeCall>,
|
|
||||||
corrected_fee: Self::Balance,
|
|
||||||
_tip: Self::Balance,
|
|
||||||
already_withdrawn: Self::LiquidityInfo,
|
|
||||||
) -> Result<(), TransactionValidityError> {
|
|
||||||
if let Some(paid) = already_withdrawn {
|
|
||||||
let refund_amount = paid.saturating_sub(corrected_fee);
|
|
||||||
let balance = Balance { coin: Coin::Serai, amount: Amount(refund_amount) };
|
|
||||||
Self::transfer_internal(FEE_ACCOUNT.into(), *who, balance)
|
|
||||||
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use pallet::*;
|
pub use pallet::*;
|
||||||
|
|||||||
@@ -1,70 +1,37 @@
|
|||||||
//! Test environment for Coins pallet.
|
//! Test environment for Coins pallet.
|
||||||
|
|
||||||
use super::*;
|
use sp_runtime::BuildStorage;
|
||||||
|
|
||||||
use frame_support::{
|
use frame_support::{derive_impl, construct_runtime};
|
||||||
construct_runtime,
|
|
||||||
traits::{ConstU32, ConstU64},
|
|
||||||
};
|
|
||||||
|
|
||||||
use sp_core::{H256, sr25519::Public};
|
use crate::{self as coins, CoinsInstance};
|
||||||
use sp_runtime::{
|
|
||||||
traits::{BlakeTwo256, IdentityLookup},
|
|
||||||
BuildStorage,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate as coins;
|
|
||||||
|
|
||||||
type Block = frame_system::mocking::MockBlock<Test>;
|
|
||||||
|
|
||||||
construct_runtime!(
|
construct_runtime!(
|
||||||
pub enum Test
|
pub enum Test
|
||||||
{
|
{
|
||||||
System: frame_system,
|
System: frame_system,
|
||||||
Coins: coins,
|
Coins: coins::<CoinsInstance>,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
|
||||||
impl frame_system::Config for Test {
|
impl frame_system::Config for Test {
|
||||||
type BaseCallFilter = frame_support::traits::Everything;
|
type AccountId = sp_core::sr25519::Public;
|
||||||
type BlockWeights = ();
|
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
|
||||||
type BlockLength = ();
|
type Block = frame_system::mocking::MockBlock<Test>;
|
||||||
type RuntimeOrigin = RuntimeOrigin;
|
|
||||||
type RuntimeCall = RuntimeCall;
|
|
||||||
type Nonce = u64;
|
|
||||||
type Hash = H256;
|
|
||||||
type Hashing = BlakeTwo256;
|
|
||||||
type AccountId = Public;
|
|
||||||
type Lookup = IdentityLookup<Self::AccountId>;
|
|
||||||
type Block = Block;
|
|
||||||
type RuntimeEvent = RuntimeEvent;
|
|
||||||
type BlockHashCount = ConstU64<250>;
|
|
||||||
type DbWeight = ();
|
|
||||||
type Version = ();
|
|
||||||
type PalletInfo = PalletInfo;
|
|
||||||
type AccountData = ();
|
|
||||||
type OnNewAccount = ();
|
|
||||||
type OnKilledAccount = ();
|
|
||||||
type SystemWeightInfo = ();
|
|
||||||
type SS58Prefix = ();
|
|
||||||
type OnSetCode = ();
|
|
||||||
type MaxConsumers = ConstU32<16>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config for Test {
|
impl crate::Config<CoinsInstance> for Test {
|
||||||
type RuntimeEvent = RuntimeEvent;
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
type AllowMint = crate::AlwaysAllowMint;
|
||||||
type AllowMint = ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
|
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
|
||||||
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
|
let mut storage = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
|
||||||
|
|
||||||
crate::GenesisConfig::<Test> { accounts: vec![], _ignore: Default::default() }
|
crate::GenesisConfig::<Test, CoinsInstance> { accounts: vec![], _instance: Default::default() }
|
||||||
.assimilate_storage(&mut t)
|
.assimilate_storage(&mut storage)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut ext = sp_io::TestExternalities::new(t);
|
storage.into()
|
||||||
ext.execute_with(|| System::set_block_number(0));
|
|
||||||
ext
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
use crate::{mock::*, primitives::*};
|
use sp_core::{Pair as _, sr25519::Pair};
|
||||||
|
|
||||||
use frame_system::RawOrigin;
|
use frame_system::RawOrigin;
|
||||||
use sp_core::Pair;
|
|
||||||
|
|
||||||
use serai_primitives::*;
|
use serai_primitives::{coin::*, balance::*, address::*, instructions::*};
|
||||||
|
|
||||||
pub type CoinsEvent = crate::Event<Test, ()>;
|
use crate::mock::*;
|
||||||
|
|
||||||
|
pub type CoinsEvent = crate::Event<Test, crate::CoinsInstance>;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mint() {
|
fn mint() {
|
||||||
new_test_ext().execute_with(|| {
|
new_test_ext().execute_with(|| {
|
||||||
// minting u64::MAX should work
|
// minting u64::MAX should work
|
||||||
let coin = Coin::Serai;
|
let coin = Coin::Serai;
|
||||||
let to = insecure_pair_from_name("random1").public();
|
let to = Pair::generate().0.public();
|
||||||
let balance = Balance { coin, amount: Amount(u64::MAX) };
|
let balance = Balance { coin, amount: Amount(u64::MAX) };
|
||||||
|
|
||||||
Coins::mint(to, balance).unwrap();
|
Coins::mint(to, balance).unwrap();
|
||||||
@@ -22,7 +22,7 @@ fn mint() {
|
|||||||
assert!(Coins::mint(to, Balance { coin, amount: Amount(1) }).is_err());
|
assert!(Coins::mint(to, Balance { coin, amount: Amount(1) }).is_err());
|
||||||
|
|
||||||
// supply now should be equal to sum of the accounts balance sum
|
// supply now should be equal to sum of the accounts balance sum
|
||||||
assert_eq!(Coins::supply(coin), balance.amount.0);
|
assert_eq!(Coins::supply(coin), balance.amount);
|
||||||
|
|
||||||
// test events
|
// test events
|
||||||
let mint_events = System::events()
|
let mint_events = System::events()
|
||||||
@@ -49,19 +49,19 @@ fn burn_with_instruction() {
|
|||||||
new_test_ext().execute_with(|| {
|
new_test_ext().execute_with(|| {
|
||||||
// mint some coin
|
// mint some coin
|
||||||
let coin = Coin::External(ExternalCoin::Bitcoin);
|
let coin = Coin::External(ExternalCoin::Bitcoin);
|
||||||
let to = insecure_pair_from_name("random1").public();
|
let to = Pair::generate().0.public();
|
||||||
let balance = Balance { coin, amount: Amount(10 * 10u64.pow(coin.decimals())) };
|
let balance = Balance { coin, amount: Amount(10 * 10u64.pow(coin.decimals())) };
|
||||||
|
|
||||||
Coins::mint(to, balance).unwrap();
|
Coins::mint(to, balance).unwrap();
|
||||||
assert_eq!(Coins::balance(to, coin), balance.amount);
|
assert_eq!(Coins::balance(to, coin), balance.amount);
|
||||||
assert_eq!(Coins::supply(coin), balance.amount.0);
|
assert_eq!(Coins::supply(coin), balance.amount);
|
||||||
|
|
||||||
// we shouldn't be able to burn more than what we have
|
// we shouldn't be able to burn more than what we have
|
||||||
let mut instruction = OutInstructionWithBalance {
|
let mut instruction = OutInstructionWithBalance {
|
||||||
instruction: OutInstruction { address: ExternalAddress::new(vec![]).unwrap() },
|
instruction: OutInstruction::Transfer(ExternalAddress::try_from(vec![]).unwrap()),
|
||||||
balance: ExternalBalance {
|
balance: ExternalBalance {
|
||||||
coin: coin.try_into().unwrap(),
|
coin: coin.try_into().unwrap(),
|
||||||
amount: Amount(balance.amount.0 + 1),
|
amount: (balance.amount + Amount(1)).unwrap(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
assert!(
|
assert!(
|
||||||
@@ -74,7 +74,7 @@ fn burn_with_instruction() {
|
|||||||
|
|
||||||
// balance & supply now should be back to 0
|
// balance & supply now should be back to 0
|
||||||
assert_eq!(Coins::balance(to, coin), Amount(0));
|
assert_eq!(Coins::balance(to, coin), Amount(0));
|
||||||
assert_eq!(Coins::supply(coin), 0);
|
assert_eq!(Coins::supply(coin), Amount(0));
|
||||||
|
|
||||||
let burn_events = System::events()
|
let burn_events = System::events()
|
||||||
.iter()
|
.iter()
|
||||||
@@ -100,19 +100,19 @@ fn transfer() {
|
|||||||
new_test_ext().execute_with(|| {
|
new_test_ext().execute_with(|| {
|
||||||
// mint some coin
|
// mint some coin
|
||||||
let coin = Coin::External(ExternalCoin::Bitcoin);
|
let coin = Coin::External(ExternalCoin::Bitcoin);
|
||||||
let from = insecure_pair_from_name("random1").public();
|
let from = Pair::generate().0.public();
|
||||||
let balance = Balance { coin, amount: Amount(10 * 10u64.pow(coin.decimals())) };
|
let balance = Balance { coin, amount: Amount(10 * 10u64.pow(coin.decimals())) };
|
||||||
|
|
||||||
Coins::mint(from, balance).unwrap();
|
Coins::mint(from, balance).unwrap();
|
||||||
assert_eq!(Coins::balance(from, coin), balance.amount);
|
assert_eq!(Coins::balance(from, coin), balance.amount);
|
||||||
assert_eq!(Coins::supply(coin), balance.amount.0);
|
assert_eq!(Coins::supply(coin), balance.amount);
|
||||||
|
|
||||||
// we can't send more than what we have
|
// we can't send more than what we have
|
||||||
let to = insecure_pair_from_name("random2").public();
|
let to = Pair::generate().0.public();
|
||||||
assert!(Coins::transfer(
|
assert!(Coins::transfer(
|
||||||
RawOrigin::Signed(from).into(),
|
RawOrigin::Signed(from).into(),
|
||||||
to,
|
to,
|
||||||
Balance { coin, amount: Amount(balance.amount.0 + 1) }
|
Balance { coin, amount: (balance.amount + Amount(1)).unwrap() }
|
||||||
)
|
)
|
||||||
.is_err());
|
.is_err());
|
||||||
|
|
||||||
@@ -124,6 +124,6 @@ fn transfer() {
|
|||||||
assert_eq!(Coins::balance(to, coin), balance.amount);
|
assert_eq!(Coins::balance(to, coin), balance.amount);
|
||||||
|
|
||||||
// supply shouldn't change
|
// supply shouldn't change
|
||||||
assert_eq!(Coins::supply(coin), balance.amount.0);
|
assert_eq!(Coins::supply(coin), balance.amount);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ bech32 = { version = "0.11", default-features = false }
|
|||||||
rand_core = { version = "0.6", default-features = false, features = ["std"] }
|
rand_core = { version = "0.6", default-features = false, features = ["std"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
non_canonical_scale_derivations = []
|
|
||||||
std = ["zeroize/std", "borsh/std", "ciphersuite/std", "dkg/std", "sp-core/std", "bech32/std"]
|
std = ["zeroize/std", "borsh/std", "ciphersuite/std", "dkg/std", "sp-core/std", "bech32/std"]
|
||||||
|
serde = []
|
||||||
|
non_canonical_scale_derivations = []
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
|
|||||||
@@ -110,6 +110,10 @@ impl core::str::FromStr for SeraiAddress {
|
|||||||
|
|
||||||
/// An address for an external network.
|
/// An address for an external network.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, borsh::BorshSerialize, borsh::BorshDeserialize)]
|
#[derive(Clone, PartialEq, Eq, Debug, borsh::BorshSerialize, borsh::BorshDeserialize)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "non_canonical_scale_derivations",
|
||||||
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
|
)]
|
||||||
pub struct ExternalAddress(
|
pub struct ExternalAddress(
|
||||||
#[borsh(
|
#[borsh(
|
||||||
serialize_with = "crate::borsh_serialize_bounded_vec",
|
serialize_with = "crate::borsh_serialize_bounded_vec",
|
||||||
@@ -124,6 +128,7 @@ impl ExternalAddress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// An error when converting from a `Vec`.
|
/// An error when converting from a `Vec`.
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum FromVecError {
|
pub enum FromVecError {
|
||||||
/// The source `Vec` was too long to be converted.
|
/// The source `Vec` was too long to be converted.
|
||||||
TooLong,
|
TooLong,
|
||||||
|
|||||||
@@ -10,11 +10,15 @@ use crate::coin::{ExternalCoin, Coin};
|
|||||||
pub type AmountRepr = u64;
|
pub type AmountRepr = u64;
|
||||||
|
|
||||||
/// A wrapper used to represent amounts.
|
/// A wrapper used to represent amounts.
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
|
#[rustfmt::skip] // Prevent rustfmt from expanding the following derive into a 10-line monstrosity
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
|
||||||
|
#[derive(Zeroize, BorshSerialize, BorshDeserialize)]
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
feature = "non_canonical_scale_derivations",
|
feature = "non_canonical_scale_derivations",
|
||||||
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
)]
|
)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(sp_core::serde::Serialize, sp_core::serde::Deserialize))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(crate = "sp_core::serde"))]
|
||||||
pub struct Amount(pub AmountRepr);
|
pub struct Amount(pub AmountRepr);
|
||||||
|
|
||||||
impl Add for Amount {
|
impl Add for Amount {
|
||||||
@@ -44,6 +48,8 @@ impl Mul for Amount {
|
|||||||
feature = "non_canonical_scale_derivations",
|
feature = "non_canonical_scale_derivations",
|
||||||
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
)]
|
)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(sp_core::serde::Serialize, sp_core::serde::Deserialize))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(crate = "sp_core::serde"))]
|
||||||
pub struct ExternalBalance {
|
pub struct ExternalBalance {
|
||||||
/// The coin this is a balance for.
|
/// The coin this is a balance for.
|
||||||
pub coin: ExternalCoin,
|
pub coin: ExternalCoin,
|
||||||
@@ -78,6 +84,8 @@ impl Mul<Amount> for ExternalBalance {
|
|||||||
feature = "non_canonical_scale_derivations",
|
feature = "non_canonical_scale_derivations",
|
||||||
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
)]
|
)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(sp_core::serde::Serialize, sp_core::serde::Deserialize))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(crate = "sp_core::serde"))]
|
||||||
pub struct Balance {
|
pub struct Balance {
|
||||||
/// The coin this is a balance for.
|
/// The coin this is a balance for.
|
||||||
pub coin: Coin,
|
pub coin: Coin,
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ use crate::network_id::{ExternalNetworkId, NetworkId};
|
|||||||
feature = "non_canonical_scale_derivations",
|
feature = "non_canonical_scale_derivations",
|
||||||
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
)]
|
)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(sp_core::serde::Serialize, sp_core::serde::Deserialize))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(crate = "sp_core::serde"))]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum ExternalCoin {
|
pub enum ExternalCoin {
|
||||||
/// Bitcoin, from the Bitcoin network.
|
/// Bitcoin, from the Bitcoin network.
|
||||||
@@ -39,6 +41,8 @@ impl ExternalCoin {
|
|||||||
feature = "non_canonical_scale_derivations",
|
feature = "non_canonical_scale_derivations",
|
||||||
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
)]
|
)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(sp_core::serde::Serialize, sp_core::serde::Deserialize))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(crate = "sp_core::serde"))]
|
||||||
pub enum Coin {
|
pub enum Coin {
|
||||||
/// The Serai coin.
|
/// The Serai coin.
|
||||||
Serai,
|
Serai,
|
||||||
@@ -123,4 +127,17 @@ impl Coin {
|
|||||||
Coin::External(c) => c.network().into(),
|
Coin::External(c) => c.network().into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The decimals used for a single human unit of this coin.
|
||||||
|
///
|
||||||
|
/// This may be less than the decimals used for a single human unit of this coin *by defined
|
||||||
|
/// convention*. If so, that means Serai is *truncating* the decimals. A coin which is defined
|
||||||
|
/// as having 8 decimals, while Serai claims it has 4 decimals, will have `0.00019999`
|
||||||
|
/// interpreted as `0.0001` (in human units, in atomic units, 19999 will be interpreted as 1).
|
||||||
|
pub fn decimals(&self) -> u32 {
|
||||||
|
match self {
|
||||||
|
Coin::Serai => 9,
|
||||||
|
Coin::External(c) => c.decimals(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ use crate::{address::ExternalAddress, balance::ExternalBalance};
|
|||||||
|
|
||||||
/// An instruction on how to transfer coins out.
|
/// An instruction on how to transfer coins out.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
|
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "non_canonical_scale_derivations",
|
||||||
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
|
)]
|
||||||
pub enum OutInstruction {
|
pub enum OutInstruction {
|
||||||
/// Transfer to the specified address.
|
/// Transfer to the specified address.
|
||||||
Transfer(ExternalAddress),
|
Transfer(ExternalAddress),
|
||||||
@@ -13,6 +17,10 @@ pub enum OutInstruction {
|
|||||||
|
|
||||||
/// An instruction on how to transfer coins out with the balance to use for the transfer out.
|
/// An instruction on how to transfer coins out with the balance to use for the transfer out.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
|
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "non_canonical_scale_derivations",
|
||||||
|
derive(scale::Encode, scale::Decode, scale::MaxEncodedLen)
|
||||||
|
)]
|
||||||
pub struct OutInstructionWithBalance {
|
pub struct OutInstructionWithBalance {
|
||||||
/// The instruction on how to transfer coins out.
|
/// The instruction on how to transfer coins out.
|
||||||
pub instruction: OutInstruction,
|
pub instruction: OutInstruction,
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ frame-system = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "se
|
|||||||
frame-support = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
frame-support = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
||||||
frame-executive = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
frame-executive = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false }
|
||||||
|
|
||||||
|
serai-coins-pallet = { path = "../coins", default-features = false }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
substrate-wasm-builder = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next" }
|
substrate-wasm-builder = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next" }
|
||||||
|
|
||||||
@@ -47,14 +49,32 @@ std = [
|
|||||||
"sp-runtime/std",
|
"sp-runtime/std",
|
||||||
"sp-api/std",
|
"sp-api/std",
|
||||||
|
|
||||||
|
"serai-abi/std",
|
||||||
|
|
||||||
"frame-system/std",
|
"frame-system/std",
|
||||||
"frame-support/std",
|
"frame-support/std",
|
||||||
"frame-executive/std",
|
"frame-executive/std",
|
||||||
|
|
||||||
"serai-abi/std",
|
"serai-coins-pallet/std",
|
||||||
]
|
]
|
||||||
|
|
||||||
try-runtime = ["sp-runtime/try-runtime", "serai-abi/try-runtime", "frame-system/try-runtime", "frame-support/try-runtime", "frame-executive/try-runtime"]
|
try-runtime = [
|
||||||
runtime-benchmarks = ["sp-runtime/runtime-benchmarks", "frame-system/runtime-benchmarks", "frame-support/runtime-benchmarks"]
|
"sp-runtime/try-runtime",
|
||||||
|
|
||||||
|
"serai-abi/try-runtime",
|
||||||
|
|
||||||
|
"frame-system/try-runtime",
|
||||||
|
"frame-support/try-runtime",
|
||||||
|
"frame-executive/try-runtime",
|
||||||
|
|
||||||
|
"serai-coins-pallet/try-runtime",
|
||||||
|
]
|
||||||
|
|
||||||
|
runtime-benchmarks = [
|
||||||
|
"sp-runtime/runtime-benchmarks",
|
||||||
|
|
||||||
|
"frame-system/runtime-benchmarks",
|
||||||
|
"frame-support/runtime-benchmarks",
|
||||||
|
]
|
||||||
|
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ use serai_abi::{
|
|||||||
primitives::address::SeraiAddress, SubstrateHeader as Header, SubstrateBlock,
|
primitives::address::SeraiAddress, SubstrateHeader as Header, SubstrateBlock,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use serai_coins_pallet::{CoinsInstance, LiquidityTokensInstance};
|
||||||
|
|
||||||
mod core_pallet;
|
mod core_pallet;
|
||||||
|
|
||||||
type Block = SubstrateBlock;
|
type Block = SubstrateBlock;
|
||||||
@@ -74,6 +76,12 @@ mod runtime {
|
|||||||
|
|
||||||
#[runtime::pallet_index(1)]
|
#[runtime::pallet_index(1)]
|
||||||
pub type Core = core_pallet::Pallet<Runtime>;
|
pub type Core = core_pallet::Pallet<Runtime>;
|
||||||
|
|
||||||
|
#[runtime::pallet_index(2)]
|
||||||
|
pub type Coins = serai_coins_pallet::Pallet<Runtime, CoinsInstance>;
|
||||||
|
|
||||||
|
#[runtime::pallet_index(3)]
|
||||||
|
pub type LiquidityTokens = serai_coins_pallet::Pallet<Runtime, LiquidityTokensInstance>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl frame_system::Config for Runtime {
|
impl frame_system::Config for Runtime {
|
||||||
@@ -118,6 +126,15 @@ impl frame_system::Config for Runtime {
|
|||||||
|
|
||||||
impl core_pallet::Config for Runtime {}
|
impl core_pallet::Config for Runtime {}
|
||||||
|
|
||||||
|
impl serai_coins_pallet::Config<CoinsInstance> for Runtime {
|
||||||
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
type AllowMint = serai_coins_pallet::AlwaysAllowMint; // TODO
|
||||||
|
}
|
||||||
|
impl serai_coins_pallet::Config<LiquidityTokensInstance> for Runtime {
|
||||||
|
type RuntimeEvent = RuntimeEvent;
|
||||||
|
type AllowMint = serai_coins_pallet::AlwaysAllowMint;
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Option<SeraiAddress>> for RuntimeOrigin {
|
impl From<Option<SeraiAddress>> for RuntimeOrigin {
|
||||||
fn from(signer: Option<SeraiAddress>) -> Self {
|
fn from(signer: Option<SeraiAddress>) -> Self {
|
||||||
match signer {
|
match signer {
|
||||||
@@ -133,8 +150,14 @@ impl From<serai_abi::Call> for RuntimeCall {
|
|||||||
serai_abi::Call::Coins(call) => {
|
serai_abi::Call::Coins(call) => {
|
||||||
use serai_abi::coins::Call;
|
use serai_abi::coins::Call;
|
||||||
match call {
|
match call {
|
||||||
Call::transfer { .. } | Call::burn { .. } | Call::burn_with_instruction { .. } => {
|
Call::transfer { to, coins } => {
|
||||||
todo!("TODO")
|
RuntimeCall::Coins(serai_coins_pallet::Call::transfer { to: to.into(), balance: coins })
|
||||||
|
}
|
||||||
|
Call::burn { coins } => {
|
||||||
|
RuntimeCall::Coins(serai_coins_pallet::Call::burn { balance: coins })
|
||||||
|
}
|
||||||
|
Call::burn_with_instruction { instruction } => {
|
||||||
|
RuntimeCall::Coins(serai_coins_pallet::Call::burn_with_instruction { instruction })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -162,8 +185,13 @@ impl From<serai_abi::Call> for RuntimeCall {
|
|||||||
serai_abi::Call::Dex(call) => {
|
serai_abi::Call::Dex(call) => {
|
||||||
use serai_abi::dex::Call;
|
use serai_abi::dex::Call;
|
||||||
match call {
|
match call {
|
||||||
|
Call::transfer_liquidity { to, liquidity_tokens } => {
|
||||||
|
RuntimeCall::LiquidityTokens(serai_coins_pallet::Call::transfer {
|
||||||
|
to: to.into(),
|
||||||
|
balance: liquidity_tokens.into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
Call::add_liquidity { .. } |
|
Call::add_liquidity { .. } |
|
||||||
Call::transfer_liquidity { .. } |
|
|
||||||
Call::remove_liquidity { .. } |
|
Call::remove_liquidity { .. } |
|
||||||
Call::swap_exact { .. } |
|
Call::swap_exact { .. } |
|
||||||
Call::swap_for_exact { .. } => todo!("TODO"),
|
Call::swap_for_exact { .. } => todo!("TODO"),
|
||||||
@@ -236,7 +264,14 @@ impl serai_abi::TransactionContext for Context {
|
|||||||
signer: &SeraiAddress,
|
signer: &SeraiAddress,
|
||||||
fee: serai_abi::primitives::balance::Amount,
|
fee: serai_abi::primitives::balance::Amount,
|
||||||
) -> Result<(), sp_runtime::transaction_validity::TransactionValidityError> {
|
) -> Result<(), sp_runtime::transaction_validity::TransactionValidityError> {
|
||||||
todo!("TODO")
|
use serai_abi::primitives::coin::Coin;
|
||||||
|
if serai_coins_pallet::Pallet::<Runtime, CoinsInstance>::balance(signer, Coin::Serai) >= fee {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(sp_runtime::transaction_validity::TransactionValidityError::Invalid(
|
||||||
|
sp_runtime::transaction_validity::InvalidTransaction::Payment,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_transaction(&self) {
|
fn start_transaction(&self) {
|
||||||
@@ -252,7 +287,16 @@ impl serai_abi::TransactionContext for Context {
|
|||||||
signer: &SeraiAddress,
|
signer: &SeraiAddress,
|
||||||
fee: serai_abi::primitives::balance::Amount,
|
fee: serai_abi::primitives::balance::Amount,
|
||||||
) -> Result<(), sp_runtime::transaction_validity::TransactionValidityError> {
|
) -> Result<(), sp_runtime::transaction_validity::TransactionValidityError> {
|
||||||
todo!("TODO")
|
use serai_abi::primitives::{coin::*, balance::*};
|
||||||
|
serai_coins_pallet::Pallet::<Runtime, CoinsInstance>::burn(
|
||||||
|
RuntimeOrigin::signed(Public::from(*signer)),
|
||||||
|
Balance { coin: Coin::Serai, amount: fee },
|
||||||
|
)
|
||||||
|
.map_err(|_| {
|
||||||
|
sp_runtime::transaction_validity::TransactionValidityError::Invalid(
|
||||||
|
sp_runtime::transaction_validity::InvalidTransaction::Payment,
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
fn end_transaction(&self, transaction_hash: [u8; 32]) {
|
fn end_transaction(&self, transaction_hash: [u8; 32]) {
|
||||||
Core::end_transaction(transaction_hash);
|
Core::end_transaction(transaction_hash);
|
||||||
|
|||||||
Reference in New Issue
Block a user