@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/lib/stake.js CHANGED
@@ -8099,8 +8099,31 @@ var __async$d = (__this, __arguments, generator) => {
8099
8099
  });
8100
8100
  };
8101
8101
  const INDEX_SCALE = BigInt(1e12);
8102
- BigInt(1e8);
8103
- const BPS$1 = BigInt(1e4);
8102
+ const USD_SCALE = BigInt(1e8);
8103
+ const BPS = BigInt(1e4);
8104
+ function growSupplyOnce$1(value, growthBps) {
8105
+ const g = BigInt(growthBps);
8106
+ return (value * (BPS + g) + BPS / BigInt(2)) / BPS;
8107
+ }
8108
+ function shrinkSupplyOnce$1(value, growthBps) {
8109
+ const g = BigInt(growthBps);
8110
+ return (value * BPS + (BPS + g) / BigInt(2)) / (BPS + g);
8111
+ }
8112
+ function priceStepUsd1e8(priceGrowthCents) {
8113
+ if (!priceGrowthCents) return BigInt(0);
8114
+ const CENT_SCALE = USD_SCALE / BigInt(100);
8115
+ return BigInt(priceGrowthCents) * CENT_SCALE;
8116
+ }
8117
+ function growPriceOnceUsd1e8(value, priceGrowthCents) {
8118
+ const step = priceStepUsd1e8(priceGrowthCents);
8119
+ return value + step;
8120
+ }
8121
+ function shrinkPriceOnceUsd1e8(value, priceGrowthCents) {
8122
+ const step = priceStepUsd1e8(priceGrowthCents);
8123
+ if (step === BigInt(0)) return value;
8124
+ if (value <= step) return BigInt(0);
8125
+ return value - step;
8126
+ }
8104
8127
  function toBigint(x) {
8105
8128
  if (typeof x === "bigint") return x;
8106
8129
  if (typeof x === "number") return BigInt(x);
@@ -8113,14 +8136,6 @@ function tokensToShares(amount, currentIndex) {
8113
8136
  const r = num % currentIndex;
8114
8137
  return r === BigInt(0) ? q : q + BigInt(1);
8115
8138
  }
8116
- function growOnce$1(value, growthBps) {
8117
- const g = BigInt(growthBps);
8118
- return (value * (BPS$1 + g) + BPS$1 / BigInt(2)) / BPS$1;
8119
- }
8120
- function shrinkOnce$1(value, growthBps) {
8121
- const g = BigInt(growthBps);
8122
- return (value * BPS$1 + (BPS$1 + g) / BigInt(2)) / (BPS$1 + g);
8123
- }
8124
8139
  function buildSolanaTrancheLadder(options) {
8125
8140
  const {
8126
8141
  currentTranche,
@@ -8141,14 +8156,14 @@ function buildSolanaTrancheLadder(options) {
8141
8156
  for (let id = currentTranche + 1; id <= endId; id++) {
8142
8157
  const prevCap = capacity.get(id - 1);
8143
8158
  const prevPrice = price.get(id - 1);
8144
- capacity.set(id, growOnce$1(prevCap, supplyGrowthBps));
8145
- price.set(id, growOnce$1(prevPrice, priceGrowthCents));
8159
+ capacity.set(id, growSupplyOnce$1(prevCap, supplyGrowthBps));
8160
+ price.set(id, growPriceOnceUsd1e8(prevPrice, priceGrowthCents));
8146
8161
  }
8147
8162
  for (let id = currentTranche - 1; id >= startId; id--) {
8148
8163
  const nextCap = capacity.get(id + 1);
8149
8164
  const nextPrice = price.get(id + 1);
8150
- capacity.set(id, shrinkOnce$1(nextCap, supplyGrowthBps));
8151
- price.set(id, shrinkOnce$1(nextPrice, priceGrowthCents));
8165
+ capacity.set(id, shrinkSupplyOnce$1(nextCap, supplyGrowthBps));
8166
+ price.set(id, shrinkPriceOnceUsd1e8(nextPrice, priceGrowthCents));
8152
8167
  }
8153
8168
  const ladder = [];
8154
8169
  for (let id = startId; id <= endId; id++) {
@@ -9152,7 +9167,7 @@ var __async$8 = (__this, __arguments, generator) => {
9152
9167
  };
9153
9168
  const commitment = "confirmed";
9154
9169
  const SCALE = new anchor.BN("1000000000000");
9155
- class SolanaStakingClient {
9170
+ const _SolanaStakingClient = class _SolanaStakingClient {
9156
9171
  constructor(config) {
9157
9172
  this.config = config;
9158
9173
  const adapter = config.provider;
@@ -9232,6 +9247,7 @@ class SolanaStakingClient {
9232
9247
  this.leaderboardClient = new LeaderboardClient(this.anchor);
9233
9248
  this.outpostClient = new OutpostClient(this.anchor);
9234
9249
  this.tokenClient = new TokenClient(this.anchor);
9250
+ this.program = new SolanaProgramService(this.anchor);
9235
9251
  }
9236
9252
  get solPubKey() {
9237
9253
  if (!this.pubKey) throw new Error("pubKey is undefined");
@@ -9412,18 +9428,56 @@ class SolanaStakingClient {
9412
9428
  }
9413
9429
  getSystemAPY() {
9414
9430
  return __async$8(this, null, function* () {
9415
- const avgPayRate = yield this.distributionClient.getAverageScaledPayRate(5);
9416
- if (avgPayRate.isZero()) {
9417
- return 0;
9418
- }
9419
- const SCALE2 = new anchor.BN("1000000000000");
9420
- const EPOCHS_PER_YEAR = 365;
9421
- const ratePerPeriod = avgPayRate.toNumber() / SCALE2.toNumber();
9422
- const apyDecimal = ratePerPeriod * EPOCHS_PER_YEAR;
9431
+ const ratePerEpoch = yield this.getEpochRateDecimalFromProgram();
9432
+ const epochsPerYear = yield this.getEpochsPerYearFromCluster();
9433
+ const apyDecimal = Math.pow(1 + ratePerEpoch, epochsPerYear) - 1;
9423
9434
  const apyPercent = apyDecimal * 100;
9424
9435
  return apyPercent;
9425
9436
  });
9426
9437
  }
9438
+ getEpochRateDecimalFromProgram() {
9439
+ return __async$8(this, null, function* () {
9440
+ const liqSolCoreProgram = this.program.getProgram("liqsolCore");
9441
+ const stakeMetricsPda = deriveStakeMetricsPda();
9442
+ const stakeMetrics = yield liqSolCoreProgram.account.stakeMetrics.fetch(stakeMetricsPda);
9443
+ const raw = BigInt(stakeMetrics.solSystemPayRate.toString());
9444
+ const rateDecimal = Number(raw) / Number(PAY_RATE_SCALE_FACTOR);
9445
+ return rateDecimal;
9446
+ });
9447
+ }
9448
+ getEpochsPerYearFromCluster() {
9449
+ return __async$8(this, null, function* () {
9450
+ const now = Date.now();
9451
+ if (this.epochsPerYearCache && now - this.epochsPerYearCache.fetchedAt < _SolanaStakingClient.EPOCHS_PER_YEAR_TTL_MS) {
9452
+ return this.epochsPerYearCache.value;
9453
+ }
9454
+ const connection = this.anchor.connection;
9455
+ const samples = yield connection.getRecentPerformanceSamples(
9456
+ 60
9457
+ );
9458
+ if (!samples.length) {
9459
+ throw new Error("No performance samples available from cluster");
9460
+ }
9461
+ const totalSlots = samples.reduce((acc, s) => acc + s.numSlots, 0);
9462
+ const totalSecs = samples.reduce((acc, s) => acc + s.samplePeriodSecs, 0);
9463
+ if (totalSecs === 0) {
9464
+ throw new Error(
9465
+ "Cluster returned zero samplePeriodSecs in performance samples"
9466
+ );
9467
+ }
9468
+ const slotsPerSecond = totalSlots / totalSecs;
9469
+ const epochInfo = yield connection.getEpochInfo();
9470
+ const slotsPerEpoch = epochInfo.slotsInEpoch;
9471
+ const secondsPerEpoch = slotsPerEpoch / slotsPerSecond;
9472
+ const secondsPerYear = 365 * 24 * 60 * 60;
9473
+ const epochsPerYear = secondsPerYear / secondsPerEpoch;
9474
+ this.epochsPerYearCache = {
9475
+ value: epochsPerYear,
9476
+ fetchedAt: now
9477
+ };
9478
+ return epochsPerYear;
9479
+ });
9480
+ }
9427
9481
  getDepositFee(amountLamports, windowSize = 5) {
9428
9482
  return __async$8(this, null, function* () {
9429
9483
  var _a, _b, _c;
@@ -9561,7 +9615,9 @@ class SolanaStakingClient {
9561
9615
  );
9562
9616
  }
9563
9617
  }
9564
- }
9618
+ };
9619
+ _SolanaStakingClient.EPOCHS_PER_YEAR_TTL_MS = 10 * 60 * 1e3;
9620
+ let SolanaStakingClient = _SolanaStakingClient;
9565
9621
 
9566
9622
  var _format$q = "hh-sol-artifact-1";
9567
9623
  var contractName$q = "Accounting";
@@ -35114,22 +35170,22 @@ const ADDRESSES = {
35114
35170
  WithdrawalVault: "0xC82FAf3Fed135B70A7E797864F504819a98E96c4",
35115
35171
  StakingModule: "0xdd24EffAa4B42f3273E2b44995639119c08daBea",
35116
35172
  YieldOracle: "0x56A27E1d10d4aEc7402dC26693fb7c0Eb66eF802",
35117
- OutpostManagerAuthority: "0xAEcd2aa6EeFa4Aa7Cb9368728aA89Ca59Aa9ea59",
35118
- iodata: "0x2c2ab53F6Bc146bd31B459E832Ce0B9Da78712d8",
35119
- Base58: "0x9F188Ec124c9ad9bF6D195650d8360Fd052F585A",
35120
- sysio_merkle: "0x6eDA3C4c34421Cd667800C17fe33E48f5698e004",
35121
- ReceiptNFT: "0xd083051d9bb8353875abCecAB50C6e4FB5e527a8",
35122
- MockAggregator: "0xFCfc3ddd4CBd9Ad3b3af3A374B8bdA1b66eE6FFF",
35123
- Pool: "0xee5F11aC9b104c18d2423f266c5E737fC95ebA92",
35124
- OutpostManager: "0xB1B6ba7FA156652849069aC7ADB281283D235B9f",
35125
- sysio_write: "0xEfA608136d372349C09a7aA57665C09Fb4a620Ca",
35126
- EthUsdPriceConsumer: "0x6337A23b61f98b1526faF2848385Abe9cB4cFF21",
35127
- BAR: "0x9264eAA449da94caF70Fc18522021a94C8DF32Fb",
35128
- OPPCommon: "0x86A8cA16ce521De3EBdd1C541fAf188795b59FD0",
35129
- OPP: "0x79e8395Bb5131FB285aCEE5329BB43E66f50F88C",
35130
- Pretoken: "0x62f98AF2f9C3EF4eF2fA7bc0245BD5a9315E7541",
35131
- OPPInbound: "0xC85f57Ff069711e0b3472De3963bd2fC2FEfF3e2",
35132
- Depositor: "0xb0BACAb6f13dd96281300be13a6346461b2f35F3"
35173
+ OutpostManagerAuthority: "0x57A3723B9f3C6022CAe394C859655E59382Fad18",
35174
+ iodata: "0x88896d4fa70C3a7Fb833C80BB5763a4c53A6aCB5",
35175
+ Base58: "0x0E9E6e8A32477F3B086Aaa26db2b928a816Da711",
35176
+ sysio_merkle: "0xf5858B784B080A08063FAe06dB6d91c5BBAA48C7",
35177
+ ReceiptNFT: "0xF1F5e063bFF6E0c09b0ac8020376E16c0be8eA10",
35178
+ EthUsdPriceConsumer: "0xFdb3Ab290179CA85204aD1Ffb8c1c3c42AEB768F",
35179
+ Pool: "0x15DaeB9562c6Dd21558f14CcdDf5C855499B8693",
35180
+ OutpostManager: "0x1aCCc78FCA9e2Ea4dcE849bf072C2248f36435cC",
35181
+ sysio_write: "0x513e472904EE67A8E27ebaF2411f3ed3851F4514",
35182
+ Pretoken: "0xd7CDc79B90336720ecf02eD5A355cB0F7099F079",
35183
+ BAR: "0x4A01414dEA81b1961aE986Bc4E95B30f73770f99",
35184
+ OPPCommon: "0x3747Cc19A351BCBCE92055c419e7d710C4b399aA",
35185
+ OPP: "0xF577FDc80014ef19DF258243e0551c888Da809E4",
35186
+ Depositor: "0xD9Eb2A2d4e9eD7e2257153041B29DCeCDee8BCFe",
35187
+ OPPInbound: "0x232C01f2528A5013af3703bE4B4ce30A793Ee8BD",
35188
+ MockAggregator: "0xFCfc3ddd4CBd9Ad3b3af3A374B8bdA1b66eE6FFF"
35133
35189
  };
35134
35190
  const CONTRACTS = {
35135
35191
  LiqEthAuthority: {
@@ -35337,7 +35393,7 @@ var __async$6 = (__this, __arguments, generator) => {
35337
35393
  step((generator = generator.apply(__this, __arguments)).next());
35338
35394
  });
35339
35395
  };
35340
- const BPS = BigInt(1e4);
35396
+ BigInt(1e4);
35341
35397
  function formatContractErrors(err) {
35342
35398
  if (err.errorName && err.errorArgs) {
35343
35399
  const errorObj = {
@@ -35410,20 +35466,27 @@ function sendOPPFinalize(opp, gasLimit) {
35410
35466
  }
35411
35467
  });
35412
35468
  }
35413
- function growOnce(value, growthBps) {
35469
+ const BPS_DENOM = BigInt(1e4);
35470
+ const USD_ONCHAIN_SCALE = BigInt(1e18);
35471
+ const USD_CLIENT_SCALE = BigInt(1e8);
35472
+ const USD_SCALE_DOWN = USD_ONCHAIN_SCALE / USD_CLIENT_SCALE;
35473
+ function growSupplyOnce(value, growthBps) {
35414
35474
  const g = BigInt(growthBps);
35415
- return (value * (BPS + g) + BPS / BigInt(2)) / BPS;
35475
+ return value * (BPS_DENOM + g) / BPS_DENOM;
35416
35476
  }
35417
- function shrinkOnce(value, growthBps) {
35477
+ function shrinkSupplyOnce(value, growthBps) {
35418
35478
  const g = BigInt(growthBps);
35419
- return (value * BPS + (BPS + g) / BigInt(2)) / (BPS + g);
35479
+ return value * BPS_DENOM / (BPS_DENOM + g);
35420
35480
  }
35421
- function getTrancheSize(startSupply, supplyGrowthBps, trancheNumber) {
35422
- let supply = startSupply;
35423
- for (let i = 0; i < trancheNumber; i++) {
35424
- supply = (supply * (BPS + BigInt(supplyGrowthBps)) + BPS / BigInt(2)) / BPS;
35425
- }
35426
- return supply;
35481
+ function growPriceOnceUsd1e18(value, stepUsd1e18) {
35482
+ return value + stepUsd1e18;
35483
+ }
35484
+ function shrinkPriceOnceUsd1e18(value, stepUsd1e18) {
35485
+ if (value <= stepUsd1e18) return BigInt(0);
35486
+ return value - stepUsd1e18;
35487
+ }
35488
+ function usd1e18To1e8(raw) {
35489
+ return raw / USD_SCALE_DOWN;
35427
35490
  }
35428
35491
  function buildEthereumTrancheLadder(options) {
35429
35492
  const {
@@ -35431,29 +35494,32 @@ function buildEthereumTrancheLadder(options) {
35431
35494
  initialTrancheSupply,
35432
35495
  currentTrancheSupply,
35433
35496
  currentPriceUsd,
35434
- supplyGrowthBps,
35435
35497
  priceGrowthCents,
35498
+ supplyGrowthBps,
35436
35499
  windowBefore = 5,
35437
35500
  windowAfter = 5
35438
35501
  } = options;
35439
35502
  const startId = Math.max(0, currentTranche - windowBefore);
35440
35503
  const endId = currentTranche + windowAfter;
35441
- const currentTrancheSize = getTrancheSize(initialTrancheSupply, supplyGrowthBps, currentTranche);
35442
35504
  const capacity = new Map();
35443
- const price = new Map();
35444
- capacity.set(currentTranche, currentTrancheSize);
35445
- price.set(currentTranche, currentPriceUsd);
35505
+ const priceUsd = new Map();
35506
+ let currentCap = initialTrancheSupply;
35507
+ for (let i = 0; i < currentTranche; i++) {
35508
+ currentCap = growSupplyOnce(currentCap, supplyGrowthBps);
35509
+ }
35510
+ capacity.set(currentTranche, currentCap);
35511
+ priceUsd.set(currentTranche, currentPriceUsd);
35446
35512
  for (let id = currentTranche + 1; id <= endId; id++) {
35447
35513
  const prevCap = capacity.get(id - 1);
35448
- const prevPrice = price.get(id - 1);
35449
- capacity.set(id, growOnce(prevCap, supplyGrowthBps));
35450
- price.set(id, growOnce(prevPrice, priceGrowthCents));
35514
+ const prevPrice = priceUsd.get(id - 1);
35515
+ capacity.set(id, growSupplyOnce(prevCap, supplyGrowthBps));
35516
+ priceUsd.set(id, growPriceOnceUsd1e18(prevPrice, priceGrowthCents));
35451
35517
  }
35452
35518
  for (let id = currentTranche - 1; id >= startId; id--) {
35453
35519
  const nextCap = capacity.get(id + 1);
35454
- const nextPrice = price.get(id + 1);
35455
- capacity.set(id, shrinkOnce(nextCap, supplyGrowthBps));
35456
- price.set(id, shrinkOnce(nextPrice, priceGrowthCents));
35520
+ const nextPrice = priceUsd.get(id + 1);
35521
+ capacity.set(id, shrinkSupplyOnce(nextCap, supplyGrowthBps));
35522
+ priceUsd.set(id, shrinkPriceOnceUsd1e18(nextPrice, priceGrowthCents));
35457
35523
  }
35458
35524
  const ladder = [];
35459
35525
  for (let id = startId; id <= endId; id++) {
@@ -35466,12 +35532,14 @@ function buildEthereumTrancheLadder(options) {
35466
35532
  } else {
35467
35533
  sold = BigInt(0);
35468
35534
  }
35535
+ const remaining = cap - sold;
35536
+ const priceClientScale = usd1e18To1e8(priceUsd.get(id));
35469
35537
  ladder.push({
35470
35538
  id,
35471
35539
  capacity: cap,
35472
35540
  sold,
35473
- remaining: cap - sold,
35474
- priceUsd: price.get(id)
35541
+ remaining,
35542
+ priceUsd: priceClientScale
35475
35543
  });
35476
35544
  }
35477
35545
  return ladder;
@@ -35488,7 +35556,7 @@ function buildEthereumTrancheSnapshot(options) {
35488
35556
  indexBn,
35489
35557
  trancheNumberBn,
35490
35558
  currentTrancheSupply,
35491
- tranchePriceWadBn,
35559
+ tranchePriceUsdBn,
35492
35560
  totalTrancheSupply,
35493
35561
  initialTrancheSupply,
35494
35562
  supplyGrowthBps,
@@ -35499,15 +35567,23 @@ function buildEthereumTrancheSnapshot(options) {
35499
35567
  const totalShares = BigInt(totalSharesBn.toString()) / BigInt(1e10);
35500
35568
  const currentIndex = BigInt(indexBn.toString());
35501
35569
  const currentTranche = Number(trancheNumberBn.toString());
35502
- const currentPriceUsd = BigInt(tranchePriceWadBn.toString()) / BigInt(1e10);
35570
+ const currentPriceUsd1e18 = BigInt(tranchePriceUsdBn.toString());
35571
+ const priceGrowthStepUsd1e18 = BigInt(priceGrowthCents.toString());
35572
+ const priceGrowthCentsNumber = Number(
35573
+ priceGrowthStepUsd1e18 / BigInt(1e16)
35574
+ );
35575
+ const currentTrancheSupplyBig = BigInt(currentTrancheSupply.toString());
35576
+ const totalTrancheSupplyBig = BigInt(totalTrancheSupply.toString());
35577
+ const initialTrancheSupplyBig = BigInt(initialTrancheSupply.toString());
35578
+ const currentPriceUsd = currentPriceUsd1e18 / BigInt(1e10);
35503
35579
  const ladder = buildEthereumTrancheLadder({
35504
35580
  currentTranche,
35505
- totalTrancheSupply,
35506
- initialTrancheSupply,
35507
- currentTrancheSupply,
35508
- currentPriceUsd,
35581
+ totalTrancheSupply: totalTrancheSupplyBig,
35582
+ initialTrancheSupply: initialTrancheSupplyBig,
35583
+ currentTrancheSupply: currentTrancheSupplyBig,
35584
+ currentPriceUsd: currentPriceUsd1e18,
35585
+ priceGrowthCents: priceGrowthStepUsd1e18,
35509
35586
  supplyGrowthBps,
35510
- priceGrowthCents,
35511
35587
  windowBefore: ladderWindowBefore,
35512
35588
  windowAfter: ladderWindowAfter
35513
35589
  });
@@ -35518,10 +35594,10 @@ function buildEthereumTrancheSnapshot(options) {
35518
35594
  currentTranche,
35519
35595
  currentPriceUsd,
35520
35596
  supplyGrowthBps,
35521
- priceGrowthCents,
35522
- currentTrancheSupply,
35523
- initialTrancheSupply,
35524
- totalPretokensSold: totalTrancheSupply,
35597
+ priceGrowthCents: priceGrowthCentsNumber,
35598
+ totalPretokensSold: totalTrancheSupplyBig,
35599
+ currentTrancheSupply: currentTrancheSupplyBig,
35600
+ initialTrancheSupply: initialTrancheSupplyBig,
35525
35601
  nativePriceUsd: ethPriceUsd,
35526
35602
  nativePriceTimestamp,
35527
35603
  ladder
@@ -36491,7 +36567,7 @@ class EthereumStakingClient {
36491
36567
  try {
36492
36568
  const blockNumber = yield this.provider.getBlockNumber();
36493
36569
  const blockTag = { blockTag: blockNumber };
36494
- const [totalSharesBn, indexBn, trancheNumberBn, trancheSupplyBn, tranchePriceWadBn, totalSupplyBn, supplyGrowthBps, priceGrowthCents, minPriceUsd, maxPriceUsd] = yield Promise.all([
36570
+ const [totalSharesBn, indexBn, trancheNumberBn, trancheSupplyBn, tranchePriceUsdBn, totalSupplyBn, supplyGrowthBps, priceGrowthCents, minPriceUsd, maxPriceUsd] = yield Promise.all([
36495
36571
  this.contract.Depositor.totalShares(blockTag),
36496
36572
  this.contract.Depositor.index(blockTag),
36497
36573
  this.contract.Pretoken.trancheNumber(blockTag),
@@ -36515,7 +36591,7 @@ class EthereumStakingClient {
36515
36591
  indexBn,
36516
36592
  trancheNumberBn,
36517
36593
  currentTrancheSupply,
36518
- tranchePriceWadBn,
36594
+ tranchePriceUsdBn,
36519
36595
  totalTrancheSupply,
36520
36596
  initialTrancheSupply,
36521
36597
  supplyGrowthBps,
@@ -36528,7 +36604,6 @@ class EthereumStakingClient {
36528
36604
  ladderWindowAfter: windowAfter
36529
36605
  });
36530
36606
  } catch (err) {
36531
- console.log(err);
36532
36607
  throw new Error(`Error fetching Ethereum tranche snapshot: ${(err == null ? void 0 : err.message) || err}`);
36533
36608
  }
36534
36609
  });
@@ -36600,6 +36675,7 @@ class Staker {
36600
36675
  if (!Array.isArray(config)) config = [config];
36601
36676
  config.forEach((cfg) => {
36602
36677
  switch (cfg.network.chainId) {
36678
+ case core.SolChainID.Devnet:
36603
36679
  case core.SolChainID.WireTestnet:
36604
36680
  this.clients.set(cfg.network.chainId, new SolanaStakingClient(cfg));
36605
36681
  break;