2023-10-19 13:22:21 +03:00
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
|
|
2023-12-05 16:52:50 +03:00
|
|
|
use serai_primitives::{Coin, SubstrateAmount, Balance};
|
|
|
|
|
|
|
|
|
|
pub trait AllowMint {
|
|
|
|
|
fn is_allowed(balance: &Balance) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AllowMint for () {
|
|
|
|
|
fn is_allowed(_: &Balance) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 13:22:21 +03:00
|
|
|
#[frame_support::pallet]
|
|
|
|
|
pub mod pallet {
|
2023-12-05 16:52:50 +03:00
|
|
|
use super::*;
|
2023-11-12 14:37:31 +03:00
|
|
|
use sp_std::{vec::Vec, any::TypeId};
|
2023-10-19 13:22:21 +03:00
|
|
|
use sp_core::sr25519::Public;
|
|
|
|
|
use sp_runtime::{
|
|
|
|
|
traits::{DispatchInfoOf, PostDispatchInfoOf},
|
|
|
|
|
transaction_validity::{TransactionValidityError, InvalidTransaction},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use frame_system::pallet_prelude::*;
|
|
|
|
|
use frame_support::pallet_prelude::*;
|
|
|
|
|
|
|
|
|
|
use pallet_transaction_payment::{Config as TpConfig, OnChargeTransaction};
|
|
|
|
|
|
|
|
|
|
use serai_primitives::*;
|
|
|
|
|
pub use coins_primitives as primitives;
|
|
|
|
|
use primitives::*;
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
type LiquidityTokensInstance = crate::Instance1;
|
|
|
|
|
|
2023-10-19 13:22:21 +03:00
|
|
|
#[pallet::config]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub trait Config<I: 'static = ()>: frame_system::Config<AccountId = Public> {
|
|
|
|
|
type RuntimeEvent: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
2023-12-05 16:52:50 +03:00
|
|
|
type AllowMint: AllowMint;
|
2023-10-19 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::genesis_config]
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
2023-10-19 06:30:58 -04:00
|
|
|
pub accounts: Vec<(T::AccountId, Balance)>,
|
2023-11-12 14:37:31 +03:00
|
|
|
pub _ignore: PhantomData<I>,
|
2023-10-19 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
2023-10-19 13:22:21 +03:00
|
|
|
fn default() -> Self {
|
2023-11-12 14:37:31 +03:00
|
|
|
GenesisConfig { accounts: Default::default(), _ignore: Default::default() }
|
2023-10-19 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::error]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub enum Error<T, I = ()> {
|
2023-10-19 13:22:21 +03:00
|
|
|
AmountOverflowed,
|
|
|
|
|
NotEnoughCoins,
|
2023-11-12 14:37:31 +03:00
|
|
|
BurnWithInstructionNotAllowed,
|
2023-12-05 16:52:50 +03:00
|
|
|
MintNotAllowed,
|
2023-10-19 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::event]
|
|
|
|
|
#[pallet::generate_deposit(fn deposit_event)]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub enum Event<T: Config<I>, I: 'static = ()> {
|
2023-10-19 13:22:21 +03:00
|
|
|
Mint { to: Public, balance: Balance },
|
2023-11-12 14:37:31 +03:00
|
|
|
Burn { from: Public, balance: Balance },
|
|
|
|
|
BurnWithInstruction { from: Public, instruction: OutInstructionWithBalance },
|
2023-10-19 13:22:21 +03:00
|
|
|
Transfer { from: Public, to: Public, balance: Balance },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::pallet]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub struct Pallet<T, I = ()>(_);
|
2023-10-19 13:22:21 +03:00
|
|
|
|
|
|
|
|
/// 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)]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub type Balances<T: Config<I>, I: 'static = ()> =
|
2023-10-19 07:16:35 -04:00
|
|
|
StorageDoubleMap<_, Blake2_128Concat, Public, Identity, Coin, SubstrateAmount, ValueQuery>;
|
2023-10-19 13:22:21 +03:00
|
|
|
|
|
|
|
|
/// 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)]
|
2023-11-12 14:37:31 +03:00
|
|
|
pub type Supply<T: Config<I>, I: 'static = ()> =
|
|
|
|
|
StorageMap<_, Identity, Coin, SubstrateAmount, ValueQuery>;
|
2023-10-19 13:22:21 +03:00
|
|
|
|
|
|
|
|
#[pallet::genesis_build]
|
2023-11-12 14:37:31 +03:00
|
|
|
impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
|
2023-10-19 13:22:21 +03:00
|
|
|
fn build(&self) {
|
|
|
|
|
// initialize the supply of the coins
|
2023-11-05 20:02:34 +03:00
|
|
|
// TODO: Don't use COINS yet GenesisConfig so we can safely expand COINS
|
|
|
|
|
for c in &COINS {
|
2023-11-12 14:37:31 +03:00
|
|
|
Supply::<T, I>::set(c, 0);
|
2023-10-19 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initialize the genesis accounts
|
2023-11-05 20:02:34 +03:00
|
|
|
for (account, balance) in &self.accounts {
|
2023-11-12 14:37:31 +03:00
|
|
|
Pallet::<T, I>::mint(*account, *balance).unwrap();
|
2023-10-19 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::hooks]
|
2023-11-12 14:37:31 +03:00
|
|
|
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
|
2023-10-19 13:22:21 +03:00
|
|
|
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
|
|
|
|
|
// burn the fees collected previous block
|
|
|
|
|
let coin = Coin::Serai;
|
|
|
|
|
let amount = Self::balance(FEE_ACCOUNT.into(), coin);
|
|
|
|
|
// we can unwrap, we are not burning more then what we have
|
|
|
|
|
// If this errors, it'll halt the runtime however (due to being called at the start of every
|
|
|
|
|
// block), requiring extra care when reviewing
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::burn_internal(FEE_ACCOUNT.into(), Balance { coin, amount }).unwrap();
|
2023-10-19 13:22:21 +03:00
|
|
|
Weight::zero() // TODO
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
2023-10-19 13:22:21 +03:00
|
|
|
/// Returns the balance of a given account for `coin`.
|
|
|
|
|
pub fn balance(of: Public, coin: Coin) -> Amount {
|
|
|
|
|
Amount(Self::balances(of, coin))
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
fn decrease_balance_internal(from: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
2023-10-19 13:22:21 +03:00
|
|
|
let coin = &balance.coin;
|
|
|
|
|
|
|
|
|
|
// sub amount from account
|
|
|
|
|
let new_amount = Self::balances(from, coin)
|
|
|
|
|
.checked_sub(balance.amount.0)
|
2023-11-12 14:37:31 +03:00
|
|
|
.ok_or(Error::<T, I>::NotEnoughCoins)?;
|
2023-10-19 13:22:21 +03:00
|
|
|
|
|
|
|
|
// save
|
|
|
|
|
if new_amount == 0 {
|
2023-11-12 14:37:31 +03:00
|
|
|
Balances::<T, I>::remove(from, coin);
|
2023-10-19 13:22:21 +03:00
|
|
|
} else {
|
2023-11-12 14:37:31 +03:00
|
|
|
Balances::<T, I>::set(from, coin, new_amount);
|
2023-10-19 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
fn increase_balance_internal(to: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
2023-10-19 13:22:21 +03:00
|
|
|
let coin = &balance.coin;
|
|
|
|
|
|
|
|
|
|
// sub amount from account
|
|
|
|
|
let new_amount = Self::balances(to, coin)
|
|
|
|
|
.checked_add(balance.amount.0)
|
2023-11-12 14:37:31 +03:00
|
|
|
.ok_or(Error::<T, I>::AmountOverflowed)?;
|
2023-10-19 13:22:21 +03:00
|
|
|
|
|
|
|
|
// save
|
2023-11-12 14:37:31 +03:00
|
|
|
Balances::<T, I>::set(to, coin, new_amount);
|
2023-10-19 13:22:21 +03:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Mint `balance` to the given account.
|
|
|
|
|
///
|
|
|
|
|
/// Errors if any amount overflows.
|
2023-11-12 14:37:31 +03:00
|
|
|
pub fn mint(to: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
2023-12-05 16:52:50 +03:00
|
|
|
if !T::AllowMint::is_allowed(&balance) {
|
|
|
|
|
Err(Error::<T, I>::MintNotAllowed)?;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 13:22:21 +03:00
|
|
|
// update the balance
|
|
|
|
|
Self::increase_balance_internal(to, balance)?;
|
|
|
|
|
|
|
|
|
|
// update the supply
|
|
|
|
|
let new_supply = Self::supply(balance.coin)
|
|
|
|
|
.checked_add(balance.amount.0)
|
2023-11-12 14:37:31 +03:00
|
|
|
.ok_or(Error::<T, I>::AmountOverflowed)?;
|
|
|
|
|
Supply::<T, I>::set(balance.coin, new_supply);
|
2023-10-19 13:22:21 +03:00
|
|
|
|
|
|
|
|
Self::deposit_event(Event::Mint { to, balance });
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
/// Burn `balance` from the specified account.
|
|
|
|
|
fn burn_internal(from: Public, balance: Balance) -> Result<(), Error<T, I>> {
|
2023-10-19 13:22:21 +03:00
|
|
|
// don't waste time if amount == 0
|
|
|
|
|
if balance.amount.0 == 0 {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update the balance
|
|
|
|
|
Self::decrease_balance_internal(from, balance)?;
|
|
|
|
|
|
|
|
|
|
// update the supply
|
2023-10-19 07:16:35 -04:00
|
|
|
let new_supply = Self::supply(balance.coin).checked_sub(balance.amount.0).unwrap();
|
2023-11-12 14:37:31 +03:00
|
|
|
Supply::<T, I>::set(balance.coin, new_supply);
|
2023-10-19 13:22:21 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Transfer `balance` from `from` to `to`.
|
2023-11-12 14:37:31 +03:00
|
|
|
pub fn transfer_internal(
|
|
|
|
|
from: Public,
|
|
|
|
|
to: Public,
|
|
|
|
|
balance: Balance,
|
|
|
|
|
) -> Result<(), Error<T, I>> {
|
2023-10-19 13:22:21 +03:00
|
|
|
// update balances of accounts
|
|
|
|
|
Self::decrease_balance_internal(from, balance)?;
|
|
|
|
|
Self::increase_balance_internal(to, balance)?;
|
|
|
|
|
Self::deposit_event(Event::Transfer { from, to, balance });
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pallet::call]
|
2023-11-12 14:37:31 +03:00
|
|
|
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
2023-10-19 13:22:21 +03:00
|
|
|
#[pallet::call_index(0)]
|
|
|
|
|
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
|
|
|
|
pub fn transfer(origin: OriginFor<T>, to: Public, balance: Balance) -> DispatchResult {
|
|
|
|
|
let from = ensure_signed(origin)?;
|
|
|
|
|
Self::transfer_internal(from, to, balance)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
/// Burn `balance` from the caller.
|
2023-10-19 13:22:21 +03:00
|
|
|
#[pallet::call_index(1)]
|
|
|
|
|
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
2023-11-12 14:37:31 +03:00
|
|
|
pub fn burn(origin: OriginFor<T>, balance: Balance) -> DispatchResult {
|
2023-10-19 13:22:21 +03:00
|
|
|
let from = ensure_signed(origin)?;
|
2023-11-12 14:37:31 +03:00
|
|
|
Self::burn_internal(from, balance)?;
|
|
|
|
|
Self::deposit_event(Event::Burn { from, balance });
|
2023-10-19 13:22:21 +03:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
/// Burn `balance` with `OutInstructionWithBalance` from the caller.
|
|
|
|
|
/// Errors if called for SRI or Instance1 instance of this pallet.
|
|
|
|
|
#[pallet::call_index(2)]
|
|
|
|
|
#[pallet::weight((0, DispatchClass::Normal))] // TODO
|
|
|
|
|
pub fn burn_with_instruction(
|
|
|
|
|
origin: OriginFor<T>,
|
|
|
|
|
instruction: OutInstructionWithBalance,
|
|
|
|
|
) -> DispatchResult {
|
|
|
|
|
if instruction.balance.coin == Coin::Serai {
|
|
|
|
|
Err(Error::<T, I>::BurnWithInstructionNotAllowed)?;
|
|
|
|
|
}
|
|
|
|
|
if TypeId::of::<I>() == TypeId::of::<LiquidityTokensInstance>() {
|
|
|
|
|
Err(Error::<T, I>::BurnWithInstructionNotAllowed)?;
|
|
|
|
|
}
|
2023-11-05 20:02:34 +03:00
|
|
|
|
2023-11-12 14:37:31 +03:00
|
|
|
let from = ensure_signed(origin)?;
|
|
|
|
|
Self::burn_internal(from, instruction.balance)?;
|
|
|
|
|
Self::deposit_event(Event::BurnWithInstruction { from, instruction });
|
|
|
|
|
Ok(())
|
2023-11-05 20:02:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Config> OnChargeTransaction<T> for Pallet<T>
|
|
|
|
|
where
|
|
|
|
|
T: TpConfig,
|
|
|
|
|
{
|
2023-10-19 13:22:21 +03:00
|
|
|
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::*;
|