@liquid-af/sdk 0.10.3 → 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.
- package/dist/client.d.ts +8 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +13 -0
- package/dist/client.js.map +1 -1
- package/dist/helpers/cashback.d.ts +14 -0
- package/dist/helpers/cashback.d.ts.map +1 -0
- package/dist/helpers/cashback.js +20 -0
- package/dist/helpers/cashback.js.map +1 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/index.js.map +1 -1
- package/dist/idl/liquid.d.ts +2491 -2491
- package/dist/idl/liquid.d.ts.map +1 -1
- package/dist/idl/liquid.json +2491 -2491
- package/dist/idl/liquid_events.d.ts +1 -1
- package/dist/idl/liquid_events.json +1 -1
- package/dist/idl/liquid_fees.d.ts +8 -8
- package/dist/idl/liquid_fees.json +8 -8
- package/dist/idl/liquid_state.d.ts +72 -72
- package/dist/idl/liquid_state.d.ts.map +1 -1
- package/dist/idl/liquid_state.json +72 -72
- package/dist/idl/liquid_swap.d.ts +998 -998
- package/dist/idl/liquid_swap.d.ts.map +1 -1
- package/dist/idl/liquid_swap.json +998 -998
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/math/cashback.d.ts +41 -0
- package/dist/math/cashback.d.ts.map +1 -0
- package/dist/math/cashback.js +86 -0
- package/dist/math/cashback.js.map +1 -0
- package/dist/math/index.d.ts +2 -0
- package/dist/math/index.d.ts.map +1 -1
- package/dist/math/index.js +1 -0
- package/dist/math/index.js.map +1 -1
- package/dist/types.d.ts +24 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +20 -0
- package/src/helpers/cashback.ts +28 -0
- package/src/helpers/index.ts +1 -0
- package/src/idl/liquid.json +2491 -2491
- package/src/idl/liquid.ts +2491 -2491
- package/src/idl/liquid_events.json +1 -1
- package/src/idl/liquid_events.ts +1 -1
- package/src/idl/liquid_fees.json +8 -8
- package/src/idl/liquid_fees.ts +8 -8
- package/src/idl/liquid_state.json +72 -72
- package/src/idl/liquid_state.ts +72 -72
- package/src/idl/liquid_swap.json +998 -998
- package/src/idl/liquid_swap.ts +998 -998
- package/src/index.ts +2 -0
- package/src/math/cashback.ts +124 -0
- package/src/math/index.ts +3 -0
- package/src/types.ts +26 -0
package/src/index.ts
CHANGED
|
@@ -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
package/src/types.ts
CHANGED
|
@@ -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;
|