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
use std::ops::{Div, Sub};
use borsh::BorshDeserialize;
use solana_program::pubkey::Pubkey;
use tabled::Tabled;
use crate::contract::state::tick::Tick;
use crate::error::ErrorCode;
use crate::program::SWAP_PROGRAM_ID;
#[derive(BorshDeserialize, Tabled)]
pub struct TickArrayHeader {
pub array_index: u16,
pub tick_spacing: u16,
pub clmmpool: Pubkey,
}
#[derive(Debug, Tabled)]
pub struct TickArrayToShow {
pub address: Pubkey,
pub array_index: u16,
pub tick_spacing: u16,
pub clmmpool: Pubkey,
pub array_space: u32,
pub start_tick_index: i32,
pub end_tick_index: i32,
}
pub struct TickArray {
pub array_index: u16,
pub tick_spacing: u16,
pub clmmpool: Pubkey,
pub ticks: [Tick; 64],
}
impl Default for TickArray {
#[inline]
fn default() -> Self {
TickArray {
array_index: 0,
tick_spacing: 0,
clmmpool: Pubkey::default(),
ticks: [Tick::default(); 64],
}
}
}
#[derive(Tabled)]
pub struct ArrayInfo {
array_index: u16,
start_tick_index: i32,
end_tick_index: i32,
}
impl TickArray {
pub const CAP: usize = 64;
pub const LEN: usize = 2 + 2 + 32 + Tick::LEN * TickArray::CAP;
#[inline]
pub fn array_spacing(&self) -> usize {
self.tick_spacing as usize * TickArray::CAP
}
#[inline]
pub fn start_tick_index(&self) -> i32 {
Tick::min(self.tick_spacing) + (self.array_index as usize * self.array_spacing()) as i32
}
#[inline]
pub fn end_tick_index(&self) -> i32 {
self.start_tick_index() + self.array_spacing() as i32 - self.tick_spacing as i32
}
#[inline]
pub fn is_in_array(&self, tick_index: i32) -> bool {
tick_index >= self.start_tick_index() && tick_index <= self.end_tick_index()
}
#[inline]
pub fn array_index(tick_index: i32, tick_spacing: u16) -> Result<u16, ErrorCode> {
let min = Tick::min(tick_spacing);
let max = Tick::max(tick_spacing);
if tick_index < min || tick_index > max {
return Err(ErrorCode::InvalidTickIndex);
}
let array_spacing = (TickArray::CAP * tick_spacing as usize) as i32;
Ok(((tick_index - min) / array_spacing) as u16)
}
pub fn array_info(tick_index: i32, tick_spacing: u16) -> ArrayInfo {
let array_index = TickArray::array_index(tick_index, tick_spacing).unwrap();
let array_spacing = tick_spacing as usize * TickArray::CAP;
let start_tick_index =
Tick::min(tick_spacing) + (array_index as usize * array_spacing) as i32;
let end_tick_index = start_tick_index + array_spacing as i32 - tick_spacing as i32;
ArrayInfo {
array_index,
start_tick_index,
end_tick_index,
}
}
pub fn is_min_tick_array(&self) -> bool {
self.start_tick_index() == Tick::min(self.tick_spacing)
}
pub fn is_max_tick_array(&self) -> bool {
self.end_tick_index() >= Tick::max(self.tick_spacing)
}
#[inline]
pub fn tick_offset(&self, tick_index: i32) -> usize {
tick_index
.sub(self.start_tick_index())
.div(self.tick_spacing as i32) as usize
}
pub fn get_tick(&self, tick_index: i32) -> Option<&Tick> {
let offset = self.tick_offset(tick_index);
if offset < TickArray::CAP as usize {
let t = self.ticks[offset as usize];
if t.is_initialized {
return Some(&self.ticks[offset as usize]);
}
}
return None;
}
pub fn search_range(&self, tick_index: i32, a_to_b: bool) -> Option<(usize, usize)> {
match a_to_b {
true => {
if tick_index < self.start_tick_index() {
return None;
}
let end = if tick_index >= self.end_tick_index() {
TickArray::CAP - 1
} else {
self.tick_offset(tick_index)
};
Some((0, end))
}
false => {
if tick_index >= self.end_tick_index() {
return None;
}
let start = if tick_index < self.start_tick_index() {
0
} else {
self.tick_offset(tick_index) + 1
};
Some((start, TickArray::CAP - 1))
}
}
}
pub fn get_next_initialized_tick(&self, tick_index: i32, a_to_b: bool) -> Option<&Tick> {
let search_range = self.search_range(tick_index, a_to_b);
if search_range.is_none() {
return None;
}
let (start, end) = search_range.unwrap();
match a_to_b {
true => {
for i in (start..=end).rev() {
let t = self.ticks[i];
if t.is_initialized {
return Some(&self.ticks[i]);
}
}
None
}
false => {
for i in start..=end {
let t = self.ticks[i];
if t.is_initialized {
return Some(&self.ticks[i]);
}
}
None
}
}
}
pub fn is_tick_array_valid(&self) -> bool {
for tick in &self.ticks {
if tick.is_initialized {
return true;
}
}
false
}
pub fn find_address(clmmpool: &Pubkey, array_index: u16) -> Pubkey {
let (address, _) = Pubkey::find_program_address(
&[
b"tick_array",
clmmpool.as_ref(),
array_index.to_le_bytes().as_ref(),
],
&SWAP_PROGRAM_ID,
);
address
}
pub fn deserialize(buf: Vec<u8>) -> Option<TickArray> {
let header = TickArrayHeader::deserialize(&mut &buf[8..44]).unwrap();
let mut tick_array = TickArray::default();
tick_array.clmmpool = header.clmmpool;
tick_array.array_index = header.array_index;
tick_array.tick_spacing = header.tick_spacing;
let mut s = 44;
let mut e = Tick::LEN + 44;
for i in 0..64 {
let tick = Tick::deserialize(&mut &buf[s..e]).unwrap();
tick_array.ticks[i] = tick;
s = s + Tick::LEN;
e = e + Tick::LEN;
}
Some(tick_array)
}
pub fn calculate_tick_array_key(tick_array: &[u8], array_index: &[u8]) -> (Pubkey, u8) {
Pubkey::find_program_address(&[b"tick_array", tick_array, array_index], &SWAP_PROGRAM_ID)
}
pub fn to_tick_array_show(&self) -> TickArrayToShow {
TickArrayToShow {
address: TickArray::find_address(&self.clmmpool, self.array_index),
array_index: self.array_index,
tick_spacing: self.tick_spacing,
clmmpool: self.clmmpool,
array_space: 0,
start_tick_index: self.start_tick_index(),
end_tick_index: self.end_tick_index(),
}
}
}