@liquid-af/sdk 0.10.2 → 0.10.4

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 (63) hide show
  1. package/README.md +2 -2
  2. package/dist/accounts/liquid-state.d.ts +10 -10
  3. package/dist/client.d.ts +18 -10
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/client.js +13 -0
  6. package/dist/client.js.map +1 -1
  7. package/dist/events/types.d.ts +1 -2
  8. package/dist/events/types.d.ts.map +1 -1
  9. package/dist/helpers/cashback.d.ts +14 -0
  10. package/dist/helpers/cashback.d.ts.map +1 -0
  11. package/dist/helpers/cashback.js +20 -0
  12. package/dist/helpers/cashback.js.map +1 -0
  13. package/dist/helpers/index.d.ts +1 -0
  14. package/dist/helpers/index.d.ts.map +1 -1
  15. package/dist/helpers/index.js +1 -0
  16. package/dist/helpers/index.js.map +1 -1
  17. package/dist/idl/liquid.d.ts +2557 -2550
  18. package/dist/idl/liquid.d.ts.map +1 -1
  19. package/dist/idl/liquid.json +2557 -2550
  20. package/dist/idl/liquid_events.d.ts +4 -11
  21. package/dist/idl/liquid_events.d.ts.map +1 -1
  22. package/dist/idl/liquid_events.json +4 -11
  23. package/dist/idl/liquid_fees.d.ts +20 -31
  24. package/dist/idl/liquid_fees.d.ts.map +1 -1
  25. package/dist/idl/liquid_fees.json +20 -31
  26. package/dist/idl/liquid_state.d.ts +180 -136
  27. package/dist/idl/liquid_state.d.ts.map +1 -1
  28. package/dist/idl/liquid_state.json +180 -136
  29. package/dist/idl/liquid_swap.d.ts +1017 -1010
  30. package/dist/idl/liquid_swap.d.ts.map +1 -1
  31. package/dist/idl/liquid_swap.json +1017 -1010
  32. package/dist/index.d.ts +1 -1
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js.map +1 -1
  35. package/dist/math/cashback.d.ts +41 -0
  36. package/dist/math/cashback.d.ts.map +1 -0
  37. package/dist/math/cashback.js +86 -0
  38. package/dist/math/cashback.js.map +1 -0
  39. package/dist/math/index.d.ts +2 -0
  40. package/dist/math/index.d.ts.map +1 -1
  41. package/dist/math/index.js +1 -0
  42. package/dist/math/index.js.map +1 -1
  43. package/dist/types.d.ts +29 -5
  44. package/dist/types.d.ts.map +1 -1
  45. package/package.json +1 -1
  46. package/src/client.ts +20 -0
  47. package/src/events/types.ts +1 -2
  48. package/src/helpers/cashback.ts +28 -0
  49. package/src/helpers/index.ts +1 -0
  50. package/src/idl/liquid.json +2557 -2550
  51. package/src/idl/liquid.ts +2557 -2550
  52. package/src/idl/liquid_events.json +4 -11
  53. package/src/idl/liquid_events.ts +4 -11
  54. package/src/idl/liquid_fees.json +20 -31
  55. package/src/idl/liquid_fees.ts +20 -31
  56. package/src/idl/liquid_state.json +180 -136
  57. package/src/idl/liquid_state.ts +180 -136
  58. package/src/idl/liquid_swap.json +1017 -1010
  59. package/src/idl/liquid_swap.ts +1017 -1010
  60. package/src/index.ts +2 -0
  61. package/src/math/cashback.ts +124 -0
  62. package/src/math/index.ts +3 -0
  63. package/src/types.ts +31 -5
package/src/index.ts CHANGED
@@ -45,6 +45,8 @@ export type {
45
45
  GlobalAmmVolume,
46
46
  CashbackRange,
47
47
  CashbackConfiguration,
48
+ CashbackBreakdown,
49
+ TierBreakdown,
48
50
  FeeDistribution,
49
51
  BondingCurveBuyResult,
50
52
  BondingCurveSellResult,
@@ -0,0 +1,124 @@
1
+ import BN from "bn.js";
2
+ import { BPS_DENOMINATOR } from "./constants.js";
3
+ import type { CashbackRange } from "../types.js";
4
+
5
+ /** Per-tier breakdown of cashback earnings */
6
+ export interface TierBreakdown {
7
+ /** Index of this range in the config's ranges array */
8
+ rangeIndex: number;
9
+ /** Multiplier in basis points (10000 = 1x) */
10
+ multiplierBps: number;
11
+ /** Amount of cashback_credits attributed to this tier (post-multiplier) */
12
+ postMultiplierAmount: BN;
13
+ /** The base (1x-equivalent) earning that produced this tier's contribution */
14
+ baseAmount: BN;
15
+ /** Bonus from multiplier: postMultiplierAmount - baseAmount */
16
+ bonusAmount: BN;
17
+ }
18
+
19
+ /** Breakdown of a user's cashback balance into base earnings vs multiplier bonus */
20
+ export interface CashbackBreakdown {
21
+ /** The user's total cashback_credits balance */
22
+ totalBalance: BN;
23
+ /** What the user would have earned at 1x multiplier across all tiers */
24
+ baseEarnings: BN;
25
+ /** Total bonus from multipliers: totalBalance - baseEarnings */
26
+ multiplierBonus: BN;
27
+ /** Per-tier breakdown (only tiers with non-zero contribution) */
28
+ tiers: TierBreakdown[];
29
+ }
30
+
31
+ const ZERO = new BN(0);
32
+ const BPS = new BN(BPS_DENOMINATOR);
33
+
34
+ /**
35
+ * Reverses the on-chain progressive multiplier to break down a cashback balance
36
+ * into base earnings vs multiplier bonus.
37
+ *
38
+ * The on-chain `apply_balance_multiplier` advances through ranges by pre-multiplier
39
+ * portions, so each tier of width W with multiplier M contributes W * M / 10000 to
40
+ * the stored balance. This function walks the same ranges in reverse to decompose
41
+ * the final balance.
42
+ *
43
+ * @param cashbackCredits - The user's current cashback_credits (post-multiplier balance)
44
+ * @param ranges - Balance-based multiplier ranges from CashbackConfiguration
45
+ * @returns Breakdown of base earnings vs multiplier bonus, with per-tier detail
46
+ */
47
+ export function getCashbackBreakdown(
48
+ cashbackCredits: BN,
49
+ ranges: CashbackRange[],
50
+ ): CashbackBreakdown {
51
+ if (cashbackCredits.isZero() || ranges.length === 0) {
52
+ return {
53
+ totalBalance: cashbackCredits.clone(),
54
+ baseEarnings: cashbackCredits.clone(),
55
+ multiplierBonus: ZERO.clone(),
56
+ tiers: [],
57
+ };
58
+ }
59
+
60
+ const tiers: TierBreakdown[] = [];
61
+ let cumulativePostMult = ZERO.clone();
62
+ let totalBase = ZERO.clone();
63
+ let remaining = cashbackCredits.clone();
64
+
65
+ for (let i = 0; i < ranges.length && remaining.gt(ZERO); i++) {
66
+ const range = ranges[i];
67
+ const multiplierBps = range.multiplierBasisPoints;
68
+ const width = range.rangeEndExclusive.sub(range.rangeStartInclusive);
69
+
70
+ // Post-multiplier contribution if this tier is fully filled
71
+ const tierPostMult = width.mul(new BN(multiplierBps)).div(BPS);
72
+
73
+ if (remaining.gte(tierPostMult)) {
74
+ // Tier fully filled
75
+ tiers.push({
76
+ rangeIndex: i,
77
+ multiplierBps,
78
+ postMultiplierAmount: tierPostMult.clone(),
79
+ baseAmount: width.clone(),
80
+ bonusAmount: tierPostMult.sub(width),
81
+ });
82
+ totalBase = totalBase.add(width);
83
+ cumulativePostMult = cumulativePostMult.add(tierPostMult);
84
+ remaining = remaining.sub(tierPostMult);
85
+ } else {
86
+ // Partially filled — solve for pre-mult portion:
87
+ // postMultInTier = p * M / 10000, so p = postMultInTier * 10000 / M
88
+ const postMultInTier = remaining.clone();
89
+ const baseInTier = postMultInTier.mul(BPS).div(new BN(multiplierBps));
90
+
91
+ tiers.push({
92
+ rangeIndex: i,
93
+ multiplierBps,
94
+ postMultiplierAmount: postMultInTier,
95
+ baseAmount: baseInTier.clone(),
96
+ bonusAmount: postMultInTier.sub(baseInTier),
97
+ });
98
+ totalBase = totalBase.add(baseInTier);
99
+ remaining = ZERO.clone();
100
+ }
101
+ }
102
+
103
+ // Balance beyond all defined ranges — use last range's multiplier
104
+ if (remaining.gt(ZERO)) {
105
+ const lastMultiplier = ranges[ranges.length - 1].multiplierBasisPoints;
106
+ const baseRemaining = remaining.mul(BPS).div(new BN(lastMultiplier));
107
+
108
+ tiers.push({
109
+ rangeIndex: ranges.length,
110
+ multiplierBps: lastMultiplier,
111
+ postMultiplierAmount: remaining.clone(),
112
+ baseAmount: baseRemaining.clone(),
113
+ bonusAmount: remaining.sub(baseRemaining),
114
+ });
115
+ totalBase = totalBase.add(baseRemaining);
116
+ }
117
+
118
+ return {
119
+ totalBalance: cashbackCredits.clone(),
120
+ baseEarnings: totalBase,
121
+ multiplierBonus: cashbackCredits.sub(totalBase),
122
+ tiers,
123
+ };
124
+ }
package/src/math/index.ts CHANGED
@@ -38,3 +38,6 @@ export {
38
38
  calculateFeesForPool,
39
39
  WSOL_MINT,
40
40
  } from "./tiered-fees.js";
41
+
42
+ export { getCashbackBreakdown } from "./cashback.js";
43
+ export type { CashbackBreakdown, TierBreakdown } from "./cashback.js";
package/src/types.ts CHANGED
@@ -188,7 +188,7 @@ export interface ReferrerInfo {
188
188
  /** User properties account */
189
189
  export interface UserProperties {
190
190
  referrer: ReferrerInfo | null;
191
- totalSolVolume: BN;
191
+ totalUsdVolume: BN;
192
192
  totalTokenVolume: BN;
193
193
  cashbackCredits: BN;
194
194
  isSpending: boolean;
@@ -205,7 +205,7 @@ export interface UserSnapshot {
205
205
  user: PublicKey;
206
206
  index: number;
207
207
  prevSnapshot: PublicKey | null;
208
- solVolume: BN;
208
+ usdVolume: BN;
209
209
  tokenVolume: BN;
210
210
  createdAt: BN;
211
211
  }
@@ -226,7 +226,7 @@ export interface AdminUpdateUserArgs {
226
226
  /** Per-token volume accumulator */
227
227
  export interface TokenVolumeAccumulator {
228
228
  mint: PublicKey;
229
- totalSolVolume: BN;
229
+ totalUsdVolume: BN;
230
230
  totalTokenVolume: BN;
231
231
  curveSolVolume: BN;
232
232
  ammSolVolume: BN;
@@ -234,13 +234,13 @@ export interface TokenVolumeAccumulator {
234
234
 
235
235
  /** Global curve volume accumulator */
236
236
  export interface GlobalCurveVolume {
237
- totalSolVolume: BN;
237
+ totalUsdVolume: BN;
238
238
  totalTokenVolume: BN;
239
239
  }
240
240
 
241
241
  /** Global AMM volume accumulator */
242
242
  export interface GlobalAmmVolume {
243
- totalSolVolume: BN;
243
+ totalUsdVolume: BN;
244
244
  totalTokenVolume: BN;
245
245
  }
246
246
 
@@ -259,6 +259,32 @@ export interface CashbackConfiguration {
259
259
  traderCashbackBasisPoints: number;
260
260
  }
261
261
 
262
+ /** Per-tier breakdown of cashback earnings */
263
+ export interface TierBreakdown {
264
+ /** Index of this range in the config's ranges array */
265
+ rangeIndex: number;
266
+ /** Multiplier in basis points (10000 = 1x) */
267
+ multiplierBps: number;
268
+ /** Amount of cashback_credits attributed to this tier (post-multiplier) */
269
+ postMultiplierAmount: BN;
270
+ /** The base (1x-equivalent) earning that produced this tier's contribution */
271
+ baseAmount: BN;
272
+ /** Bonus from multiplier: postMultiplierAmount - baseAmount */
273
+ bonusAmount: BN;
274
+ }
275
+
276
+ /** Breakdown of a user's cashback balance into base earnings vs multiplier bonus */
277
+ export interface CashbackBreakdown {
278
+ /** The user's total cashback_credits balance */
279
+ totalBalance: BN;
280
+ /** What the user would have earned at 1x multiplier across all tiers */
281
+ baseEarnings: BN;
282
+ /** Total bonus from multipliers: totalBalance - baseEarnings */
283
+ multiplierBonus: BN;
284
+ /** Per-tier breakdown (only tiers with non-zero contribution) */
285
+ tiers: TierBreakdown[];
286
+ }
287
+
262
288
  /** Fee breakdown from a trade */
263
289
  export interface FeeDistribution {
264
290
  protocolFee: BN;