Add Substrate "assets" pallet

While over-engineered for our purposes, it's still usable.

Also cleans the runtime a bit.
This commit is contained in:
Luke Parker
2023-01-05 19:36:49 -05:00
parent daa88a051f
commit f760c09006
7 changed files with 139 additions and 57 deletions

View File

@@ -0,0 +1,31 @@
use core::{ops::{Add, Mul}};
use scale::{Encode, Decode, MaxEncodedLen};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
/// The type used for amounts.
#[derive(
Clone, Copy, PartialEq, Eq, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Amount(pub u64);
/// One whole coin with eight decimals.
#[allow(clippy::inconsistent_digit_grouping)]
pub const COIN: Amount = Amount(1_000_000_00);
impl Add for Amount {
type Output = Amount;
fn add(self, other: Amount) -> Amount {
Amount(self.0 + other.0)
}
}
impl Mul for Amount {
type Output = Amount;
fn mul(self, other: Amount) -> Amount {
Amount(self.0 * other.0)
}
}

View File

@@ -0,0 +1,19 @@
use scale::{Encode, Decode, MaxEncodedLen};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
/// The type used to identify coins.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Coin(pub u32);
impl From<u32> for Coin {
fn from(coin: u32) -> Coin {
Coin(coin)
}
}
pub const BITCOIN: Coin = Coin(0);
pub const ETHER: Coin = Coin(1);
pub const DAI: Coin = Coin(2);
pub const MONERO: Coin = Coin(3);

View File

@@ -1,36 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use core::ops::{Add, Mul};
mod amount;
pub use amount::*;
use scale::{Encode, Decode, MaxEncodedLen};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
/// The type used for amounts.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Amount(pub u64);
impl Add<Amount> for Amount {
type Output = Amount;
fn add(self, other: Amount) -> Amount {
Amount(self.0 + other.0)
}
}
impl Mul<Amount> for Amount {
type Output = Amount;
fn mul(self, other: Amount) -> Amount {
Amount(self.0 * other.0)
}
}
/// One whole coin with eight decimals.
#[allow(clippy::inconsistent_digit_grouping)]
pub const COIN: Amount = Amount(1_000_000_00);
/// The type used to identify coins.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Coin(pub u32);
mod coins;
pub use coins::*;