Distribute spl tokens (#13559)
* Add helpers to covert between sdk types * Add distribute-spl-tokens to args and arg-parsing * Build spl-token transfer-checked instructions * Check spl-token balances properly * Add display handling to support spl-token * Small refactor to allow failures in allocation iter * Use Associated Token Account for spl-token distributions * Add spl token support to balances command * Update readme * Add spl-token tests * Rename spl-tokens file * Move a couple more things out of commands * Stop requiring lockup_date heading for non-stake distributions * Use epsilon for allocation retention
This commit is contained in:
62
tokens/src/token_display.rs
Normal file
62
tokens/src/token_display.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use std::{
|
||||
fmt::{Debug, Display, Formatter, Result},
|
||||
ops::Add,
|
||||
};
|
||||
|
||||
const SOL_SYMBOL: &str = "◎";
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum TokenType {
|
||||
Sol,
|
||||
SplToken,
|
||||
}
|
||||
|
||||
pub struct Token {
|
||||
amount: f64,
|
||||
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,),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from(amount: f64, is_sol: bool) -> Self {
|
||||
let token_type = if is_sol {
|
||||
TokenType::Sol
|
||||
} else {
|
||||
TokenType::SplToken
|
||||
};
|
||||
Self { amount, token_type }
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Token {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
self.write_with_symbol(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Token {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
self.write_with_symbol(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Token {
|
||||
type Output = Token;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
if self.token_type == other.token_type {
|
||||
Self {
|
||||
amount: self.amount + other.amount,
|
||||
token_type: self.token_type,
|
||||
}
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user