@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.blockTimestamp = params.blockTimestamp;
27
+ this.tickCumulative = params.tickCumulative;
28
+ this.padding = params.padding;
29
+ }
30
+ static decode(data, offset = 0) {
31
+ const blockTimestamp = new anchor_1.BN(data.readUInt32LE(offset));
32
+ const tickCumulative = 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
+ blockTimestamp,
42
+ tickCumulative,
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
+ blockTimestamp: this.blockTimestamp.sub(other.blockTimestamp),
54
+ tickCumulative: this.tickCumulative.sub(other.tickCumulative),
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
+ blockTimestamp: this.blockTimestamp.add(other.blockTimestamp),
64
+ tickCumulative: this.tickCumulative.add(other.tickCumulative),
65
+ padding: this.padding
66
+ });
67
+ }
68
+ getWeightedObservation(time) {
69
+ // cumT = (self.tickCumulative * time) / self.blockTimestamp
70
+ const cum = this.tickCumulative.mul(time).div(this.blockTimestamp);
71
+ return new Observation({
72
+ blockTimestamp: time,
73
+ tickCumulative: cum,
74
+ padding: this.padding
75
+ });
76
+ }
77
+ adjustToTimestamp(targetTimestamp, observationPrev) {
78
+ // delta = self.sub(prevObservation)
79
+ const delta = this.sub(observationPrev);
80
+ // weightedDelta = delta.getWeightedObservation(targetTimestamp - self.blockTimestamp)
81
+ const timeForWeighted = targetTimestamp.sub(this.blockTimestamp);
82
+ const weightedDelta = delta.getWeightedObservation(timeForWeighted);
83
+ // return self.add(weightedDelta)
84
+ return this.add(weightedDelta);
85
+ }
86
+ }
87
+ class ObservationState {
88
+ constructor(params) {
89
+ this.initialized = params.initialized;
90
+ this.recentEpoch = params.recentEpoch;
91
+ this.observationIndex = params.observationIndex;
92
+ this.poolId = params.poolId;
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 recentEpoch = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
101
+ cursor += 8;
102
+ const observationIndex = data.readUInt16LE(cursor);
103
+ cursor += 2;
104
+ const poolId = 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, recentEpoch, observationIndex, poolId, observations, padding }),
119
+ cursor
120
+ ];
121
+ }
122
+ }
123
+ class RewardInfo {
124
+ constructor(params) {
125
+ this.rewardState = params.rewardState;
126
+ this.openTime = params.openTime;
127
+ this.endTime = params.endTime;
128
+ this.lastUpdateTime = params.lastUpdateTime;
129
+ this.emissionsPerSecondX64 = params.emissionsPerSecondX64;
130
+ this.rewardTotalEmissioned = params.rewardTotalEmissioned;
131
+ this.rewardClaimed = params.rewardClaimed;
132
+ this.tokenMint = params.tokenMint;
133
+ this.tokenVault = params.tokenVault;
134
+ this.authority = params.authority;
135
+ this.rewardGrowthGlobalX64 = params.rewardGrowthGlobalX64;
136
+ }
137
+ static decode(data, offset = 0) {
138
+ let cursor = offset;
139
+ const rewardState = data.readUInt8(cursor);
140
+ cursor += 1;
141
+ const openTime = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
142
+ cursor += 8;
143
+ const endTime = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
144
+ cursor += 8;
145
+ const lastUpdateTime = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
146
+ cursor += 8;
147
+ const emissionsPerSecondX64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
148
+ cursor += 16;
149
+ const rewardTotalEmissioned = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
150
+ cursor += 8;
151
+ const rewardClaimed = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
152
+ cursor += 8;
153
+ const tokenMint = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
154
+ cursor += 32;
155
+ const tokenVault = 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 rewardGrowthGlobalX64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
160
+ cursor += 16;
161
+ return [new RewardInfo({
162
+ rewardState,
163
+ openTime,
164
+ endTime,
165
+ lastUpdateTime,
166
+ emissionsPerSecondX64,
167
+ rewardTotalEmissioned,
168
+ rewardClaimed,
169
+ tokenMint,
170
+ tokenVault,
171
+ authority,
172
+ rewardGrowthGlobalX64
173
+ }), cursor];
174
+ }
175
+ }
176
+ class PoolState {
177
+ constructor(params) {
178
+ this.bump = params.bump;
179
+ this.ammConfig = params.ammConfig;
180
+ this.owner = params.owner;
181
+ this.tokenMint0 = params.tokenMint0;
182
+ this.tokenMint1 = params.tokenMint1;
183
+ this.tokenVault0 = params.tokenVault0;
184
+ this.tokenVault1 = params.tokenVault1;
185
+ this.observationKey = params.observationKey;
186
+ this.mintDecimals0 = params.mintDecimals0;
187
+ this.mintDecimals1 = params.mintDecimals1;
188
+ this.tickSpacing = params.tickSpacing;
189
+ this.liquidity = params.liquidity;
190
+ this.sqrtPriceX64 = params.sqrtPriceX64;
191
+ this.tickCurrent = params.tickCurrent;
192
+ this.padding3 = params.padding3;
193
+ this.padding4 = params.padding4;
194
+ this.feeGrowthGlobal0X64 = params.feeGrowthGlobal0X64;
195
+ this.feeGrowthGlobal1X64 = params.feeGrowthGlobal1X64;
196
+ this.protocolFeesToken0 = params.protocolFeesToken0;
197
+ this.protocolFeesToken1 = params.protocolFeesToken1;
198
+ this.swapInAmountToken0 = params.swapInAmountToken0;
199
+ this.swapOutAmountToken1 = params.swapOutAmountToken1;
200
+ this.swapInAmountToken1 = params.swapInAmountToken1;
201
+ this.swapOutAmountToken0 = params.swapOutAmountToken0;
202
+ this.status = params.status;
203
+ this.padding = params.padding;
204
+ this.rewardInfos = params.rewardInfos;
205
+ this.tickArrayBitmap = params.tickArrayBitmap;
206
+ this.totalFeesToken0 = params.totalFeesToken0;
207
+ this.totalFeesClaimedToken0 = params.totalFeesClaimedToken0;
208
+ this.totalFeesToken1 = params.totalFeesToken1;
209
+ this.totalFeesClaimedToken1 = params.totalFeesClaimedToken1;
210
+ this.fundFeesToken0 = params.fundFeesToken0;
211
+ this.fundFeesToken1 = params.fundFeesToken1;
212
+ this.openTime = params.openTime;
213
+ this.recentEpoch = params.recentEpoch;
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 ammConfig = 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 tokenMint0 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
226
+ cursor += 32;
227
+ const tokenMint1 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
228
+ cursor += 32;
229
+ const tokenVault0 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
230
+ cursor += 32;
231
+ const tokenVault1 = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
232
+ cursor += 32;
233
+ const observationKey = new web3_js_1.PublicKey(data.subarray(cursor, cursor + 32));
234
+ cursor += 32;
235
+ const mintDecimals0 = data.readUInt8(cursor);
236
+ cursor += 1;
237
+ const mintDecimals1 = data.readUInt8(cursor);
238
+ cursor += 1;
239
+ const tickSpacing = data.readUInt16LE(cursor);
240
+ cursor += 2;
241
+ const liquidity = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
242
+ cursor += 16;
243
+ const sqrtPriceX64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
244
+ cursor += 16;
245
+ const tickCurrent = 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 feeGrowthGlobal0X64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
252
+ cursor += 16;
253
+ const feeGrowthGlobal1X64 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
254
+ cursor += 16;
255
+ const protocolFeesToken0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
256
+ cursor += 8;
257
+ const protocolFeesToken1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
258
+ cursor += 8;
259
+ const swapInAmountToken0 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
260
+ cursor += 16;
261
+ const swapOutAmountToken1 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
262
+ cursor += 16;
263
+ const swapInAmountToken1 = new anchor_1.BN(data.subarray(cursor, cursor + 16), "le");
264
+ cursor += 16;
265
+ const swapOutAmountToken0 = 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 rewardInfos = [];
276
+ for (let i = 0; i < 3; i++) {
277
+ const decoded = RewardInfo.decode(data, cursor);
278
+ rewardInfos.push(decoded[0]);
279
+ cursor = decoded[1];
280
+ }
281
+ // tickArrayBitmap [u64;16]
282
+ const tickArrayBitmap = [];
283
+ for (let i = 0; i < 16; i++) {
284
+ tickArrayBitmap.push(new anchor_1.BN(data.subarray(cursor, cursor + 8), "le"));
285
+ cursor += 8;
286
+ }
287
+ const totalFeesToken0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
288
+ cursor += 8;
289
+ const totalFeesClaimedToken0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
290
+ cursor += 8;
291
+ const totalFeesToken1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
292
+ cursor += 8;
293
+ const totalFeesClaimedToken1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
294
+ cursor += 8;
295
+ const fundFeesToken0 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
296
+ cursor += 8;
297
+ const fundFeesToken1 = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
298
+ cursor += 8;
299
+ const openTime = new anchor_1.BN(data.subarray(cursor, cursor + 8), "le");
300
+ cursor += 8;
301
+ const recentEpoch = 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
+ ammConfig,
317
+ owner,
318
+ tokenMint0,
319
+ tokenMint1,
320
+ tokenVault0,
321
+ tokenVault1,
322
+ observationKey,
323
+ mintDecimals0,
324
+ mintDecimals1,
325
+ tickSpacing,
326
+ liquidity,
327
+ sqrtPriceX64,
328
+ tickCurrent,
329
+ padding3,
330
+ padding4,
331
+ feeGrowthGlobal0X64,
332
+ feeGrowthGlobal1X64,
333
+ protocolFeesToken0,
334
+ protocolFeesToken1,
335
+ swapInAmountToken0,
336
+ swapOutAmountToken1,
337
+ swapInAmountToken1,
338
+ swapOutAmountToken0,
339
+ status,
340
+ padding,
341
+ rewardInfos,
342
+ tickArrayBitmap,
343
+ totalFeesToken0,
344
+ totalFeesClaimedToken0,
345
+ totalFeesToken1,
346
+ totalFeesClaimedToken1,
347
+ fundFeesToken0,
348
+ fundFeesToken1,
349
+ openTime,
350
+ recentEpoch,
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.confThreshBps, params.stalenessThresh, params.volatilityThresh, params.minLiquidity, params.stalenessConfRateBps);
361
+ this.poolId = params.poolId;
362
+ this.quoteToken = params.quoteToken;
363
+ this.poolState = params.poolState;
364
+ this.observationState = params.observationState;
365
+ this.primarySeconds = params.primarySeconds;
366
+ this.secondarySeconds = params.secondarySeconds;
367
+ }
368
+ ;
369
+ static create(params) {
370
+ return __awaiter(this, void 0, void 0, function* () {
371
+ const { connection, poolId } = params;
372
+ // Fetch poolState
373
+ const poolAccountInfo = yield connection.getAccountInfo(new web3_js_1.PublicKey(poolId));
374
+ if (!poolAccountInfo)
375
+ throw new Error("Pool account not found");
376
+ const [poolState, _] = PoolState.decode(poolAccountInfo.data, 8);
377
+ // Fetch observationState from poolState
378
+ const obsPubkey = poolState.observationKey;
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 [observationState, __] = ObservationState.decode(obsAccountInfo.data, 8);
383
+ return new RaydiumCLMMOracle({
384
+ poolId: poolId,
385
+ quoteToken: params.quoteToken,
386
+ poolState: poolState,
387
+ observationState: observationState,
388
+ weight: params.weight,
389
+ primarySeconds: params.primarySeconds,
390
+ secondarySeconds: params.secondarySeconds,
391
+ confThreshBps: params.confThreshBps,
392
+ stalenessThresh: params.stalenessThresh,
393
+ volatilityThresh: params.volatilityThresh,
394
+ minLiquidity: params.minLiquidity,
395
+ stalenessConfRateBps: params.stalenessConfRateBps
396
+ });
397
+ });
398
+ }
399
+ static deriveObservationKey(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
+ getObservationAtIndex(index) {
408
+ return this.observationState.observations[index];
409
+ }
410
+ getObservationAtTimestamp(timestamp, startIndex) {
411
+ let observationCurrent = this.getObservationAtIndex(startIndex);
412
+ let index = startIndex;
413
+ // const obsIndex = this.observationState.observationIndex;
414
+ // Loop backwards until we find the observation <= timestamp
415
+ while (observationCurrent.blockTimestamp.gt(timestamp)) {
416
+ index = (index - 1 + 100) % 100;
417
+ observationCurrent = this.getObservationAtIndex(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.getObservationAtIndex(prevIndex);
426
+ // Adjust current observation to the target timestamp
427
+ const result = observationCurrent.adjustToTimestamp(timestamp, observationPrev);
428
+ return result;
429
+ }
430
+ getDeltaObservations(currentTime, primarySeconds, secondarySeconds) {
431
+ const obsIndex = this.observationState.observationIndex;
432
+ const observationCurrent = this.getObservationAtTimestamp(currentTime, obsIndex);
433
+ const observationPrimary = this.getObservationAtTimestamp(new anchor_1.BN(currentTime.sub(new anchor_1.BN(primarySeconds))), obsIndex);
434
+ const deltaPrimary = observationCurrent.sub(observationPrimary);
435
+ const observationSecondary = this.getObservationAtTimestamp(new anchor_1.BN(currentTime.sub(new anchor_1.BN(secondarySeconds))), obsIndex);
436
+ const deltaSecondary = observationCurrent.sub(observationSecondary);
437
+ return [observationPrimary, observationSecondary];
438
+ }
439
+ getSqrtPriceX64FromTick(tick) {
440
+ let numTick = tick.toNumber();
441
+ if (!Number.isInteger(numTick)) {
442
+ throw new Error("tick must be integer");
443
+ }
444
+ if (numTick < MIN_TICK || numTick > MAX_TICK) {
445
+ throw new Error("tick must be in MIN_TICK and MAX_TICK");
446
+ }
447
+ const tickAbs = numTick < 0 ? numTick * -1 : numTick;
448
+ let ratio = (tickAbs & 0x1) != 0 ? new anchor_1.BN("18445821805675395072") : new anchor_1.BN("18446744073709551616");
449
+ if ((tickAbs & 0x2) != 0)
450
+ ratio = mulRightShift(ratio, new anchor_1.BN("18444899583751176192"));
451
+ if ((tickAbs & 0x4) != 0)
452
+ ratio = mulRightShift(ratio, new anchor_1.BN("18443055278223355904"));
453
+ if ((tickAbs & 0x8) != 0)
454
+ ratio = mulRightShift(ratio, new anchor_1.BN("18439367220385607680"));
455
+ if ((tickAbs & 0x10) != 0)
456
+ ratio = mulRightShift(ratio, new anchor_1.BN("18431993317065453568"));
457
+ if ((tickAbs & 0x20) != 0)
458
+ ratio = mulRightShift(ratio, new anchor_1.BN("18417254355718170624"));
459
+ if ((tickAbs & 0x40) != 0)
460
+ ratio = mulRightShift(ratio, new anchor_1.BN("18387811781193609216"));
461
+ if ((tickAbs & 0x80) != 0)
462
+ ratio = mulRightShift(ratio, new anchor_1.BN("18329067761203558400"));
463
+ if ((tickAbs & 0x100) != 0)
464
+ ratio = mulRightShift(ratio, new anchor_1.BN("18212142134806163456"));
465
+ if ((tickAbs & 0x200) != 0)
466
+ ratio = mulRightShift(ratio, new anchor_1.BN("17980523815641700352"));
467
+ if ((tickAbs & 0x400) != 0)
468
+ ratio = mulRightShift(ratio, new anchor_1.BN("17526086738831433728"));
469
+ if ((tickAbs & 0x800) != 0)
470
+ ratio = mulRightShift(ratio, new anchor_1.BN("16651378430235570176"));
471
+ if ((tickAbs & 0x1000) != 0)
472
+ ratio = mulRightShift(ratio, new anchor_1.BN("15030750278694412288"));
473
+ if ((tickAbs & 0x2000) != 0)
474
+ ratio = mulRightShift(ratio, new anchor_1.BN("12247334978884435968"));
475
+ if ((tickAbs & 0x4000) != 0)
476
+ ratio = mulRightShift(ratio, new anchor_1.BN("8131365268886854656"));
477
+ if ((tickAbs & 0x8000) != 0)
478
+ ratio = mulRightShift(ratio, new anchor_1.BN("3584323654725218816"));
479
+ if ((tickAbs & 0x10000) != 0)
480
+ ratio = mulRightShift(ratio, new anchor_1.BN("696457651848324352"));
481
+ if ((tickAbs & 0x20000) != 0)
482
+ ratio = mulRightShift(ratio, new anchor_1.BN("26294789957507116"));
483
+ if ((tickAbs & 0x40000) != 0)
484
+ ratio = mulRightShift(ratio, new anchor_1.BN("37481735321082"));
485
+ if (numTick > 0)
486
+ ratio = constants_1.U128_MAX.div(ratio);
487
+ return ratio;
488
+ }
489
+ sqrtPriceX64ToPrice(sqrtPriceX64, decimalsA, decimalsB) {
490
+ return this.x64ToDecimal(sqrtPriceX64)
491
+ .pow(2)
492
+ .mul(decimal_js_1.default.pow(10, decimalsA - decimalsB));
493
+ }
494
+ x64ToDecimal(num, decimalPlaces) {
495
+ return new decimal_js_1.default(num.toString()).div(decimal_js_1.default.pow(2, 64)).toDecimalPlaces(decimalPlaces);
496
+ }
497
+ getTwapPrimary(currentTime) {
498
+ let observation = this.getDeltaObservations(currentTime, this.primarySeconds, this.secondarySeconds)[0];
499
+ let tick = observation.tickCumulative.div(observation.blockTimestamp);
500
+ let twap = this.sqrtPriceX64ToPrice(this.getSqrtPriceX64FromTick(tick), this.poolState.mintDecimals0, this.poolState.mintDecimals1);
501
+ return twap;
502
+ }
503
+ getTwapSecondary(currentTime) {
504
+ let observation = this.getDeltaObservations(currentTime, this.primarySeconds, this.secondarySeconds)[1];
505
+ let tick = observation.tickCumulative.div(observation.blockTimestamp);
506
+ let twap = this.sqrtPriceX64ToPrice(this.getSqrtPriceX64FromTick(tick), this.poolState.mintDecimals0, this.poolState.mintDecimals1);
507
+ return twap;
508
+ }
509
+ getConfidence(currentTime) {
510
+ const conf = this.getTwapPrimary(currentTime).sub(this.getTwapSecondary(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.getConfidence(currentTime);
517
+ const liquidity = this.poolState.liquidity; // assuming poolState is already the decoded data
518
+ const price = this.getTwapPrimary(currentTime);
519
+ let errors = 0;
520
+ // if (confidence.mul(10_000).greaterThan(this.confThreshBps!)) {
521
+ // errors |= ErrorCode.TOO_UNCERTAIN;
522
+ // }
523
+ if (liquidity.lt(this.minLiquidity)) {
524
+ errors |= oracle_1.ErrorCode.NOT_ENOUGH_LIQUIDITY;
525
+ }
526
+ ;
527
+ let err = new oracle_1.OracleErrors(errors);
528
+ let oraclePrice = new oracle_1.OraclePrice(price, confidence, currentTime);
529
+ return new oracle_1.OracleResult(oraclePrice, err);
530
+ });
531
+ }
532
+ }
533
+ exports.RaydiumCLMMOracle = RaydiumCLMMOracle;
534
+ function mulRightShift(val, mulBy) {
535
+ return signedRightShift(val.mul(mulBy), 64, 256);
536
+ }
537
+ function signedLeftShift(n0, shiftBy, bitWidth) {
538
+ const twosN0 = n0.toTwos(bitWidth).shln(shiftBy);
539
+ twosN0.imaskn(bitWidth + 1);
540
+ return twosN0.fromTwos(bitWidth);
541
+ }
542
+ function signedRightShift(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,139 @@
1
+ import Decimal from 'decimal.js';
2
+ import { BN } from '@coral-xyz/anchor';
3
+ import { Connection, PublicKey } from '@solana/web3.js';
4
+ import { Oracle, OracleResult } from './oracle';
5
+ declare enum Side {
6
+ Base = 0,
7
+ Quote = 1
8
+ }
9
+ declare enum Quote {
10
+ Usdc = 0,
11
+ Wsol = 1
12
+ }
13
+ declare class Observation {
14
+ timestamp: BN;
15
+ cumT0Price: BN;
16
+ cumT1Price: BN;
17
+ constructor(params: {
18
+ timestamp: BN;
19
+ cumT0Price: BN;
20
+ cumT1Price: BN;
21
+ });
22
+ static decode(data: Buffer, offset?: number): [Observation, number];
23
+ sub(other: Observation): Observation;
24
+ add(other: Observation): Observation;
25
+ getWeightedObservation(time: BN): Observation;
26
+ adjustToTimestamp(targetTimestamp: BN, observationPrev: Observation): Observation;
27
+ getTwap(side: Side): BN;
28
+ }
29
+ declare class ObservationState {
30
+ initialized: boolean;
31
+ observationIndex: number;
32
+ poolId: PublicKey;
33
+ observations: Observation[];
34
+ lastUpdateTimestamp: BN;
35
+ padding: BN[];
36
+ constructor(params: {
37
+ initialized: boolean;
38
+ observationIndex: number;
39
+ poolId: PublicKey;
40
+ observations: Observation[];
41
+ lastUpdateTimestamp: BN;
42
+ padding: BN[];
43
+ });
44
+ static decode(data: Buffer, offset?: number): [ObservationState, number];
45
+ }
46
+ declare class PoolState {
47
+ ammConfig: PublicKey;
48
+ poolCreator: PublicKey;
49
+ token0Vault: PublicKey;
50
+ token1Vault: PublicKey;
51
+ lpMint: PublicKey;
52
+ token0Mint: PublicKey;
53
+ token1Mint: PublicKey;
54
+ token0Program: PublicKey;
55
+ token1Program: PublicKey;
56
+ observationKey: PublicKey;
57
+ authBump: number;
58
+ status: number;
59
+ lpMintDecimals: number;
60
+ mint0Decimals: number;
61
+ mint1Decimals: number;
62
+ lpSupply: BN;
63
+ protocolFeesToken0: BN;
64
+ protocolFeesToken1: BN;
65
+ fundFeesToken0: BN;
66
+ fundFeesToken1: BN;
67
+ openTime: BN;
68
+ recentEpoch: BN;
69
+ creatorFeeOn: number;
70
+ enableCreatorFee: boolean;
71
+ padding1: number[];
72
+ creatorFeesToken0: BN;
73
+ creatorFeesToken1: BN;
74
+ padding: BN[];
75
+ constructor(params: {
76
+ ammConfig: PublicKey;
77
+ poolCreator: PublicKey;
78
+ token0Vault: PublicKey;
79
+ token1Vault: PublicKey;
80
+ lpMint: PublicKey;
81
+ token0Mint: PublicKey;
82
+ token1Mint: PublicKey;
83
+ token0Program: PublicKey;
84
+ token1Program: PublicKey;
85
+ observationKey: PublicKey;
86
+ authBump: number;
87
+ status: number;
88
+ lpMintDecimals: number;
89
+ mint0Decimals: number;
90
+ mint1Decimals: number;
91
+ lpSupply: BN;
92
+ protocolFeesToken0: BN;
93
+ protocolFeesToken1: BN;
94
+ fundFeesToken0: BN;
95
+ fundFeesToken1: BN;
96
+ openTime: BN;
97
+ recentEpoch: BN;
98
+ creatorFeeOn: number;
99
+ enableCreatorFee: boolean;
100
+ padding1: number[];
101
+ creatorFeesToken0: BN;
102
+ creatorFeesToken1: BN;
103
+ padding: BN[];
104
+ });
105
+ static decode(data: Buffer, offset?: number): [PoolState, number];
106
+ }
107
+ export declare class RaydiumCPMMOracle extends Oracle {
108
+ poolId: PublicKey;
109
+ quoteToken: Quote;
110
+ poolState: PoolState;
111
+ side: Side;
112
+ observationState: ObservationState;
113
+ private constructor();
114
+ static create(params: {
115
+ connection: Connection;
116
+ poolId: PublicKey;
117
+ quoteToken: Quote;
118
+ side: Side;
119
+ weight: number;
120
+ confThreshBps?: number;
121
+ stalenessThresh?: number;
122
+ volatilityThresh?: number | Decimal;
123
+ minLiquidity?: number | Decimal;
124
+ stalenessConfRateBps?: number;
125
+ }): Promise<RaydiumCPMMOracle>;
126
+ setSide(side: Side): void;
127
+ static deriveObservationKey(poolId: PublicKey): [PublicKey, number];
128
+ getObservationAtIndex(index: number): Observation;
129
+ getObservationAtTimestamp(timestamp: BN, startIndex: number): Observation;
130
+ getDeltaObservations(currentTime: BN): Observation[];
131
+ getDecimals(): number;
132
+ getTwap30sec(currentTime: BN): Decimal;
133
+ getTwap2min(currentTime: BN): Decimal;
134
+ getTwap5min(currentTime: BN): Decimal;
135
+ getTwap20min(currentTime: BN): Decimal;
136
+ getConfidence(currentTime: BN): Decimal;
137
+ fetch(): Promise<OracleResult>;
138
+ }
139
+ export {};