@liquid-af/sdk 0.1.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/README.md +776 -0
- package/dist/accounts/index.d.ts +5 -0
- package/dist/accounts/index.d.ts.map +1 -0
- package/dist/accounts/index.js +5 -0
- package/dist/accounts/index.js.map +1 -0
- package/dist/accounts/liquid-fees.d.ts +63 -0
- package/dist/accounts/liquid-fees.d.ts.map +1 -0
- package/dist/accounts/liquid-fees.js +27 -0
- package/dist/accounts/liquid-fees.js.map +1 -0
- package/dist/accounts/liquid-state.d.ts +112 -0
- package/dist/accounts/liquid-state.d.ts.map +1 -0
- package/dist/accounts/liquid-state.js +92 -0
- package/dist/accounts/liquid-state.js.map +1 -0
- package/dist/accounts/liquid-swap.d.ts +97 -0
- package/dist/accounts/liquid-swap.d.ts.map +1 -0
- package/dist/accounts/liquid-swap.js +54 -0
- package/dist/accounts/liquid-swap.js.map +1 -0
- package/dist/accounts/liquid.d.ts +175 -0
- package/dist/accounts/liquid.d.ts.map +1 -0
- package/dist/accounts/liquid.js +66 -0
- package/dist/accounts/liquid.js.map +1 -0
- package/dist/client.d.ts +621 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +511 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +56 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +44 -0
- package/dist/config.js.map +1 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +63 -0
- package/dist/errors.js.map +1 -0
- package/dist/events/index.d.ts +4 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +2 -0
- package/dist/events/index.js.map +1 -0
- package/dist/events/parser.d.ts +40 -0
- package/dist/events/parser.d.ts.map +1 -0
- package/dist/events/parser.js +67 -0
- package/dist/events/parser.js.map +1 -0
- package/dist/events/types.d.ts +286 -0
- package/dist/events/types.d.ts.map +1 -0
- package/dist/events/types.js +2 -0
- package/dist/events/types.js.map +1 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/index.js +3 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/helpers/preview.d.ts +259 -0
- package/dist/helpers/preview.d.ts.map +1 -0
- package/dist/helpers/preview.js +458 -0
- package/dist/helpers/preview.js.map +1 -0
- package/dist/helpers/user.d.ts +11 -0
- package/dist/helpers/user.d.ts.map +1 -0
- package/dist/helpers/user.js +20 -0
- package/dist/helpers/user.js.map +1 -0
- package/dist/idl/index.d.ts +53 -0
- package/dist/idl/index.d.ts.map +1 -0
- package/dist/idl/index.js +64 -0
- package/dist/idl/index.js.map +1 -0
- package/dist/idl/liquid.d.ts +10523 -0
- package/dist/idl/liquid.d.ts.map +1 -0
- package/dist/idl/liquid.js +2 -0
- package/dist/idl/liquid.js.map +1 -0
- package/dist/idl/liquid.json +10516 -0
- package/dist/idl/liquid_fees.d.ts +1520 -0
- package/dist/idl/liquid_fees.d.ts.map +1 -0
- package/dist/idl/liquid_fees.js +2 -0
- package/dist/idl/liquid_fees.js.map +1 -0
- package/dist/idl/liquid_fees.json +1513 -0
- package/dist/idl/liquid_state.d.ts +2936 -0
- package/dist/idl/liquid_state.d.ts.map +1 -0
- package/dist/idl/liquid_state.js +2 -0
- package/dist/idl/liquid_state.js.map +1 -0
- package/dist/idl/liquid_state.json +2929 -0
- package/dist/idl/liquid_swap.d.ts +5849 -0
- package/dist/idl/liquid_swap.d.ts.map +1 -0
- package/dist/idl/liquid_swap.js +2 -0
- package/dist/idl/liquid_swap.js.map +1 -0
- package/dist/idl/liquid_swap.json +5842 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/instructions/index.d.ts +9 -0
- package/dist/instructions/index.d.ts.map +1 -0
- package/dist/instructions/index.js +9 -0
- package/dist/instructions/index.js.map +1 -0
- package/dist/instructions/liquid-fees.d.ts +111 -0
- package/dist/instructions/liquid-fees.d.ts.map +1 -0
- package/dist/instructions/liquid-fees.js +169 -0
- package/dist/instructions/liquid-fees.js.map +1 -0
- package/dist/instructions/liquid-state.d.ts +173 -0
- package/dist/instructions/liquid-state.d.ts.map +1 -0
- package/dist/instructions/liquid-state.js +194 -0
- package/dist/instructions/liquid-state.js.map +1 -0
- package/dist/instructions/liquid-swap.d.ts +122 -0
- package/dist/instructions/liquid-swap.d.ts.map +1 -0
- package/dist/instructions/liquid-swap.js +167 -0
- package/dist/instructions/liquid-swap.js.map +1 -0
- package/dist/instructions/liquid.d.ts +297 -0
- package/dist/instructions/liquid.d.ts.map +1 -0
- package/dist/instructions/liquid.js +483 -0
- package/dist/instructions/liquid.js.map +1 -0
- package/dist/instructions/program-cache.d.ts +35 -0
- package/dist/instructions/program-cache.d.ts.map +1 -0
- package/dist/instructions/program-cache.js +68 -0
- package/dist/instructions/program-cache.js.map +1 -0
- package/dist/math/amm.d.ts +42 -0
- package/dist/math/amm.d.ts.map +1 -0
- package/dist/math/amm.js +109 -0
- package/dist/math/amm.js.map +1 -0
- package/dist/math/bonding-curve.d.ts +34 -0
- package/dist/math/bonding-curve.d.ts.map +1 -0
- package/dist/math/bonding-curve.js +80 -0
- package/dist/math/bonding-curve.js.map +1 -0
- package/dist/math/constants.d.ts +14 -0
- package/dist/math/constants.d.ts.map +1 -0
- package/dist/math/constants.js +14 -0
- package/dist/math/constants.js.map +1 -0
- package/dist/math/fees.d.ts +88 -0
- package/dist/math/fees.d.ts.map +1 -0
- package/dist/math/fees.js +135 -0
- package/dist/math/fees.js.map +1 -0
- package/dist/math/index.d.ts +8 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/index.js +6 -0
- package/dist/math/index.js.map +1 -0
- package/dist/math/tiered-fees.d.ts +80 -0
- package/dist/math/tiered-fees.d.ts.map +1 -0
- package/dist/math/tiered-fees.js +129 -0
- package/dist/math/tiered-fees.js.map +1 -0
- package/dist/oracle.d.ts +53 -0
- package/dist/oracle.d.ts.map +1 -0
- package/dist/oracle.js +70 -0
- package/dist/oracle.js.map +1 -0
- package/dist/pda/index.d.ts +89 -0
- package/dist/pda/index.d.ts.map +1 -0
- package/dist/pda/index.js +127 -0
- package/dist/pda/index.js.map +1 -0
- package/dist/pda/liquid-fees.d.ts +27 -0
- package/dist/pda/liquid-fees.d.ts.map +1 -0
- package/dist/pda/liquid-fees.js +36 -0
- package/dist/pda/liquid-fees.js.map +1 -0
- package/dist/pda/liquid-state.d.ts +56 -0
- package/dist/pda/liquid-state.d.ts.map +1 -0
- package/dist/pda/liquid-state.js +79 -0
- package/dist/pda/liquid-state.js.map +1 -0
- package/dist/pda/liquid-swap.d.ts +76 -0
- package/dist/pda/liquid-swap.d.ts.map +1 -0
- package/dist/pda/liquid-swap.js +103 -0
- package/dist/pda/liquid-swap.js.map +1 -0
- package/dist/pda/liquid.d.ts +67 -0
- package/dist/pda/liquid.d.ts.map +1 -0
- package/dist/pda/liquid.js +91 -0
- package/dist/pda/liquid.js.map +1 -0
- package/dist/provider.d.ts +26 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +47 -0
- package/dist/provider.js.map +1 -0
- package/dist/transaction/builder.d.ts +30 -0
- package/dist/transaction/builder.d.ts.map +1 -0
- package/dist/transaction/builder.js +48 -0
- package/dist/transaction/builder.js.map +1 -0
- package/dist/transaction/index.d.ts +3 -0
- package/dist/transaction/index.d.ts.map +1 -0
- package/dist/transaction/index.js +3 -0
- package/dist/transaction/index.js.map +1 -0
- package/dist/transaction/send.d.ts +25 -0
- package/dist/transaction/send.d.ts.map +1 -0
- package/dist/transaction/send.js +52 -0
- package/dist/transaction/send.js.map +1 -0
- package/dist/types.d.ts +311 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +46 -0
- package/dist/types.js.map +1 -0
- package/package.json +112 -0
- package/src/accounts/index.ts +26 -0
- package/src/accounts/liquid-fees.ts +38 -0
- package/src/accounts/liquid-state.ts +134 -0
- package/src/accounts/liquid-swap.ts +79 -0
- package/src/accounts/liquid.ts +100 -0
- package/src/client.ts +1001 -0
- package/src/config.ts +91 -0
- package/src/errors.ts +94 -0
- package/src/events/index.ts +42 -0
- package/src/events/parser.ts +90 -0
- package/src/events/types.ts +310 -0
- package/src/helpers/index.ts +23 -0
- package/src/helpers/preview.ts +798 -0
- package/src/helpers/user.ts +24 -0
- package/src/idl/index.ts +94 -0
- package/src/idl/liquid.json +10516 -0
- package/src/idl/liquid.ts +10522 -0
- package/src/idl/liquid_fees.json +1513 -0
- package/src/idl/liquid_fees.ts +1519 -0
- package/src/idl/liquid_state.json +2929 -0
- package/src/idl/liquid_state.ts +2935 -0
- package/src/idl/liquid_swap.json +5842 -0
- package/src/idl/liquid_swap.ts +5848 -0
- package/src/index.ts +98 -0
- package/src/instructions/index.ts +109 -0
- package/src/instructions/liquid-fees.ts +289 -0
- package/src/instructions/liquid-state.ts +336 -0
- package/src/instructions/liquid-swap.ts +414 -0
- package/src/instructions/liquid.ts +884 -0
- package/src/instructions/program-cache.ts +106 -0
- package/src/math/amm.ts +146 -0
- package/src/math/bonding-curve.ts +122 -0
- package/src/math/constants.ts +19 -0
- package/src/math/fees.ts +191 -0
- package/src/math/index.ts +40 -0
- package/src/math/tiered-fees.ts +165 -0
- package/src/oracle.ts +97 -0
- package/src/pda/index.ts +331 -0
- package/src/pda/liquid-fees.ts +58 -0
- package/src/pda/liquid-state.ts +123 -0
- package/src/pda/liquid-swap.ts +162 -0
- package/src/pda/liquid.ts +152 -0
- package/src/provider.ts +60 -0
- package/src/transaction/builder.ts +80 -0
- package/src/transaction/index.ts +6 -0
- package/src/transaction/send.ts +72 -0
- package/src/types.ts +354 -0
package/dist/math/amm.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import { FEE_RATE_DENOMINATOR } from "./constants.js";
|
|
3
|
+
/**
|
|
4
|
+
* Calculates expected output for selling base tokens on the AMM.
|
|
5
|
+
* Uses constant product formula: quoteOut = (amountIn * quoteVault) / (baseVault + amountIn)
|
|
6
|
+
*
|
|
7
|
+
* @param amountIn - Base tokens to sell
|
|
8
|
+
* @param baseVault - Current base vault balance
|
|
9
|
+
* @param quoteVault - Current quote vault balance
|
|
10
|
+
* @param fees - Fee rates in basis points
|
|
11
|
+
* @returns Sell output with gross/net quote amounts and fee breakdown
|
|
12
|
+
*/
|
|
13
|
+
export const calculateAmmSellOutput = (amountIn, baseVault, quoteVault, fees) => {
|
|
14
|
+
// Gross output: (amountIn * quoteVault) / (baseVault + amountIn)
|
|
15
|
+
const grossQuoteOut = amountIn.mul(quoteVault).div(baseVault.add(amountIn));
|
|
16
|
+
const denom = new BN(FEE_RATE_DENOMINATOR);
|
|
17
|
+
const lpFee = grossQuoteOut.mul(new BN(fees.lpFeeRate)).div(denom);
|
|
18
|
+
const creatorFee = grossQuoteOut
|
|
19
|
+
.mul(new BN(fees.creatorFeeRate))
|
|
20
|
+
.div(denom);
|
|
21
|
+
const protocolFee = grossQuoteOut
|
|
22
|
+
.mul(new BN(fees.protocolFeeRate))
|
|
23
|
+
.div(denom);
|
|
24
|
+
const totalFees = lpFee.add(creatorFee).add(protocolFee);
|
|
25
|
+
const quoteOutNet = grossQuoteOut.sub(totalFees);
|
|
26
|
+
return {
|
|
27
|
+
quoteOut: grossQuoteOut,
|
|
28
|
+
quoteOutNet,
|
|
29
|
+
lpFee,
|
|
30
|
+
creatorFee,
|
|
31
|
+
protocolFee,
|
|
32
|
+
totalFees,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Calculates required quote input for buying base tokens on the AMM.
|
|
37
|
+
* Reverse calculation: how much quote is needed for the desired base output.
|
|
38
|
+
*
|
|
39
|
+
* @param amountOut - Base tokens to buy
|
|
40
|
+
* @param baseVault - Current base vault balance
|
|
41
|
+
* @param quoteVault - Current quote vault balance
|
|
42
|
+
* @param fees - Fee rates in basis points
|
|
43
|
+
* @returns Buy input with gross/net quote amounts and fee breakdown
|
|
44
|
+
*/
|
|
45
|
+
export const calculateAmmBuyInput = (amountOut, baseVault, quoteVault, fees) => {
|
|
46
|
+
const denom = new BN(FEE_RATE_DENOMINATOR);
|
|
47
|
+
// Quote needed for swap (without fees), ceiling division
|
|
48
|
+
const quoteForSwap = quoteVault
|
|
49
|
+
.mul(amountOut)
|
|
50
|
+
.div(baseVault.sub(amountOut))
|
|
51
|
+
.add(new BN(1));
|
|
52
|
+
// Total fee rate
|
|
53
|
+
const totalFeeRate = fees.lpFeeRate + fees.creatorFeeRate + fees.protocolFeeRate;
|
|
54
|
+
// Gross quote needed (including fees), ceiling division
|
|
55
|
+
const quoteInGross = quoteForSwap
|
|
56
|
+
.mul(denom)
|
|
57
|
+
.div(new BN(FEE_RATE_DENOMINATOR - totalFeeRate))
|
|
58
|
+
.add(new BN(1));
|
|
59
|
+
// Calculate individual fees
|
|
60
|
+
const lpFee = quoteInGross.mul(new BN(fees.lpFeeRate)).div(denom);
|
|
61
|
+
const creatorFee = quoteInGross.mul(new BN(fees.creatorFeeRate)).div(denom);
|
|
62
|
+
const protocolFee = quoteInGross
|
|
63
|
+
.mul(new BN(fees.protocolFeeRate))
|
|
64
|
+
.div(denom);
|
|
65
|
+
const totalFees = lpFee.add(creatorFee).add(protocolFee);
|
|
66
|
+
const quoteIn = quoteInGross.sub(totalFees);
|
|
67
|
+
return {
|
|
68
|
+
quoteIn,
|
|
69
|
+
quoteInGross,
|
|
70
|
+
lpFee,
|
|
71
|
+
creatorFee,
|
|
72
|
+
protocolFee,
|
|
73
|
+
totalFees,
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Converts LP tokens to the equivalent base and quote token amounts.
|
|
78
|
+
*
|
|
79
|
+
* @param lpAmount - LP tokens to convert
|
|
80
|
+
* @param lpSupply - Total LP supply
|
|
81
|
+
* @param baseVault - Base vault balance
|
|
82
|
+
* @param quoteVault - Quote vault balance
|
|
83
|
+
* @param roundUp - Use ceiling division (for deposits) vs floor division (for withdrawals)
|
|
84
|
+
* @returns Equivalent base and quote token amounts for the given LP tokens
|
|
85
|
+
*/
|
|
86
|
+
export const calculateLpToTokens = (lpAmount, lpSupply, baseVault, quoteVault, roundUp = false) => {
|
|
87
|
+
let baseAmount;
|
|
88
|
+
let quoteAmount;
|
|
89
|
+
if (roundUp) {
|
|
90
|
+
// Ceiling division for deposits (require slightly more from user)
|
|
91
|
+
baseAmount = lpAmount
|
|
92
|
+
.mul(baseVault)
|
|
93
|
+
.add(lpSupply)
|
|
94
|
+
.sub(new BN(1))
|
|
95
|
+
.div(lpSupply);
|
|
96
|
+
quoteAmount = lpAmount
|
|
97
|
+
.mul(quoteVault)
|
|
98
|
+
.add(lpSupply)
|
|
99
|
+
.sub(new BN(1))
|
|
100
|
+
.div(lpSupply);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// Floor division for withdrawals (give slightly less to user)
|
|
104
|
+
baseAmount = lpAmount.mul(baseVault).div(lpSupply);
|
|
105
|
+
quoteAmount = lpAmount.mul(quoteVault).div(lpSupply);
|
|
106
|
+
}
|
|
107
|
+
return { baseAmount, quoteAmount };
|
|
108
|
+
};
|
|
109
|
+
//# sourceMappingURL=amm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"amm.js","sourceRoot":"","sources":["../../src/math/amm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAUtD;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACrC,QAAY,EACZ,SAAa,EACb,UAAc,EACd,IAAiB,EACD,EAAE;IAClB,iEAAiE;IACjE,MAAM,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;IAE5E,MAAM,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AC,CAAC;aACd,GAAG,CAAC,QAAQ,CAAC,CAAC;QAChB,WAAW,GAAG,QAAQ;aACpB,GAAG,CAAC,UAAU,CAAC;aACf,GAAG,CAAC,QAAQ,CAAC;aACb,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;aACd,GAAG,CAAC,QAAQ,CAAC,CAAC;IACjB,CAAC;SAAM,CAAC;QACP,8DAA8D;QAC9D,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACnD,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACtD,CAAC;IAED,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC;AACpC,CAAC,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import type { BondingCurveBuyResult, BondingCurveSellResult } from "../types.js";
|
|
3
|
+
import { type FeeConfig } from "./fees.js";
|
|
4
|
+
/**
|
|
5
|
+
* Calculates expected output for a buy transaction on the bonding curve.
|
|
6
|
+
* Uses constant product formula: k = virtualQuote * virtualToken
|
|
7
|
+
*
|
|
8
|
+
* @param amountInGross - Total quote amount the user is spending (before fees)
|
|
9
|
+
* @param curveState - Current curve reserves
|
|
10
|
+
* @param config - Fee configuration
|
|
11
|
+
* @param hasCreatorRef - Whether the creator has a referrer
|
|
12
|
+
* @param hasTraderRef - Whether the trader has a referrer
|
|
13
|
+
* @returns Buy result with tokens out, new reserves, net amount, and fee breakdown
|
|
14
|
+
*/
|
|
15
|
+
export declare const calculateBuyExpectation: (amountInGross: BN, curveState: {
|
|
16
|
+
virtualQuoteReserves: BN;
|
|
17
|
+
virtualTokenReserves: BN;
|
|
18
|
+
}, config: FeeConfig, hasCreatorRef: boolean, hasTraderRef: boolean) => BondingCurveBuyResult;
|
|
19
|
+
/**
|
|
20
|
+
* Calculates expected output for a sell transaction on the bonding curve.
|
|
21
|
+
* Uses constant product formula: k = virtualQuote * virtualToken
|
|
22
|
+
*
|
|
23
|
+
* @param amountInTokens - Amount of tokens to sell
|
|
24
|
+
* @param curveState - Current curve reserves
|
|
25
|
+
* @param config - Fee configuration
|
|
26
|
+
* @param hasCreatorRef - Whether the creator has a referrer
|
|
27
|
+
* @param hasTraderRef - Whether the trader has a referrer
|
|
28
|
+
* @returns Sell result with quote out (gross/net), new reserves, and fee breakdown
|
|
29
|
+
*/
|
|
30
|
+
export declare const calculateSellExpectation: (amountInTokens: BN, curveState: {
|
|
31
|
+
virtualQuoteReserves: BN;
|
|
32
|
+
virtualTokenReserves: BN;
|
|
33
|
+
}, config: FeeConfig, hasCreatorRef: boolean, hasTraderRef: boolean) => BondingCurveSellResult;
|
|
34
|
+
//# sourceMappingURL=bonding-curve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bonding-curve.d.ts","sourceRoot":"","sources":["../../src/math/bonding-curve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,KAAK,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACjF,OAAO,EAAiB,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAE1D;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GACnC,eAAe,EAAE,EACjB,YAAY;IAAE,oBAAoB,EAAE,EAAE,CAAC;IAAC,oBAAoB,EAAE,EAAE,CAAA;CAAE,EAClE,QAAQ,SAAS,EACjB,eAAe,OAAO,EACtB,cAAc,OAAO,KACnB,qBAwCF,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,wBAAwB,GACpC,gBAAgB,EAAE,EAClB,YAAY;IAAE,oBAAoB,EAAE,EAAE,CAAC;IAAC,oBAAoB,EAAE,EAAE,CAAA;CAAE,EAClE,QAAQ,SAAS,EACjB,eAAe,OAAO,EACtB,cAAc,OAAO,KACnB,sBAyCF,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import { calculateFees } from "./fees.js";
|
|
3
|
+
/**
|
|
4
|
+
* Calculates expected output for a buy transaction on the bonding curve.
|
|
5
|
+
* Uses constant product formula: k = virtualQuote * virtualToken
|
|
6
|
+
*
|
|
7
|
+
* @param amountInGross - Total quote amount the user is spending (before fees)
|
|
8
|
+
* @param curveState - Current curve reserves
|
|
9
|
+
* @param config - Fee configuration
|
|
10
|
+
* @param hasCreatorRef - Whether the creator has a referrer
|
|
11
|
+
* @param hasTraderRef - Whether the trader has a referrer
|
|
12
|
+
* @returns Buy result with tokens out, new reserves, net amount, and fee breakdown
|
|
13
|
+
*/
|
|
14
|
+
export const calculateBuyExpectation = (amountInGross, curveState, config, hasCreatorRef, hasTraderRef) => {
|
|
15
|
+
const fees = calculateFees(amountInGross, config, hasCreatorRef, hasTraderRef);
|
|
16
|
+
// Net quote amount that goes into the curve after fees
|
|
17
|
+
const amountInNet = amountInGross.sub(fees.totalFees);
|
|
18
|
+
// Constant product: k = x * y
|
|
19
|
+
const k = curveState.virtualQuoteReserves.mul(curveState.virtualTokenReserves);
|
|
20
|
+
// New quote reserves after adding net amount
|
|
21
|
+
const newVirtualQuote = curveState.virtualQuoteReserves.add(amountInNet);
|
|
22
|
+
// Solve for new token reserves: newToken = k / newQuote + 1 (ceiling division)
|
|
23
|
+
const newVirtualToken = k.div(newVirtualQuote).add(new BN(1));
|
|
24
|
+
// Tokens out = difference in token reserves
|
|
25
|
+
const tokensOut = curveState.virtualTokenReserves.sub(newVirtualToken);
|
|
26
|
+
// On-chain, protocol fee is only reduced by referral amounts that are
|
|
27
|
+
// actually transferred. These helpers assume referrals succeed, so
|
|
28
|
+
// subtract them to match the expected on-chain protocol fee.
|
|
29
|
+
const effectiveProtocolFee = fees.protocolFee
|
|
30
|
+
.sub(fees.creatorReferralFee)
|
|
31
|
+
.sub(fees.traderReferralFee);
|
|
32
|
+
return {
|
|
33
|
+
...fees,
|
|
34
|
+
protocolFee: effectiveProtocolFee,
|
|
35
|
+
tokensOut,
|
|
36
|
+
newVirtualQuote,
|
|
37
|
+
newVirtualToken,
|
|
38
|
+
amountInNet,
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Calculates expected output for a sell transaction on the bonding curve.
|
|
43
|
+
* Uses constant product formula: k = virtualQuote * virtualToken
|
|
44
|
+
*
|
|
45
|
+
* @param amountInTokens - Amount of tokens to sell
|
|
46
|
+
* @param curveState - Current curve reserves
|
|
47
|
+
* @param config - Fee configuration
|
|
48
|
+
* @param hasCreatorRef - Whether the creator has a referrer
|
|
49
|
+
* @param hasTraderRef - Whether the trader has a referrer
|
|
50
|
+
* @returns Sell result with quote out (gross/net), new reserves, and fee breakdown
|
|
51
|
+
*/
|
|
52
|
+
export const calculateSellExpectation = (amountInTokens, curveState, config, hasCreatorRef, hasTraderRef) => {
|
|
53
|
+
// Constant product: k = x * y
|
|
54
|
+
const k = curveState.virtualQuoteReserves.mul(curveState.virtualTokenReserves);
|
|
55
|
+
// New token reserves after adding sold tokens
|
|
56
|
+
const newVirtualToken = curveState.virtualTokenReserves.add(amountInTokens);
|
|
57
|
+
// Solve for new quote reserves: newQuote = k / newToken + 1 (ceiling division)
|
|
58
|
+
const newVirtualQuote = k.div(newVirtualToken).add(new BN(1));
|
|
59
|
+
// Gross quote out (before fees)
|
|
60
|
+
const quoteOutGross = curveState.virtualQuoteReserves.sub(newVirtualQuote);
|
|
61
|
+
// Calculate fees on gross amount
|
|
62
|
+
const fees = calculateFees(quoteOutGross, config, hasCreatorRef, hasTraderRef);
|
|
63
|
+
// Net quote amount user receives
|
|
64
|
+
const quoteOutNet = quoteOutGross.sub(fees.totalFees);
|
|
65
|
+
// On-chain, protocol fee is only reduced by referral amounts that are
|
|
66
|
+
// actually transferred. These helpers assume referrals succeed, so
|
|
67
|
+
// subtract them to match the expected on-chain protocol fee.
|
|
68
|
+
const effectiveProtocolFee = fees.protocolFee
|
|
69
|
+
.sub(fees.creatorReferralFee)
|
|
70
|
+
.sub(fees.traderReferralFee);
|
|
71
|
+
return {
|
|
72
|
+
...fees,
|
|
73
|
+
protocolFee: effectiveProtocolFee,
|
|
74
|
+
quoteOutGross,
|
|
75
|
+
quoteOutNet,
|
|
76
|
+
newVirtualQuote,
|
|
77
|
+
newVirtualToken,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=bonding-curve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bonding-curve.js","sourceRoot":"","sources":["../../src/math/bonding-curve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,OAAO,EAAE,aAAa,EAAkB,MAAM,WAAW,CAAC;AAE1D;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACtC,aAAiB,EACjB,UAAkE,EAClE,MAAiB,EACjB,aAAsB,EACtB,YAAqB,EACG,EAAE;IAC1B,MAAM,IAAI,GAAG,aAAa,CACzB,aAAa,EACb,MAAM,EACN,aAAa,EACb,YAAY,CACZ,CAAC;IAEF,uDAAuD;IACvD,MAAM,WAAW,GAAG,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAEtD,8BAA8B;IAC9B,MAAM,CAAC,GAAG,UAAU,CAAC,oBAAoB,CAAC,GAAG,CAC5C,UAAU,CAAC,oBAAoB,CAC/B,CAAC;IAEF,6CAA6C;IAC7C,MAAM,eAAe,GAAG,UAAU,CAAC,oBAAoB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAEzE,+EAA+E;IAC/E,MAAM,eAAe,GAAG,CAAC,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAE9D,4CAA4C;IAC5C,MAAM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,IAAI,CAAC,WAAW;SAC3C,GAAG,CAAC,IAAI,CAAC,kBAAkB,CAAC;SAC5B,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;IAE9B,OAAO;QACN,GAAG,IAAI;QACP,WAAW,EAAE,oBAAoB;QACjC,aAAa;QACb,WAAW;QACX,eAAe;QACf,eAAe;KACf,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
/** Basis points denominator (10,000 = 100%) */
|
|
3
|
+
export declare const BPS_DENOMINATOR = 10000;
|
|
4
|
+
/** Fee rate denominator for AMM (same as BPS) */
|
|
5
|
+
export declare const FEE_RATE_DENOMINATOR = 10000;
|
|
6
|
+
/** Q32 constant for fixed-point oracle math (2^32) */
|
|
7
|
+
export declare const Q32: BN;
|
|
8
|
+
/** Number of oracle observations stored */
|
|
9
|
+
export declare const OBSERVATION_NUM = 100;
|
|
10
|
+
/** Default oracle update duration in seconds */
|
|
11
|
+
export declare const OBSERVATION_UPDATE_DURATION_DEFAULT = 15;
|
|
12
|
+
/** Minimum rent-exempt balance for a 0-byte account */
|
|
13
|
+
export declare const MIN_VAULT_BALANCE = 890880;
|
|
14
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/math/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,+CAA+C;AAC/C,eAAO,MAAM,eAAe,QAAS,CAAC;AAEtC,iDAAiD;AACjD,eAAO,MAAM,oBAAoB,QAAS,CAAC;AAE3C,sDAAsD;AACtD,eAAO,MAAM,GAAG,IAAqB,CAAC;AAEtC,2CAA2C;AAC3C,eAAO,MAAM,eAAe,MAAM,CAAC;AAEnC,gDAAgD;AAChD,eAAO,MAAM,mCAAmC,KAAK,CAAC;AAEtD,uDAAuD;AACvD,eAAO,MAAM,iBAAiB,SAAU,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
/** Basis points denominator (10,000 = 100%) */
|
|
3
|
+
export const BPS_DENOMINATOR = 10000;
|
|
4
|
+
/** Fee rate denominator for AMM (same as BPS) */
|
|
5
|
+
export const FEE_RATE_DENOMINATOR = 10000;
|
|
6
|
+
/** Q32 constant for fixed-point oracle math (2^32) */
|
|
7
|
+
export const Q32 = new BN(1).shln(32);
|
|
8
|
+
/** Number of oracle observations stored */
|
|
9
|
+
export const OBSERVATION_NUM = 100;
|
|
10
|
+
/** Default oracle update duration in seconds */
|
|
11
|
+
export const OBSERVATION_UPDATE_DURATION_DEFAULT = 15;
|
|
12
|
+
/** Minimum rent-exempt balance for a 0-byte account */
|
|
13
|
+
export const MIN_VAULT_BALANCE = 890880;
|
|
14
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/math/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,+CAA+C;AAC/C,MAAM,CAAC,MAAM,eAAe,GAAG,KAAM,CAAC;AAEtC,iDAAiD;AACjD,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAM,CAAC;AAE3C,sDAAsD;AACtD,MAAM,CAAC,MAAM,GAAG,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAEtC,2CAA2C;AAC3C,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC;AAEnC,gDAAgD;AAChD,MAAM,CAAC,MAAM,mCAAmC,GAAG,EAAE,CAAC;AAEtD,uDAAuD;AACvD,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAO,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import type { PublicKey } from "@solana/web3.js";
|
|
3
|
+
import type { FeeDistribution, FeeRecipient, DistributionResult } from "../types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Calculates basis points of an amount.
|
|
6
|
+
* result = (amount * bps) / 10000
|
|
7
|
+
*
|
|
8
|
+
* @param amount - The amount to calculate basis points of
|
|
9
|
+
* @param bps - Basis points (1 bps = 0.01%)
|
|
10
|
+
* @returns The calculated portion
|
|
11
|
+
*/
|
|
12
|
+
export declare const calcBps: (amount: BN, bps: number) => BN;
|
|
13
|
+
/**
|
|
14
|
+
* Fee configuration for bonding curve trades.
|
|
15
|
+
*/
|
|
16
|
+
export interface FeeConfig {
|
|
17
|
+
protocolFeeBps: number;
|
|
18
|
+
creatorFeeBps: number;
|
|
19
|
+
creatorReferralBps: number;
|
|
20
|
+
traderReferralBps: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Calculates fee distribution for a given trade amount.
|
|
24
|
+
* Referral fees are carved from the protocol fee, not the trade amount.
|
|
25
|
+
*
|
|
26
|
+
* @param amount - Gross trade amount (before fees)
|
|
27
|
+
* @param config - Fee configuration in basis points
|
|
28
|
+
* @param hasCreatorRef - Whether the creator has a referrer
|
|
29
|
+
* @param hasTraderRef - Whether the trader has a referrer
|
|
30
|
+
* @returns Fee distribution breakdown
|
|
31
|
+
*/
|
|
32
|
+
export declare const calculateFees: (amount: BN, config: FeeConfig, hasCreatorRef: boolean, hasTraderRef: boolean) => FeeDistribution;
|
|
33
|
+
/**
|
|
34
|
+
* Calculates expected distribution amounts for each fee recipient.
|
|
35
|
+
* Distributable = vaultBalance - MIN_VAULT_BALANCE (rent).
|
|
36
|
+
*
|
|
37
|
+
* @param vaultBalance - Current vault balance in lamports
|
|
38
|
+
* @param recipients - Fee recipients with basis point allocations
|
|
39
|
+
* @returns Distribution amounts per recipient
|
|
40
|
+
*/
|
|
41
|
+
export declare const calculateDistribution: (vaultBalance: number, recipients: FeeRecipient[]) => DistributionResult[];
|
|
42
|
+
/**
|
|
43
|
+
* Calculates total distribution (sum of all recipient amounts).
|
|
44
|
+
*
|
|
45
|
+
* @param vaultBalance - Current vault balance in lamports
|
|
46
|
+
* @param recipients - Fee recipients with basis point allocations
|
|
47
|
+
* @returns Total amount distributed across all recipients
|
|
48
|
+
*/
|
|
49
|
+
export declare const calculateTotalDistribution: (vaultBalance: number, recipients: FeeRecipient[]) => number;
|
|
50
|
+
/**
|
|
51
|
+
* Calculates dust remaining after distribution (due to integer rounding).
|
|
52
|
+
*
|
|
53
|
+
* @param vaultBalance - Current vault balance in lamports
|
|
54
|
+
* @param recipients - Fee recipients with basis point allocations
|
|
55
|
+
* @returns Undistributed lamports remaining in the vault
|
|
56
|
+
*/
|
|
57
|
+
export declare const calculateDistributionDust: (vaultBalance: number, recipients: FeeRecipient[]) => number;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a recipients array from pubkeys and basis points.
|
|
60
|
+
*
|
|
61
|
+
* @param pubkeys - Recipient public keys
|
|
62
|
+
* @param bpsArray - Basis point allocations (must match pubkeys length)
|
|
63
|
+
* @returns Array of fee recipients
|
|
64
|
+
*/
|
|
65
|
+
export declare const createRecipients: (pubkeys: PublicKey[], bpsArray: number[]) => FeeRecipient[];
|
|
66
|
+
/**
|
|
67
|
+
* Creates equal-share recipients (auto-calculates BPS to sum to 10,000).
|
|
68
|
+
*
|
|
69
|
+
* @param count - Number of recipients
|
|
70
|
+
* @param pubkeys - Recipient public keys (must have at least `count` entries)
|
|
71
|
+
* @returns Array of fee recipients with equal allocations
|
|
72
|
+
*/
|
|
73
|
+
export declare const createEqualRecipients: (count: number, pubkeys: PublicKey[]) => FeeRecipient[];
|
|
74
|
+
/**
|
|
75
|
+
* Creates a single recipient with 100% allocation.
|
|
76
|
+
*
|
|
77
|
+
* @param pubkey - Recipient public key
|
|
78
|
+
* @returns Array with a single fee recipient at 10,000 BPS
|
|
79
|
+
*/
|
|
80
|
+
export declare const createSingleRecipient: (pubkey: PublicKey) => FeeRecipient[];
|
|
81
|
+
/**
|
|
82
|
+
* Validates that recipients sum to exactly 10,000 BPS.
|
|
83
|
+
*
|
|
84
|
+
* @param recipients - Fee recipients to validate
|
|
85
|
+
* @returns True if basis points sum to exactly 10,000
|
|
86
|
+
*/
|
|
87
|
+
export declare const validateRecipientsSum: (recipients: FeeRecipient[]) => boolean;
|
|
88
|
+
//# sourceMappingURL=fees.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fees.d.ts","sourceRoot":"","sources":["../../src/math/fees.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,KAAK,EACX,eAAe,EACf,YAAY,EACZ,kBAAkB,EAClB,MAAM,aAAa,CAAC;AAErB;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,GAAI,QAAQ,EAAE,EAAE,KAAK,MAAM,KAAG,EACG,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,SAAS;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,iBAAiB,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,GACzB,QAAQ,EAAE,EACV,QAAQ,SAAS,EACjB,eAAe,OAAO,EACtB,cAAc,OAAO,KACnB,eAyBF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,GACjC,cAAc,MAAM,EACpB,YAAY,YAAY,EAAE,KACxB,kBAAkB,EAMpB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,GACtC,cAAc,MAAM,EACpB,YAAY,YAAY,EAAE,KACxB,MAKF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,GACrC,cAAc,MAAM,EACpB,YAAY,YAAY,EAAE,KACxB,MAOF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAC5B,SAAS,SAAS,EAAE,EACpB,UAAU,MAAM,EAAE,KAChB,YAAY,EAQd,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,GACjC,OAAO,MAAM,EACb,SAAS,SAAS,EAAE,KAClB,YAAY,EAYd,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,QAAQ,SAAS,KAAG,YAAY,EAErE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,YAAY,YAAY,EAAE,KAAG,OAGlE,CAAC"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import { BPS_DENOMINATOR, MIN_VAULT_BALANCE } from "./constants.js";
|
|
3
|
+
/**
|
|
4
|
+
* Calculates basis points of an amount.
|
|
5
|
+
* result = (amount * bps) / 10000
|
|
6
|
+
*
|
|
7
|
+
* @param amount - The amount to calculate basis points of
|
|
8
|
+
* @param bps - Basis points (1 bps = 0.01%)
|
|
9
|
+
* @returns The calculated portion
|
|
10
|
+
*/
|
|
11
|
+
export const calcBps = (amount, bps) => amount.mul(new BN(bps)).div(new BN(BPS_DENOMINATOR));
|
|
12
|
+
/**
|
|
13
|
+
* Calculates fee distribution for a given trade amount.
|
|
14
|
+
* Referral fees are carved from the protocol fee, not the trade amount.
|
|
15
|
+
*
|
|
16
|
+
* @param amount - Gross trade amount (before fees)
|
|
17
|
+
* @param config - Fee configuration in basis points
|
|
18
|
+
* @param hasCreatorRef - Whether the creator has a referrer
|
|
19
|
+
* @param hasTraderRef - Whether the trader has a referrer
|
|
20
|
+
* @returns Fee distribution breakdown
|
|
21
|
+
*/
|
|
22
|
+
export const calculateFees = (amount, config, hasCreatorRef, hasTraderRef) => {
|
|
23
|
+
const baseProtocolFee = calcBps(amount, config.protocolFeeBps);
|
|
24
|
+
const creatorFee = calcBps(amount, config.creatorFeeBps);
|
|
25
|
+
// Referral amounts are BPS of the protocol fee (not trade amount)
|
|
26
|
+
const creatorReferralFee = hasCreatorRef
|
|
27
|
+
? calcBps(baseProtocolFee, config.creatorReferralBps)
|
|
28
|
+
: new BN(0);
|
|
29
|
+
const traderReferralFee = hasTraderRef
|
|
30
|
+
? calcBps(baseProtocolFee, config.traderReferralBps)
|
|
31
|
+
: new BN(0);
|
|
32
|
+
// Protocol fee starts at base amount. On-chain, it is only reduced
|
|
33
|
+
// when referral transfers actually succeed.
|
|
34
|
+
const protocolFee = baseProtocolFee;
|
|
35
|
+
// Referrals are carved from protocol fee (not additive), so total = protocol + creator.
|
|
36
|
+
const totalFees = protocolFee.add(creatorFee);
|
|
37
|
+
return {
|
|
38
|
+
protocolFee,
|
|
39
|
+
creatorFee,
|
|
40
|
+
creatorReferralFee,
|
|
41
|
+
traderReferralFee,
|
|
42
|
+
totalFees,
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Calculates expected distribution amounts for each fee recipient.
|
|
47
|
+
* Distributable = vaultBalance - MIN_VAULT_BALANCE (rent).
|
|
48
|
+
*
|
|
49
|
+
* @param vaultBalance - Current vault balance in lamports
|
|
50
|
+
* @param recipients - Fee recipients with basis point allocations
|
|
51
|
+
* @returns Distribution amounts per recipient
|
|
52
|
+
*/
|
|
53
|
+
export const calculateDistribution = (vaultBalance, recipients) => {
|
|
54
|
+
const distributable = Math.max(0, vaultBalance - MIN_VAULT_BALANCE);
|
|
55
|
+
return recipients.map((r) => ({
|
|
56
|
+
pubkey: r.pubkey,
|
|
57
|
+
amount: Math.floor((distributable * r.basisPoints) / BPS_DENOMINATOR),
|
|
58
|
+
}));
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Calculates total distribution (sum of all recipient amounts).
|
|
62
|
+
*
|
|
63
|
+
* @param vaultBalance - Current vault balance in lamports
|
|
64
|
+
* @param recipients - Fee recipients with basis point allocations
|
|
65
|
+
* @returns Total amount distributed across all recipients
|
|
66
|
+
*/
|
|
67
|
+
export const calculateTotalDistribution = (vaultBalance, recipients) => {
|
|
68
|
+
return calculateDistribution(vaultBalance, recipients).reduce((sum, r) => sum + r.amount, 0);
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Calculates dust remaining after distribution (due to integer rounding).
|
|
72
|
+
*
|
|
73
|
+
* @param vaultBalance - Current vault balance in lamports
|
|
74
|
+
* @param recipients - Fee recipients with basis point allocations
|
|
75
|
+
* @returns Undistributed lamports remaining in the vault
|
|
76
|
+
*/
|
|
77
|
+
export const calculateDistributionDust = (vaultBalance, recipients) => {
|
|
78
|
+
const distributable = Math.max(0, vaultBalance - MIN_VAULT_BALANCE);
|
|
79
|
+
const totalDistributed = calculateTotalDistribution(vaultBalance, recipients);
|
|
80
|
+
return distributable - totalDistributed;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Creates a recipients array from pubkeys and basis points.
|
|
84
|
+
*
|
|
85
|
+
* @param pubkeys - Recipient public keys
|
|
86
|
+
* @param bpsArray - Basis point allocations (must match pubkeys length)
|
|
87
|
+
* @returns Array of fee recipients
|
|
88
|
+
*/
|
|
89
|
+
export const createRecipients = (pubkeys, bpsArray) => {
|
|
90
|
+
if (pubkeys.length !== bpsArray.length) {
|
|
91
|
+
throw new Error("pubkeys and bpsArray must have the same length");
|
|
92
|
+
}
|
|
93
|
+
return pubkeys.map((pubkey, i) => ({
|
|
94
|
+
pubkey,
|
|
95
|
+
basisPoints: bpsArray[i],
|
|
96
|
+
}));
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Creates equal-share recipients (auto-calculates BPS to sum to 10,000).
|
|
100
|
+
*
|
|
101
|
+
* @param count - Number of recipients
|
|
102
|
+
* @param pubkeys - Recipient public keys (must have at least `count` entries)
|
|
103
|
+
* @returns Array of fee recipients with equal allocations
|
|
104
|
+
*/
|
|
105
|
+
export const createEqualRecipients = (count, pubkeys) => {
|
|
106
|
+
if (pubkeys.length < count) {
|
|
107
|
+
throw new Error(`Need at least ${count} pubkeys, got ${pubkeys.length}`);
|
|
108
|
+
}
|
|
109
|
+
const bpsPerRecipient = Math.floor(BPS_DENOMINATOR / count);
|
|
110
|
+
const remainder = BPS_DENOMINATOR % count;
|
|
111
|
+
return pubkeys.slice(0, count).map((pubkey, i) => ({
|
|
112
|
+
pubkey,
|
|
113
|
+
basisPoints: i === 0 ? bpsPerRecipient + remainder : bpsPerRecipient,
|
|
114
|
+
}));
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Creates a single recipient with 100% allocation.
|
|
118
|
+
*
|
|
119
|
+
* @param pubkey - Recipient public key
|
|
120
|
+
* @returns Array with a single fee recipient at 10,000 BPS
|
|
121
|
+
*/
|
|
122
|
+
export const createSingleRecipient = (pubkey) => {
|
|
123
|
+
return [{ pubkey, basisPoints: BPS_DENOMINATOR }];
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Validates that recipients sum to exactly 10,000 BPS.
|
|
127
|
+
*
|
|
128
|
+
* @param recipients - Fee recipients to validate
|
|
129
|
+
* @returns True if basis points sum to exactly 10,000
|
|
130
|
+
*/
|
|
131
|
+
export const validateRecipientsSum = (recipients) => {
|
|
132
|
+
const sum = recipients.reduce((acc, r) => acc + r.basisPoints, 0);
|
|
133
|
+
return sum === BPS_DENOMINATOR;
|
|
134
|
+
};
|
|
135
|
+
//# sourceMappingURL=fees.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fees.js","sourceRoot":"","sources":["../../src/math/fees.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAOpE;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,MAAU,EAAE,GAAW,EAAM,EAAE,CACtD,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,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GAAG,KAAK,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CACd,iBAAiB,KAAK,iBAAiB,OAAO,CAAC,MAAM,EAAE,CACvD,CAAC;IACH,CAAC;IACD,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,GAAG,KAAK,CAAC,CAAC;IAC5D,MAAM,SAAS,GAAG,eAAe,GAAG,KAAK,CAAC;IAC1C,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;QAClD,MAAM;QACN,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,eAAe,GAAG,SAAS,CAAC,CAAC,CAAC,eAAe;KACpE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,MAAiB,EAAkB,EAAE;IAC1E,OAAO,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,eAAe,EAAE,CAAC,CAAC;AACnD,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,UAA0B,EAAW,EAAE;IAC5E,MAAM,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;IAClE,OAAO,GAAG,KAAK,eAAe,CAAC;AAChC,CAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { BPS_DENOMINATOR, FEE_RATE_DENOMINATOR, Q32, OBSERVATION_NUM, OBSERVATION_UPDATE_DURATION_DEFAULT, MIN_VAULT_BALANCE, } from "./constants.js";
|
|
2
|
+
export { calcBps, calculateFees, calculateDistribution, calculateTotalDistribution, calculateDistributionDust, createRecipients, createEqualRecipients, createSingleRecipient, validateRecipientsSum, } from "./fees.js";
|
|
3
|
+
export type { FeeConfig } from "./fees.js";
|
|
4
|
+
export { calculateBuyExpectation, calculateSellExpectation, } from "./bonding-curve.js";
|
|
5
|
+
export { calculateAmmSellOutput, calculateAmmBuyInput, calculateLpToTokens, } from "./amm.js";
|
|
6
|
+
export type { AmmFeeRates } from "./amm.js";
|
|
7
|
+
export { calculatePoolMarketCap, lookupFeeTier, calculateFeesForPool, WSOL_MINT, } from "./tiered-fees.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/math/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,eAAe,EACf,oBAAoB,EACpB,GAAG,EACH,eAAe,EACf,mCAAmC,EACnC,iBAAiB,GACjB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACN,OAAO,EACP,aAAa,EACb,qBAAqB,EACrB,0BAA0B,EAC1B,yBAAyB,EACzB,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,GACrB,MAAM,WAAW,CAAC;AACnB,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,OAAO,EACN,uBAAuB,EACvB,wBAAwB,GACxB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACN,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,GACnB,MAAM,UAAU,CAAC;AAClB,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,OAAO,EACN,sBAAsB,EACtB,aAAa,EACb,oBAAoB,EACpB,SAAS,GACT,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { BPS_DENOMINATOR, FEE_RATE_DENOMINATOR, Q32, OBSERVATION_NUM, OBSERVATION_UPDATE_DURATION_DEFAULT, MIN_VAULT_BALANCE, } from "./constants.js";
|
|
2
|
+
export { calcBps, calculateFees, calculateDistribution, calculateTotalDistribution, calculateDistributionDust, createRecipients, createEqualRecipients, createSingleRecipient, validateRecipientsSum, } from "./fees.js";
|
|
3
|
+
export { calculateBuyExpectation, calculateSellExpectation, } from "./bonding-curve.js";
|
|
4
|
+
export { calculateAmmSellOutput, calculateAmmBuyInput, calculateLpToTokens, } from "./amm.js";
|
|
5
|
+
export { calculatePoolMarketCap, lookupFeeTier, calculateFeesForPool, WSOL_MINT, } from "./tiered-fees.js";
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/math/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,eAAe,EACf,oBAAoB,EACpB,GAAG,EACH,eAAe,EACf,mCAAmC,EACnC,iBAAiB,GACjB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACN,OAAO,EACP,aAAa,EACb,qBAAqB,EACrB,0BAA0B,EAC1B,yBAAyB,EACzB,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,GACrB,MAAM,WAAW,CAAC;AAGnB,OAAO,EACN,uBAAuB,EACvB,wBAAwB,GACxB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACN,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,GACnB,MAAM,UAAU,CAAC;AAGlB,OAAO,EACN,sBAAsB,EACtB,aAAa,EACb,oBAAoB,EACpB,SAAS,GACT,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import BN from "bn.js";
|
|
3
|
+
import type { FeeTier } from "../types.js";
|
|
4
|
+
/** WSOL mint address (native SOL wrapped as SPL token) */
|
|
5
|
+
export declare const WSOL_MINT: PublicKey;
|
|
6
|
+
/**
|
|
7
|
+
* Calculate pool fully diluted market cap in USD.
|
|
8
|
+
*
|
|
9
|
+
* The market cap is calculated as:
|
|
10
|
+
* - Pool token price (in USD) × base token total supply
|
|
11
|
+
* - Pool token price = (quote vault value in USD) / base vault amount
|
|
12
|
+
*
|
|
13
|
+
* For WSOL pools, the quote vault is converted to USD using the oracle price.
|
|
14
|
+
* For USDC pools, the quote vault is already in USD (assumed 1:1 peg).
|
|
15
|
+
*
|
|
16
|
+
* @param quoteVaultAmount - Quote vault balance (lamports for WSOL, 6 decimals for USDC)
|
|
17
|
+
* @param baseVaultAmount - Base vault balance (token decimals)
|
|
18
|
+
* @param baseTotalSupply - Total supply of base token (token decimals)
|
|
19
|
+
* @param solPriceUsd - SOL/USD price from oracle (6 decimals)
|
|
20
|
+
* @param quoteMint - Quote mint pubkey (to detect WSOL vs stable)
|
|
21
|
+
* @returns Market cap in USD (6 decimals)
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* // Pool with 100 SOL and 1M tokens, SOL = $150, supply = 1B tokens
|
|
26
|
+
* // Token price = (100 SOL × $150) / 1M = $0.015 per token
|
|
27
|
+
* // Market cap = $0.015 × 1B = $15M
|
|
28
|
+
* const marketCap = calculatePoolMarketCap(
|
|
29
|
+
* new BN(100_000_000_000), // 100 SOL in lamports
|
|
30
|
+
* new BN(1_000_000_000_000), // 1M tokens (6 decimals)
|
|
31
|
+
* new BN(1_000_000_000_000_000), // 1B tokens (6 decimals)
|
|
32
|
+
* new BN(150_000_000), // $150 (6 decimals)
|
|
33
|
+
* WSOL_MINT,
|
|
34
|
+
* );
|
|
35
|
+
* // Returns: 15_000_000_000_000 ($15M with 6 decimals)
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function calculatePoolMarketCap(quoteVaultAmount: BN, baseVaultAmount: BN, baseTotalSupply: BN, solPriceUsd: BN, quoteMint: PublicKey): BN;
|
|
39
|
+
/**
|
|
40
|
+
* Find appropriate fee tier for given market cap.
|
|
41
|
+
*
|
|
42
|
+
* Performs a linear scan through the active fee tiers to find the tier
|
|
43
|
+
* whose range contains the given market cap. If the market cap exceeds
|
|
44
|
+
* all tier ranges, returns the last tier.
|
|
45
|
+
*
|
|
46
|
+
* @param marketCapUsd - Pool market cap in USD (6 decimals)
|
|
47
|
+
* @param tiers - Array of active fee tiers (sorted, validated)
|
|
48
|
+
* @returns Tuple of [lpFeeRate, protocolFeeRate, creatorFeeRate] in basis points
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const tiers = [
|
|
53
|
+
* { rangeStartInclusive: new BN(0), rangeEndExclusive: new BN(1_000_000_000_000), lpFeeRate: 30, protocolFeeRate: 5, creatorFeeRate: 5 },
|
|
54
|
+
* { rangeStartInclusive: new BN(1_000_000_000_000), rangeEndExclusive: new BN('18446744073709551615'), lpFeeRate: 20, protocolFeeRate: 3, creatorFeeRate: 3 },
|
|
55
|
+
* ];
|
|
56
|
+
*
|
|
57
|
+
* // Market cap = $500K -> uses first tier (high fees)
|
|
58
|
+
* const [lp, protocol, creator] = lookupFeeTier(new BN(500_000_000_000), tiers);
|
|
59
|
+
* // Returns: [30, 5, 5]
|
|
60
|
+
*
|
|
61
|
+
* // Market cap = $5M -> uses second tier (low fees)
|
|
62
|
+
* const [lp2, protocol2, creator2] = lookupFeeTier(new BN(5_000_000_000_000), tiers);
|
|
63
|
+
* // Returns: [20, 3, 3]
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function lookupFeeTier(marketCapUsd: BN, tiers: FeeTier[]): [number, number, number];
|
|
67
|
+
/**
|
|
68
|
+
* Calculate the fee rates that would apply for a given pool state.
|
|
69
|
+
* Helper function that combines market cap calculation and tier lookup.
|
|
70
|
+
*
|
|
71
|
+
* @param quoteVaultAmount - Quote vault balance
|
|
72
|
+
* @param baseVaultAmount - Base vault balance
|
|
73
|
+
* @param baseTotalSupply - Total supply of base token
|
|
74
|
+
* @param solPriceUsd - SOL/USD price from oracle (6 decimals)
|
|
75
|
+
* @param quoteMint - Quote mint pubkey
|
|
76
|
+
* @param tiers - Array of active fee tiers
|
|
77
|
+
* @returns Tuple of [lpFeeRate, protocolFeeRate, creatorFeeRate] in basis points
|
|
78
|
+
*/
|
|
79
|
+
export declare function calculateFeesForPool(quoteVaultAmount: BN, baseVaultAmount: BN, baseTotalSupply: BN, solPriceUsd: BN, quoteMint: PublicKey, tiers: FeeTier[]): [number, number, number];
|
|
80
|
+
//# sourceMappingURL=tiered-fees.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tiered-fees.d.ts","sourceRoot":"","sources":["../../src/math/tiered-fees.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,0DAA0D;AAC1D,eAAO,MAAM,SAAS,WAErB,CAAC;AAKF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,sBAAsB,CACrC,gBAAgB,EAAE,EAAE,EACpB,eAAe,EAAE,EAAE,EACnB,eAAe,EAAE,EAAE,EACnB,WAAW,EAAE,EAAE,EACf,SAAS,EAAE,SAAS,GAClB,EAAE,CA6BJ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,aAAa,CAC5B,YAAY,EAAE,EAAE,EAChB,KAAK,EAAE,OAAO,EAAE,GACd,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAsB1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CACnC,gBAAgB,EAAE,EAAE,EACpB,eAAe,EAAE,EAAE,EACnB,eAAe,EAAE,EAAE,EACnB,WAAW,EAAE,EAAE,EACf,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,OAAO,EAAE,GACd,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAU1B"}
|