-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathpool.rs
247 lines (219 loc) · 7.9 KB
/
pool.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
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
245
246
247
use anchor_lang::prelude::*;
use anchor_spl::token_interface::Mint;
use std::ops::{BitAnd, BitOr, BitXor};
/// Seed to derive account address and signature
pub const POOL_SEED: &str = "pool";
pub const POOL_LP_MINT_SEED: &str = "pool_lp_mint";
pub const POOL_VAULT_SEED: &str = "pool_vault";
pub const Q32: u128 = (u32::MAX as u128) + 1; // 2^32
pub enum PoolStatusBitIndex {
Deposit,
Withdraw,
Swap,
}
#[derive(PartialEq, Eq)]
pub enum PoolStatusBitFlag {
Enable,
Disable,
}
#[account(zero_copy(unsafe))]
#[repr(packed)]
#[derive(Default, Debug)]
pub struct PoolState {
/// Which config the pool belongs
pub amm_config: Pubkey,
/// pool creator
pub pool_creator: Pubkey,
/// Token A
pub token_0_vault: Pubkey,
/// Token B
pub token_1_vault: Pubkey,
/// Pool tokens are issued when A or B tokens are deposited.
/// Pool tokens can be withdrawn back to the original A or B token.
pub lp_mint: Pubkey,
/// Mint information for token A
pub token_0_mint: Pubkey,
/// Mint information for token B
pub token_1_mint: Pubkey,
/// token_0 program
pub token_0_program: Pubkey,
/// token_1 program
pub token_1_program: Pubkey,
/// observation account to store oracle data
pub observation_key: Pubkey,
pub auth_bump: u8,
/// Bitwise representation of the state of the pool
/// bit0, 1: disable deposit(vaule is 1), 0: normal
/// bit1, 1: disable withdraw(vaule is 2), 0: normal
/// bit2, 1: disable swap(vaule is 4), 0: normal
pub status: u8,
pub lp_mint_decimals: u8,
/// mint0 and mint1 decimals
pub mint_0_decimals: u8,
pub mint_1_decimals: u8,
/// True circulating supply without burns and lock ups
pub lp_supply: u64,
/// The amounts of token_0 and token_1 that are owed to the liquidity provider.
pub protocol_fees_token_0: u64,
pub protocol_fees_token_1: u64,
pub fund_fees_token_0: u64,
pub fund_fees_token_1: u64,
/// The timestamp allowed for swap in the pool.
pub open_time: u64,
/// recent epoch
pub recent_epoch: u64,
/// padding for future updates
pub padding: [u64; 31],
}
impl PoolState {
pub const LEN: usize = 8 + 10 * 32 + 1 * 5 + 8 * 7 + 8 * 31;
pub fn initialize(
&mut self,
auth_bump: u8,
lp_supply: u64,
open_time: u64,
pool_creator: Pubkey,
amm_config: Pubkey,
token_0_vault: Pubkey,
token_1_vault: Pubkey,
token_0_mint: &InterfaceAccount<Mint>,
token_1_mint: &InterfaceAccount<Mint>,
lp_mint: &InterfaceAccount<Mint>,
observation_key: Pubkey,
) {
self.amm_config = amm_config.key();
self.pool_creator = pool_creator.key();
self.token_0_vault = token_0_vault;
self.token_1_vault = token_1_vault;
self.lp_mint = lp_mint.key();
self.token_0_mint = token_0_mint.key();
self.token_1_mint = token_1_mint.key();
self.token_0_program = *token_0_mint.to_account_info().owner;
self.token_1_program = *token_1_mint.to_account_info().owner;
self.observation_key = observation_key;
self.auth_bump = auth_bump;
self.lp_mint_decimals = lp_mint.decimals;
self.mint_0_decimals = token_0_mint.decimals;
self.mint_1_decimals = token_1_mint.decimals;
self.lp_supply = lp_supply;
self.protocol_fees_token_0 = 0;
self.protocol_fees_token_1 = 0;
self.fund_fees_token_0 = 0;
self.fund_fees_token_1 = 0;
self.open_time = open_time;
self.recent_epoch = Clock::get().unwrap().epoch;
self.padding = [0u64; 31];
}
pub fn set_status(&mut self, status: u8) {
self.status = status
}
pub fn set_status_by_bit(&mut self, bit: PoolStatusBitIndex, flag: PoolStatusBitFlag) {
let s = u8::from(1) << (bit as u8);
if flag == PoolStatusBitFlag::Disable {
self.status = self.status.bitor(s);
} else {
let m = u8::from(255).bitxor(s);
self.status = self.status.bitand(m);
}
}
/// Get status by bit, if it is `noraml` status, return true
pub fn get_status_by_bit(&self, bit: PoolStatusBitIndex) -> bool {
let status = u8::from(1) << (bit as u8);
self.status.bitand(status) == 0
}
pub fn vault_amount_without_fee(&self, vault_0: u64, vault_1: u64) -> (u64, u64) {
(
vault_0
.checked_sub(self.protocol_fees_token_0 + self.fund_fees_token_0)
.unwrap(),
vault_1
.checked_sub(self.protocol_fees_token_1 + self.fund_fees_token_1)
.unwrap(),
)
}
pub fn token_price_x32(&self, vault_0: u64, vault_1: u64) -> (u128, u128) {
let (token_0_amount, token_1_amount) = self.vault_amount_without_fee(vault_0, vault_1);
(
token_1_amount as u128 * Q32 as u128 / token_0_amount as u128,
token_0_amount as u128 * Q32 as u128 / token_1_amount as u128,
)
}
}
#[cfg(test)]
pub mod pool_test {
use super::*;
mod pool_status_test {
use super::*;
#[test]
fn get_set_status_by_bit() {
let mut pool_state = PoolState::default();
pool_state.set_status(4); // 0000100
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Swap),
false
);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Deposit),
true
);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Withdraw),
true
);
// disable -> disable, nothing to change
pool_state.set_status_by_bit(PoolStatusBitIndex::Swap, PoolStatusBitFlag::Disable);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Swap),
false
);
// disable -> enable
pool_state.set_status_by_bit(PoolStatusBitIndex::Swap, PoolStatusBitFlag::Enable);
assert_eq!(pool_state.get_status_by_bit(PoolStatusBitIndex::Swap), true);
// enable -> enable, nothing to change
pool_state.set_status_by_bit(PoolStatusBitIndex::Swap, PoolStatusBitFlag::Enable);
assert_eq!(pool_state.get_status_by_bit(PoolStatusBitIndex::Swap), true);
// enable -> disable
pool_state.set_status_by_bit(PoolStatusBitIndex::Swap, PoolStatusBitFlag::Disable);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Swap),
false
);
pool_state.set_status(5); // 0000101
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Swap),
false
);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Deposit),
false
);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Withdraw),
true
);
pool_state.set_status(7); // 0000111
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Swap),
false
);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Deposit),
false
);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Withdraw),
false
);
pool_state.set_status(3); // 0000011
assert_eq!(pool_state.get_status_by_bit(PoolStatusBitIndex::Swap), true);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Deposit),
false
);
assert_eq!(
pool_state.get_status_by_bit(PoolStatusBitIndex::Withdraw),
false
);
}
}
}