@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.
Files changed (225) hide show
  1. package/README.md +776 -0
  2. package/dist/accounts/index.d.ts +5 -0
  3. package/dist/accounts/index.d.ts.map +1 -0
  4. package/dist/accounts/index.js +5 -0
  5. package/dist/accounts/index.js.map +1 -0
  6. package/dist/accounts/liquid-fees.d.ts +63 -0
  7. package/dist/accounts/liquid-fees.d.ts.map +1 -0
  8. package/dist/accounts/liquid-fees.js +27 -0
  9. package/dist/accounts/liquid-fees.js.map +1 -0
  10. package/dist/accounts/liquid-state.d.ts +112 -0
  11. package/dist/accounts/liquid-state.d.ts.map +1 -0
  12. package/dist/accounts/liquid-state.js +92 -0
  13. package/dist/accounts/liquid-state.js.map +1 -0
  14. package/dist/accounts/liquid-swap.d.ts +97 -0
  15. package/dist/accounts/liquid-swap.d.ts.map +1 -0
  16. package/dist/accounts/liquid-swap.js +54 -0
  17. package/dist/accounts/liquid-swap.js.map +1 -0
  18. package/dist/accounts/liquid.d.ts +175 -0
  19. package/dist/accounts/liquid.d.ts.map +1 -0
  20. package/dist/accounts/liquid.js +66 -0
  21. package/dist/accounts/liquid.js.map +1 -0
  22. package/dist/client.d.ts +621 -0
  23. package/dist/client.d.ts.map +1 -0
  24. package/dist/client.js +511 -0
  25. package/dist/client.js.map +1 -0
  26. package/dist/config.d.ts +56 -0
  27. package/dist/config.d.ts.map +1 -0
  28. package/dist/config.js +44 -0
  29. package/dist/config.js.map +1 -0
  30. package/dist/errors.d.ts +39 -0
  31. package/dist/errors.d.ts.map +1 -0
  32. package/dist/errors.js +63 -0
  33. package/dist/errors.js.map +1 -0
  34. package/dist/events/index.d.ts +4 -0
  35. package/dist/events/index.d.ts.map +1 -0
  36. package/dist/events/index.js +2 -0
  37. package/dist/events/index.js.map +1 -0
  38. package/dist/events/parser.d.ts +40 -0
  39. package/dist/events/parser.d.ts.map +1 -0
  40. package/dist/events/parser.js +67 -0
  41. package/dist/events/parser.js.map +1 -0
  42. package/dist/events/types.d.ts +286 -0
  43. package/dist/events/types.d.ts.map +1 -0
  44. package/dist/events/types.js +2 -0
  45. package/dist/events/types.js.map +1 -0
  46. package/dist/helpers/index.d.ts +4 -0
  47. package/dist/helpers/index.d.ts.map +1 -0
  48. package/dist/helpers/index.js +3 -0
  49. package/dist/helpers/index.js.map +1 -0
  50. package/dist/helpers/preview.d.ts +259 -0
  51. package/dist/helpers/preview.d.ts.map +1 -0
  52. package/dist/helpers/preview.js +458 -0
  53. package/dist/helpers/preview.js.map +1 -0
  54. package/dist/helpers/user.d.ts +11 -0
  55. package/dist/helpers/user.d.ts.map +1 -0
  56. package/dist/helpers/user.js +20 -0
  57. package/dist/helpers/user.js.map +1 -0
  58. package/dist/idl/index.d.ts +53 -0
  59. package/dist/idl/index.d.ts.map +1 -0
  60. package/dist/idl/index.js +64 -0
  61. package/dist/idl/index.js.map +1 -0
  62. package/dist/idl/liquid.d.ts +10523 -0
  63. package/dist/idl/liquid.d.ts.map +1 -0
  64. package/dist/idl/liquid.js +2 -0
  65. package/dist/idl/liquid.js.map +1 -0
  66. package/dist/idl/liquid.json +10516 -0
  67. package/dist/idl/liquid_fees.d.ts +1520 -0
  68. package/dist/idl/liquid_fees.d.ts.map +1 -0
  69. package/dist/idl/liquid_fees.js +2 -0
  70. package/dist/idl/liquid_fees.js.map +1 -0
  71. package/dist/idl/liquid_fees.json +1513 -0
  72. package/dist/idl/liquid_state.d.ts +2936 -0
  73. package/dist/idl/liquid_state.d.ts.map +1 -0
  74. package/dist/idl/liquid_state.js +2 -0
  75. package/dist/idl/liquid_state.js.map +1 -0
  76. package/dist/idl/liquid_state.json +2929 -0
  77. package/dist/idl/liquid_swap.d.ts +5849 -0
  78. package/dist/idl/liquid_swap.d.ts.map +1 -0
  79. package/dist/idl/liquid_swap.js +2 -0
  80. package/dist/idl/liquid_swap.js.map +1 -0
  81. package/dist/idl/liquid_swap.json +5842 -0
  82. package/dist/index.d.ts +19 -0
  83. package/dist/index.d.ts.map +1 -0
  84. package/dist/index.js +15 -0
  85. package/dist/index.js.map +1 -0
  86. package/dist/instructions/index.d.ts +9 -0
  87. package/dist/instructions/index.d.ts.map +1 -0
  88. package/dist/instructions/index.js +9 -0
  89. package/dist/instructions/index.js.map +1 -0
  90. package/dist/instructions/liquid-fees.d.ts +111 -0
  91. package/dist/instructions/liquid-fees.d.ts.map +1 -0
  92. package/dist/instructions/liquid-fees.js +169 -0
  93. package/dist/instructions/liquid-fees.js.map +1 -0
  94. package/dist/instructions/liquid-state.d.ts +173 -0
  95. package/dist/instructions/liquid-state.d.ts.map +1 -0
  96. package/dist/instructions/liquid-state.js +194 -0
  97. package/dist/instructions/liquid-state.js.map +1 -0
  98. package/dist/instructions/liquid-swap.d.ts +122 -0
  99. package/dist/instructions/liquid-swap.d.ts.map +1 -0
  100. package/dist/instructions/liquid-swap.js +167 -0
  101. package/dist/instructions/liquid-swap.js.map +1 -0
  102. package/dist/instructions/liquid.d.ts +297 -0
  103. package/dist/instructions/liquid.d.ts.map +1 -0
  104. package/dist/instructions/liquid.js +483 -0
  105. package/dist/instructions/liquid.js.map +1 -0
  106. package/dist/instructions/program-cache.d.ts +35 -0
  107. package/dist/instructions/program-cache.d.ts.map +1 -0
  108. package/dist/instructions/program-cache.js +68 -0
  109. package/dist/instructions/program-cache.js.map +1 -0
  110. package/dist/math/amm.d.ts +42 -0
  111. package/dist/math/amm.d.ts.map +1 -0
  112. package/dist/math/amm.js +109 -0
  113. package/dist/math/amm.js.map +1 -0
  114. package/dist/math/bonding-curve.d.ts +34 -0
  115. package/dist/math/bonding-curve.d.ts.map +1 -0
  116. package/dist/math/bonding-curve.js +80 -0
  117. package/dist/math/bonding-curve.js.map +1 -0
  118. package/dist/math/constants.d.ts +14 -0
  119. package/dist/math/constants.d.ts.map +1 -0
  120. package/dist/math/constants.js +14 -0
  121. package/dist/math/constants.js.map +1 -0
  122. package/dist/math/fees.d.ts +88 -0
  123. package/dist/math/fees.d.ts.map +1 -0
  124. package/dist/math/fees.js +135 -0
  125. package/dist/math/fees.js.map +1 -0
  126. package/dist/math/index.d.ts +8 -0
  127. package/dist/math/index.d.ts.map +1 -0
  128. package/dist/math/index.js +6 -0
  129. package/dist/math/index.js.map +1 -0
  130. package/dist/math/tiered-fees.d.ts +80 -0
  131. package/dist/math/tiered-fees.d.ts.map +1 -0
  132. package/dist/math/tiered-fees.js +129 -0
  133. package/dist/math/tiered-fees.js.map +1 -0
  134. package/dist/oracle.d.ts +53 -0
  135. package/dist/oracle.d.ts.map +1 -0
  136. package/dist/oracle.js +70 -0
  137. package/dist/oracle.js.map +1 -0
  138. package/dist/pda/index.d.ts +89 -0
  139. package/dist/pda/index.d.ts.map +1 -0
  140. package/dist/pda/index.js +127 -0
  141. package/dist/pda/index.js.map +1 -0
  142. package/dist/pda/liquid-fees.d.ts +27 -0
  143. package/dist/pda/liquid-fees.d.ts.map +1 -0
  144. package/dist/pda/liquid-fees.js +36 -0
  145. package/dist/pda/liquid-fees.js.map +1 -0
  146. package/dist/pda/liquid-state.d.ts +56 -0
  147. package/dist/pda/liquid-state.d.ts.map +1 -0
  148. package/dist/pda/liquid-state.js +79 -0
  149. package/dist/pda/liquid-state.js.map +1 -0
  150. package/dist/pda/liquid-swap.d.ts +76 -0
  151. package/dist/pda/liquid-swap.d.ts.map +1 -0
  152. package/dist/pda/liquid-swap.js +103 -0
  153. package/dist/pda/liquid-swap.js.map +1 -0
  154. package/dist/pda/liquid.d.ts +67 -0
  155. package/dist/pda/liquid.d.ts.map +1 -0
  156. package/dist/pda/liquid.js +91 -0
  157. package/dist/pda/liquid.js.map +1 -0
  158. package/dist/provider.d.ts +26 -0
  159. package/dist/provider.d.ts.map +1 -0
  160. package/dist/provider.js +47 -0
  161. package/dist/provider.js.map +1 -0
  162. package/dist/transaction/builder.d.ts +30 -0
  163. package/dist/transaction/builder.d.ts.map +1 -0
  164. package/dist/transaction/builder.js +48 -0
  165. package/dist/transaction/builder.js.map +1 -0
  166. package/dist/transaction/index.d.ts +3 -0
  167. package/dist/transaction/index.d.ts.map +1 -0
  168. package/dist/transaction/index.js +3 -0
  169. package/dist/transaction/index.js.map +1 -0
  170. package/dist/transaction/send.d.ts +25 -0
  171. package/dist/transaction/send.d.ts.map +1 -0
  172. package/dist/transaction/send.js +52 -0
  173. package/dist/transaction/send.js.map +1 -0
  174. package/dist/types.d.ts +311 -0
  175. package/dist/types.d.ts.map +1 -0
  176. package/dist/types.js +46 -0
  177. package/dist/types.js.map +1 -0
  178. package/package.json +112 -0
  179. package/src/accounts/index.ts +26 -0
  180. package/src/accounts/liquid-fees.ts +38 -0
  181. package/src/accounts/liquid-state.ts +134 -0
  182. package/src/accounts/liquid-swap.ts +79 -0
  183. package/src/accounts/liquid.ts +100 -0
  184. package/src/client.ts +1001 -0
  185. package/src/config.ts +91 -0
  186. package/src/errors.ts +94 -0
  187. package/src/events/index.ts +42 -0
  188. package/src/events/parser.ts +90 -0
  189. package/src/events/types.ts +310 -0
  190. package/src/helpers/index.ts +23 -0
  191. package/src/helpers/preview.ts +798 -0
  192. package/src/helpers/user.ts +24 -0
  193. package/src/idl/index.ts +94 -0
  194. package/src/idl/liquid.json +10516 -0
  195. package/src/idl/liquid.ts +10522 -0
  196. package/src/idl/liquid_fees.json +1513 -0
  197. package/src/idl/liquid_fees.ts +1519 -0
  198. package/src/idl/liquid_state.json +2929 -0
  199. package/src/idl/liquid_state.ts +2935 -0
  200. package/src/idl/liquid_swap.json +5842 -0
  201. package/src/idl/liquid_swap.ts +5848 -0
  202. package/src/index.ts +98 -0
  203. package/src/instructions/index.ts +109 -0
  204. package/src/instructions/liquid-fees.ts +289 -0
  205. package/src/instructions/liquid-state.ts +336 -0
  206. package/src/instructions/liquid-swap.ts +414 -0
  207. package/src/instructions/liquid.ts +884 -0
  208. package/src/instructions/program-cache.ts +106 -0
  209. package/src/math/amm.ts +146 -0
  210. package/src/math/bonding-curve.ts +122 -0
  211. package/src/math/constants.ts +19 -0
  212. package/src/math/fees.ts +191 -0
  213. package/src/math/index.ts +40 -0
  214. package/src/math/tiered-fees.ts +165 -0
  215. package/src/oracle.ts +97 -0
  216. package/src/pda/index.ts +331 -0
  217. package/src/pda/liquid-fees.ts +58 -0
  218. package/src/pda/liquid-state.ts +123 -0
  219. package/src/pda/liquid-swap.ts +162 -0
  220. package/src/pda/liquid.ts +152 -0
  221. package/src/provider.ts +60 -0
  222. package/src/transaction/builder.ts +80 -0
  223. package/src/transaction/index.ts +6 -0
  224. package/src/transaction/send.ts +72 -0
  225. package/src/types.ts +354 -0
@@ -0,0 +1,165 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import BN from "bn.js";
3
+ import type { FeeTier } from "../types.js";
4
+
5
+ /** WSOL mint address (native SOL wrapped as SPL token) */
6
+ export const WSOL_MINT = new PublicKey(
7
+ "So11111111111111111111111111111111111111112"
8
+ );
9
+
10
+ /** Lamports per SOL constant */
11
+ const LAMPORTS_PER_SOL = new BN(1_000_000_000);
12
+
13
+ /**
14
+ * Calculate pool fully diluted market cap in USD.
15
+ *
16
+ * The market cap is calculated as:
17
+ * - Pool token price (in USD) × base token total supply
18
+ * - Pool token price = (quote vault value in USD) / base vault amount
19
+ *
20
+ * For WSOL pools, the quote vault is converted to USD using the oracle price.
21
+ * For USDC pools, the quote vault is already in USD (assumed 1:1 peg).
22
+ *
23
+ * @param quoteVaultAmount - Quote vault balance (lamports for WSOL, 6 decimals for USDC)
24
+ * @param baseVaultAmount - Base vault balance (token decimals)
25
+ * @param baseTotalSupply - Total supply of base token (token decimals)
26
+ * @param solPriceUsd - SOL/USD price from oracle (6 decimals)
27
+ * @param quoteMint - Quote mint pubkey (to detect WSOL vs stable)
28
+ * @returns Market cap in USD (6 decimals)
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * // Pool with 100 SOL and 1M tokens, SOL = $150, supply = 1B tokens
33
+ * // Token price = (100 SOL × $150) / 1M = $0.015 per token
34
+ * // Market cap = $0.015 × 1B = $15M
35
+ * const marketCap = calculatePoolMarketCap(
36
+ * new BN(100_000_000_000), // 100 SOL in lamports
37
+ * new BN(1_000_000_000_000), // 1M tokens (6 decimals)
38
+ * new BN(1_000_000_000_000_000), // 1B tokens (6 decimals)
39
+ * new BN(150_000_000), // $150 (6 decimals)
40
+ * WSOL_MINT,
41
+ * );
42
+ * // Returns: 15_000_000_000_000 ($15M with 6 decimals)
43
+ * ```
44
+ */
45
+ export function calculatePoolMarketCap(
46
+ quoteVaultAmount: BN,
47
+ baseVaultAmount: BN,
48
+ baseTotalSupply: BN,
49
+ solPriceUsd: BN,
50
+ quoteMint: PublicKey
51
+ ): BN {
52
+ // Prevent division by zero
53
+ if (baseVaultAmount.isZero()) {
54
+ throw new Error("Base vault amount cannot be zero");
55
+ }
56
+
57
+ if (quoteMint.equals(WSOL_MINT)) {
58
+ // For WSOL pools: convert quote reserves to USD, then compute market cap
59
+ // Formula: market_cap = (quote_lamports × sol_price_usd / 1e9) × base_supply / base_vault
60
+ //
61
+ // Combined to avoid intermediate truncation:
62
+ // market_cap = (quote_lamports × sol_price_usd × base_supply) / (1e9 × base_vault)
63
+ const marketCap = quoteVaultAmount
64
+ .mul(solPriceUsd)
65
+ .mul(baseTotalSupply)
66
+ .div(LAMPORTS_PER_SOL)
67
+ .div(baseVaultAmount);
68
+
69
+ return marketCap;
70
+ } else {
71
+ // For USDC and other stablecoins: assume 1:1 with USD
72
+ // quote_vault already in 6 decimals for USDC
73
+ // Formula: market_cap = (quote_amount × base_supply) / base_vault
74
+ const marketCap = quoteVaultAmount
75
+ .mul(baseTotalSupply)
76
+ .div(baseVaultAmount);
77
+
78
+ return marketCap;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Find appropriate fee tier for given market cap.
84
+ *
85
+ * Performs a linear scan through the active fee tiers to find the tier
86
+ * whose range contains the given market cap. If the market cap exceeds
87
+ * all tier ranges, returns the last tier.
88
+ *
89
+ * @param marketCapUsd - Pool market cap in USD (6 decimals)
90
+ * @param tiers - Array of active fee tiers (sorted, validated)
91
+ * @returns Tuple of [lpFeeRate, protocolFeeRate, creatorFeeRate] in basis points
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * const tiers = [
96
+ * { rangeStartInclusive: new BN(0), rangeEndExclusive: new BN(1_000_000_000_000), lpFeeRate: 30, protocolFeeRate: 5, creatorFeeRate: 5 },
97
+ * { rangeStartInclusive: new BN(1_000_000_000_000), rangeEndExclusive: new BN('18446744073709551615'), lpFeeRate: 20, protocolFeeRate: 3, creatorFeeRate: 3 },
98
+ * ];
99
+ *
100
+ * // Market cap = $500K -> uses first tier (high fees)
101
+ * const [lp, protocol, creator] = lookupFeeTier(new BN(500_000_000_000), tiers);
102
+ * // Returns: [30, 5, 5]
103
+ *
104
+ * // Market cap = $5M -> uses second tier (low fees)
105
+ * const [lp2, protocol2, creator2] = lookupFeeTier(new BN(5_000_000_000_000), tiers);
106
+ * // Returns: [20, 3, 3]
107
+ * ```
108
+ */
109
+ export function lookupFeeTier(
110
+ marketCapUsd: BN,
111
+ tiers: FeeTier[]
112
+ ): [number, number, number] {
113
+ if (tiers.length === 0) {
114
+ throw new Error("No fee tiers configured");
115
+ }
116
+
117
+ // Linear scan to find matching tier
118
+ for (const tier of tiers) {
119
+ if (
120
+ marketCapUsd.gte(tier.rangeStartInclusive) &&
121
+ marketCapUsd.lt(tier.rangeEndExclusive)
122
+ ) {
123
+ return [tier.lpFeeRate, tier.protocolFeeRate, tier.creatorFeeRate];
124
+ }
125
+ }
126
+
127
+ // If market cap exceeds all tiers, use the last tier
128
+ const lastTier = tiers[tiers.length - 1];
129
+ return [
130
+ lastTier.lpFeeRate,
131
+ lastTier.protocolFeeRate,
132
+ lastTier.creatorFeeRate,
133
+ ];
134
+ }
135
+
136
+ /**
137
+ * Calculate the fee rates that would apply for a given pool state.
138
+ * Helper function that combines market cap calculation and tier lookup.
139
+ *
140
+ * @param quoteVaultAmount - Quote vault balance
141
+ * @param baseVaultAmount - Base vault balance
142
+ * @param baseTotalSupply - Total supply of base token
143
+ * @param solPriceUsd - SOL/USD price from oracle (6 decimals)
144
+ * @param quoteMint - Quote mint pubkey
145
+ * @param tiers - Array of active fee tiers
146
+ * @returns Tuple of [lpFeeRate, protocolFeeRate, creatorFeeRate] in basis points
147
+ */
148
+ export function calculateFeesForPool(
149
+ quoteVaultAmount: BN,
150
+ baseVaultAmount: BN,
151
+ baseTotalSupply: BN,
152
+ solPriceUsd: BN,
153
+ quoteMint: PublicKey,
154
+ tiers: FeeTier[]
155
+ ): [number, number, number] {
156
+ const marketCap = calculatePoolMarketCap(
157
+ quoteVaultAmount,
158
+ baseVaultAmount,
159
+ baseTotalSupply,
160
+ solPriceUsd,
161
+ quoteMint
162
+ );
163
+
164
+ return lookupFeeTier(marketCap, tiers);
165
+ }
package/src/oracle.ts ADDED
@@ -0,0 +1,97 @@
1
+ /**
2
+ * SOL/USD feed ID from Pyth.
3
+ *
4
+ * This is a universal identifier for the SOL/USD price pair used across all networks
5
+ * (mainnet, devnet, testnet, and localnet). Feed IDs are asset pair identifiers,
6
+ * not account addresses. The actual price feed accounts differ per network, but all
7
+ * contain data for this feed ID.
8
+ *
9
+ * Must match the constant in programs/liquid/src/instructions/shared/oracle.rs
10
+ */
11
+ export const SOL_USD_FEED_ID =
12
+ "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d";
13
+
14
+ /**
15
+ * Standard Pyth exponent for SOL/USD (8 decimals)
16
+ */
17
+ export const PYTH_EXPONENT = -8;
18
+
19
+ /**
20
+ * USD decimals used in the protocol
21
+ */
22
+ export const USD_DECIMALS = 6;
23
+
24
+ /**
25
+ * Calculates the expected virtual SOL reserves for a given virtual reserve value and SOL price.
26
+ * Matches the calculation in oracle.rs::calculate_virtual_sol_reserves
27
+ *
28
+ * @param initialVirtualReserveUsd - Initial virtual reserve value in USD (6 decimals)
29
+ * @param solPriceUsd - SOL price in USD (6 decimals)
30
+ * @returns Virtual SOL reserves in lamports
31
+ */
32
+ export function calculateVirtualSolReserves(
33
+ initialVirtualReserveUsd: bigint,
34
+ solPriceUsd: bigint
35
+ ): bigint {
36
+ // virtual_sol (lamports) = initial_virtual_reserve_usd / sol_price_usd * 10^9
37
+ return (initialVirtualReserveUsd * BigInt(1_000_000_000)) / solPriceUsd;
38
+ }
39
+
40
+ /**
41
+ * Calculates the market cap of a token in USD for native curves.
42
+ * Matches the calculation in oracle.rs::calculate_market_cap_usd
43
+ *
44
+ * Market Cap = Token Price (in SOL) × Total Supply × SOL Price (in USD)
45
+ *
46
+ * @param virtualSolReserves - Virtual SOL reserves in lamports
47
+ * @param virtualTokenReserves - Virtual token reserves (6 decimals)
48
+ * @param tokenTotalSupply - Total supply of tokens (6 decimals)
49
+ * @param solPriceUsd - SOL price in USD (6 decimals)
50
+ * @returns Market cap in USD (6 decimals)
51
+ */
52
+ export function calculateNativeMarketCapUsd(
53
+ virtualSolReserves: bigint,
54
+ virtualTokenReserves: bigint,
55
+ tokenTotalSupply: bigint,
56
+ solPriceUsd: bigint
57
+ ): bigint {
58
+ // Token price in lamports per token (scaled by 1e6 for precision)
59
+ const tokenPriceLamports =
60
+ (virtualSolReserves * BigInt(1_000_000)) / virtualTokenReserves;
61
+
62
+ // Market cap in lamports
63
+ const marketCapLamports =
64
+ (tokenPriceLamports * tokenTotalSupply) / BigInt(1_000_000);
65
+
66
+ // Convert to USD
67
+ const marketCapUsd =
68
+ (marketCapLamports * solPriceUsd) / BigInt(1_000_000_000);
69
+
70
+ return marketCapUsd;
71
+ }
72
+
73
+ /**
74
+ * Calculates the market cap of a token in USD for stable curves (USDC quote).
75
+ *
76
+ * Market Cap = Token Price (in USDC) × Total Supply
77
+ *
78
+ * @param virtualQuoteReserves - Virtual USDC reserves (6 decimals)
79
+ * @param virtualTokenReserves - Virtual token reserves (6 decimals)
80
+ * @param tokenTotalSupply - Total supply of tokens (6 decimals)
81
+ * @returns Market cap in USD (6 decimals)
82
+ */
83
+ export function calculateTokenMarketCapUsd(
84
+ virtualQuoteReserves: bigint,
85
+ virtualTokenReserves: bigint,
86
+ tokenTotalSupply: bigint
87
+ ): bigint {
88
+ // Token price in USDC (scaled by 1e6 for precision)
89
+ const tokenPriceUsdc =
90
+ (virtualQuoteReserves * BigInt(1_000_000)) / virtualTokenReserves;
91
+
92
+ // Market cap = price * total_supply
93
+ const marketCapUsd =
94
+ (tokenPriceUsdc * tokenTotalSupply) / BigInt(1_000_000);
95
+
96
+ return marketCapUsd;
97
+ }
@@ -0,0 +1,331 @@
1
+ export {
2
+ getLiquidGlobalConfigPDA,
3
+ getBondingCurvePDA,
4
+ getBondingCurveSolVaultPDA,
5
+ getBondingCurveTokenAccount,
6
+ getReferralVaultPDA,
7
+ getReferralTokenVault,
8
+ getBuybackVaultPDA,
9
+ getStableBondingCurvePDA,
10
+ } from "./liquid.js";
11
+
12
+ export {
13
+ getSwapGlobalConfigPDA,
14
+ getSwapAuthorityPDA,
15
+ getPoolPDA,
16
+ getPoolLpMintPDA,
17
+ getPoolVaultPDA,
18
+ getObservationPDA,
19
+ getProtocolFeeVaultPDA,
20
+ getTempWsolPDA,
21
+ getSwapBuybackVaultPDA,
22
+ } from "./liquid-swap.js";
23
+
24
+ export {
25
+ getFeeConfigPDA,
26
+ getFeeVaultPDA,
27
+ getRecipientVaultPDA,
28
+ } from "./liquid-fees.js";
29
+
30
+ export {
31
+ getUserPropertiesPDA,
32
+ getGlobalCurveVolumePDA,
33
+ getGlobalAmmVolumePDA,
34
+ getTokenVolumePDA,
35
+ getCashbackConfigPDA,
36
+ getUserSnapshotPDA,
37
+ getDealPDA,
38
+ } from "./liquid-state.js";
39
+
40
+ import type { PublicKey } from "@solana/web3.js";
41
+ import { MAINNET_CONFIG, WSOL_MINT, type LiquidConfig } from "../config.js";
42
+ import {
43
+ getBondingCurvePDA,
44
+ getBondingCurveSolVaultPDA,
45
+ getReferralVaultPDA,
46
+ getBuybackVaultPDA,
47
+ getStableBondingCurvePDA,
48
+ } from "./liquid.js";
49
+ import {
50
+ getPoolPDA,
51
+ getPoolLpMintPDA,
52
+ getPoolVaultPDA,
53
+ getObservationPDA,
54
+ getSwapGlobalConfigPDA,
55
+ getSwapAuthorityPDA,
56
+ getSwapBuybackVaultPDA,
57
+ } from "./liquid-swap.js";
58
+ import { getFeeConfigPDA, getFeeVaultPDA } from "./liquid-fees.js";
59
+ import { getUserPropertiesPDA, getTokenVolumePDA } from "./liquid-state.js";
60
+
61
+ /** All PDAs associated with a bonding curve token */
62
+ export interface BondingCurvePDAs {
63
+ bondingCurve: PublicKey;
64
+ bondingCurveSolVault: PublicKey;
65
+ referralVault: PublicKey;
66
+ buybackVault: PublicKey;
67
+ feeConfig: PublicKey;
68
+ feeVault: PublicKey;
69
+ userProperties: PublicKey;
70
+ tokenVolume: PublicKey;
71
+ }
72
+
73
+ /**
74
+ * Derives all PDAs associated with a bonding curve token.
75
+ *
76
+ * @param mint - Token mint address
77
+ * @param creator - Bonding curve creator public key
78
+ * @param config - Liquid protocol configuration
79
+ * @returns All bonding curve-related PDAs
80
+ */
81
+ export const getAllBondingCurvePDAs = (
82
+ mint: PublicKey,
83
+ creator: PublicKey,
84
+ config: LiquidConfig
85
+ ): BondingCurvePDAs => {
86
+ const [bondingCurve] = getBondingCurvePDA(mint, config.liquidProgramId);
87
+ const [bondingCurveSolVault] = getBondingCurveSolVaultPDA(
88
+ bondingCurve,
89
+ config.liquidProgramId
90
+ );
91
+ const [referralVault] = getReferralVaultPDA(
92
+ creator,
93
+ config.liquidProgramId
94
+ );
95
+ const [buybackVault] = getBuybackVaultPDA(
96
+ bondingCurve,
97
+ config.liquidProgramId
98
+ );
99
+ const [feeConfig] = getFeeConfigPDA(mint, config.liquidFeesProgramId);
100
+ const [feeVault] = getFeeVaultPDA(feeConfig, config.liquidFeesProgramId);
101
+ const [userProperties] = getUserPropertiesPDA(
102
+ creator,
103
+ config.liquidStateProgramId
104
+ );
105
+ const [tokenVolume] = getTokenVolumePDA(mint, config.liquidStateProgramId);
106
+
107
+ return {
108
+ bondingCurve,
109
+ bondingCurveSolVault,
110
+ referralVault,
111
+ buybackVault,
112
+ feeConfig,
113
+ feeVault,
114
+ userProperties,
115
+ tokenVolume,
116
+ };
117
+ };
118
+
119
+ /** All PDAs associated with an AMM pool */
120
+ export interface PoolPDAs {
121
+ poolState: PublicKey;
122
+ lpMint: PublicKey;
123
+ baseVault: PublicKey;
124
+ quoteVault: PublicKey;
125
+ observation: PublicKey;
126
+ ammConfig: PublicKey;
127
+ ammAuthority: PublicKey;
128
+ feeConfig: PublicKey;
129
+ tokenVolume: PublicKey;
130
+ }
131
+
132
+ /**
133
+ * Derives all PDAs associated with an AMM pool.
134
+ *
135
+ * @param baseMint - Base token mint address
136
+ * @param quoteMint - Quote token mint address
137
+ * @param config - Liquid protocol configuration
138
+ * @returns All pool-related PDAs
139
+ */
140
+ export const getAllPoolPDAs = (
141
+ baseMint: PublicKey,
142
+ quoteMint: PublicKey,
143
+ config: LiquidConfig
144
+ ): PoolPDAs => {
145
+ const [poolState] = getPoolPDA(
146
+ baseMint,
147
+ quoteMint,
148
+ config.liquidSwapProgramId
149
+ );
150
+ const [lpMint] = getPoolLpMintPDA(poolState, config.liquidSwapProgramId);
151
+ const [baseVault] = getPoolVaultPDA(
152
+ poolState,
153
+ baseMint,
154
+ config.liquidSwapProgramId
155
+ );
156
+ const [quoteVault] = getPoolVaultPDA(
157
+ poolState,
158
+ quoteMint,
159
+ config.liquidSwapProgramId
160
+ );
161
+ const [observation] = getObservationPDA(
162
+ poolState,
163
+ config.liquidSwapProgramId
164
+ );
165
+ const [ammConfig] = getSwapGlobalConfigPDA(config.liquidSwapProgramId);
166
+ const [ammAuthority] = getSwapAuthorityPDA(config.liquidSwapProgramId);
167
+ const [feeConfig] = getFeeConfigPDA(baseMint, config.liquidFeesProgramId);
168
+ const [tokenVolume] = getTokenVolumePDA(
169
+ baseMint,
170
+ config.liquidStateProgramId
171
+ );
172
+
173
+ return {
174
+ poolState,
175
+ lpMint,
176
+ baseVault,
177
+ quoteVault,
178
+ observation,
179
+ ammConfig,
180
+ ammAuthority,
181
+ feeConfig,
182
+ tokenVolume,
183
+ };
184
+ };
185
+
186
+ /** All PDAs needed for bonding curve to AMM migration */
187
+ export interface MigrationPDAs {
188
+ bondingCurve: PublicKey;
189
+ ammPoolState: PublicKey;
190
+ ammLpMint: PublicKey;
191
+ ammBaseVault: PublicKey;
192
+ ammQuoteVault: PublicKey;
193
+ ammObservation: PublicKey;
194
+ ammConfig: PublicKey;
195
+ ammAuthority: PublicKey;
196
+ ammBuybackVault: PublicKey;
197
+ }
198
+
199
+ /**
200
+ * Derives all PDAs needed for migration from bonding curve to AMM.
201
+ *
202
+ * @param tokenMint - Token mint address
203
+ * @param config - Liquid protocol configuration
204
+ * @returns All migration-related PDAs
205
+ */
206
+ export const getMigrationPDAs = (
207
+ tokenMint: PublicKey,
208
+ config: LiquidConfig = MAINNET_CONFIG
209
+ ): MigrationPDAs => {
210
+ const [bondingCurve] = getBondingCurvePDA(
211
+ tokenMint,
212
+ config.liquidProgramId
213
+ );
214
+ const [ammPoolState] = getPoolPDA(
215
+ tokenMint,
216
+ WSOL_MINT,
217
+ config.liquidSwapProgramId
218
+ );
219
+ const [ammLpMint] = getPoolLpMintPDA(
220
+ ammPoolState,
221
+ config.liquidSwapProgramId
222
+ );
223
+ const [ammBaseVault] = getPoolVaultPDA(
224
+ ammPoolState,
225
+ tokenMint,
226
+ config.liquidSwapProgramId
227
+ );
228
+ const [ammQuoteVault] = getPoolVaultPDA(
229
+ ammPoolState,
230
+ WSOL_MINT,
231
+ config.liquidSwapProgramId
232
+ );
233
+ const [ammObservation] = getObservationPDA(
234
+ ammPoolState,
235
+ config.liquidSwapProgramId
236
+ );
237
+ const [ammConfig] = getSwapGlobalConfigPDA(config.liquidSwapProgramId);
238
+ const [ammAuthority] = getSwapAuthorityPDA(config.liquidSwapProgramId);
239
+ const [ammBuybackVault] = getSwapBuybackVaultPDA(
240
+ ammPoolState,
241
+ WSOL_MINT,
242
+ config.liquidSwapProgramId
243
+ );
244
+
245
+ return {
246
+ bondingCurve,
247
+ ammPoolState,
248
+ ammLpMint,
249
+ ammBaseVault,
250
+ ammQuoteVault,
251
+ ammObservation,
252
+ ammConfig,
253
+ ammAuthority,
254
+ ammBuybackVault,
255
+ };
256
+ };
257
+
258
+ /** All PDAs needed for token bonding curve to AMM migration */
259
+ export interface TokenMigrationPDAs {
260
+ bondingCurve: PublicKey;
261
+ ammPoolState: PublicKey;
262
+ ammLpMint: PublicKey;
263
+ ammBaseVault: PublicKey;
264
+ ammQuoteVault: PublicKey;
265
+ ammObservation: PublicKey;
266
+ ammConfig: PublicKey;
267
+ ammAuthority: PublicKey;
268
+ ammBuybackVault: PublicKey;
269
+ }
270
+
271
+ /**
272
+ * Derives all PDAs needed for migration from token bonding curve to AMM.
273
+ *
274
+ * @param tokenMint - Token mint address
275
+ * @param quoteMint - Quote token mint address (e.g., USDC)
276
+ * @param config - Liquid protocol configuration
277
+ * @returns All migration-related PDAs
278
+ */
279
+ export const getTokenMigrationPDAs = (
280
+ tokenMint: PublicKey,
281
+ quoteMint: PublicKey,
282
+ config: LiquidConfig
283
+ ): TokenMigrationPDAs => {
284
+ const [bondingCurve] = getStableBondingCurvePDA(
285
+ tokenMint,
286
+ quoteMint,
287
+ config.liquidProgramId
288
+ );
289
+ const [ammPoolState] = getPoolPDA(
290
+ tokenMint,
291
+ quoteMint,
292
+ config.liquidSwapProgramId
293
+ );
294
+ const [ammLpMint] = getPoolLpMintPDA(
295
+ ammPoolState,
296
+ config.liquidSwapProgramId
297
+ );
298
+ const [ammBaseVault] = getPoolVaultPDA(
299
+ ammPoolState,
300
+ tokenMint,
301
+ config.liquidSwapProgramId
302
+ );
303
+ const [ammQuoteVault] = getPoolVaultPDA(
304
+ ammPoolState,
305
+ quoteMint,
306
+ config.liquidSwapProgramId
307
+ );
308
+ const [ammObservation] = getObservationPDA(
309
+ ammPoolState,
310
+ config.liquidSwapProgramId
311
+ );
312
+ const [ammConfig] = getSwapGlobalConfigPDA(config.liquidSwapProgramId);
313
+ const [ammAuthority] = getSwapAuthorityPDA(config.liquidSwapProgramId);
314
+ const [ammBuybackVault] = getSwapBuybackVaultPDA(
315
+ ammPoolState,
316
+ quoteMint,
317
+ config.liquidSwapProgramId
318
+ );
319
+
320
+ return {
321
+ bondingCurve,
322
+ ammPoolState,
323
+ ammLpMint,
324
+ ammBaseVault,
325
+ ammQuoteVault,
326
+ ammObservation,
327
+ ammConfig,
328
+ ammAuthority,
329
+ ammBuybackVault,
330
+ };
331
+ };
@@ -0,0 +1,58 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+
3
+ const SEED_FEE_CONFIG = Buffer.from("fee_config");
4
+ const SEED_FEE_VAULT = Buffer.from("fee_vault");
5
+ const SEED_RECIPIENT_VAULT = Buffer.from("recipient_vault");
6
+
7
+ /**
8
+ * Derives the fee config PDA for a token mint.
9
+ *
10
+ * @param tokenMint - Token mint address
11
+ * @param programId - Liquid Fees program ID
12
+ * @returns Tuple of [PDA address, bump seed]
13
+ */
14
+ export const getFeeConfigPDA = (
15
+ tokenMint: PublicKey,
16
+ programId: PublicKey
17
+ ): [PublicKey, number] => {
18
+ return PublicKey.findProgramAddressSync(
19
+ [SEED_FEE_CONFIG, tokenMint.toBuffer()],
20
+ programId
21
+ );
22
+ };
23
+
24
+ /**
25
+ * Derives the fee vault PDA for a fee config.
26
+ *
27
+ * @param feeConfig - Fee config PDA address
28
+ * @param programId - Liquid Fees program ID
29
+ * @returns Tuple of [PDA address, bump seed]
30
+ */
31
+ export const getFeeVaultPDA = (
32
+ feeConfig: PublicKey,
33
+ programId: PublicKey
34
+ ): [PublicKey, number] => {
35
+ return PublicKey.findProgramAddressSync(
36
+ [SEED_FEE_VAULT, feeConfig.toBuffer()],
37
+ programId
38
+ );
39
+ };
40
+
41
+ /**
42
+ * Derives the recipient vault PDA for a specific recipient within a fee config.
43
+ *
44
+ * @param feeConfig - Fee config PDA address
45
+ * @param recipient - Recipient wallet address
46
+ * @param programId - Liquid Fees program ID
47
+ * @returns Tuple of [PDA address, bump seed]
48
+ */
49
+ export const getRecipientVaultPDA = (
50
+ feeConfig: PublicKey,
51
+ recipient: PublicKey,
52
+ programId: PublicKey
53
+ ): [PublicKey, number] => {
54
+ return PublicKey.findProgramAddressSync(
55
+ [SEED_RECIPIENT_VAULT, feeConfig.toBuffer(), recipient.toBuffer()],
56
+ programId
57
+ );
58
+ };