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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use {
crate::{
error::TokenError,
extension::{Extension, ExtensionType},
pod::*,
},
bytemuck::{Pod, Zeroable},
solana_program::{clock::Epoch, entrypoint::ProgramResult},
std::{cmp, convert::TryFrom},
};
pub mod instruction;
pub mod processor;
pub const MAX_FEE_BASIS_POINTS: u16 = 10_000;
const ONE_IN_BASIS_POINTS: u128 = MAX_FEE_BASIS_POINTS as u128;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct TransferFee {
pub epoch: PodU64,
pub maximum_fee: PodU64,
pub transfer_fee_basis_points: PodU16,
}
impl TransferFee {
pub fn calculate(&self, amount: u64) -> Option<u64> {
let transfer_fee_basis_points = u16::from(self.transfer_fee_basis_points) as u128;
if transfer_fee_basis_points == 0 || amount == 0 {
Some(0)
} else {
let numerator = (amount as u128).checked_mul(transfer_fee_basis_points)?;
let mut raw_fee = numerator.checked_div(ONE_IN_BASIS_POINTS)?;
let remainder = numerator.checked_rem(ONE_IN_BASIS_POINTS)?;
if remainder > 0 {
raw_fee = raw_fee.checked_add(1)?;
}
let raw_fee = u64::try_from(raw_fee).ok()?;
Some(cmp::min(raw_fee, u64::from(self.maximum_fee)))
}
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct TransferFeeConfig {
pub transfer_fee_config_authority: OptionalNonZeroPubkey,
pub withdraw_withheld_authority: OptionalNonZeroPubkey,
pub withheld_amount: PodU64,
pub older_transfer_fee: TransferFee,
pub newer_transfer_fee: TransferFee,
}
impl TransferFeeConfig {
pub fn get_epoch_fee(&self, epoch: Epoch) -> &TransferFee {
if epoch >= self.newer_transfer_fee.epoch.into() {
&self.newer_transfer_fee
} else {
&self.older_transfer_fee
}
}
pub fn calculate_epoch_fee(&self, epoch: Epoch, amount: u64) -> Option<u64> {
self.get_epoch_fee(epoch).calculate(amount)
}
}
impl Extension for TransferFeeConfig {
const TYPE: ExtensionType = ExtensionType::TransferFeeConfig;
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct TransferFeeAmount {
pub withheld_amount: PodU64,
}
impl TransferFeeAmount {
pub fn closable(&self) -> ProgramResult {
if self.withheld_amount == 0.into() {
Ok(())
} else {
Err(TokenError::AccountHasWithheldTransferFees.into())
}
}
}
impl Extension for TransferFeeAmount {
const TYPE: ExtensionType = ExtensionType::TransferFeeAmount;
}
#[cfg(test)]
pub(crate) mod test {
use {super::*, solana_program::pubkey::Pubkey, std::convert::TryFrom};
const NEWER_EPOCH: u64 = 100;
const OLDER_EPOCH: u64 = 1;
pub(crate) fn test_transfer_fee_config() -> TransferFeeConfig {
TransferFeeConfig {
transfer_fee_config_authority: OptionalNonZeroPubkey::try_from(Some(Pubkey::new(
&[10; 32],
)))
.unwrap(),
withdraw_withheld_authority: OptionalNonZeroPubkey::try_from(Some(Pubkey::new(
&[11; 32],
)))
.unwrap(),
withheld_amount: PodU64::from(u64::MAX),
older_transfer_fee: TransferFee {
epoch: PodU64::from(OLDER_EPOCH),
maximum_fee: PodU64::from(10),
transfer_fee_basis_points: PodU16::from(100),
},
newer_transfer_fee: TransferFee {
epoch: PodU64::from(NEWER_EPOCH),
maximum_fee: PodU64::from(5_000),
transfer_fee_basis_points: PodU16::from(1),
},
}
}
#[test]
fn epoch_fee() {
let transfer_fee_config = test_transfer_fee_config();
assert_eq!(
transfer_fee_config.get_epoch_fee(NEWER_EPOCH).epoch,
NEWER_EPOCH.into()
);
assert_eq!(
transfer_fee_config.get_epoch_fee(NEWER_EPOCH + 1).epoch,
NEWER_EPOCH.into()
);
assert_eq!(
transfer_fee_config.get_epoch_fee(u64::MAX).epoch,
NEWER_EPOCH.into()
);
assert_eq!(
transfer_fee_config.get_epoch_fee(NEWER_EPOCH - 1).epoch,
OLDER_EPOCH.into()
);
assert_eq!(
transfer_fee_config.get_epoch_fee(OLDER_EPOCH).epoch,
OLDER_EPOCH.into()
);
assert_eq!(
transfer_fee_config.get_epoch_fee(OLDER_EPOCH + 1).epoch,
OLDER_EPOCH.into()
);
}
#[test]
fn calculate_fee_max() {
let one = u64::try_from(ONE_IN_BASIS_POINTS).unwrap();
let transfer_fee = TransferFee {
epoch: PodU64::from(0),
maximum_fee: PodU64::from(5_000),
transfer_fee_basis_points: PodU16::from(1),
};
let maximum_fee = u64::from(transfer_fee.maximum_fee);
assert_eq!(maximum_fee, transfer_fee.calculate(u64::MAX).unwrap());
assert_eq!(
maximum_fee,
transfer_fee.calculate(maximum_fee * one).unwrap()
);
assert_eq!(
maximum_fee,
transfer_fee.calculate(maximum_fee * one + 1).unwrap()
);
assert_eq!(
maximum_fee,
transfer_fee.calculate(maximum_fee * one - 1).unwrap()
);
}
#[test]
fn calculate_fee_min() {
let one = u64::try_from(ONE_IN_BASIS_POINTS).unwrap();
let transfer_fee = TransferFee {
epoch: PodU64::from(0),
maximum_fee: PodU64::from(5_000),
transfer_fee_basis_points: PodU16::from(1),
};
let minimum_fee = 1;
assert_eq!(minimum_fee, transfer_fee.calculate(1).unwrap());
assert_eq!(minimum_fee, transfer_fee.calculate(2).unwrap());
assert_eq!(minimum_fee, transfer_fee.calculate(one).unwrap());
assert_eq!(minimum_fee + 1, transfer_fee.calculate(one + 1).unwrap());
assert_eq!(0, transfer_fee.calculate(0).unwrap());
}
#[test]
fn calculate_fee_zero() {
let one = u64::try_from(ONE_IN_BASIS_POINTS).unwrap();
let transfer_fee = TransferFee {
epoch: PodU64::from(0),
maximum_fee: PodU64::from(u64::MAX),
transfer_fee_basis_points: PodU16::from(0),
};
assert_eq!(0, transfer_fee.calculate(0).unwrap());
assert_eq!(0, transfer_fee.calculate(u64::MAX).unwrap());
assert_eq!(0, transfer_fee.calculate(1).unwrap());
assert_eq!(0, transfer_fee.calculate(one).unwrap());
let transfer_fee = TransferFee {
epoch: PodU64::from(0),
maximum_fee: PodU64::from(0),
transfer_fee_basis_points: PodU16::from(MAX_FEE_BASIS_POINTS),
};
assert_eq!(0, transfer_fee.calculate(0).unwrap());
assert_eq!(0, transfer_fee.calculate(u64::MAX).unwrap());
assert_eq!(0, transfer_fee.calculate(1).unwrap());
assert_eq!(0, transfer_fee.calculate(one).unwrap());
}
}