-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.rs
61 lines (56 loc) · 1.77 KB
/
token.rs
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
use crate::errors::ErrorCode;
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Token, TokenAccount, Transfer};
use crate::WooPool;
pub fn balance<'info>(
woopool: &Account<'info, WooPool>,
token_vault: &Account<'info, TokenAccount>,
) -> Result<u128> {
let balance: u128 = if woopool.token_mint == woopool.quote_token_mint {
(token_vault.amount as u128)
.checked_sub(woopool.unclaimed_fee)
.ok_or(ErrorCode::ReserveLessThanFee)?
} else {
token_vault.amount as u128
};
Ok(balance)
}
pub fn transfer_from_owner_to_vault<'info>(
position_authority: &Signer<'info>,
token_owner_account: &Account<'info, TokenAccount>,
token_vault: &Account<'info, TokenAccount>,
token_program: &Program<'info, Token>,
amount: u64,
) -> Result<()> {
token::transfer(
CpiContext::new(
token_program.to_account_info(),
Transfer {
from: token_owner_account.to_account_info(),
to: token_vault.to_account_info(),
authority: position_authority.to_account_info(),
},
),
amount,
)
}
pub fn transfer_from_vault_to_owner<'info>(
woopool: &Account<'info, WooPool>,
token_vault: &Account<'info, TokenAccount>,
token_owner_account: &Account<'info, TokenAccount>,
token_program: &Program<'info, Token>,
amount: u64,
) -> Result<()> {
token::transfer(
CpiContext::new_with_signer(
token_program.to_account_info(),
Transfer {
from: token_vault.to_account_info(),
to: token_owner_account.to_account_info(),
authority: woopool.to_account_info(),
},
&[&woopool.seeds()],
),
amount,
)
}