Use u64 behind the scenes for solana-tokens (#13815)
* Use u64 behind the scenes for Allocations * Fixup readme * Clippy and remove errant comments
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use solana_account_decoder::parse_token::token_amount_to_ui_amount;
|
||||
use solana_sdk::native_token::lamports_to_sol;
|
||||
use std::{
|
||||
fmt::{Debug, Display, Formatter, Result},
|
||||
ops::Add,
|
||||
@@ -12,25 +14,39 @@ pub enum TokenType {
|
||||
}
|
||||
|
||||
pub struct Token {
|
||||
amount: f64,
|
||||
amount: u64,
|
||||
decimals: u8,
|
||||
token_type: TokenType,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
fn write_with_symbol(&self, f: &mut Formatter) -> Result {
|
||||
match &self.token_type {
|
||||
TokenType::Sol => write!(f, "{}{}", SOL_SYMBOL, self.amount,),
|
||||
TokenType::SplToken => write!(f, "{} tokens", self.amount,),
|
||||
TokenType::Sol => {
|
||||
let amount = lamports_to_sol(self.amount);
|
||||
write!(f, "{}{}", SOL_SYMBOL, amount)
|
||||
}
|
||||
TokenType::SplToken => {
|
||||
let amount = token_amount_to_ui_amount(self.amount, self.decimals).ui_amount;
|
||||
write!(f, "{} tokens", amount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from(amount: f64, is_sol: bool) -> Self {
|
||||
let token_type = if is_sol {
|
||||
TokenType::Sol
|
||||
} else {
|
||||
TokenType::SplToken
|
||||
};
|
||||
Self { amount, token_type }
|
||||
pub fn sol(amount: u64) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
decimals: 9,
|
||||
token_type: TokenType::Sol,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spl_token(amount: u64, decimals: u8) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
decimals,
|
||||
token_type: TokenType::SplToken,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +69,7 @@ impl Add for Token {
|
||||
if self.token_type == other.token_type {
|
||||
Self {
|
||||
amount: self.amount + other.amount,
|
||||
decimals: self.decimals,
|
||||
token_type: self.token_type,
|
||||
}
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user