@wireio/stake 0.5.1 → 0.6.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wireio/stake",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "LIQ Staking Module for Wire Network",
5
5
  "homepage": "https://gitea.gitgo.app/Wire/sdk-stake",
6
6
  "license": "FSL-1.1-Apache-2.0",
@@ -66,24 +66,57 @@ export const ADDRESSES: AddressBook = {
66
66
  YieldOracle: "0x56A27E1d10d4aEc7402dC26693fb7c0Eb66eF802",
67
67
 
68
68
  //Outpost contracts
69
- OutpostManagerAuthority: "0xAEcd2aa6EeFa4Aa7Cb9368728aA89Ca59Aa9ea59",
70
- iodata: "0x2c2ab53F6Bc146bd31B459E832Ce0B9Da78712d8",
71
- Base58: "0x9F188Ec124c9ad9bF6D195650d8360Fd052F585A",
72
- sysio_merkle: "0x6eDA3C4c34421Cd667800C17fe33E48f5698e004",
73
- ReceiptNFT: "0xd083051d9bb8353875abCecAB50C6e4FB5e527a8",
69
+ OutpostManagerAuthority: "0x57A3723B9f3C6022CAe394C859655E59382Fad18",
70
+ iodata: "0x88896d4fa70C3a7Fb833C80BB5763a4c53A6aCB5",
71
+ Base58: "0x0E9E6e8A32477F3B086Aaa26db2b928a816Da711",
72
+ sysio_merkle: "0xf5858B784B080A08063FAe06dB6d91c5BBAA48C7",
73
+ ReceiptNFT: "0xF1F5e063bFF6E0c09b0ac8020376E16c0be8eA10",
74
+ EthUsdPriceConsumer: "0xFdb3Ab290179CA85204aD1Ffb8c1c3c42AEB768F",
75
+ Pool: "0x15DaeB9562c6Dd21558f14CcdDf5C855499B8693",
76
+ OutpostManager: "0x1aCCc78FCA9e2Ea4dcE849bf072C2248f36435cC",
77
+ sysio_write: "0x513e472904EE67A8E27ebaF2411f3ed3851F4514",
78
+ Pretoken: "0xd7CDc79B90336720ecf02eD5A355cB0F7099F079",
79
+ BAR: "0x4A01414dEA81b1961aE986Bc4E95B30f73770f99",
80
+ OPPCommon: "0x3747Cc19A351BCBCE92055c419e7d710C4b399aA",
81
+ OPP: "0xF577FDc80014ef19DF258243e0551c888Da809E4",
82
+ Depositor: "0xD9Eb2A2d4e9eD7e2257153041B29DCeCDee8BCFe",
83
+ OPPInbound: "0x232C01f2528A5013af3703bE4B4ce30A793Ee8BD",
74
84
  MockAggregator: "0xFCfc3ddd4CBd9Ad3b3af3A374B8bdA1b66eE6FFF",
75
- Pool: "0xee5F11aC9b104c18d2423f266c5E737fC95ebA92",
76
- OutpostManager: "0xB1B6ba7FA156652849069aC7ADB281283D235B9f",
77
- sysio_write: "0xEfA608136d372349C09a7aA57665C09Fb4a620Ca",
78
- EthUsdPriceConsumer: "0x6337A23b61f98b1526faF2848385Abe9cB4cFF21",
79
- BAR: "0x9264eAA449da94caF70Fc18522021a94C8DF32Fb",
80
- OPPCommon: "0x86A8cA16ce521De3EBdd1C541fAf188795b59FD0",
81
- OPP: "0x79e8395Bb5131FB285aCEE5329BB43E66f50F88C",
82
- Pretoken: "0x62f98AF2f9C3EF4eF2fA7bc0245BD5a9315E7541",
83
- OPPInbound: "0xC85f57Ff069711e0b3472De3963bd2fC2FEfF3e2",
84
- Depositor: "0xb0BACAb6f13dd96281300be13a6346461b2f35F3"
85
+
85
86
  };
86
87
 
88
+ // Latest
89
+ // LiqETH contracts
90
+ // LiqEthAuthority: "0x612536e386801b337363Cfa4CC0a7C33Ad588136",
91
+ // BeaconState: "0x48462108c8254568e1d2D4fE3178579d9298ceC4",
92
+ // WithdrawalQueue: "0x49aeFA8B860d908476724e00b48fDa7529f63DbA",
93
+ // LiqEthToken: "0x1e0fb59F0C1Db95fD8f8743B1B3e709CdccF4002",
94
+ // Accounting: "0x679A76d462B9A499F8D23Cb3Fa6CbeB81B5779b9",
95
+ // DepositManager: "0xe63a8f755195D019Db893BF0542A2533895ec927",
96
+ // WithdrawalVault: "0x5C9C8d9Ec8F1Cb64D231b301712405c5E14C175d",
97
+ // StakingModule: "0x5dB271F09f840b49E5E77A3B8fDA30F56A66af20",
98
+ // YieldOracle: "0x323022827e4922a14f30eF8Ad83A393628e0Ac34",
99
+
100
+ // //Outpost contracts
101
+ // OutpostManagerAuthority: "0x5047Cf5F7bae2d5737B29AAbE910ceE0F3344059",
102
+ // iodata: "0x82A24E7240DaBaAD49A157dB2a203Edb6486d81f",
103
+ // Base58: "0x97aE9B0A8D1678fC73EF7B67539236A85284E2FF",
104
+ // sysio_merkle: "0x3A30901fC4dD25E75dcF9c790b620183aF0Bc3D5",
105
+ // ReceiptNFT: "0xee4f7075B8ac06bb709075D5963C8e012Ef74cc1",
106
+ // EthUsdPriceConsumer: "0x202719423E364aB0C8b4005ee25A20c4A5AB28DC",
107
+ // Pool: "0x8fB1a92c4107Ac1072f51Fac3617A5494Eb71573",
108
+ // OutpostManager: "0x88760aD68a557Aa4FeCde82dA9B024948e7B6074",
109
+ // sysio_write: "0x7485Bb3AA184D33dC54149bBE0BEd548791a6D8f",
110
+ // Pretoken: "0x474B3944e8b9C82264434f394d2367f046869Adc",
111
+ // BAR: "0x779Bb699A0ad10EB8A13d177e594B1268f72C602",
112
+ // OPPCommon: "0x14280b40a8a1F037dDBf9bddf8b78Bc4f11276BA",
113
+ // OPP: "0xBae03C8bb8f557dc48AE9D6F9032d503819B11a7",
114
+ // Depositor: "0x66f41027F9642384c73ce9C8944340a1Af497982",
115
+ // OPPInbound: "0x2AA0833Fc72314faD52a2dA932973abB8491E96F",
116
+ // MockAggregator: "0xFCfc3ddd4CBd9Ad3b3af3A374B8bdA1b66eE6FFF",
117
+
118
+
119
+
87
120
  export type Contracts<T extends string = ContractName> = Record<T, ContractConfig>;
88
121
 
89
122
  export type ContractConfig = {
@@ -349,7 +349,7 @@ export class EthereumStakingClient implements IStakingClient {
349
349
 
350
350
 
351
351
  // Fetch all required contract data
352
- const [totalSharesBn, indexBn, trancheNumberBn, trancheSupplyBn, tranchePriceWadBn, totalSupplyBn, supplyGrowthBps, priceGrowthCents, minPriceUsd, maxPriceUsd] = await Promise.all([
352
+ const [totalSharesBn, indexBn, trancheNumberBn, trancheSupplyBn, tranchePriceUsdBn, totalSupplyBn, supplyGrowthBps, priceGrowthCents, minPriceUsd, maxPriceUsd] = await Promise.all([
353
353
  this.contract.Depositor.totalShares(blockTag),
354
354
  this.contract.Depositor.index(blockTag),
355
355
  this.contract.Pretoken.trancheNumber(blockTag),
@@ -362,7 +362,6 @@ export class EthereumStakingClient implements IStakingClient {
362
362
  this.contract.EthUsdPriceConsumer.MAX_PRICE(),
363
363
  ]);
364
364
 
365
-
366
365
  const totalTrancheSupply = BigInt(totalSupplyBn.toString()) / BigInt(1e10);
367
366
  const currentTrancheSupply = BigInt(trancheSupplyBn.toString()) / BigInt(1e10);
368
367
 
@@ -380,7 +379,7 @@ export class EthereumStakingClient implements IStakingClient {
380
379
  indexBn,
381
380
  trancheNumberBn,
382
381
  currentTrancheSupply,
383
- tranchePriceWadBn,
382
+ tranchePriceUsdBn,
384
383
  totalTrancheSupply,
385
384
  initialTrancheSupply,
386
385
  supplyGrowthBps,
@@ -395,8 +394,6 @@ export class EthereumStakingClient implements IStakingClient {
395
394
  });
396
395
  }
397
396
  catch (err: any) {
398
- console.log(err);
399
-
400
397
  throw new Error(`Error fetching Ethereum tranche snapshot: ${err?.message || err}`);
401
398
  }
402
399
  }
@@ -111,53 +111,57 @@ export async function sendOPPFinalize(opp: ethers.Contract, gasLimit?: ethers.Bi
111
111
  }
112
112
 
113
113
 
114
+ const BPS_DENOM = BigInt(10_000);
114
115
 
115
- /**
116
- * Apply one forward growth step: value * (BPS + growthBps) / BPS.
117
- * Simple integer round-half-up.
118
- */
119
- function growOnce(value: bigint, growthBps: number): bigint {
116
+ // On-chain USD: 1e18 (wei-style)
117
+ const USD_ONCHAIN_SCALE = BigInt(1_000_000_000_000_000_000); // 1e18
118
+ // Client snapshot USD: 1e8 (to match fromScale8)
119
+ const USD_CLIENT_SCALE = BigInt(100_000_000); // 1e8
120
+ // Factor to go from 1e18 → 1e8
121
+ const USD_SCALE_DOWN = USD_ONCHAIN_SCALE / USD_CLIENT_SCALE; // 1e10
122
+
123
+ /** 2.5% growth in BPS for supply side */
124
+ function growSupplyOnce(value: bigint, growthBps: number): bigint {
120
125
  const g = BigInt(growthBps);
121
- return (value * (BPS + g) + BPS / BigInt(2)) / BPS;
126
+ return (value * (BPS_DENOM + g)) / BPS_DENOM;
122
127
  }
123
128
 
124
-
125
- /**
126
- * Apply one backward step: value * BPS / (BPS + growthBps).
127
- * Also integer round-half-up.
128
- */
129
- function shrinkOnce(value: bigint, growthBps: number): bigint {
129
+ function shrinkSupplyOnce(value: bigint, growthBps: number): bigint {
130
130
  const g = BigInt(growthBps);
131
- return (value * BPS + (BPS + g) / BigInt(2)) / (BPS + g);
131
+ return (value * BPS_DENOM) / (BPS_DENOM + g);
132
132
  }
133
133
 
134
+ /** Linear USD price step in on-chain 1e18 scale */
135
+ function growPriceOnceUsd1e18(value: bigint, stepUsd1e18: bigint): bigint {
136
+ return value + stepUsd1e18;
137
+ }
134
138
 
135
- /**
136
- * Calculate the full supply for a given tranche using BigInt math.
137
- * trancheNumber is 1-based (tranche 1 = startSupply)
138
- */
139
- function getTrancheSize(startSupply: bigint, supplyGrowthBps: number, trancheNumber: number): bigint {
140
- let supply = startSupply;
141
- for (let i = 0; i < trancheNumber; i++) {
142
- supply = (supply * (BPS + BigInt(supplyGrowthBps)) + BPS / BigInt(2)) / BPS;
143
- }
144
- return supply;
139
+ function shrinkPriceOnceUsd1e18(value: bigint, stepUsd1e18: bigint): bigint {
140
+ if (value <= stepUsd1e18) return BigInt(0);
141
+ return value - stepUsd1e18;
145
142
  }
146
143
 
144
+ /** Convert on-chain 1e18 USD to client 1e8 USD */
145
+ function usd1e18To1e8(raw: bigint): bigint {
146
+ // 1e18 / 1e8 = 1e10 factor difference
147
+ return raw / USD_SCALE_DOWN;
148
+ }
147
149
 
148
150
  /**
149
- * Build a local tranche ladder around the current tranche
150
- * using only on-chain config + current state.
151
+ * Build a local tranche ladder around the current tranche.
151
152
  *
153
+ * Inside this function:
154
+ * - `currentPriceUsd` and `priceGrowthCents` are treated as 1e18-scaled USD.
155
+ * - Output `priceUsd` is 1e8-scaled USD (for fromScale8()).
152
156
  */
153
157
  export function buildEthereumTrancheLadder(options: {
154
158
  currentTranche: number;
155
- totalTrancheSupply: bigint,
159
+ totalTrancheSupply: bigint; // not used in local window, but kept for API parity
156
160
  initialTrancheSupply: bigint;
157
- currentTrancheSupply: bigint;
158
- currentPriceUsd: bigint;
159
- supplyGrowthBps: number;
160
- priceGrowthCents: number;
161
+ currentTrancheSupply: bigint; // remaining in current tranche (1e8 scale)
162
+ currentPriceUsd: bigint; // 1e18 scale
163
+ priceGrowthCents: bigint; // 1e18 step, e.g. $0.02 → 2e16
164
+ supplyGrowthBps: number; // 250 = 2.5%
161
165
  windowBefore?: number;
162
166
  windowAfter?: number;
163
167
  }): TrancheLadderItem[] {
@@ -166,44 +170,50 @@ export function buildEthereumTrancheLadder(options: {
166
170
  initialTrancheSupply,
167
171
  currentTrancheSupply,
168
172
  currentPriceUsd,
169
- supplyGrowthBps,
170
173
  priceGrowthCents,
174
+ supplyGrowthBps,
171
175
  windowBefore = 5,
172
176
  windowAfter = 5,
173
177
  } = options;
174
178
 
175
179
  const startId = Math.max(0, currentTranche - windowBefore);
176
180
  const endId = currentTranche + windowAfter;
177
-
178
- //calculate total tranche size (e.g. 60,600 on tranche 2)
179
- const currentTrancheSize = getTrancheSize(initialTrancheSupply, supplyGrowthBps, currentTranche);
180
181
 
181
- const capacity = new Map<number, bigint>();
182
- const price = new Map<number, bigint>();
182
+ const capacity = new Map<number, bigint>(); // 1e8 pre-token units
183
+ const priceUsd = new Map<number, bigint>(); // 1e18 USD
184
+
185
+ // Capacity at the current tranche derived from initial supply & BPS growth
186
+ let currentCap = initialTrancheSupply;
187
+ for (let i = 0; i < currentTranche; i++) {
188
+ currentCap = growSupplyOnce(currentCap, supplyGrowthBps);
189
+ }
183
190
 
184
- // Seed current
185
- capacity.set(currentTranche, currentTrancheSize);
186
- price.set(currentTranche, currentPriceUsd);
191
+ capacity.set(currentTranche, currentCap);
192
+ priceUsd.set(currentTranche, currentPriceUsd);
187
193
 
188
194
  // Forward (future tranches)
189
195
  for (let id = currentTranche + 1; id <= endId; id++) {
190
196
  const prevCap = capacity.get(id - 1)!;
191
- const prevPrice = price.get(id - 1)!;
192
- capacity.set(id, growOnce(prevCap, supplyGrowthBps));
193
- price.set(id, growOnce(prevPrice, priceGrowthCents));
197
+ const prevPrice = priceUsd.get(id - 1)!;
198
+
199
+ capacity.set(id, growSupplyOnce(prevCap, supplyGrowthBps));
200
+ priceUsd.set(id, growPriceOnceUsd1e18(prevPrice, priceGrowthCents));
194
201
  }
195
202
 
196
203
  // Backward (past tranches)
197
204
  for (let id = currentTranche - 1; id >= startId; id--) {
198
205
  const nextCap = capacity.get(id + 1)!;
199
- const nextPrice = price.get(id + 1)!;
200
- capacity.set(id, shrinkOnce(nextCap, supplyGrowthBps));
201
- price.set(id, shrinkOnce(nextPrice, priceGrowthCents));
206
+ const nextPrice = priceUsd.get(id + 1)!;
207
+
208
+ capacity.set(id, shrinkSupplyOnce(nextCap, supplyGrowthBps));
209
+ priceUsd.set(id, shrinkPriceOnceUsd1e18(nextPrice, priceGrowthCents));
202
210
  }
203
211
 
212
+ // Build ladder view
204
213
  const ladder: TrancheLadderItem[] = [];
205
214
  for (let id = startId; id <= endId; id++) {
206
215
  const cap = capacity.get(id)!;
216
+
207
217
  let sold: bigint;
208
218
  if (id < currentTranche) {
209
219
  sold = cap;
@@ -213,19 +223,21 @@ export function buildEthereumTrancheLadder(options: {
213
223
  sold = BigInt(0);
214
224
  }
215
225
 
226
+ const remaining = cap - sold;
227
+ const priceClientScale = usd1e18To1e8(priceUsd.get(id)!); // 1e8
228
+
216
229
  ladder.push({
217
230
  id,
218
231
  capacity: cap,
219
232
  sold,
220
- remaining: cap - sold,
221
- priceUsd: price.get(id)!,
233
+ remaining,
234
+ priceUsd: priceClientScale,
222
235
  });
223
236
  }
224
237
 
225
238
  return ladder;
226
239
  }
227
240
 
228
-
229
241
  /**
230
242
  * Turn raw liqsol_core accounts into a chain-agnostic TrancheSnapshot for SOL.
231
243
  * All math stays here; TokenClient just wires accounts + connection.
@@ -236,11 +248,11 @@ export async function buildEthereumTrancheSnapshot(options: {
236
248
  indexBn;
237
249
  trancheNumberBn;
238
250
  currentTrancheSupply;
239
- tranchePriceWadBn;
251
+ tranchePriceUsdBn;
240
252
  totalTrancheSupply;
241
253
  initialTrancheSupply;
242
254
  supplyGrowthBps;
243
- priceGrowthCents;
255
+ priceGrowthCents; // BigNumber from contract (1e18 for $0.02)
244
256
  minPriceUsd;
245
257
  maxPriceUsd;
246
258
 
@@ -256,53 +268,73 @@ export async function buildEthereumTrancheSnapshot(options: {
256
268
  ladderWindowBefore,
257
269
  ladderWindowAfter,
258
270
 
259
- totalSharesBn,
260
- indexBn,
261
- trancheNumberBn,
262
- currentTrancheSupply,
263
- tranchePriceWadBn,
271
+ totalSharesBn,
272
+ indexBn,
273
+ trancheNumberBn,
274
+ currentTrancheSupply,
275
+ tranchePriceUsdBn,
264
276
  totalTrancheSupply,
265
277
  initialTrancheSupply,
266
278
  supplyGrowthBps,
267
279
  priceGrowthCents,
268
- minPriceUsd,
280
+ minPriceUsd,
269
281
  maxPriceUsd,
270
282
  } = options;
271
283
 
284
+ // ---- BigNumber -> bigint conversions ----
285
+
286
+ // Shares: keep your prior behaviour (1e8 scale) via /1e10
287
+ const totalShares = BigInt(totalSharesBn.toString()) / BigInt(10_000_000_000); // 1e10
272
288
 
273
- // convert default BigNumber to bigint for hub to handle, and partially convert from 1e18 to 1e8 for the hub
274
- const totalShares = BigInt(totalSharesBn.toString()) / BigInt(1e10);
275
289
  const currentIndex = BigInt(indexBn.toString()); // RAY (1e27)
290
+
276
291
  const currentTranche = Number(trancheNumberBn.toString());
277
- const currentPriceUsd = BigInt(tranchePriceWadBn.toString()) / BigInt(1e10); // 1e18 WAD
278
292
 
293
+ // Prices & step in 1e18 scale from contract
294
+ const currentPriceUsd1e18 = BigInt(tranchePriceUsdBn.toString());
295
+ const priceGrowthStepUsd1e18 = BigInt(priceGrowthCents.toString());
296
+
297
+ // Convert price step to “cents” number for snapshot:
298
+ // 1 USD = 1e18 → 1 cent = 1e16.
299
+ const priceGrowthCentsNumber = Number(
300
+ priceGrowthStepUsd1e18 / BigInt(10_000_000_000_000_000) // 1e16
301
+ );
302
+
303
+ // Pre-token supplies (already 1e8-ish scale on-chain)
304
+ const currentTrancheSupplyBig = BigInt(currentTrancheSupply.toString());
305
+ const totalTrancheSupplyBig = BigInt(totalTrancheSupply.toString());
306
+ const initialTrancheSupplyBig = BigInt(initialTrancheSupply.toString());
307
+
308
+ // UI-current price (1e8 scale)
309
+ const currentPriceUsd = currentPriceUsd1e18 / BigInt(10_000_000_000); // 1e10
310
+
311
+ // ---- Build ladder ----
279
312
 
280
313
  const ladder = buildEthereumTrancheLadder({
281
314
  currentTranche,
282
- totalTrancheSupply,
283
- initialTrancheSupply,
284
- currentTrancheSupply,
285
- currentPriceUsd,
315
+ totalTrancheSupply: totalTrancheSupplyBig,
316
+ initialTrancheSupply: initialTrancheSupplyBig,
317
+ currentTrancheSupply: currentTrancheSupplyBig,
318
+ currentPriceUsd: currentPriceUsd1e18, // 1e18
319
+ priceGrowthCents: priceGrowthStepUsd1e18, // 1e18 step
286
320
  supplyGrowthBps,
287
- priceGrowthCents,
288
321
  windowBefore: ladderWindowBefore,
289
322
  windowAfter: ladderWindowAfter,
290
323
  });
291
324
 
292
-
293
325
  return {
294
326
  chainID,
295
327
  currentIndex,
296
328
  totalShares,
297
329
  currentTranche,
298
- currentPriceUsd,
330
+ currentPriceUsd, // 1e8
299
331
  supplyGrowthBps,
300
- priceGrowthCents,
301
- currentTrancheSupply,
302
- initialTrancheSupply,
303
- totalPretokensSold: totalTrancheSupply,
332
+ priceGrowthCents: priceGrowthCentsNumber, // <-- number as required
333
+ totalPretokensSold: totalTrancheSupplyBig,
334
+ currentTrancheSupply: currentTrancheSupplyBig,
335
+ initialTrancheSupply: initialTrancheSupplyBig,
304
336
  nativePriceUsd: ethPriceUsd,
305
337
  nativePriceTimestamp,
306
338
  ladder,
307
339
  };
308
- }
340
+ }
@@ -3,6 +3,7 @@ import {
3
3
  ComputeBudgetProgram,
4
4
  Connection,
5
5
  ConnectionConfig,
6
+ PerfSample,
6
7
  PublicKey as SolPubKey,
7
8
  SystemProgram,
8
9
  Transaction,
@@ -40,8 +41,10 @@ import { TokenClient } from './clients/token.client';
40
41
  import {
41
42
  deriveLiqsolMintPda,
42
43
  deriveReservePoolPda,
44
+ deriveStakeMetricsPda,
43
45
  deriveVaultPda,
44
46
  INDEX_SCALE,
47
+ PAY_RATE_SCALE_FACTOR,
45
48
  } from './constants';
46
49
 
47
50
  import { buildSolanaTrancheSnapshot, ceilDiv } from './utils';
@@ -73,6 +76,7 @@ export class SolanaStakingClient implements IStakingClient {
73
76
  public leaderboardClient: LeaderboardClient;
74
77
  public outpostClient: OutpostClient;
75
78
  public tokenClient: TokenClient;
79
+ public program: SolanaProgramService
76
80
 
77
81
  get solPubKey(): SolPubKey {
78
82
  if (!this.pubKey) throw new Error('pubKey is undefined');
@@ -186,6 +190,7 @@ export class SolanaStakingClient implements IStakingClient {
186
190
  this.leaderboardClient = new LeaderboardClient(this.anchor);
187
191
  this.outpostClient = new OutpostClient(this.anchor);
188
192
  this.tokenClient = new TokenClient(this.anchor);
193
+ this.program = new SolanaProgramService(this.anchor);
189
194
  }
190
195
 
191
196
  // ---------------------------------------------------------------------
@@ -477,25 +482,102 @@ export class SolanaStakingClient implements IStakingClient {
477
482
  // READ-ONLY Public Methods
478
483
  // ---------------------------------------------------------------------
479
484
 
480
- // Estimated total APY for staking yield (percent, e.g. 7.23)
485
+ /**
486
+ * Returns the system APY (percent) for Solana,
487
+ * using compound interest per epoch and a
488
+ * cluster-derived epochs-per-year.
489
+ */
481
490
  async getSystemAPY(): Promise<number> {
482
- // Reuse same window as the dashboard (5 most recent valid entries)
483
- const avgPayRate = await this.distributionClient.getAverageScaledPayRate(5);
491
+ // 1) Per-epoch rate (decimal) from on-chain stakeMetrics
492
+ const ratePerEpoch = await this.getEpochRateDecimalFromProgram();
493
+ // 2) Live epochs-per-year estimate from cluster
494
+ const epochsPerYear = await this.getEpochsPerYearFromCluster();
495
+ // 3) Compound: (1 + r)^N - 1
496
+ const apyDecimal = Math.pow(1 + ratePerEpoch, epochsPerYear) - 1;
497
+ // 4) Convert to percent
498
+ const apyPercent = apyDecimal * 100;
499
+
500
+ return apyPercent;
501
+ }
502
+
503
+ /**
504
+ * Reads the liqsol_core stakeMetrics account and returns the
505
+ * Solana per-epoch system rate as a **decimal** (not BPS),
506
+ * de-scaled using PAY_RATE_SCALE_FACTOR (1e12).
507
+ */
508
+ private async getEpochRateDecimalFromProgram(): Promise<number> {
509
+ const liqSolCoreProgram = this.program.getProgram('liqsolCore');
510
+ const stakeMetricsPda = deriveStakeMetricsPda();
511
+ const stakeMetrics =
512
+ await liqSolCoreProgram.account.stakeMetrics.fetch(stakeMetricsPda);
513
+
514
+ // solSystemPayRate is stored on-chain with PAY_RATE_SCALE_FACTOR (1e12)
515
+ const raw = BigInt(stakeMetrics.solSystemPayRate.toString());
516
+
517
+ // Convert to JS number in **decimal per epoch** units
518
+ const rateDecimal = Number(raw) / Number(PAY_RATE_SCALE_FACTOR);
519
+
520
+ return rateDecimal;
521
+ }
522
+
523
+ // Simple cache so we don’t hammer RPC
524
+ private epochsPerYearCache?: { value: number; fetchedAt: number };
525
+ private static readonly EPOCHS_PER_YEAR_TTL_MS = 10 * 60 * 1000; // 10 minutes
526
+
527
+ /**
528
+ * Derive "epochs per year" from the live Solana cluster.
529
+ *
530
+ * Uses:
531
+ * - getRecentPerformanceSamples() to estimate slots/second
532
+ * - getEpochInfo() to read slotsInEpoch
533
+ */
534
+ private async getEpochsPerYearFromCluster(): Promise<number> {
535
+ const now = Date.now();
484
536
 
485
- if (avgPayRate.isZero()) {
486
- return 0;
537
+ if (
538
+ this.epochsPerYearCache &&
539
+ now - this.epochsPerYearCache.fetchedAt <
540
+ SolanaStakingClient.EPOCHS_PER_YEAR_TTL_MS
541
+ ) {
542
+ return this.epochsPerYearCache.value;
487
543
  }
488
544
 
489
- // 10^12, same scale used on-chain and in the dashboard
490
- const SCALE = new BN('1000000000000');
491
- const EPOCHS_PER_YEAR = 365; // matches DEFAULT_PAY_RATE semantics
545
+ const connection = this.anchor.connection;
492
546
 
493
- // Safe: pay rate is well below 1e12, so .toNumber() won't overflow
494
- const ratePerPeriod = avgPayRate.toNumber() / SCALE.toNumber(); // e.g. 0.0001917…
495
- const apyDecimal = ratePerPeriod * EPOCHS_PER_YEAR; // e.g. ~0.07
496
- const apyPercent = apyDecimal * 100; // e.g. 7
547
+ // 1) Estimate slots/second from recent performance samples
548
+ const samples: PerfSample[] = await connection.getRecentPerformanceSamples(
549
+ 60,
550
+ );
551
+ if (!samples.length) {
552
+ throw new Error('No performance samples available from cluster');
553
+ }
497
554
 
498
- return apyPercent;
555
+ const totalSlots = samples.reduce((acc, s) => acc + s.numSlots, 0);
556
+ const totalSecs = samples.reduce((acc, s) => acc + s.samplePeriodSecs, 0);
557
+
558
+ if (totalSecs === 0) {
559
+ throw new Error(
560
+ 'Cluster returned zero samplePeriodSecs in performance samples',
561
+ );
562
+ }
563
+
564
+ const slotsPerSecond = totalSlots / totalSecs;
565
+
566
+ // 2) Slots per epoch from cluster
567
+ const epochInfo = await connection.getEpochInfo(); // finalized commitment by default
568
+ const slotsPerEpoch = epochInfo.slotsInEpoch;
569
+
570
+ const secondsPerEpoch = slotsPerEpoch / slotsPerSecond;
571
+ const secondsPerYear = 365 * 24 * 60 * 60;
572
+
573
+ const epochsPerYear = secondsPerYear / secondsPerEpoch;
574
+
575
+ this.epochsPerYearCache = {
576
+ value: epochsPerYear,
577
+ fetchedAt: now,
578
+ };
579
+
580
+ return epochsPerYear;
499
581
  }
500
582
 
501
583
  // ---------------------------------------------
@@ -520,7 +602,7 @@ export class SolanaStakingClient implements IStakingClient {
520
602
  return BigInt(0);
521
603
  }
522
604
 
523
- const [avgPayRate, globalConfig] : [BN, GlobalConfig] = await Promise.all([
605
+ const [avgPayRate, globalConfig]: [BN, GlobalConfig] = await Promise.all([
524
606
  this.distributionClient.getAverageScaledPayRate(windowSize),
525
607
  this.distributionClient.getGlobalConfig(),
526
608
  ]);
@@ -756,4 +838,5 @@ export class SolanaStakingClient implements IStakingClient {
756
838
  );
757
839
  }
758
840
  }
841
+
759
842
  }