@drift-labs/sdk 2.95.0-beta.11 → 2.95.0-beta.12

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.95.0-beta.11
1
+ 2.95.0-beta.12
@@ -1,5 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
- import { BN } from '../index';
2
+ import { BN, SpotMarketAccount } from '../index';
3
+ export declare function nextRevenuePoolSettleApr(spotMarket: SpotMarketAccount, vaultBalance: BN, // vault token amount
4
+ amount?: BN): number;
3
5
  export declare function stakeAmountToShares(amount: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
4
6
  export declare function unstakeSharesToAmount(nShares: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
5
7
  export declare function unstakeSharesToAmountWithOpenRequest(nShares: BN, withdrawRequestShares: BN, withdrawRequestAmount: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
@@ -1,8 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unstakeSharesToAmountWithOpenRequest = exports.unstakeSharesToAmount = exports.stakeAmountToShares = void 0;
3
+ exports.unstakeSharesToAmountWithOpenRequest = exports.unstakeSharesToAmount = exports.stakeAmountToShares = exports.nextRevenuePoolSettleApr = void 0;
4
4
  const numericConstants_1 = require("../constants/numericConstants");
5
5
  const index_1 = require("../index");
6
+ const spotBalance_1 = require("../math/spotBalance");
7
+ function nextRevenuePoolSettleApr(spotMarket, vaultBalance, // vault token amount
8
+ amount // delta token amount
9
+ ) {
10
+ const MAX_APR = new index_1.BN(10).mul(numericConstants_1.PERCENTAGE_PRECISION); // 1000% APR
11
+ // Conmputing the APR:
12
+ const revenuePoolBN = (0, spotBalance_1.getTokenAmount)(spotMarket.revenuePool.scaledBalance, spotMarket, index_1.SpotBalanceType.DEPOSIT);
13
+ const payoutRatio = 0.1;
14
+ const ratioForStakers = spotMarket.insuranceFund.totalFactor > 0 &&
15
+ spotMarket.insuranceFund.userFactor > 0 &&
16
+ spotMarket.insuranceFund.revenueSettlePeriod.gt(numericConstants_1.ZERO)
17
+ ? spotMarket.insuranceFund.userFactor /
18
+ spotMarket.insuranceFund.totalFactor
19
+ : 0;
20
+ // Settle periods from on-chain data:
21
+ const revSettlePeriod = spotMarket.insuranceFund.revenueSettlePeriod.toNumber() * 1000;
22
+ const settlesPerYear = 31536000000 / revSettlePeriod;
23
+ const projectedAnnualRev = revenuePoolBN
24
+ .muln(settlesPerYear)
25
+ .muln(payoutRatio);
26
+ const uncappedApr = vaultBalance.add(amount).eq(numericConstants_1.ZERO)
27
+ ? 0
28
+ : projectedAnnualRev.muln(1000).div(vaultBalance.add(amount)).toNumber() *
29
+ 100 *
30
+ 1000;
31
+ const cappedApr = Math.min(uncappedApr, MAX_APR.toNumber());
32
+ const nextApr = cappedApr * ratioForStakers;
33
+ return nextApr;
34
+ }
35
+ exports.nextRevenuePoolSettleApr = nextRevenuePoolSettleApr;
6
36
  function stakeAmountToShares(amount, totalIfShares, insuranceFundVaultBalance) {
7
37
  let nShares;
8
38
  if (insuranceFundVaultBalance.gt(numericConstants_1.ZERO)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.95.0-beta.11",
3
+ "version": "2.95.0-beta.12",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -1,5 +1,51 @@
1
- import { ZERO } from '../constants/numericConstants';
2
- import { BN } from '../index';
1
+ import { PERCENTAGE_PRECISION, ZERO } from '../constants/numericConstants';
2
+ import { BN, SpotMarketAccount, SpotBalanceType } from '../index';
3
+ import { getTokenAmount } from '../math/spotBalance';
4
+
5
+ export function nextRevenuePoolSettleApr(
6
+ spotMarket: SpotMarketAccount,
7
+ vaultBalance: BN, // vault token amount
8
+ amount?: BN // delta token amount
9
+ ): number {
10
+ const MAX_APR = new BN(10).mul(PERCENTAGE_PRECISION); // 1000% APR
11
+
12
+ // Conmputing the APR:
13
+ const revenuePoolBN = getTokenAmount(
14
+ spotMarket.revenuePool.scaledBalance,
15
+ spotMarket,
16
+ SpotBalanceType.DEPOSIT
17
+ );
18
+
19
+ const payoutRatio = 0.1;
20
+ const ratioForStakers =
21
+ spotMarket.insuranceFund.totalFactor > 0 &&
22
+ spotMarket.insuranceFund.userFactor > 0 &&
23
+ spotMarket.insuranceFund.revenueSettlePeriod.gt(ZERO)
24
+ ? spotMarket.insuranceFund.userFactor /
25
+ spotMarket.insuranceFund.totalFactor
26
+ : 0;
27
+
28
+ // Settle periods from on-chain data:
29
+ const revSettlePeriod =
30
+ spotMarket.insuranceFund.revenueSettlePeriod.toNumber() * 1000;
31
+
32
+ const settlesPerYear = 31536000000 / revSettlePeriod;
33
+
34
+ const projectedAnnualRev = revenuePoolBN
35
+ .muln(settlesPerYear)
36
+ .muln(payoutRatio);
37
+
38
+ const uncappedApr = vaultBalance.add(amount).eq(ZERO)
39
+ ? 0
40
+ : projectedAnnualRev.muln(1000).div(vaultBalance.add(amount)).toNumber() *
41
+ 100 *
42
+ 1000;
43
+ const cappedApr = Math.min(uncappedApr, MAX_APR.toNumber());
44
+
45
+ const nextApr = cappedApr * ratioForStakers;
46
+
47
+ return nextApr;
48
+ }
3
49
 
4
50
  export function stakeAmountToShares(
5
51
  amount: BN,