add specific network/coin/balance types (#619)

* add specific network/coin/balance types

* misc fixes

* fix clippy

* misc fixes

* fix pr comments

* Make halting for external networks

* fix encode/decode
This commit is contained in:
akildemir
2024-10-07 05:16:11 +03:00
committed by GitHub
parent d7ecab605e
commit 435f1d9ae1
91 changed files with 1536 additions and 1055 deletions

View File

@@ -1,13 +1,13 @@
#![cfg_attr(not(feature = "std"), no_std)]
use serai_primitives::{Coin, SubstrateAmount, Balance};
use serai_primitives::{Balance, Coin, ExternalBalance, SubstrateAmount};
pub trait AllowMint {
fn is_allowed(balance: &Balance) -> bool;
fn is_allowed(balance: &ExternalBalance) -> bool;
}
impl AllowMint for () {
fn is_allowed(_: &Balance) -> bool {
fn is_allowed(_: &ExternalBalance) -> bool {
true
}
}
@@ -161,7 +161,10 @@ pub mod pallet {
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 (balance.coin != Coin::Serai) && (!T::AllowMint::is_allowed(&balance)) {
if !ExternalCoin::try_from(balance.coin)
.map(|coin| T::AllowMint::is_allowed(&ExternalBalance { coin, amount: balance.amount }))
.unwrap_or(true)
{
Err(Error::<T, I>::MintNotAllowed)?;
}
@@ -230,22 +233,18 @@ pub mod pallet {
}
/// 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)?;
}
let from = ensure_signed(origin)?;
Self::burn_internal(from, instruction.balance)?;
Self::burn_internal(from, instruction.balance.into())?;
Self::deposit_event(Event::BurnWithInstruction { from, instruction });
Ok(())
}

View File

@@ -13,7 +13,7 @@ use serde::{Serialize, Deserialize};
use scale::{Encode, Decode, MaxEncodedLen};
use scale_info::TypeInfo;
use serai_primitives::{Balance, SeraiAddress, ExternalAddress, Data, system_address};
use serai_primitives::{system_address, Data, ExternalAddress, ExternalBalance, SeraiAddress};
pub const FEE_ACCOUNT: SeraiAddress = system_address(b"Coins-fees");
@@ -32,7 +32,7 @@ pub struct OutInstruction {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OutInstructionWithBalance {
pub instruction: OutInstruction,
pub balance: Balance,
pub balance: ExternalBalance,
}
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]