Restore the coins pallet to the runtime

This commit is contained in:
Luke Parker
2025-03-06 05:53:18 -05:00
parent b08ae8e6a7
commit d3d539553c
12 changed files with 273 additions and 215 deletions

View File

@@ -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,

View File

@@ -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<Amount> 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,

View File

@@ -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(),
}
}
}

View File

@@ -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,