Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
code stash
Browse files Browse the repository at this point in the history
  • Loading branch information
kb1ns committed Sep 29, 2022
1 parent db4c2b9 commit 3be99c4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 98 deletions.
89 changes: 14 additions & 75 deletions fuso-support/src/external_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,23 @@
// limitations under the License.

use codec::{Decode, Encode};
use sp_runtime::RuntimeDebug;
use sp_std::{convert::TryFrom, fmt::Debug, vec::Vec};

#[derive(Encode, Decode, PartialEq, Eq, Clone, RuntimeDebug)]
pub enum ExternalChain {
BTC,
LTC,
ETH,
ERC20(Vec<u8>),
TRX,
TRC20(Vec<u8>),
DOT,
FIL,
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)]
pub enum XToken<Balance> {
//( symbol, contract_address, total, stable, decimals
NEP141(Vec<u8>, Vec<u8>, Balance, bool, u8),
ERC20(Vec<u8>, Vec<u8>, Balance, bool, u8),
BEP20(Vec<u8>, Vec<u8>, Balance, bool, u8),
DAO(Vec<u8>, Balance),
}

impl TryFrom<(u16, Option<Vec<u8>>)> for ExternalChain {
type Error = ();

fn try_from((chain, contract): (u16, Option<Vec<u8>>)) -> Result<ExternalChain, ()> {
match chain {
0 => Ok(ExternalChain::BTC),
1 => Ok(ExternalChain::LTC),
2 => match contract {
Some(addr) => Ok(ExternalChain::ERC20(addr)),
None => Ok(ExternalChain::ETH),
},
3 => match contract {
Some(addr) => Ok(ExternalChain::TRC20(addr)),
None => Ok(ExternalChain::TRX),
},
4 => Ok(ExternalChain::DOT),
5 => Ok(ExternalChain::FIL),
_ => Err(()),
impl<Balance> XToken<Balance> {
pub fn is_stable(&self) -> bool {
match *self {
XToken::NEP141(_, _, _, stable, _) => stable,
XToken::ERC20(_, _, _, stable, _) => stable,
XToken::BEP20(_, _, _, stable, _) => stable,
XToken::DAO(_, _) => false,
}
}
}

#[derive(Encode, Decode, PartialEq, Eq, Clone, RuntimeDebug)]
pub struct ExternalChainAddress {
chain: ExternalChain,
pubkey: Vec<u8>,
}

// TODO
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum AddressError {
IllegalSs58,
IllegalKeccak256,
}

impl TryFrom<(ExternalChain, Vec<u8>)> for ExternalChainAddress {
type Error = AddressError;

// TODO
fn try_from((chain, _encoded_addr): (ExternalChain, Vec<u8>)) -> Result<Self, AddressError> {
match chain {
ExternalChain::BTC => Err(AddressError::IllegalSs58),
ExternalChain::ETH | ExternalChain::ERC20(_) => Err(AddressError::IllegalKeccak256),
_ => Err(AddressError::IllegalKeccak256),
}
}
}

#[cfg(feature = "std")]
impl Display for ExternalChain {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
// TODO codec
ExternalChain::ERC20(addr) => write!(f, "ERC20({})", HexDisplay::from(contract)),
ExternalChain::TRC20(addr) => write!(f, "TRC20({})", HexDisplay::from(contract)),
_ => write!(f, "{:?}", self),
}
}
}

#[cfg(feature = "std")]
impl Display for ExternalChainAddress {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
5 changes: 3 additions & 2 deletions fuso-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub extern crate alloc;

pub use alloc::collections;

pub mod external_chain;
pub mod traits;
pub use external_chain::*;

pub mod constants {
pub const RESERVE_FOR_STAKING: u8 = 0u8;
Expand All @@ -29,4 +28,6 @@ pub mod constants {
pub const DOMINATOR_INACTIVE: u8 = 1u8;
pub const DOMINATOR_ACTIVE: u8 = 2u8;
pub const DOMINATOR_EVICTED: u8 = 3u8;
pub const STANDARD_DECIMALS: u8 = 18;
pub const MAX_DECIMALS: u8 = 24;
}
2 changes: 2 additions & 0 deletions fuso-support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub trait Token<AccountId> {
+ Codec
+ MaybeSerializeDeserialize;

fn create()

fn native_token_id() -> Self::TokenId;

fn is_stable(token_id: &Self::TokenId) -> bool;
Expand Down
28 changes: 7 additions & 21 deletions pallet-fuso-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ pub mod pallet {
transactional,
};
use frame_system::pallet_prelude::*;
use fuso_support::traits::{ReservableToken, Token};
use fuso_support::{
constants::*,
traits::{ReservableToken, Token},
XToken,
};
use pallet_octopus_support::traits::TokenIdAndAssetIdProvider;
use scale_info::TypeInfo;
use sp_runtime::traits::{
Expand All @@ -47,33 +51,15 @@ pub mod pallet {
use sp_runtime::DispatchResult;
use sp_std::vec::Vec;

pub const STANDARD_DECIMALS: u8 = 18;
pub const MAX_DECIMALS: u8 = 24;
// pub const STANDARD_DECIMALS: u8 = 18;
// pub const MAX_DECIMALS: u8 = 24;

#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, TypeInfo, Debug)]
pub struct TokenAccountData<Balance> {
pub free: Balance,
pub reserved: Balance,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, TypeInfo)]
pub enum XToken<Balance> {
//( symbol, contract_address, total, stable, decimals
NEP141(Vec<u8>, Vec<u8>, Balance, bool, u8),
ERC20(Vec<u8>, Vec<u8>, Balance, bool, u8),
BEP20(Vec<u8>, Vec<u8>, Balance, bool, u8),
}

impl<Balance> XToken<Balance> {
pub fn is_stable(&self) -> bool {
match *self {
XToken::NEP141(_, _, _, stable, _) => stable,
XToken::ERC20(_, _, _, stable, _) => stable,
XToken::BEP20(_, _, _, stable, _) => stable,
}
}
}

pub type BalanceOf<T> = <T as pallet_balances::Config>::Balance;

#[pallet::config]
Expand Down

0 comments on commit 3be99c4

Please sign in to comment.