diff --git a/substrate/coins/Cargo.toml b/substrate/coins/Cargo.toml index f72f9136..4abd525e 100644 --- a/substrate/coins/Cargo.toml +++ b/substrate/coins/Cargo.toml @@ -22,30 +22,26 @@ workspace = true scale = { package = "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/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-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 } -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] sp-io = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next", default-features = false, features = ["std"] } [features] std = [ - "frame-system/std", - "frame-support/std", - "sp-core/std", "sp-std/std", "sp-runtime/std", - "pallet-transaction-payment/std", + "frame-system/std", + "frame-support/std", "serai-primitives/std", ] @@ -53,8 +49,6 @@ std = [ try-runtime = [ "frame-system/try-runtime", "frame-support/try-runtime", - - "sp-runtime/try-runtime", ] runtime-benchmarks = [ diff --git a/substrate/coins/README.md b/substrate/coins/README.md new file mode 100644 index 00000000..af59335b --- /dev/null +++ b/substrate/coins/README.md @@ -0,0 +1,3 @@ +# Coins Pallet + +Pallet implementing the necessary coins logic for the Serai protocol. diff --git a/substrate/coins/src/lib.rs b/substrate/coins/src/lib.rs index 4499f432..7295a6a7 100644 --- a/substrate/coins/src/lib.rs +++ b/substrate/coins/src/lib.rs @@ -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)] +extern crate alloc; + #[cfg(test)] mod mock; #[cfg(test)] 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 { + /// Whether or not the mint of the specified coins is allowed. 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 { true } } -// TODO: Investigate why Substrate generates this -#[allow(unreachable_patterns, clippy::cast_possible_truncation)] +#[allow(clippy::cast_possible_truncation)] #[frame_support::pallet] -pub mod pallet { - use super::*; - use sp_std::{vec::Vec, any::TypeId}; +mod pallet { + use core::any::TypeId; + use alloc::vec::Vec; + 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::{coin::*, balance::*, instructions::OutInstructionWithBalance}; - use serai_primitives::*; - pub use coins_primitives as primitives; - use primitives::*; + use super::*; - 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] pub trait Config: frame_system::Config { + /// The event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// What decides if mints are allowed. type AllowMint: AllowMint; } + /// The genesis state to use for this pallet. #[pallet::genesis_config] #[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)] pub struct GenesisConfig, 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 _ignore: PhantomData, + /// PhantomData to bind `I`. + pub _instance: PhantomData, } impl, I: 'static> Default for GenesisConfig { fn default() -> Self { - GenesisConfig { accounts: Default::default(), _ignore: Default::default() } + GenesisConfig { accounts: Default::default(), _instance: Default::default() } } } + /// An error incurred. #[pallet::error] pub enum Error { - AmountOverflowed, - NotEnoughCoins, - BurnWithInstructionNotAllowed, + /// The mint wasn't allowed. 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::generate_deposit(fn deposit_event)] pub enum Event, I: 'static = ()> { - Mint { to: Public, balance: Balance }, - Burn { from: Public, balance: Balance }, - BurnWithInstruction { from: Public, instruction: OutInstructionWithBalance }, - Transfer { from: Public, to: Public, balance: Balance }, + /// Coins were minted. + Mint { + /// The account minted to. + 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] pub struct Pallet(_); /// 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. + // Identity is used as the second key's hasher due to Coin being a small, fixed-space ID. #[pallet::storage] - #[pallet::getter(fn balances)] - pub type Balances, I: 'static = ()> = - StorageDoubleMap<_, Blake2_128Concat, Public, Identity, Coin, SubstrateAmount, ValueQuery>; + type Balances, I: 'static = ()> = + StorageDoubleMap<_, Blake2_128Concat, Public, Identity, Coin, Amount, ValueQuery>; /// 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, I: 'static = ()> = - StorageMap<_, Identity, Coin, SubstrateAmount, ValueQuery>; + type Supply, I: 'static = ()> = StorageMap<_, Identity, Coin, Amount, ValueQuery>; #[pallet::genesis_build] impl, I: 'static> BuildGenesisConfig for GenesisConfig { 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::::set(c, 0); - } - // initialize the genesis accounts for (account, balance) in &self.accounts { Pallet::::mint(*account, *balance).unwrap(); @@ -111,36 +152,29 @@ pub mod pallet { } } - #[pallet::hooks] - impl, I: 'static> Hooks> for Pallet { - fn on_initialize(_: BlockNumberFor) -> 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 - Self::burn_internal(FEE_ACCOUNT.into(), Balance { coin, amount }).unwrap(); - Weight::zero() // TODO - } - } - impl, I: 'static> Pallet { - /// Returns the balance of a given account for `coin`. - pub fn balance(of: Public, coin: Coin) -> Amount { - Amount(Self::balances(of, coin)) + /// Returns the balance of `coin` for the specified account. + pub fn balance( + of: impl scale::EncodeLike, + coin: impl scale::EncodeLike, + ) -> Amount { + Balances::::get(of, coin) + } + + /// Returns the supply of `coin`. + pub fn supply(coin: impl scale::EncodeLike) -> Amount { + Supply::::get(coin) } fn decrease_balance_internal(from: Public, balance: Balance) -> Result<(), Error> { let coin = &balance.coin; // sub amount from account - let new_amount = Self::balances(from, coin) - .checked_sub(balance.amount.0) - .ok_or(Error::::NotEnoughCoins)?; + let new_amount = + (Self::balance(from, coin) - balance.amount).ok_or(Error::::NotEnoughCoins)?; // save - if new_amount == 0 { + if new_amount == Amount(0) { Balances::::remove(from, coin); } else { Balances::::set(from, coin, new_amount); @@ -152,9 +186,8 @@ pub mod pallet { let coin = &balance.coin; // add amount to account - let new_amount = Self::balances(to, coin) - .checked_add(balance.amount.0) - .ok_or(Error::::AmountOverflowed)?; + let new_amount = + (Self::balance(to, coin) + balance.amount).ok_or(Error::::AmountOverflowed)?; // save Balances::::set(to, coin, new_amount); @@ -165,21 +198,22 @@ pub mod pallet { /// /// Errors if any amount overflows. pub fn mint(to: Public, balance: Balance) -> Result<(), Error> { - // 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) { - Err(Error::::MintNotAllowed)?; + // 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::::MintNotAllowed)?; + } } // update the balance Self::increase_balance_internal(to, balance)?; // update the supply - let new_supply = Self::supply(balance.coin) - .checked_add(balance.amount.0) + let new_supply = (Supply::::get(balance.coin) + balance.amount) .ok_or(Error::::AmountOverflowed)?; Supply::::set(balance.coin, new_supply); @@ -189,27 +223,25 @@ pub mod pallet { /// Burn `balance` from the specified account. fn burn_internal(from: Public, balance: Balance) -> Result<(), Error> { - // 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 - let new_supply = Self::supply(balance.coin).checked_sub(balance.amount.0).unwrap(); - Supply::::set(balance.coin, new_supply); + Supply::::mutate(balance.coin, |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(()) } /// Transfer `balance` from `from` to `to`. - pub fn transfer_internal( - from: Public, - to: Public, - balance: Balance, - ) -> Result<(), Error> { + fn transfer_internal(from: Public, to: Public, balance: Balance) -> Result<(), Error> { // update balances of accounts Self::decrease_balance_internal(from, balance)?; Self::increase_balance_internal(to, balance)?; @@ -220,6 +252,7 @@ pub mod pallet { #[pallet::call] impl, I: 'static> Pallet { + /// Transfer `balance` from the signer to `to`. #[pallet::call_index(0)] #[pallet::weight((0, DispatchClass::Normal))] // TODO pub fn transfer(origin: OriginFor, to: Public, balance: Balance) -> DispatchResult { @@ -228,7 +261,7 @@ pub mod pallet { Ok(()) } - /// Burn `balance` from the caller. + /// Burn `balance` from the signer. #[pallet::call_index(1)] #[pallet::weight((0, DispatchClass::Normal))] // TODO pub fn burn(origin: OriginFor, balance: Balance) -> DispatchResult { @@ -238,14 +271,15 @@ pub mod pallet { Ok(()) } - /// Burn `balance` with `OutInstructionWithBalance` from the caller. + /// Burn `balance` from the signer with `instruction`. #[pallet::call_index(2)] #[pallet::weight((0, DispatchClass::Normal))] // TODO pub fn burn_with_instruction( origin: OriginFor, instruction: OutInstructionWithBalance, ) -> DispatchResult { - if TypeId::of::() == TypeId::of::() { + // Only allow specifying an instruction if this is Coins, not LiquidityTokens + if TypeId::of::() != TypeId::of::() { Err(Error::::BurnWithInstructionNotAllowed)?; } @@ -255,49 +289,6 @@ pub mod pallet { Ok(()) } } - - impl OnChargeTransaction for Pallet - where - T: TpConfig, - { - type Balance = SubstrateAmount; - type LiquidityInfo = Option; - - fn withdraw_fee( - who: &Public, - _call: &T::RuntimeCall, - _dispatch_info: &DispatchInfoOf, - fee: Self::Balance, - _tip: Self::Balance, - ) -> Result { - 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, - _post_info: &PostDispatchInfoOf, - 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::*; diff --git a/substrate/coins/src/mock.rs b/substrate/coins/src/mock.rs index bd4ebc55..abfbde43 100644 --- a/substrate/coins/src/mock.rs +++ b/substrate/coins/src/mock.rs @@ -1,70 +1,37 @@ //! Test environment for Coins pallet. -use super::*; +use sp_runtime::BuildStorage; -use frame_support::{ - construct_runtime, - traits::{ConstU32, ConstU64}, -}; +use frame_support::{derive_impl, construct_runtime}; -use sp_core::{H256, sr25519::Public}; -use sp_runtime::{ - traits::{BlakeTwo256, IdentityLookup}, - BuildStorage, -}; - -use crate as coins; - -type Block = frame_system::mocking::MockBlock; +use crate::{self as coins, CoinsInstance}; construct_runtime!( pub enum Test { System: frame_system, - Coins: coins, + Coins: coins::, } ); +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Nonce = u64; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = Public; - type Lookup = IdentityLookup; - 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>; + type AccountId = sp_core::sr25519::Public; + type Lookup = sp_runtime::traits::IdentityLookup; + type Block = frame_system::mocking::MockBlock; } -impl Config for Test { +impl crate::Config for Test { type RuntimeEvent = RuntimeEvent; - - type AllowMint = (); + type AllowMint = crate::AlwaysAllowMint; } pub(crate) fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + let mut storage = frame_system::GenesisConfig::::default().build_storage().unwrap(); - crate::GenesisConfig:: { accounts: vec![], _ignore: Default::default() } - .assimilate_storage(&mut t) + crate::GenesisConfig:: { accounts: vec![], _instance: Default::default() } + .assimilate_storage(&mut storage) .unwrap(); - let mut ext = sp_io::TestExternalities::new(t); - ext.execute_with(|| System::set_block_number(0)); - ext + storage.into() } diff --git a/substrate/coins/src/tests.rs b/substrate/coins/src/tests.rs index 52b81d37..b3847c68 100644 --- a/substrate/coins/src/tests.rs +++ b/substrate/coins/src/tests.rs @@ -1,18 +1,18 @@ -use crate::{mock::*, primitives::*}; - +use sp_core::{Pair as _, sr25519::Pair}; use frame_system::RawOrigin; -use sp_core::Pair; -use serai_primitives::*; +use serai_primitives::{coin::*, balance::*, address::*, instructions::*}; -pub type CoinsEvent = crate::Event; +use crate::mock::*; + +pub type CoinsEvent = crate::Event; #[test] fn mint() { new_test_ext().execute_with(|| { // minting u64::MAX should work 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) }; Coins::mint(to, balance).unwrap(); @@ -22,7 +22,7 @@ fn mint() { assert!(Coins::mint(to, Balance { coin, amount: Amount(1) }).is_err()); // 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 let mint_events = System::events() @@ -49,19 +49,19 @@ fn burn_with_instruction() { new_test_ext().execute_with(|| { // mint some coin 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())) }; Coins::mint(to, balance).unwrap(); 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 let mut instruction = OutInstructionWithBalance { - instruction: OutInstruction { address: ExternalAddress::new(vec![]).unwrap() }, + instruction: OutInstruction::Transfer(ExternalAddress::try_from(vec![]).unwrap()), balance: ExternalBalance { coin: coin.try_into().unwrap(), - amount: Amount(balance.amount.0 + 1), + amount: (balance.amount + Amount(1)).unwrap(), }, }; assert!( @@ -74,7 +74,7 @@ fn burn_with_instruction() { // balance & supply now should be back to 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() .iter() @@ -100,19 +100,19 @@ fn transfer() { new_test_ext().execute_with(|| { // mint some coin 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())) }; Coins::mint(from, balance).unwrap(); 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 - let to = insecure_pair_from_name("random2").public(); + let to = Pair::generate().0.public(); assert!(Coins::transfer( RawOrigin::Signed(from).into(), to, - Balance { coin, amount: Amount(balance.amount.0 + 1) } + Balance { coin, amount: (balance.amount + Amount(1)).unwrap() } ) .is_err()); @@ -124,6 +124,6 @@ fn transfer() { assert_eq!(Coins::balance(to, coin), balance.amount); // supply shouldn't change - assert_eq!(Coins::supply(coin), balance.amount.0); + assert_eq!(Coins::supply(coin), balance.amount); }) } diff --git a/substrate/primitives/Cargo.toml b/substrate/primitives/Cargo.toml index 255cdd6d..c6c839c0 100644 --- a/substrate/primitives/Cargo.toml +++ b/substrate/primitives/Cargo.toml @@ -32,6 +32,7 @@ bech32 = { version = "0.11", default-features = false } rand_core = { version = "0.6", default-features = false, features = ["std"] } [features] -non_canonical_scale_derivations = [] std = ["zeroize/std", "borsh/std", "ciphersuite/std", "dkg/std", "sp-core/std", "bech32/std"] +serde = [] +non_canonical_scale_derivations = [] default = ["std"] diff --git a/substrate/primitives/src/address.rs b/substrate/primitives/src/address.rs index ee771777..6991b16f 100644 --- a/substrate/primitives/src/address.rs +++ b/substrate/primitives/src/address.rs @@ -110,6 +110,10 @@ impl core::str::FromStr for SeraiAddress { /// An address for an external network. #[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( #[borsh( serialize_with = "crate::borsh_serialize_bounded_vec", @@ -124,6 +128,7 @@ impl ExternalAddress { } /// An error when converting from a `Vec`. +#[derive(Debug)] pub enum FromVecError { /// The source `Vec` was too long to be converted. TooLong, diff --git a/substrate/primitives/src/balance.rs b/substrate/primitives/src/balance.rs index c37846d7..4101cc82 100644 --- a/substrate/primitives/src/balance.rs +++ b/substrate/primitives/src/balance.rs @@ -10,11 +10,15 @@ use crate::coin::{ExternalCoin, Coin}; pub type AmountRepr = u64; /// 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( feature = "non_canonical_scale_derivations", 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); impl Add for Amount { @@ -44,6 +48,8 @@ impl Mul for Amount { feature = "non_canonical_scale_derivations", 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 { /// The coin this is a balance for. pub coin: ExternalCoin, @@ -78,6 +84,8 @@ impl Mul for ExternalBalance { feature = "non_canonical_scale_derivations", 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 { /// The coin this is a balance for. pub coin: Coin, diff --git a/substrate/primitives/src/coin.rs b/substrate/primitives/src/coin.rs index 80405f50..f4d7927f 100644 --- a/substrate/primitives/src/coin.rs +++ b/substrate/primitives/src/coin.rs @@ -13,6 +13,8 @@ use crate::network_id::{ExternalNetworkId, NetworkId}; feature = "non_canonical_scale_derivations", 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] pub enum ExternalCoin { /// Bitcoin, from the Bitcoin network. @@ -39,6 +41,8 @@ impl ExternalCoin { feature = "non_canonical_scale_derivations", 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 { /// The Serai coin. Serai, @@ -123,4 +127,17 @@ impl Coin { 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(), + } + } } diff --git a/substrate/primitives/src/instructions/out.rs b/substrate/primitives/src/instructions/out.rs index 0dc04c62..3468f963 100644 --- a/substrate/primitives/src/instructions/out.rs +++ b/substrate/primitives/src/instructions/out.rs @@ -6,6 +6,10 @@ use crate::{address::ExternalAddress, balance::ExternalBalance}; /// An instruction on how to transfer coins out. #[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 { /// Transfer to the specified address. 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. #[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 { /// The instruction on how to transfer coins out. pub instruction: OutInstruction, diff --git a/substrate/runtime/Cargo.toml b/substrate/runtime/Cargo.toml index 58f56f9a..970c7b3c 100644 --- a/substrate/runtime/Cargo.toml +++ b/substrate/runtime/Cargo.toml @@ -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-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] substrate-wasm-builder = { git = "https://github.com/serai-dex/polkadot-sdk", branch = "serai-next" } @@ -47,14 +49,32 @@ std = [ "sp-runtime/std", "sp-api/std", + "serai-abi/std", + "frame-system/std", "frame-support/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"] -runtime-benchmarks = ["sp-runtime/runtime-benchmarks", "frame-system/runtime-benchmarks", "frame-support/runtime-benchmarks"] +try-runtime = [ + "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"] diff --git a/substrate/runtime/src/lib.rs b/substrate/runtime/src/lib.rs index 8dde5fb8..b5aa080d 100644 --- a/substrate/runtime/src/lib.rs +++ b/substrate/runtime/src/lib.rs @@ -17,6 +17,8 @@ use serai_abi::{ primitives::address::SeraiAddress, SubstrateHeader as Header, SubstrateBlock, }; +use serai_coins_pallet::{CoinsInstance, LiquidityTokensInstance}; + mod core_pallet; type Block = SubstrateBlock; @@ -74,6 +76,12 @@ mod runtime { #[runtime::pallet_index(1)] pub type Core = core_pallet::Pallet; + + #[runtime::pallet_index(2)] + pub type Coins = serai_coins_pallet::Pallet; + + #[runtime::pallet_index(3)] + pub type LiquidityTokens = serai_coins_pallet::Pallet; } impl frame_system::Config for Runtime { @@ -118,6 +126,15 @@ impl frame_system::Config for Runtime { impl core_pallet::Config for Runtime {} +impl serai_coins_pallet::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type AllowMint = serai_coins_pallet::AlwaysAllowMint; // TODO +} +impl serai_coins_pallet::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type AllowMint = serai_coins_pallet::AlwaysAllowMint; +} + impl From> for RuntimeOrigin { fn from(signer: Option) -> Self { match signer { @@ -133,8 +150,14 @@ impl From for RuntimeCall { serai_abi::Call::Coins(call) => { use serai_abi::coins::Call; match call { - Call::transfer { .. } | Call::burn { .. } | Call::burn_with_instruction { .. } => { - todo!("TODO") + Call::transfer { to, coins } => { + 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 for RuntimeCall { serai_abi::Call::Dex(call) => { use serai_abi::dex::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::transfer_liquidity { .. } | Call::remove_liquidity { .. } | Call::swap_exact { .. } | Call::swap_for_exact { .. } => todo!("TODO"), @@ -236,7 +264,14 @@ impl serai_abi::TransactionContext for Context { signer: &SeraiAddress, fee: serai_abi::primitives::balance::Amount, ) -> Result<(), sp_runtime::transaction_validity::TransactionValidityError> { - todo!("TODO") + use serai_abi::primitives::coin::Coin; + if serai_coins_pallet::Pallet::::balance(signer, Coin::Serai) >= fee { + Ok(()) + } else { + Err(sp_runtime::transaction_validity::TransactionValidityError::Invalid( + sp_runtime::transaction_validity::InvalidTransaction::Payment, + )) + } } fn start_transaction(&self) { @@ -252,7 +287,16 @@ impl serai_abi::TransactionContext for Context { signer: &SeraiAddress, fee: serai_abi::primitives::balance::Amount, ) -> Result<(), sp_runtime::transaction_validity::TransactionValidityError> { - todo!("TODO") + use serai_abi::primitives::{coin::*, balance::*}; + serai_coins_pallet::Pallet::::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]) { Core::end_transaction(transaction_hash);