Skip to content

Commit

Permalink
refactor: separate decimals data (#2718)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofxxx committed May 12, 2024
1 parent d523be2 commit 566445c
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 64 deletions.
67 changes: 3 additions & 64 deletions tee-worker/litentry/core/common/src/web3_token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use std::{vec, vec::Vec};
use litentry_primitives::Web3TokenType;

use crate::Web3Network;
pub mod token_decimals_filter;
use token_decimals_filter::TokenDecimalsFilter;

pub trait TokenName {
fn get_token_name(&self) -> &'static str;
Expand Down Expand Up @@ -167,70 +169,7 @@ pub trait TokenDecimals {

impl TokenDecimals for Web3TokenType {
fn get_decimals(&self, network: Web3Network) -> u64 {
let decimals = match (self, network) {
// Bnb
(Self::Bnb, Web3Network::Bsc) | (Self::Bnb, Web3Network::Ethereum) |
// Eth
(Self::Eth, Web3Network::Bsc) | (Self::Eth, Web3Network::Ethereum) |
// SpaceId
(Self::SpaceId, Web3Network::Bsc) | (Self::SpaceId, Web3Network::Ethereum) |
// Lit
(Self::Lit, Web3Network::Bsc) | (Self::Lit, Web3Network::Ethereum) |
// Usdc
(Self::Usdc, Web3Network::Bsc) |
// Usdt
(Self::Usdt, Web3Network::Bsc) |
// Crv
(Self::Crv, Web3Network::Ethereum) |
// Matic
(Self::Matic, Web3Network::Bsc) | (Self::Matic, Web3Network::Ethereum) |
// Dydx
(Self::Dydx, Web3Network::Ethereum) |
// Amp
(Self::Amp, Web3Network::Ethereum) |
// Cvx
(Self::Cvx, Web3Network::Ethereum) |
// Tusd
(Self::Tusd, Web3Network::Bsc) | (Self::Tusd, Web3Network::Ethereum) |
// Usdd
(Self::Usdd, Web3Network::Bsc) | (Self::Usdd, Web3Network::Ethereum) |
// Link
(Self::Link, Web3Network::Bsc) | (Self::Link, Web3Network::Ethereum) |
// Grt
(Self::Grt, Web3Network::Bsc) | (Self::Grt, Web3Network::Ethereum) |
// Comp
(Self::Comp, Web3Network::Ethereum) |
// People
(Self::People, Web3Network::Ethereum) |
// Gtc
(Self::Gtc, Web3Network::Ethereum) |
// Nfp
(Self::Nfp, Web3Network::Bsc) |
// Sol
(Self::Sol, Web3Network::Bsc) | (Self::Sol, Web3Network::Ethereum) => 18,
// Ton
(Self::Ton, Web3Network::Bsc) | (Self::Ton, Web3Network::Ethereum) |
// Mcrt
(Self::Mcrt, Web3Network::Bsc) | (Self::Mcrt, Web3Network::Ethereum) => 9,
// Wbtc
(Self::Wbtc, Web3Network::Bsc) | (Self::Wbtc, Web3Network::Ethereum) |
// Mcrt
(Self::Mcrt, Web3Network::Solana) |
// Btc
(Self::Btc, Web3Network::BitcoinP2tr) | (Self::Btc, Web3Network::BitcoinP2pkh) |
(Self::Btc, Web3Network::BitcoinP2sh) | (Self::Btc, Web3Network::BitcoinP2wpkh) |
(Self::Btc, Web3Network::BitcoinP2wsh) => 8,
// Usdc
(Self::Usdc, Web3Network::Ethereum) |
// Usdt
(Self::Usdt, Web3Network::Ethereum) |
// Trx
(Self::Trx, Web3Network::Bsc) | (Self::Trx, Web3Network::Ethereum) => 6,
// Gusd
(Self::Gusd, Web3Network::Ethereum) => 2,
_ => 1,
};

let decimals = TokenDecimalsFilter::filter(self.clone(), network);
10_u64.pow(decimals)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

use litentry_primitives::{Web3Network, Web3TokenType};

pub const DEFAULT_TOKEN_DECIMALS: u32 = 1;

pub const TOKEN_DECIMALS_2: (u32, [(Web3TokenType, Web3Network); 1]) = (
2,
[
// Gusd
(Web3TokenType::Gusd, Web3Network::Ethereum),
],
);

pub const TOKEN_DECIMALS_6: (u32, [(Web3TokenType, Web3Network); 4]) = (
6,
[
// Usdc
(Web3TokenType::Usdc, Web3Network::Ethereum),
// Usdt
(Web3TokenType::Usdt, Web3Network::Ethereum),
// Trx
(Web3TokenType::Trx, Web3Network::Bsc),
(Web3TokenType::Trx, Web3Network::Ethereum),
],
);

pub const TOKEN_DECIMALS_8: (u32, [(Web3TokenType, Web3Network); 8]) = (
8,
[
// Wbtc
(Web3TokenType::Wbtc, Web3Network::Bsc),
(Web3TokenType::Wbtc, Web3Network::Ethereum),
// Mcrt
(Web3TokenType::Mcrt, Web3Network::Solana),
// Btc
(Web3TokenType::Btc, Web3Network::BitcoinP2tr),
(Web3TokenType::Btc, Web3Network::BitcoinP2pkh),
(Web3TokenType::Btc, Web3Network::BitcoinP2sh),
(Web3TokenType::Btc, Web3Network::BitcoinP2wpkh),
(Web3TokenType::Btc, Web3Network::BitcoinP2wsh),
],
);

pub const TOKEN_DECIMALS_9: (u32, [(Web3TokenType, Web3Network); 4]) = (
9,
[
// Ton
(Web3TokenType::Ton, Web3Network::Bsc),
(Web3TokenType::Ton, Web3Network::Ethereum),
// Mcrt
(Web3TokenType::Mcrt, Web3Network::Bsc),
(Web3TokenType::Mcrt, Web3Network::Ethereum),
],
);

pub const TOKEN_DECIMALS_18: (u32, [(Web3TokenType, Web3Network); 30]) = (
18,
[
// Bnb
(Web3TokenType::Bnb, Web3Network::Bsc),
(Web3TokenType::Bnb, Web3Network::Ethereum),
// Eth
(Web3TokenType::Eth, Web3Network::Bsc),
(Web3TokenType::Eth, Web3Network::Ethereum),
// SpaceId
(Web3TokenType::SpaceId, Web3Network::Bsc),
(Web3TokenType::SpaceId, Web3Network::Ethereum),
// Lit
(Web3TokenType::Lit, Web3Network::Bsc),
(Web3TokenType::Lit, Web3Network::Ethereum),
// Usdc
(Web3TokenType::Usdc, Web3Network::Bsc),
// Usdt
(Web3TokenType::Usdt, Web3Network::Bsc),
// Crv
(Web3TokenType::Crv, Web3Network::Ethereum),
// Matic
(Web3TokenType::Matic, Web3Network::Bsc),
(Web3TokenType::Matic, Web3Network::Ethereum),
// Dydx
(Web3TokenType::Dydx, Web3Network::Ethereum),
// Amp
(Web3TokenType::Amp, Web3Network::Ethereum),
// Cvx
(Web3TokenType::Cvx, Web3Network::Ethereum),
// Tusd
(Web3TokenType::Tusd, Web3Network::Bsc),
(Web3TokenType::Tusd, Web3Network::Ethereum),
// Usdd
(Web3TokenType::Usdd, Web3Network::Bsc),
(Web3TokenType::Usdd, Web3Network::Ethereum),
// Link
(Web3TokenType::Link, Web3Network::Bsc),
(Web3TokenType::Link, Web3Network::Ethereum),
// Grt
(Web3TokenType::Grt, Web3Network::Bsc),
(Web3TokenType::Grt, Web3Network::Ethereum),
// Comp
(Web3TokenType::Comp, Web3Network::Ethereum),
// People
(Web3TokenType::People, Web3Network::Ethereum),
// Gtc
(Web3TokenType::Gtc, Web3Network::Ethereum),
// Nfp
(Web3TokenType::Nfp, Web3Network::Bsc),
// Sol
(Web3TokenType::Sol, Web3Network::Bsc),
(Web3TokenType::Sol, Web3Network::Ethereum),
],
);

pub struct TokenDecimalsFilter;
impl TokenDecimalsFilter {
pub fn filter(token: Web3TokenType, network: Web3Network) -> u32 {
let target = (token, network);

let (decimals, data) = TOKEN_DECIMALS_18;
if data.contains(&target) {
return decimals
}

let (decimals, data) = TOKEN_DECIMALS_8;
if data.contains(&target) {
return decimals
}

let (decimals, data) = TOKEN_DECIMALS_9;
if data.contains(&target) {
return decimals
}

let (decimals, data) = TOKEN_DECIMALS_6;
if data.contains(&target) {
return decimals
}

let (decimals, data) = TOKEN_DECIMALS_2;
if data.contains(&target) {
return decimals
}

DEFAULT_TOKEN_DECIMALS
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn filter_token_decimal_18_works() {
let target = (Web3TokenType::Bnb, Web3Network::Bsc);
let d = TokenDecimalsFilter::filter(target.0, target.1);
assert_eq!(d, 18);
}

#[test]
fn filter_token_decimal_8_works() {
let target = (Web3TokenType::Mcrt, Web3Network::Solana);
let d = TokenDecimalsFilter::filter(target.0, target.1);
assert_eq!(d, 8);
}

#[test]
fn filter_token_decimal_9_works() {
let target = (Web3TokenType::Mcrt, Web3Network::Bsc);
let d = TokenDecimalsFilter::filter(target.0, target.1);
assert_eq!(d, 9);
}

#[test]
fn filter_token_decimal_6_works() {
let target = (Web3TokenType::Usdt, Web3Network::Ethereum);
let d = TokenDecimalsFilter::filter(target.0, target.1);
assert_eq!(d, 6);
}

#[test]
fn filter_token_decimal_2_works() {
let target = (Web3TokenType::Gusd, Web3Network::Ethereum);
let d = TokenDecimalsFilter::filter(target.0, target.1);
assert_eq!(d, 2);
}

#[test]
fn filter_token_decimal_default_works() {
let target = (Web3TokenType::Gusd, Web3Network::Arbitrum);
let d = TokenDecimalsFilter::filter(target.0, target.1);
assert_eq!(d, 1);
}
}

0 comments on commit 566445c

Please sign in to comment.