@exponent-labs/exponent-fetcher 0.0.3

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.
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.computeD = exports.computeStableSwapInvariant = exports.getAmountByShare = exports.calculateUnlockedAmount = void 0;
7
+ const anchor_1 = require("@coral-xyz/anchor");
8
+ const bn_sqrt_1 = __importDefault(require("bn-sqrt"));
9
+ /**
10
+ * Calculates the unlocked (withdrawable) amount of tokens in a Meteora vault.
11
+ *
12
+ * @param onChainTime - Current blockchain timestamp in seconds
13
+ * @param vaultState - The state of the Meteora vault
14
+ * @returns A BN representing the unlocked amount that can be withdrawn from the vault
15
+ *
16
+ */
17
+ function calculateUnlockedAmount(onChainTime, vaultState) {
18
+ const { lockedProfitTracker: { lastReport, lockedProfitDegradation, lastUpdatedLockedProfit }, totalAmount: vaultTotalAmount, } = vaultState;
19
+ const lockedProfitDegradationDenominator = new anchor_1.BN(1_000_000_000_000);
20
+ const duration = new anchor_1.BN(onChainTime).sub(lastReport);
21
+ const lockedFundRatio = duration.mul(lockedProfitDegradation);
22
+ if (lockedFundRatio.gt(lockedProfitDegradationDenominator)) {
23
+ return vaultTotalAmount;
24
+ }
25
+ const lockedProfit = lastUpdatedLockedProfit
26
+ .mul(lockedProfitDegradationDenominator.sub(lockedFundRatio))
27
+ .div(lockedProfitDegradationDenominator);
28
+ return vaultTotalAmount.sub(lockedProfit);
29
+ }
30
+ exports.calculateUnlockedAmount = calculateUnlockedAmount;
31
+ /**
32
+ * Calculates the amount of tokens corresponding to a share of the vault.
33
+ *
34
+ * @param share - Share amount of LP tokens
35
+ * @param totalSupply - Total supply of LP tokens
36
+ * @param vaultState - Current state of the vault
37
+ * @param onChainTime - Current blockchain timestamp in seconds
38
+ * @returns The amount of underlying tokens represented by the shares
39
+ *
40
+ */
41
+ function getAmountByShare(share, totalSupply, vaultState, onChainTime) {
42
+ const withdrawableAmount = calculateUnlockedAmount(onChainTime, vaultState);
43
+ return totalSupply.isZero() ? new anchor_1.BN(0) : share.mul(withdrawableAmount).div(totalSupply);
44
+ }
45
+ exports.getAmountByShare = getAmountByShare;
46
+ /**
47
+ * Computes the StableSwap invariant (D) based on the Curve Finance formula.
48
+ *
49
+ * This function implements the iterative algorithm to find the value D such that:
50
+ * Ann * S + D * P / (A * N^n) = D^(n+1) / (n^n * prod(x_i))
51
+ *
52
+ * Where:
53
+ * - Ann is amplification coefficient * n^n
54
+ * - S is the sum of all coin amounts
55
+ * - P is the product of all coin amounts
56
+ * - n is the number of coins (2 in this case)
57
+ *
58
+ * @param ampFactor - Amplification coefficient (A)
59
+ * @param amountA - Amount of token A in the pool
60
+ * @param amountB - Amount of token B in the pool
61
+ * @returns The computed invariant D
62
+ *
63
+ */
64
+ const computeStableSwapInvariant = (ampFactor, amountA, amountB) => {
65
+ const N_COINS = new anchor_1.BN(2);
66
+ const ZERO = new anchor_1.BN(0);
67
+ const ONE = new anchor_1.BN(1);
68
+ const MAX_ITERS = 20;
69
+ const Ann = ampFactor.mul(N_COINS); // A*n^n
70
+ const S = amountA.add(amountB); // sum(x_i), a.k.a S
71
+ if (S === ZERO) {
72
+ return ZERO;
73
+ }
74
+ let dPrev = ZERO;
75
+ let d = S;
76
+ for (let i = 0; d.sub(dPrev).abs().gt(ONE) && i < MAX_ITERS; i++) {
77
+ dPrev = d;
78
+ let dP = d;
79
+ dP = dP.mul(d).div(amountA.mul(N_COINS));
80
+ dP = dP.mul(d).div(amountB.mul(N_COINS));
81
+ const dNumerator = d.mul(Ann.mul(S).add(dP.mul(N_COINS)));
82
+ const dDenominator = d.mul(Ann.sub(ONE)).add(dP.mul(N_COINS.add(ONE)));
83
+ d = dNumerator.div(dDenominator);
84
+ }
85
+ return d;
86
+ };
87
+ exports.computeStableSwapInvariant = computeStableSwapInvariant;
88
+ /**
89
+ * Computes the invariant D value for a token pair based on curve type.
90
+ *
91
+ * @param curve - Type of curve (ConstantProduct or Stable with parameters)
92
+ * @param tokenAmountA - Amount of token A
93
+ * @param tokenAmountB - Amount of token B
94
+ * @returns The calculated invariant D value
95
+ *
96
+ * Corresponds to `compute_d` function in Rust implementation.
97
+ *
98
+ */
99
+ function computeD(curve, tokenAmountA, tokenAmountB) {
100
+ if ("cosntantProduct" in curve) {
101
+ return (0, bn_sqrt_1.default)(tokenAmountA.mul(tokenAmountB));
102
+ }
103
+ else if ("stable" in curve) {
104
+ const PRECISION = new anchor_1.BN(1_000_000);
105
+ const { amp, tokenMultiplier, depeg } = curve.stable;
106
+ const upscaledTokenA = tokenMultiplier.tokenAMultiplier.mul(tokenAmountA);
107
+ const upscaledTokenB = tokenMultiplier.tokenBMultiplier.mul(tokenAmountB);
108
+ let invariantD = (0, exports.computeStableSwapInvariant)(amp, upscaledTokenA, upscaledTokenB);
109
+ if (!depeg.depegType["none"]) {
110
+ return invariantD.div(PRECISION);
111
+ }
112
+ return invariantD;
113
+ }
114
+ }
115
+ exports.computeD = computeD;
116
+ //# sourceMappingURL=meteora.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"meteora.js","sourceRoot":"","sources":["../../src/utils/meteora.ts"],"names":[],"mappings":";;;;;;AACA,8CAAsC;AACtC,sDAA0B;AAE1B;;;;;;;GAOG;AACH,SAAgB,uBAAuB,CAAC,WAAmB,EAAE,UAAsB;IACjF,MAAM,EACJ,mBAAmB,EAAE,EAAE,UAAU,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,EACrF,WAAW,EAAE,gBAAgB,GAC9B,GAAG,UAAU,CAAA;IACd,MAAM,kCAAkC,GAAG,IAAI,WAAE,CAAC,iBAAiB,CAAC,CAAA;IAEpE,MAAM,QAAQ,GAAG,IAAI,WAAE,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;IAE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,KAAK,EAAE,CAAC;QAC/B,OAAO,IAAA,iBAAI,EAAC,YAAY,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,CAAA;IAC7C,CAAC;SAAM,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC;QAC7B,MAAM,SAAS,GAAG,IAAI,WAAE,CAAC,SAAS,CAAC,CAAA;QACnC,MAAM,EAAE,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,MAAM,CAAA;QAEpD,MAAM,cAAc,GAAG,eAAe,CAAC,gBAAgB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;QACzE,MAAM,cAAc,GAAG,eAAe,CAAC,gBAAgB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAA;QAEzE,IAAI,UAAU,GAAG,IAAA,kCAA0B,EAAC,GAAG,EAAE,cAAc,EAAE,cAAc,CAAC,CAAA;QAEhF,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;YAC7B,OAAO,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;QAClC,CAAC;QAED,OAAO,UAAU,CAAA;IACnB,CAAC;AACH,CAAC;AAlBD,4BAkBC"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@exponent-labs/exponent-fetcher",
3
+ "version": "0.0.3",
4
+ "main": "build/index.js",
5
+ "types": "build/index.d.ts",
6
+ "license": "AGPL-3.0",
7
+ "scripts": {
8
+ "build": "tsc --build"
9
+ },
10
+ "dependencies": {
11
+ "@coral-xyz/anchor": "0.30.0",
12
+ "@exponent-labs/exponent-idl": "^0.0.3",
13
+ "@exponent-labs/exponent-pda": "^0.0.3",
14
+ "@exponent-labs/exponent-types": "^0.0.3",
15
+ "@exponent-labs/fragmetric-idl": "^0.0.3",
16
+ "@exponent-labs/generic-sy-idl": "^0.0.3",
17
+ "@exponent-labs/generic-sy-pda": "^0.0.3",
18
+ "@exponent-labs/jito-restaking-sy-idl": "^0.0.3",
19
+ "@exponent-labs/kamino-reserve-deserializer": "^0.0.3",
20
+ "@exponent-labs/marginfi-sy-idl": "^0.0.3",
21
+ "@exponent-labs/meteora-idl": "^0.0.3",
22
+ "@exponent-labs/perena-sy-idl": "^0.0.3",
23
+ "@exponent-labs/precise-number": "^0.0.3",
24
+ "@exponent-labs/rust-decimal": "^0.0.3",
25
+ "@solana/spl-stake-pool": "1.1.8",
26
+ "@solana/spl-token": "0.4.6",
27
+ "bn-sqrt": "^1.0.0",
28
+ "decimal.js": "^10.4.3"
29
+ },
30
+ "devDependencies": {
31
+ "@types/bn.js": "^5.1.0",
32
+ "typescript": "5.4.5"
33
+ },
34
+ "gitHead": "209b8847e9a0fadb5b5ec96b9b47f0ace4a3bf9d"
35
+ }