Skip to content

Commit

Permalink
refactor: reorder Read impl in fungibles pallet (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin authored Jan 21, 2025
1 parent eea99ae commit 984d89c
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 265 deletions.
154 changes: 77 additions & 77 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,83 +41,6 @@ pub mod pallet {

use super::*;

/// State reads for the fungibles API with required input.
#[derive(Encode, Decode, Debug, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(PartialEq, Clone))]
#[repr(u8)]
#[allow(clippy::unnecessary_cast)]
pub enum Read<T: Config> {
/// Total token supply for a specified token.
#[codec(index = 0)]
TotalSupply(TokenIdOf<T>),
/// Account balance for a specified `token` and `owner`.
#[codec(index = 1)]
BalanceOf {
/// The token.
token: TokenIdOf<T>,
/// The owner of the token.
owner: AccountIdOf<T>,
},
/// Allowance for a `spender` approved by an `owner`, for a specified `token`.
#[codec(index = 2)]
Allowance {
/// The token.
token: TokenIdOf<T>,
/// The owner of the token.
owner: AccountIdOf<T>,
/// The spender with an allowance.
spender: AccountIdOf<T>,
},
/// Name of the specified token.
#[codec(index = 8)]
TokenName(TokenIdOf<T>),
/// Symbol for the specified token.
#[codec(index = 9)]
TokenSymbol(TokenIdOf<T>),
/// Decimals for the specified token.
#[codec(index = 10)]
TokenDecimals(TokenIdOf<T>),
/// Whether a specified token exists.
#[codec(index = 18)]
TokenExists(TokenIdOf<T>),
}

/// Results of state reads for the fungibles API.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(PartialEq, Clone))]
pub enum ReadResult<T: Config> {
/// Total token supply for a specified token.
TotalSupply(BalanceOf<T>),
/// Account balance for a specified token and owner.
BalanceOf(BalanceOf<T>),
/// Allowance for a spender approved by an owner, for a specified token.
Allowance(BalanceOf<T>),
/// Name of the specified token, if available.
TokenName(Option<Vec<u8>>),
/// Symbol for the specified token, if available.
TokenSymbol(Option<Vec<u8>>),
/// Decimals for the specified token.
TokenDecimals(u8),
/// Whether the specified token exists.
TokenExists(bool),
}

impl<T: Config> ReadResult<T> {
/// Encodes the result.
pub fn encode(&self) -> Vec<u8> {
use ReadResult::*;
match self {
TotalSupply(result) => result.encode(),
BalanceOf(result) => result.encode(),
Allowance(result) => result.encode(),
TokenName(result) => result.encode(),
TokenSymbol(result) => result.encode(),
TokenDecimals(result) => result.encode(),
TokenExists(result) => result.encode(),
}
}
}

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config + pallet_assets::Config<Self::AssetsInstance> {
Expand Down Expand Up @@ -485,6 +408,83 @@ pub mod pallet {
}
}

/// State reads for the fungibles API with required input.
#[derive(Encode, Decode, Debug, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(PartialEq, Clone))]
#[repr(u8)]
#[allow(clippy::unnecessary_cast)]
pub enum Read<T: Config> {
/// Total token supply for a specified token.
#[codec(index = 0)]
TotalSupply(TokenIdOf<T>),
/// Account balance for a specified `token` and `owner`.
#[codec(index = 1)]
BalanceOf {
/// The token.
token: TokenIdOf<T>,
/// The owner of the token.
owner: AccountIdOf<T>,
},
/// Allowance for a `spender` approved by an `owner`, for a specified `token`.
#[codec(index = 2)]
Allowance {
/// The token.
token: TokenIdOf<T>,
/// The owner of the token.
owner: AccountIdOf<T>,
/// The spender with an allowance.
spender: AccountIdOf<T>,
},
/// Name of the specified token.
#[codec(index = 8)]
TokenName(TokenIdOf<T>),
/// Symbol for the specified token.
#[codec(index = 9)]
TokenSymbol(TokenIdOf<T>),
/// Decimals for the specified token.
#[codec(index = 10)]
TokenDecimals(TokenIdOf<T>),
/// Whether a specified token exists.
#[codec(index = 18)]
TokenExists(TokenIdOf<T>),
}

/// Results of state reads for the fungibles API.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(PartialEq, Clone))]
pub enum ReadResult<T: Config> {
/// Total token supply for a specified token.
TotalSupply(BalanceOf<T>),
/// Account balance for a specified token and owner.
BalanceOf(BalanceOf<T>),
/// Allowance for a spender approved by an owner, for a specified token.
Allowance(BalanceOf<T>),
/// Name of the specified token, if available.
TokenName(Option<Vec<u8>>),
/// Symbol for the specified token, if available.
TokenSymbol(Option<Vec<u8>>),
/// Decimals for the specified token.
TokenDecimals(u8),
/// Whether the specified token exists.
TokenExists(bool),
}

impl<T: Config> ReadResult<T> {
/// Encodes the result.
pub fn encode(&self) -> Vec<u8> {
use ReadResult::*;
match self {
TotalSupply(result) => result.encode(),
BalanceOf(result) => result.encode(),
Allowance(result) => result.encode(),
TokenName(result) => result.encode(),
TokenSymbol(result) => result.encode(),
TokenDecimals(result) => result.encode(),
TokenExists(result) => result.encode(),
}
}
}

impl<T: Config> crate::Read for Pallet<T> {
/// The type of read requested.
type Read = Read<T>;
Expand Down
Loading

0 comments on commit 984d89c

Please sign in to comment.