forked from otter-sec/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_fee.rs
More file actions
189 lines (172 loc) · 5.91 KB
/
Copy pathtransfer_fee.rs
File metadata and controls
189 lines (172 loc) · 5.91 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use anchor_lang::solana_program::account_info::AccountInfo;
use anchor_lang::solana_program::pubkey::Pubkey;
use anchor_lang::Result;
use anchor_lang::{context::CpiContext, Accounts};
pub fn transfer_fee_initialize<'info>(
ctx: CpiContext<'_, '_, '_, 'info, TransferFeeInitialize<'info>>,
transfer_fee_config_authority: Option<&Pubkey>,
withdraw_withheld_authority: Option<&Pubkey>,
transfer_fee_basis_points: u16,
maximum_fee: u64,
) -> Result<()> {
let ix = spl_token_2022::extension::transfer_fee::instruction::initialize_transfer_fee_config(
ctx.accounts.token_program_id.key,
ctx.accounts.mint.key,
transfer_fee_config_authority,
withdraw_withheld_authority,
transfer_fee_basis_points,
maximum_fee,
)?;
anchor_lang::solana_program::program::invoke_signed(
&ix,
&[ctx.accounts.token_program_id, ctx.accounts.mint],
ctx.signer_seeds,
)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct TransferFeeInitialize<'info> {
pub token_program_id: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
}
pub fn transfer_fee_set<'info>(
ctx: CpiContext<'_, '_, '_, 'info, TransferFeeSetTransferFee<'info>>,
transfer_fee_basis_points: u16,
maximum_fee: u64,
) -> Result<()> {
let ix = spl_token_2022::extension::transfer_fee::instruction::set_transfer_fee(
ctx.accounts.token_program_id.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
&[],
transfer_fee_basis_points,
maximum_fee,
)?;
anchor_lang::solana_program::program::invoke_signed(
&ix,
&[
ctx.accounts.token_program_id,
ctx.accounts.mint,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct TransferFeeSetTransferFee<'info> {
pub token_program_id: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
pub fn transfer_checked_with_fee<'info>(
ctx: CpiContext<'_, '_, '_, 'info, TransferCheckedWithFee<'info>>,
amount: u64,
decimals: u8,
fee: u64,
) -> Result<()> {
let ix = spl_token_2022::extension::transfer_fee::instruction::transfer_checked_with_fee(
ctx.accounts.token_program_id.key,
ctx.accounts.source.key,
ctx.accounts.mint.key,
ctx.accounts.destination.key,
ctx.accounts.authority.key,
&[],
amount,
decimals,
fee,
)?;
anchor_lang::solana_program::program::invoke_signed(
&ix,
&[
ctx.accounts.token_program_id,
ctx.accounts.source,
ctx.accounts.mint,
ctx.accounts.destination,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct TransferCheckedWithFee<'info> {
pub token_program_id: AccountInfo<'info>,
pub source: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub destination: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
pub fn harvest_withheld_tokens_to_mint<'info>(
ctx: CpiContext<'_, '_, '_, 'info, HarvestWithheldTokensToMint<'info>>,
sources: Vec<AccountInfo<'info>>,
) -> Result<()> {
let ix = spl_token_2022::extension::transfer_fee::instruction::harvest_withheld_tokens_to_mint(
ctx.accounts.token_program_id.key,
ctx.accounts.mint.key,
sources.iter().map(|a| a.key).collect::<Vec<_>>().as_slice(),
)?;
let mut account_infos = vec![ctx.accounts.token_program_id, ctx.accounts.mint];
account_infos.extend_from_slice(&sources);
anchor_lang::solana_program::program::invoke_signed(&ix, &account_infos, ctx.signer_seeds)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct HarvestWithheldTokensToMint<'info> {
pub token_program_id: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
}
pub fn withdraw_withheld_tokens_from_mint<'info>(
ctx: CpiContext<'_, '_, '_, 'info, WithdrawWithheldTokensFromMint<'info>>,
) -> Result<()> {
let ix =
spl_token_2022::extension::transfer_fee::instruction::withdraw_withheld_tokens_from_mint(
ctx.accounts.token_program_id.key,
ctx.accounts.mint.key,
ctx.accounts.destination.key,
ctx.accounts.authority.key,
&[],
)?;
anchor_lang::solana_program::program::invoke_signed(
&ix,
&[
ctx.accounts.token_program_id,
ctx.accounts.mint,
ctx.accounts.destination,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct WithdrawWithheldTokensFromMint<'info> {
pub token_program_id: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub destination: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
pub fn withdraw_withheld_tokens_from_accounts<'info>(
ctx: CpiContext<'_, '_, '_, 'info, WithdrawWithheldTokensFromAccounts<'info>>,
sources: Vec<AccountInfo<'info>>,
) -> Result<()> {
let ix = spl_token_2022::extension::transfer_fee::instruction::withdraw_withheld_tokens_from_accounts(
ctx.accounts.token_program_id.key,
ctx.accounts.mint.key,
ctx.accounts.destination.key,
ctx.accounts.authority.key,
&[],
sources.iter().map(|a| a.key).collect::<Vec<_>>().as_slice(),
)?;
let mut account_infos = vec![ctx.accounts.token_program_id, ctx.accounts.mint, ctx.accounts.destination, ctx.accounts.authority];
account_infos.extend_from_slice(&sources);
anchor_lang::solana_program::program::invoke_signed(&ix, &account_infos, ctx.signer_seeds)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct WithdrawWithheldTokensFromAccounts<'info> {
pub token_program_id: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub destination: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}