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
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::PROGRAM_ID;
#[derive(BorshDeserialize)]
pub struct TickArrayV2 {
pub array_index: u16,
pub tick_spacing: u16,
pub clmmpool: Pubkey,
}
#[derive(Debug, Tabled)]
pub struct TickArrayToShow {
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],
}
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 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 get_tick_array_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(),
],
&PROGRAM_ID,
);
address
}
pub fn is_tick_array_valid(&self) -> bool {
for tick in &self.ticks {
if tick.is_initialized {
return true;
}
}
false
}
}