1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::error::ErrorCode;
use crate::precise_number::{InnerUint, PreciseNumber};
use anchor_lang::solana_program::system_program;
use anchor_lang::{prelude::*, solana_program};
use anchor_spl::token::{Mint, TokenAccount};
use std::convert::*;
pub trait OrArithError<T> {
fn or_arith_error(self) -> Result<T>;
}
impl OrArithError<PreciseNumber> for Option<PreciseNumber> {
fn or_arith_error(self) -> Result<PreciseNumber> {
self.ok_or(ErrorCode::ArithmeticError.into())
}
}
impl OrArithError<u128> for Option<u128> {
fn or_arith_error(self) -> Result<u128> {
self.ok_or(ErrorCode::ArithmeticError.into())
}
}
impl OrArithError<u64> for Option<u64> {
fn or_arith_error(self) -> Result<u64> {
self.ok_or(ErrorCode::ArithmeticError.into())
}
}
pub fn get_percent_prec(percent: u32) -> Result<PreciseNumber> {
let max_u32 = PreciseNumber::new(u32::MAX as u128).or_arith_error()?;
let percent_prec = PreciseNumber::new(percent as u128).or_arith_error()?;
percent_prec.checked_div(&max_u32).or_arith_error()
}
pub fn get_percent(value: u64, percent: u32) -> Result<u64> {
u64::try_from(
u128::try_from(value)
.ok()
.or_arith_error()?
.checked_mul(u128::try_from(percent).ok().or_arith_error()?)
.or_arith_error()?
.checked_div(u32::MAX as u128)
.or_arith_error()?,
)
.ok()
.or_arith_error()
}
pub fn precise_supply(mint: &Account<Mint>) -> PreciseNumber {
precise_supply_amt(mint.supply, mint)
}
fn get_pow_10(decimals: u8) -> PreciseNumber {
match decimals {
0 => PreciseNumber::new(1),
1 => PreciseNumber::new(10),
2 => PreciseNumber::new(100),
3 => PreciseNumber::new(1000),
4 => PreciseNumber::new(10000),
5 => PreciseNumber::new(100000),
6 => PreciseNumber::new(1000000),
7 => PreciseNumber::new(10000000),
8 => PreciseNumber::new(100000000),
9 => PreciseNumber::new(1000000000),
10 => PreciseNumber::new(10000000000),
11 => PreciseNumber::new(100000000000),
12 => PreciseNumber::new(1000000000000),
_ => unreachable!(),
}
.unwrap()
}
fn get_u128_pow_10(decimals: u8) -> u128 {
match decimals {
0 => 1,
1 => 10,
2 => 100,
3 => 1000,
4 => 10000,
5 => 100000,
6 => 1000000,
7 => 10000000,
8 => 100000000,
9 => 1000000000,
10 => 10000000000,
11 => 100000000000,
12 => 1000000000000,
_ => unreachable!(),
}
}
pub fn precise_supply_amt(amt: u64, mint: &Mint) -> PreciseNumber {
PreciseNumber {
value: InnerUint::from(amt)
.checked_mul(InnerUint::from(get_u128_pow_10(12_u8 - mint.decimals)))
.unwrap()
.checked_mul(InnerUint::from(1_000_000u64))
.unwrap(),
}
}
pub fn to_mint_amount(amt: &PreciseNumber, mint: &Mint, ceil: bool) -> u64 {
let pow_10 = get_pow_10(mint.decimals);
let pre_round = amt.checked_mul(&pow_10).unwrap();
let post_round = if ceil {
pre_round.ceiling().unwrap()
} else {
pre_round.floor().unwrap()
};
post_round.to_imprecise().unwrap() as u64
}
pub fn verify_empty_or_mint<'info>(
maybe_token_account: &UncheckedAccount<'info>,
mint: &Pubkey,
) -> Result<()> {
if *maybe_token_account.owner == system_program::ID {
Ok(())
} else {
let acc: Account<'info, TokenAccount> = Account::try_from(maybe_token_account)?;
if acc.mint == *mint {
Ok(())
} else {
Err(error!(ErrorCode::InvalidMint))
}
}
}
#[derive(Accounts)]
pub struct CloseTokenAccount<'info> {
pub from: AccountInfo<'info>,
pub to: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
pub fn close_token_account<'a, 'b, 'c, 'info>(
ctx: CpiContext<'a, 'b, 'c, 'info, CloseTokenAccount<'info>>,
) -> Result<()> {
let ix = spl_token::instruction::close_account(
&spl_token::ID,
ctx.accounts.from.key,
ctx.accounts.to.key,
ctx.accounts.authority.key,
&[],
)?;
solana_program::program::invoke_signed(
&ix,
&[
ctx.accounts.from.clone(),
ctx.accounts.to.clone(),
ctx.accounts.authority.clone(),
ctx.program.clone(),
],
ctx.signer_seeds,
)
.map_err(|e| e.into())
}