@symmetry-hq/temp-v3-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/dist/idl/idl.d.ts +8 -0
  2. package/dist/idl/idl.js +4958 -0
  3. package/dist/idl/types.d.ts +4955 -0
  4. package/dist/idl/types.js +2 -0
  5. package/dist/index.d.ts +24 -0
  6. package/dist/index.js +60 -0
  7. package/dist/layouts/basket.d.ts +42 -0
  8. package/dist/layouts/basket.js +43 -0
  9. package/dist/layouts/config.d.ts +97 -0
  10. package/dist/layouts/config.js +102 -0
  11. package/dist/layouts/fraction.d.ts +6 -0
  12. package/dist/layouts/fraction.js +9 -0
  13. package/dist/layouts/oracle.d.ts +36 -0
  14. package/dist/layouts/oracle.js +37 -0
  15. package/dist/src/constants.d.ts +15 -0
  16. package/dist/src/constants.js +19 -0
  17. package/dist/src/idl/idl.d.ts +8 -0
  18. package/dist/src/idl/idl.js +4958 -0
  19. package/dist/src/idl/types.d.ts +4955 -0
  20. package/dist/src/idl/types.js +2 -0
  21. package/dist/src/index.d.ts +53 -0
  22. package/dist/src/index.js +199 -0
  23. package/dist/src/instructions/accounts.d.ts +27 -0
  24. package/dist/src/instructions/accounts.js +110 -0
  25. package/dist/src/instructions/auction.d.ts +7 -0
  26. package/dist/src/instructions/auction.js +43 -0
  27. package/dist/src/instructions/automation/auction.d.ts +6 -0
  28. package/dist/src/instructions/automation/auction.js +40 -0
  29. package/dist/src/instructions/automation/claimBounty.d.ts +12 -0
  30. package/dist/src/instructions/automation/claimBounty.js +44 -0
  31. package/dist/src/instructions/automation/priceUpdate.d.ts +15 -0
  32. package/dist/src/instructions/automation/priceUpdate.js +47 -0
  33. package/dist/src/instructions/automation/rebalanceSwap.d.ts +10 -0
  34. package/dist/src/instructions/automation/rebalanceSwap.js +42 -0
  35. package/dist/src/instructions/basket.d.ts +120 -0
  36. package/dist/src/instructions/basket.js +622 -0
  37. package/dist/src/instructions/bounty.d.ts +18 -0
  38. package/dist/src/instructions/bounty.js +81 -0
  39. package/dist/src/instructions/fee.d.ts +8 -0
  40. package/dist/src/instructions/fee.js +46 -0
  41. package/dist/src/instructions/globalConfig.d.ts +11 -0
  42. package/dist/src/instructions/globalConfig.js +43 -0
  43. package/dist/src/instructions/luts.d.ts +33 -0
  44. package/dist/src/instructions/luts.js +91 -0
  45. package/dist/src/instructions/management/addBounty.d.ts +7 -0
  46. package/dist/src/instructions/management/addBounty.js +39 -0
  47. package/dist/src/instructions/management/admin.d.ts +9 -0
  48. package/dist/src/instructions/management/admin.js +43 -0
  49. package/dist/src/instructions/management/claimFees.d.ts +7 -0
  50. package/dist/src/instructions/management/claimFees.js +47 -0
  51. package/dist/src/instructions/management/createBasket.d.ts +22 -0
  52. package/dist/src/instructions/management/createBasket.js +101 -0
  53. package/dist/src/instructions/management/edit.d.ts +34 -0
  54. package/dist/src/instructions/management/edit.js +192 -0
  55. package/dist/src/instructions/management/luts.d.ts +29 -0
  56. package/dist/src/instructions/management/luts.js +88 -0
  57. package/dist/src/instructions/pda.d.ts +26 -0
  58. package/dist/src/instructions/pda.js +110 -0
  59. package/dist/src/instructions/price.d.ts +17 -0
  60. package/dist/src/instructions/price.js +47 -0
  61. package/dist/src/instructions/user/deposit.d.ts +32 -0
  62. package/dist/src/instructions/user/deposit.js +168 -0
  63. package/dist/src/instructions/user/withdraw.d.ts +16 -0
  64. package/dist/src/instructions/user/withdraw.js +82 -0
  65. package/dist/src/layouts/basket.d.ts +41 -0
  66. package/dist/src/layouts/basket.js +43 -0
  67. package/dist/src/layouts/config.d.ts +133 -0
  68. package/dist/src/layouts/config.js +137 -0
  69. package/dist/src/layouts/fraction.d.ts +6 -0
  70. package/dist/src/layouts/fraction.js +9 -0
  71. package/dist/src/layouts/intents/bounty.d.ts +18 -0
  72. package/dist/src/layouts/intents/bounty.js +19 -0
  73. package/dist/src/layouts/intents/intent.d.ts +101 -0
  74. package/dist/src/layouts/intents/intent.js +113 -0
  75. package/dist/src/layouts/intents/rebalanceIntent.d.ts +56 -0
  76. package/dist/src/layouts/intents/rebalanceIntent.js +63 -0
  77. package/dist/src/layouts/lookupTable.d.ts +7 -0
  78. package/dist/src/layouts/lookupTable.js +10 -0
  79. package/dist/src/layouts/oracle.d.ts +42 -0
  80. package/dist/src/layouts/oracle.js +43 -0
  81. package/dist/src/states/basket.d.ts +8 -0
  82. package/dist/src/states/basket.js +54 -0
  83. package/dist/src/states/intents/intent.d.ts +14 -0
  84. package/dist/src/states/intents/intent.js +90 -0
  85. package/dist/src/states/intents/rebalanceIntent.d.ts +8 -0
  86. package/dist/src/states/intents/rebalanceIntent.js +54 -0
  87. package/dist/src/states/oracles/clmm_oracle.d.ts +178 -0
  88. package/dist/src/states/oracles/clmm_oracle.js +546 -0
  89. package/dist/src/states/oracles/constants.d.ts +8 -0
  90. package/dist/src/states/oracles/constants.js +12 -0
  91. package/dist/src/states/oracles/oracle.d.ts +60 -0
  92. package/dist/src/states/oracles/oracle.js +237 -0
  93. package/dist/src/states/oracles/pythOracle.d.ts +39 -0
  94. package/dist/src/states/oracles/pythOracle.js +202 -0
  95. package/dist/src/states/oracles/pyth_oracle.d.ts +39 -0
  96. package/dist/src/states/oracles/pyth_oracle.js +202 -0
  97. package/dist/src/states/oracles/raydiumClmmOracle.d.ts +178 -0
  98. package/dist/src/states/oracles/raydiumClmmOracle.js +546 -0
  99. package/dist/src/states/oracles/raydiumCpmmOracle.d.ts +139 -0
  100. package/dist/src/states/oracles/raydiumCpmmOracle.js +420 -0
  101. package/dist/src/states/oracles/raydium_cpmm_oracle.d.ts +139 -0
  102. package/dist/src/states/oracles/raydium_cpmm_oracle.js +420 -0
  103. package/dist/src/states/oracles/switchboardOracle.d.ts +0 -0
  104. package/dist/src/states/oracles/switchboardOracle.js +1 -0
  105. package/dist/src/states/oracles/switchboard_oracle.d.ts +0 -0
  106. package/dist/src/states/oracles/switchboard_oracle.js +1 -0
  107. package/dist/src/txUtils.d.ts +26 -0
  108. package/dist/src/txUtils.js +183 -0
  109. package/dist/states/basket.d.ts +8 -0
  110. package/dist/states/basket.js +57 -0
  111. package/dist/test.d.ts +1 -0
  112. package/dist/test.js +39 -0
  113. package/package.json +30 -0
  114. package/src/constants.ts +24 -0
  115. package/src/index.ts +260 -0
  116. package/src/instructions/automation/auction.ts +55 -0
  117. package/src/instructions/automation/claimBounty.ts +69 -0
  118. package/src/instructions/automation/priceUpdate.ts +73 -0
  119. package/src/instructions/automation/rebalanceSwap.ts +60 -0
  120. package/src/instructions/management/addBounty.ts +56 -0
  121. package/src/instructions/management/admin.ts +65 -0
  122. package/src/instructions/management/claimFees.ts +59 -0
  123. package/src/instructions/management/createBasket.ts +148 -0
  124. package/src/instructions/management/edit.ts +255 -0
  125. package/src/instructions/management/luts.ts +134 -0
  126. package/src/instructions/pda.ts +160 -0
  127. package/src/instructions/user/deposit.ts +237 -0
  128. package/src/instructions/user/withdraw.ts +130 -0
  129. package/src/layouts/basket.ts +82 -0
  130. package/src/layouts/config.ts +301 -0
  131. package/src/layouts/fraction.ts +12 -0
  132. package/src/layouts/intents/bounty.ts +35 -0
  133. package/src/layouts/intents/intent.ts +204 -0
  134. package/src/layouts/intents/rebalanceIntent.ts +111 -0
  135. package/src/layouts/lookupTable.ts +14 -0
  136. package/src/layouts/oracle.ts +96 -0
  137. package/src/states/basket.ts +56 -0
  138. package/src/states/intents/intent.ts +107 -0
  139. package/src/states/intents/rebalanceIntent.ts +57 -0
  140. package/src/states/oracles/constants.ts +13 -0
  141. package/src/states/oracles/oracle.ts +260 -0
  142. package/src/states/oracles/pythOracle.ts +270 -0
  143. package/src/states/oracles/raydiumClmmOracle.ts +812 -0
  144. package/src/states/oracles/raydiumCpmmOracle.ts +614 -0
  145. package/src/states/oracles/switchboardOracle.ts +0 -0
  146. package/src/txUtils.ts +250 -0
  147. package/test.ts +30 -0
  148. package/tsconfig.json +101 -0
@@ -0,0 +1,546 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.RaydiumCLMMOracle = void 0;
16
+ const decimal_js_1 = __importDefault(require("decimal.js"));
17
+ const anchor_1 = require("@coral-xyz/anchor");
18
+ const web3_js_1 = require("@solana/web3.js");
19
+ const constants_1 = require("./constants");
20
+ const oracle_1 = require("./oracle");
21
+ const CLMM_PROGRAM_ID = new web3_js_1.PublicKey("CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK");
22
+ const MIN_TICK = -443636;
23
+ const MAX_TICK = 443636;
24
+ class Observation {
25
+ constructor(params) {
26
+ this.block_timestamp = params.block_timestamp;
27
+ this.tick_cumulative = params.tick_cumulative;
28
+ this.padding = params.padding;
29
+ }
30
+ static decode(data, offset = 0) {
31
+ const block_timestamp = new anchor_1.BN(data.readUInt32LE(offset));
32
+ const tick_cumulative = new anchor_1.BN(data.subarray(offset + 4, offset + 12), "le").fromTwos(64);
33
+ const padding = [];
34
+ let cursor = offset + 12;
35
+ for (let i = 0; i < 4; i++) {
36
+ padding.push(new anchor_1.BN(data.subarray(cursor, cursor + 8), "le"));
37
+ cursor += 8;
38
+ }
39
+ return [
40
+ new Observation({
41
+ block_timestamp,
42
+ tick_cumulative,
43
+ padding
44
+ }),
45
+ cursor
46
+ ];
47
+ }
48
+ sub(other) {
49
+ if (!(other instanceof Observation)) {
50
+ throw new TypeError("Subtraction is only supported between Observation instances");
51
+ }
52
+ return new Observation({
53
+ block_timestamp: this.block_timestamp.sub(other.block_timestamp),
54
+ tick_cumulative: this.tick_cumulative.sub(other.tick_cumulative),
55
+ padding: this.padding
56
+ });
57
+ }
58
+ add(other) {
59
+ if (!(other instanceof Observation)) {
60
+ throw new TypeError("Addition is only supported between Observation instances");
61
+ }
62
+ return new Observation({
63
+ block_timestamp: this.block_timestamp.add(other.block_timestamp),
64
+ tick_cumulative: this.tick_cumulative.add(other.tick_cumulative),
65
+ padding: this.padding
66
+ });
67
+ }
68
+ get_weighted_observation(time) {
69
+ // cum_t = (self.tick_cumulative * time) / self.block_timestamp
70
+ const cum = this.tick_cumulative.mul(time).div(this.block_timestamp);
71
+ return new Observation({
72
+ block_timestamp: time,
73
+ tick_cumulative: cum,
74
+ padding: this.padding
75
+ });
76
+ }
77
+ adjust_to_timestamp(target_timestamp, observation_prev) {
78
+ // delta = self.sub(prev_observation)
79
+ const delta = this.sub(observation_prev);
80
+ // weighted_delta = delta.get_weighted_observation(target_timestamp - self.block_timestamp)
81
+ const timeForWeighted = target_timestamp.sub(this.block_timestamp);
82
+ const weighted_delta = delta.get_weighted_observation(timeForWeighted);
83
+ // return self.add(weighted_delta)
84
+ return this.add(weighted_delta);
85
+ }
86
+ }
87
+ class ObservationState {
88
+ constructor(params) {
89
+ this.initialized = params.initialized;
90
+ this.recent_epoch = params.recent_epoch;
91
+ this.observation_index = params.observation_index;
92
+ this.pool_id = params.pool_id;
93
+ this.observations = params.observations;
94
+ this.padding = params.padding;
95
+ }
96
+ static decode(data, offset = 8) {
97
+ let cursor = offset;
98
+ const initialized = data.readUInt8(cursor) !== 0;
99
+ cursor += 1;
100
+ const recent_epoch = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
101
+ cursor += 8;
102
+ const observation_index = data.readUInt16LE(cursor);
103
+ cursor += 2;
104
+ const pool_id = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
105
+ cursor += 32;
106
+ const observations = [];
107
+ for (let i = 0; i < 100; i++) {
108
+ let obs;
109
+ [obs, cursor] = Observation.decode(data, cursor);
110
+ observations.push(obs);
111
+ }
112
+ const padding = [];
113
+ for (let i = 0; i < 4; i++) {
114
+ padding.push(new anchor_1.BN(data.subarray(cursor, cursor + 8), "le"));
115
+ cursor += 8;
116
+ }
117
+ return [
118
+ new ObservationState({ initialized, recent_epoch, observation_index, pool_id, observations, padding }),
119
+ cursor
120
+ ];
121
+ }
122
+ }
123
+ class RewardInfo {
124
+ constructor(params) {
125
+ this.reward_state = params.reward_state;
126
+ this.open_time = params.open_time;
127
+ this.end_time = params.end_time;
128
+ this.last_update_time = params.last_update_time;
129
+ this.emissions_per_second_x64 = params.emissions_per_second_x64;
130
+ this.reward_total_emissioned = params.reward_total_emissioned;
131
+ this.reward_claimed = params.reward_claimed;
132
+ this.token_mint = params.token_mint;
133
+ this.token_vault = params.token_vault;
134
+ this.authority = params.authority;
135
+ this.reward_growth_global_x64 = params.reward_growth_global_x64;
136
+ }
137
+ static decode(data, offset = 0) {
138
+ let cursor = offset;
139
+ const reward_state = data.readUInt8(cursor);
140
+ cursor += 1;
141
+ const open_time = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
142
+ cursor += 8;
143
+ const end_time = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
144
+ cursor += 8;
145
+ const last_update_time = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
146
+ cursor += 8;
147
+ const emissions_per_second_x64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
148
+ cursor += 16;
149
+ const reward_total_emissioned = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
150
+ cursor += 8;
151
+ const reward_claimed = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
152
+ cursor += 8;
153
+ const token_mint = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
154
+ cursor += 32;
155
+ const token_vault = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
156
+ cursor += 32;
157
+ const authority = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
158
+ cursor += 32;
159
+ const reward_growth_global_x64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
160
+ cursor += 16;
161
+ return [new RewardInfo({
162
+ reward_state,
163
+ open_time,
164
+ end_time,
165
+ last_update_time,
166
+ emissions_per_second_x64,
167
+ reward_total_emissioned,
168
+ reward_claimed,
169
+ token_mint,
170
+ token_vault,
171
+ authority,
172
+ reward_growth_global_x64
173
+ }), cursor];
174
+ }
175
+ }
176
+ class PoolState {
177
+ constructor(params) {
178
+ this.bump = params.bump;
179
+ this.amm_config = params.amm_config;
180
+ this.owner = params.owner;
181
+ this.token_mint_0 = params.token_mint_0;
182
+ this.token_mint_1 = params.token_mint_1;
183
+ this.token_vault_0 = params.token_vault_0;
184
+ this.token_vault_1 = params.token_vault_1;
185
+ this.observation_key = params.observation_key;
186
+ this.mint_decimals_0 = params.mint_decimals_0;
187
+ this.mint_decimals_1 = params.mint_decimals_1;
188
+ this.tick_spacing = params.tick_spacing;
189
+ this.liquidity = params.liquidity;
190
+ this.sqrt_price_x64 = params.sqrt_price_x64;
191
+ this.tick_current = params.tick_current;
192
+ this.padding3 = params.padding3;
193
+ this.padding4 = params.padding4;
194
+ this.fee_growth_global_0_x64 = params.fee_growth_global_0_x64;
195
+ this.fee_growth_global_1_x64 = params.fee_growth_global_1_x64;
196
+ this.protocol_fees_token_0 = params.protocol_fees_token_0;
197
+ this.protocol_fees_token_1 = params.protocol_fees_token_1;
198
+ this.swap_in_amount_token_0 = params.swap_in_amount_token_0;
199
+ this.swap_out_amount_token_1 = params.swap_out_amount_token_1;
200
+ this.swap_in_amount_token_1 = params.swap_in_amount_token_1;
201
+ this.swap_out_amount_token_0 = params.swap_out_amount_token_0;
202
+ this.status = params.status;
203
+ this.padding = params.padding;
204
+ this.reward_infos = params.reward_infos;
205
+ this.tick_array_bitmap = params.tick_array_bitmap;
206
+ this.total_fees_token_0 = params.total_fees_token_0;
207
+ this.total_fees_claimed_token_0 = params.total_fees_claimed_token_0;
208
+ this.total_fees_token_1 = params.total_fees_token_1;
209
+ this.total_fees_claimed_token_1 = params.total_fees_claimed_token_1;
210
+ this.fund_fees_token_0 = params.fund_fees_token_0;
211
+ this.fund_fees_token_1 = params.fund_fees_token_1;
212
+ this.open_time = params.open_time;
213
+ this.recent_epoch = params.recent_epoch;
214
+ this.padding1 = params.padding1;
215
+ this.padding2 = params.padding2;
216
+ }
217
+ static decode(data, offset = 8) {
218
+ let cursor = offset;
219
+ const bump = [data.readUInt8(cursor)];
220
+ cursor += 1;
221
+ const amm_config = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
222
+ cursor += 32;
223
+ const owner = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
224
+ cursor += 32;
225
+ const token_mint_0 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
226
+ cursor += 32;
227
+ const token_mint_1 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
228
+ cursor += 32;
229
+ const token_vault_0 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
230
+ cursor += 32;
231
+ const token_vault_1 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
232
+ cursor += 32;
233
+ const observation_key = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
234
+ cursor += 32;
235
+ const mint_decimals_0 = data.readUInt8(cursor);
236
+ cursor += 1;
237
+ const mint_decimals_1 = data.readUInt8(cursor);
238
+ cursor += 1;
239
+ const tick_spacing = data.readUInt16LE(cursor);
240
+ cursor += 2;
241
+ const liquidity = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
242
+ cursor += 16;
243
+ const sqrt_price_x64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
244
+ cursor += 16;
245
+ const tick_current = data.readInt32LE(cursor);
246
+ cursor += 4;
247
+ const padding3 = data.readUInt16LE(cursor);
248
+ cursor += 2;
249
+ const padding4 = data.readUInt16LE(cursor);
250
+ cursor += 2;
251
+ const fee_growth_global_0_x64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
252
+ cursor += 16;
253
+ const fee_growth_global_1_x64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
254
+ cursor += 16;
255
+ const protocol_fees_token_0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
256
+ cursor += 8;
257
+ const protocol_fees_token_1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
258
+ cursor += 8;
259
+ const swap_in_amount_token_0 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
260
+ cursor += 16;
261
+ const swap_out_amount_token_1 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
262
+ cursor += 16;
263
+ const swap_in_amount_token_1 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
264
+ cursor += 16;
265
+ const swap_out_amount_token_0 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
266
+ cursor += 16;
267
+ const status = data.readUInt8(cursor);
268
+ cursor += 1;
269
+ const padding = [];
270
+ for (let i = 0; i < 7; i++) {
271
+ padding.push(data.readUInt8(cursor));
272
+ cursor += 1;
273
+ }
274
+ // RewardInfos
275
+ const reward_infos = [];
276
+ for (let i = 0; i < 3; i++) {
277
+ const decoded = RewardInfo.decode(data, cursor);
278
+ reward_infos.push(decoded[0]);
279
+ cursor = decoded[1];
280
+ }
281
+ // tick_array_bitmap [u64;16]
282
+ const tick_array_bitmap = [];
283
+ for (let i = 0; i < 16; i++) {
284
+ tick_array_bitmap.push(new anchor_1.BN(data.subarray(cursor, cursor + 8), "le"));
285
+ cursor += 8;
286
+ }
287
+ const total_fees_token_0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
288
+ cursor += 8;
289
+ const total_fees_claimed_token_0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
290
+ cursor += 8;
291
+ const total_fees_token_1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
292
+ cursor += 8;
293
+ const total_fees_claimed_token_1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
294
+ cursor += 8;
295
+ const fund_fees_token_0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
296
+ cursor += 8;
297
+ const fund_fees_token_1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
298
+ cursor += 8;
299
+ const open_time = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
300
+ cursor += 8;
301
+ const recent_epoch = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
302
+ cursor += 8;
303
+ const padding1 = [];
304
+ for (let i = 0; i < 24; i++) {
305
+ padding1.push(new anchor_1.BN(data.subarray(cursor, cursor + 8), "le"));
306
+ cursor += 8;
307
+ }
308
+ const padding2 = [];
309
+ for (let i = 0; i < 32; i++) {
310
+ padding2.push(new anchor_1.BN(data.subarray(cursor, cursor + 8), "le"));
311
+ cursor += 8;
312
+ }
313
+ return [
314
+ new PoolState({
315
+ bump,
316
+ amm_config,
317
+ owner,
318
+ token_mint_0,
319
+ token_mint_1,
320
+ token_vault_0,
321
+ token_vault_1,
322
+ observation_key,
323
+ mint_decimals_0,
324
+ mint_decimals_1,
325
+ tick_spacing,
326
+ liquidity,
327
+ sqrt_price_x64,
328
+ tick_current,
329
+ padding3,
330
+ padding4,
331
+ fee_growth_global_0_x64,
332
+ fee_growth_global_1_x64,
333
+ protocol_fees_token_0,
334
+ protocol_fees_token_1,
335
+ swap_in_amount_token_0,
336
+ swap_out_amount_token_1,
337
+ swap_in_amount_token_1,
338
+ swap_out_amount_token_0,
339
+ status,
340
+ padding,
341
+ reward_infos,
342
+ tick_array_bitmap,
343
+ total_fees_token_0,
344
+ total_fees_claimed_token_0,
345
+ total_fees_token_1,
346
+ total_fees_claimed_token_1,
347
+ fund_fees_token_0,
348
+ fund_fees_token_1,
349
+ open_time,
350
+ recent_epoch,
351
+ padding1,
352
+ padding2,
353
+ }),
354
+ cursor,
355
+ ];
356
+ }
357
+ }
358
+ class RaydiumCLMMOracle extends oracle_1.Oracle {
359
+ constructor(params) {
360
+ super(params.weight, params.conf_thresh_bps, params.staleness_thresh, params.volatility_thresh, params.min_liquidity, params.staleness_conf_rate_bps);
361
+ this.pool_id = params.pool_id;
362
+ this.quote_token = params.quote_token;
363
+ this.pool_state = params.pool_state;
364
+ this.observation_state = params.observation_state;
365
+ this.primary_seconds = params.primary_seconds;
366
+ this.secondary_seconds = params.secondary_seconds;
367
+ }
368
+ ;
369
+ static create(params) {
370
+ return __awaiter(this, void 0, void 0, function* () {
371
+ const { connection, pool_id } = params;
372
+ // Fetch pool_state
373
+ const poolAccountInfo = yield connection.getAccountInfo(new web3_js_1.PublicKey(pool_id));
374
+ if (!poolAccountInfo)
375
+ throw new Error("Pool account not found");
376
+ const [pool_state, _] = PoolState.decode(poolAccountInfo.data, 8);
377
+ // Fetch observation_state from pool_state
378
+ const obsPubkey = pool_state.observation_key;
379
+ const obsAccountInfo = yield connection.getAccountInfo(new web3_js_1.PublicKey(obsPubkey));
380
+ if (!obsAccountInfo)
381
+ throw new Error("Observation account not found");
382
+ const [observation_state, __] = ObservationState.decode(obsAccountInfo.data, 8);
383
+ return new RaydiumCLMMOracle({
384
+ pool_id: pool_id,
385
+ quote_token: params.quote_token,
386
+ pool_state: pool_state,
387
+ observation_state: observation_state,
388
+ weight: params.weight,
389
+ primary_seconds: params.primary_seconds,
390
+ secondary_seconds: params.secondary_seconds,
391
+ conf_thresh_bps: params.conf_thresh_bps,
392
+ staleness_thresh: params.staleness_thresh,
393
+ volatility_thresh: params.volatility_thresh,
394
+ min_liquidity: params.min_liquidity,
395
+ staleness_conf_rate_bps: params.staleness_conf_rate_bps
396
+ });
397
+ });
398
+ }
399
+ static derive_observation_key(poolId) {
400
+ const seeds = [
401
+ Buffer.from("observation"),
402
+ poolId.toBuffer(),
403
+ ];
404
+ return web3_js_1.PublicKey.findProgramAddressSync(seeds, CLMM_PROGRAM_ID);
405
+ }
406
+ ;
407
+ get_observation_at_index(index) {
408
+ return this.observation_state.observations[index];
409
+ }
410
+ get_observation_at_timestamp(timestamp, startIndex) {
411
+ let observationCurrent = this.get_observation_at_index(startIndex);
412
+ let index = startIndex;
413
+ // const obsIndex = this.observation_state.observation_index;
414
+ // Loop backwards until we find the observation <= timestamp
415
+ while (observationCurrent.block_timestamp.gt(timestamp)) {
416
+ index = (index - 1 + 100) % 100;
417
+ observationCurrent = this.get_observation_at_index(index);
418
+ if (index === startIndex) {
419
+ throw Error("Observations do not go back far enough in time");
420
+ break; // looped all the way around
421
+ }
422
+ }
423
+ // Previous index for interpolation
424
+ const prevIndex = (index - 1 + 100) % 100;
425
+ const observationPrev = this.get_observation_at_index(prevIndex);
426
+ // Adjust current observation to the target timestamp
427
+ const result = observationCurrent.adjust_to_timestamp(timestamp, observationPrev);
428
+ return result;
429
+ }
430
+ get_delta_observations(currentTime, primary_seconds, secondary_seconds) {
431
+ const obsIndex = this.observation_state.observation_index;
432
+ const observationCurrent = this.get_observation_at_timestamp(currentTime, obsIndex);
433
+ const observationPrimary = this.get_observation_at_timestamp(new anchor_1.BN(currentTime.sub(new anchor_1.BN(primary_seconds))), obsIndex);
434
+ const deltaPrimary = observationCurrent.sub(observationPrimary);
435
+ const observationSecondary = this.get_observation_at_timestamp(new anchor_1.BN(currentTime.sub(new anchor_1.BN(secondary_seconds))), obsIndex);
436
+ const deltaSecondary = observationCurrent.sub(observationSecondary);
437
+ return [observationPrimary, observationSecondary];
438
+ }
439
+ get_sqrt_price_x64_from_tick(tick) {
440
+ let num_tick = tick.toNumber();
441
+ if (!Number.isInteger(num_tick)) {
442
+ throw new Error("tick must be integer");
443
+ }
444
+ if (num_tick < MIN_TICK || num_tick > MAX_TICK) {
445
+ throw new Error("tick must be in MIN_TICK and MAX_TICK");
446
+ }
447
+ const tickAbs = num_tick < 0 ? num_tick * -1 : num_tick;
448
+ let ratio = (tickAbs & 0x1) != 0 ? new anchor_1.BN("18445821805675395072") : new anchor_1.BN("18446744073709551616");
449
+ if ((tickAbs & 0x2) != 0)
450
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18444899583751176192"));
451
+ if ((tickAbs & 0x4) != 0)
452
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18443055278223355904"));
453
+ if ((tickAbs & 0x8) != 0)
454
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18439367220385607680"));
455
+ if ((tickAbs & 0x10) != 0)
456
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18431993317065453568"));
457
+ if ((tickAbs & 0x20) != 0)
458
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18417254355718170624"));
459
+ if ((tickAbs & 0x40) != 0)
460
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18387811781193609216"));
461
+ if ((tickAbs & 0x80) != 0)
462
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18329067761203558400"));
463
+ if ((tickAbs & 0x100) != 0)
464
+ ratio = mul_right_shift(ratio, new anchor_1.BN("18212142134806163456"));
465
+ if ((tickAbs & 0x200) != 0)
466
+ ratio = mul_right_shift(ratio, new anchor_1.BN("17980523815641700352"));
467
+ if ((tickAbs & 0x400) != 0)
468
+ ratio = mul_right_shift(ratio, new anchor_1.BN("17526086738831433728"));
469
+ if ((tickAbs & 0x800) != 0)
470
+ ratio = mul_right_shift(ratio, new anchor_1.BN("16651378430235570176"));
471
+ if ((tickAbs & 0x1000) != 0)
472
+ ratio = mul_right_shift(ratio, new anchor_1.BN("15030750278694412288"));
473
+ if ((tickAbs & 0x2000) != 0)
474
+ ratio = mul_right_shift(ratio, new anchor_1.BN("12247334978884435968"));
475
+ if ((tickAbs & 0x4000) != 0)
476
+ ratio = mul_right_shift(ratio, new anchor_1.BN("8131365268886854656"));
477
+ if ((tickAbs & 0x8000) != 0)
478
+ ratio = mul_right_shift(ratio, new anchor_1.BN("3584323654725218816"));
479
+ if ((tickAbs & 0x10000) != 0)
480
+ ratio = mul_right_shift(ratio, new anchor_1.BN("696457651848324352"));
481
+ if ((tickAbs & 0x20000) != 0)
482
+ ratio = mul_right_shift(ratio, new anchor_1.BN("26294789957507116"));
483
+ if ((tickAbs & 0x40000) != 0)
484
+ ratio = mul_right_shift(ratio, new anchor_1.BN("37481735321082"));
485
+ if (num_tick > 0)
486
+ ratio = constants_1.U128_MAX.div(ratio);
487
+ return ratio;
488
+ }
489
+ sqrt_price_x64_to_price(sqrtPriceX64, decimalsA, decimalsB) {
490
+ return this.x64_to_decimal(sqrtPriceX64)
491
+ .pow(2)
492
+ .mul(decimal_js_1.default.pow(10, decimalsA - decimalsB));
493
+ }
494
+ x64_to_decimal(num, decimalPlaces) {
495
+ return new decimal_js_1.default(num.toString()).div(decimal_js_1.default.pow(2, 64)).toDecimalPlaces(decimalPlaces);
496
+ }
497
+ get_twap_primary(current_time) {
498
+ let observation = this.get_delta_observations(current_time, this.primary_seconds, this.secondary_seconds)[0];
499
+ let tick = observation.tick_cumulative.div(observation.block_timestamp);
500
+ let twap = this.sqrt_price_x64_to_price(this.get_sqrt_price_x64_from_tick(tick), this.pool_state.mint_decimals_0, this.pool_state.mint_decimals_1);
501
+ return twap;
502
+ }
503
+ get_twap_secondary(current_time) {
504
+ let observation = this.get_delta_observations(current_time, this.primary_seconds, this.secondary_seconds)[1];
505
+ let tick = observation.tick_cumulative.div(observation.block_timestamp);
506
+ let twap = this.sqrt_price_x64_to_price(this.get_sqrt_price_x64_from_tick(tick), this.pool_state.mint_decimals_0, this.pool_state.mint_decimals_1);
507
+ return twap;
508
+ }
509
+ get_confidence(currentTime) {
510
+ const conf = this.get_twap_primary(currentTime).sub(this.get_twap_secondary(currentTime)).abs();
511
+ return conf;
512
+ }
513
+ fetch() {
514
+ return __awaiter(this, void 0, void 0, function* () {
515
+ const currentTime = new anchor_1.BN(Math.floor(Date.now() / 1000)); // current UNIX timestamp in seconds
516
+ const confidence = this.get_confidence(currentTime);
517
+ const liquidity = this.pool_state.liquidity; // assuming pool_state is already the decoded data
518
+ const price = this.get_twap_primary(currentTime);
519
+ let errors = 0;
520
+ // if (confidence.mul(10_000).greaterThan(this.conf_thresh_bps!)) {
521
+ // errors |= ErrorCode.TOO_UNCERTAIN;
522
+ // }
523
+ if (liquidity.lt(this.min_liquidity)) {
524
+ errors |= oracle_1.ErrorCode.NOT_ENOUGH_LIQUIDITY;
525
+ }
526
+ ;
527
+ let err = new oracle_1.OracleErrors(errors);
528
+ let oracle_price = new oracle_1.OraclePrice(price, confidence, currentTime);
529
+ return new oracle_1.OracleResult(oracle_price, err);
530
+ });
531
+ }
532
+ }
533
+ exports.RaydiumCLMMOracle = RaydiumCLMMOracle;
534
+ function mul_right_shift(val, mulBy) {
535
+ return signed_right_shift(val.mul(mulBy), 64, 256);
536
+ }
537
+ function signed_left_shift(n0, shiftBy, bitWidth) {
538
+ const twosN0 = n0.toTwos(bitWidth).shln(shiftBy);
539
+ twosN0.imaskn(bitWidth + 1);
540
+ return twosN0.fromTwos(bitWidth);
541
+ }
542
+ function signed_right_shift(n0, shiftBy, bitWidth) {
543
+ const twoN0 = n0.toTwos(bitWidth).shrn(shiftBy);
544
+ twoN0.imaskn(bitWidth - shiftBy + 1);
545
+ return twoN0.fromTwos(bitWidth - shiftBy);
546
+ }
@@ -0,0 +1,8 @@
1
+ export declare const U8_MAX: any;
2
+ export declare const U16_MAX: any;
3
+ export declare const U32_MAX: any;
4
+ export declare const U64_MAX: any;
5
+ export declare const U128_MAX: any;
6
+ export declare const HERMES_PUBLIC_ENDPOINT = "https://hermes.pyth.network";
7
+ export declare const SOLANA_DEVNET_ENDPOINT = "https://api.devnet.solana.com";
8
+ export declare const SOLANA_MAINNET_ENDPOINT = "https://api.mainnet-beta.solana.com";
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SOLANA_MAINNET_ENDPOINT = exports.SOLANA_DEVNET_ENDPOINT = exports.HERMES_PUBLIC_ENDPOINT = exports.U128_MAX = exports.U64_MAX = exports.U32_MAX = exports.U16_MAX = exports.U8_MAX = void 0;
4
+ const anchor_1 = require("@coral-xyz/anchor");
5
+ exports.U8_MAX = new anchor_1.BN(0xff);
6
+ exports.U16_MAX = new anchor_1.BN(0xffff);
7
+ exports.U32_MAX = new anchor_1.BN(0xffffffff);
8
+ exports.U64_MAX = new anchor_1.BN(1).shln(64).sub(new anchor_1.BN(1));
9
+ exports.U128_MAX = new anchor_1.BN(1).shln(128).sub(new anchor_1.BN(1));
10
+ exports.HERMES_PUBLIC_ENDPOINT = "https://hermes.pyth.network";
11
+ exports.SOLANA_DEVNET_ENDPOINT = "https://api.devnet.solana.com";
12
+ exports.SOLANA_MAINNET_ENDPOINT = "https://api.mainnet-beta.solana.com";
@@ -0,0 +1,60 @@
1
+ import Decimal from 'decimal.js';
2
+ import { BN } from '@coral-xyz/anchor';
3
+ export declare enum ErrorCode {
4
+ NONE = 0,
5
+ INVALID_PRICE = 1,
6
+ TOO_VOLATILE = 2,
7
+ TOO_UNCERTAIN = 4,
8
+ STALE = 8,
9
+ NOT_ENOUGH_LIQUIDITY = 16,
10
+ NOT_ENABLED = 32,
11
+ NOT_ENOUGH_ORACLES = 64,
12
+ MISSING_REQUIRED_ORACLES = 128
13
+ }
14
+ export declare class OracleErrors extends Error {
15
+ code: ErrorCode;
16
+ constructor(code: ErrorCode, message?: string);
17
+ static NONE: OracleErrors;
18
+ static INVALID_PRICE: OracleErrors;
19
+ static TOO_VOLATILE: OracleErrors;
20
+ static TOO_UNCERTAIN: OracleErrors;
21
+ static STALE: OracleErrors;
22
+ static NOT_ENOUGH_LIQUIDITY: OracleErrors;
23
+ static NOT_ENABLED: OracleErrors;
24
+ static NOT_ENOUGH_ORACLES: OracleErrors;
25
+ static MISSING_REQUIRED_ORACLES: OracleErrors;
26
+ }
27
+ export declare class OraclePrice {
28
+ price: Decimal;
29
+ conf: Decimal;
30
+ updateTime: BN;
31
+ mid: Decimal;
32
+ low: Decimal;
33
+ high: Decimal;
34
+ constructor(price: Decimal, conf: Decimal, updateTime: BN);
35
+ }
36
+ export declare class OracleResult {
37
+ price: OraclePrice | null;
38
+ error: OracleErrors;
39
+ constructor(price: OraclePrice | null, error?: OracleErrors);
40
+ ok(): boolean;
41
+ }
42
+ export declare abstract class Oracle {
43
+ weight: number;
44
+ confThreshBps: number;
45
+ stalenessThresh: number;
46
+ volatilityThresh: Decimal;
47
+ minLiquidity: Decimal;
48
+ stalenessConfRateBps: number;
49
+ constructor(weight: number, confThreshBps?: number, stalenessThresh?: number, volatilityThresh?: number | Decimal, minLiquidity?: number | Decimal, stalenessConfRateBps?: number);
50
+ abstract fetch(): Promise<OracleResult>;
51
+ }
52
+ export declare class OracleAggregator {
53
+ oracles: Oracle[];
54
+ requiredOracles: Oracle[];
55
+ minConfBps: number;
56
+ confThreshBps: number;
57
+ minOraclesThresh: number;
58
+ constructor(oracles: Oracle[], requiredOracles: Oracle[], minConfBps?: number, confThreshBps?: number, minOraclesThresh?: number);
59
+ fetch(): Promise<OracleResult>;
60
+ }