@gearbox-protocol/sdk 13.0.0-beta.4 → 13.0.0-beta.5
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/cjs/common-utils/index.js +22 -0
- package/dist/cjs/common-utils/package.json +1 -0
- package/dist/cjs/{sdk → common-utils}/utils/assetsMath.js +78 -12
- package/dist/cjs/common-utils/utils/bigintMath.js +65 -0
- package/dist/cjs/common-utils/utils/creditAccount/calcHealthFactor.js +76 -0
- package/dist/cjs/common-utils/utils/creditAccount/calcOverallAPY.js +81 -0
- package/dist/cjs/{sdk/utils/priceMath.js → common-utils/utils/creditAccount/calcQuotaBorrowRate.js} +19 -12
- package/dist/cjs/{sdk/utils/bigintMath.js → common-utils/utils/creditAccount/calcRelativeBaseBorrowRate.js} +11 -10
- package/dist/cjs/common-utils/utils/creditAccount/debt.js +64 -0
- package/dist/cjs/common-utils/utils/creditAccount/getTimeToLiquidation.js +38 -0
- package/dist/cjs/common-utils/utils/creditAccount/index.js +38 -0
- package/dist/cjs/common-utils/utils/creditAccount/liquidationPrice.js +47 -0
- package/dist/cjs/common-utils/utils/creditAccount/quotaUtils.js +149 -0
- package/dist/cjs/common-utils/utils/creditAccount/sort.js +95 -0
- package/dist/cjs/common-utils/utils/creditAccount/types.js +16 -0
- package/dist/cjs/{sdk → common-utils}/utils/endpoints.js +11 -17
- package/dist/cjs/common-utils/utils/index.js +30 -0
- package/dist/cjs/common-utils/utils/priceMath.js +66 -0
- package/dist/cjs/sdk/utils/index.js +0 -10
- package/dist/esm/common-utils/index.js +1 -0
- package/dist/esm/common-utils/package.json +1 -0
- package/dist/esm/{sdk → common-utils}/utils/assetsMath.js +78 -12
- package/dist/esm/common-utils/utils/bigintMath.js +41 -0
- package/dist/esm/common-utils/utils/creditAccount/calcHealthFactor.js +55 -0
- package/dist/esm/common-utils/utils/creditAccount/calcOverallAPY.js +60 -0
- package/dist/esm/common-utils/utils/creditAccount/calcQuotaBorrowRate.js +18 -0
- package/dist/esm/common-utils/utils/creditAccount/calcRelativeBaseBorrowRate.js +10 -0
- package/dist/esm/common-utils/utils/creditAccount/debt.js +43 -0
- package/dist/esm/common-utils/utils/creditAccount/getTimeToLiquidation.js +18 -0
- package/dist/esm/common-utils/utils/creditAccount/index.js +9 -0
- package/dist/esm/common-utils/utils/creditAccount/liquidationPrice.js +23 -0
- package/dist/esm/common-utils/utils/creditAccount/quotaUtils.js +125 -0
- package/dist/esm/common-utils/utils/creditAccount/sort.js +67 -0
- package/dist/esm/common-utils/utils/creditAccount/types.js +0 -0
- package/dist/esm/{sdk → common-utils}/utils/endpoints.js +9 -14
- package/dist/esm/common-utils/utils/index.js +5 -0
- package/dist/esm/common-utils/utils/priceMath.js +42 -0
- package/dist/esm/sdk/utils/index.js +0 -5
- package/dist/types/common-utils/index.d.ts +1 -0
- package/dist/types/common-utils/utils/assetsMath.d.ts +114 -0
- package/dist/types/common-utils/utils/bigintMath.d.ts +43 -0
- package/dist/types/common-utils/utils/creditAccount/calcHealthFactor.d.ts +25 -0
- package/dist/types/common-utils/utils/creditAccount/calcOverallAPY.d.ts +37 -0
- package/dist/types/common-utils/utils/creditAccount/calcQuotaBorrowRate.d.ts +18 -0
- package/dist/types/common-utils/utils/creditAccount/calcRelativeBaseBorrowRate.d.ts +15 -0
- package/dist/types/common-utils/utils/creditAccount/debt.d.ts +35 -0
- package/dist/types/common-utils/utils/creditAccount/getTimeToLiquidation.d.ts +16 -0
- package/dist/types/common-utils/utils/creditAccount/index.d.ts +9 -0
- package/dist/types/common-utils/utils/creditAccount/liquidationPrice.d.ts +25 -0
- package/dist/types/common-utils/utils/creditAccount/quotaUtils.d.ts +81 -0
- package/dist/types/common-utils/utils/creditAccount/sort.d.ts +55 -0
- package/dist/types/common-utils/utils/creditAccount/types.d.ts +18 -0
- package/dist/types/{sdk → common-utils}/utils/endpoints.d.ts +13 -5
- package/dist/types/common-utils/utils/index.d.ts +5 -0
- package/dist/types/common-utils/utils/priceMath.d.ts +47 -0
- package/dist/types/sdk/utils/index.d.ts +0 -5
- package/package.json +6 -1
- package/dist/cjs/sdk/utils/creditAccount.js +0 -411
- package/dist/esm/sdk/utils/bigintMath.js +0 -9
- package/dist/esm/sdk/utils/creditAccount.js +0 -396
- package/dist/esm/sdk/utils/priceMath.js +0 -11
- package/dist/types/sdk/utils/assetsMath.d.ts +0 -42
- package/dist/types/sdk/utils/bigintMath.d.ts +0 -6
- package/dist/types/sdk/utils/creditAccount.d.ts +0 -128
- package/dist/types/sdk/utils/priceMath.d.ts +0 -9
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility namespace for common `bigint` operations.
|
|
3
|
+
*
|
|
4
|
+
* The constructor is private because this class is not meant to be
|
|
5
|
+
* instantiated; all helpers are exposed as static functions.
|
|
6
|
+
*/
|
|
7
|
+
export declare class BigIntMath {
|
|
8
|
+
private constructor();
|
|
9
|
+
/**
|
|
10
|
+
* Returns the absolute (non-negative) value of a bigint.
|
|
11
|
+
*
|
|
12
|
+
* @param x Input value.
|
|
13
|
+
* @returns `x` when `x` is already non-negative, otherwise `-x`.
|
|
14
|
+
*/
|
|
15
|
+
static abs: (x: bigint) => bigint;
|
|
16
|
+
/**
|
|
17
|
+
* Returns the greater of two bigint values.
|
|
18
|
+
*
|
|
19
|
+
* @param a First candidate value.
|
|
20
|
+
* @param b Second candidate value.
|
|
21
|
+
* @returns The larger value between `a` and `b`.
|
|
22
|
+
*/
|
|
23
|
+
static max: (a: bigint, b: bigint) => bigint;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the smaller of two bigint values.
|
|
26
|
+
*
|
|
27
|
+
* @param a First candidate value.
|
|
28
|
+
* @param b Second candidate value.
|
|
29
|
+
* @returns The smaller value between `a` and `b`.
|
|
30
|
+
*/
|
|
31
|
+
static min: (a: bigint, b: bigint) => bigint;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the negative form of a bigint if it is currently positive.
|
|
34
|
+
*
|
|
35
|
+
* Useful when a value should be represented as an outflow/debit:
|
|
36
|
+
* - positive values become negative
|
|
37
|
+
* - zero and negative values are returned unchanged
|
|
38
|
+
*
|
|
39
|
+
* @param a Input value.
|
|
40
|
+
* @returns A non-positive bigint representation of `a`.
|
|
41
|
+
*/
|
|
42
|
+
static neg: (a: bigint) => bigint;
|
|
43
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import { type Asset } from "../../../sdk/index.js";
|
|
3
|
+
import type { QuotaInfoIsActiveSlice, TokenDataSlice } from "./types.js";
|
|
4
|
+
export interface CalcHealthFactorProps {
|
|
5
|
+
assets: Array<Asset>;
|
|
6
|
+
quotas: Record<Address, Asset>;
|
|
7
|
+
quotasInfo: Record<Address, QuotaInfoIsActiveSlice>;
|
|
8
|
+
prices: Record<Address, bigint>;
|
|
9
|
+
liquidationThresholds: Record<Address, bigint>;
|
|
10
|
+
underlyingToken: Address;
|
|
11
|
+
debt: bigint;
|
|
12
|
+
tokensList: Record<Address, TokenDataSlice>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Computes account health factor in percentage-factor units.
|
|
16
|
+
*
|
|
17
|
+
* The function evaluates collateral value under liquidation thresholds,
|
|
18
|
+
* applies quota caps for quoted tokens, and compares the resulting
|
|
19
|
+
* liquidation-adjusted collateral against borrowed value.
|
|
20
|
+
*
|
|
21
|
+
* @param props Credit account balances, quotas, prices, thresholds, and debt context.
|
|
22
|
+
* @returns Health factor as a number in `PERCENTAGE_FACTOR` scale,
|
|
23
|
+
* or `65535` when debt is zero.
|
|
24
|
+
*/
|
|
25
|
+
export declare function calcHealthFactor({ assets, quotas, quotasInfo, liquidationThresholds, underlyingToken, debt, prices, tokensList, }: CalcHealthFactorProps): number;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import { type Asset } from "../../../sdk/index.js";
|
|
3
|
+
import type { QuotaInfoSlice, TokenDataSlice } from "./types.js";
|
|
4
|
+
export interface CalcOverallAPYProps {
|
|
5
|
+
caAssets: Array<Asset>;
|
|
6
|
+
lpAPY: Record<Address, number> | undefined;
|
|
7
|
+
quotas: Record<Address, Asset>;
|
|
8
|
+
quotaRates: Record<Address, QuotaInfoSlice>;
|
|
9
|
+
feeInterest: number;
|
|
10
|
+
prices: Record<Address, bigint>;
|
|
11
|
+
totalValue: bigint | undefined;
|
|
12
|
+
debt: bigint | undefined;
|
|
13
|
+
baseRateWithFee: number;
|
|
14
|
+
underlyingToken: Address;
|
|
15
|
+
tokensList: Record<Address, TokenDataSlice>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Computes net portfolio APY for a credit account.
|
|
19
|
+
*
|
|
20
|
+
* The resulting value combines:
|
|
21
|
+
* - positive LP yield from supplied assets
|
|
22
|
+
* - negative quota borrowing cost (with interest fee)
|
|
23
|
+
* - negative base debt borrowing cost
|
|
24
|
+
* and normalizes by user-owned equity (`totalValue - debt`) in underlying value terms.
|
|
25
|
+
*
|
|
26
|
+
* ```
|
|
27
|
+
* [
|
|
28
|
+
* Sum(amount_i * price_i * apy_i - quota_i * quotaPrice * quotaRate_i * (1 + feeInterest)) -
|
|
29
|
+
* debt * debtPrice * baseRateWithFee
|
|
30
|
+
* ] / (totalValue - debt) * debtPrice
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param props Credit account assets, rates, prices, debt, and token metadata.
|
|
34
|
+
* @returns APY in percentage-factor scale as `bigint`, or `undefined`
|
|
35
|
+
* when required inputs are missing or invalid.
|
|
36
|
+
*/
|
|
37
|
+
export declare function calcOverallAPY({ caAssets, lpAPY, prices, quotas, quotaRates, feeInterest, totalValue, debt, baseRateWithFee, underlyingToken, tokensList, }: CalcOverallAPYProps): bigint | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import type { Asset } from "../../../sdk/index.js";
|
|
3
|
+
import type { QuotaInfoSlice } from "./types.js";
|
|
4
|
+
export interface CalcQuotaBorrowRateProps {
|
|
5
|
+
quotas: Record<Address, Asset>;
|
|
6
|
+
quotaRates: Record<Address, QuotaInfoSlice>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Aggregates weighted quota borrow rate exposure across active quotas.
|
|
10
|
+
*
|
|
11
|
+
* For each quota token, contribution is:
|
|
12
|
+
* `activeQuotaBalance * quotaRate`.
|
|
13
|
+
* Inactive quotas are ignored.
|
|
14
|
+
*
|
|
15
|
+
* @param props Quota balances and per-token quota rates.
|
|
16
|
+
* @returns Sum of `balance * rate` terms in percentage-factor scale.
|
|
17
|
+
*/
|
|
18
|
+
export declare function calcQuotaBorrowRate({ quotas, quotaRates, }: CalcQuotaBorrowRateProps): bigint;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface CalcRelativeBaseBorrowRateProps {
|
|
2
|
+
debt: bigint;
|
|
3
|
+
baseRateWithFee: number;
|
|
4
|
+
assetAmountInUnderlying: bigint;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Computes base borrow-rate impact relative to asset amount.
|
|
8
|
+
*
|
|
9
|
+
* This helper is used as an intermediate weighted term:
|
|
10
|
+
* `debt * baseRateWithFee * assetAmountInUnderlying`.
|
|
11
|
+
*
|
|
12
|
+
* @param props Debt amount, base borrow rate (with fee), and normalized asset amount.
|
|
13
|
+
* @returns Relative borrow-rate contribution in bigint arithmetic.
|
|
14
|
+
*/
|
|
15
|
+
export declare function calcRelativeBaseBorrowRate({ debt, baseRateWithFee, assetAmountInUnderlying, }: CalcRelativeBaseBorrowRateProps): bigint;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import { type Asset } from "../../../sdk/index.js";
|
|
3
|
+
import type { TokenDataSlice } from "./types.js";
|
|
4
|
+
export interface CalcMaxLendingDebtProps {
|
|
5
|
+
assets: Array<Asset>;
|
|
6
|
+
prices: Record<Address, bigint>;
|
|
7
|
+
liquidationThresholds: Record<Address, bigint>;
|
|
8
|
+
underlyingToken: Address;
|
|
9
|
+
tokensList: Record<Address, TokenDataSlice>;
|
|
10
|
+
targetHF?: bigint;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Calculates additional debt that can be borrowed while targeting
|
|
14
|
+
* a minimum health factor after the borrow.
|
|
15
|
+
*
|
|
16
|
+
* The derivation comes from the post-borrow HF equation and clamps
|
|
17
|
+
* negative results to zero.
|
|
18
|
+
*
|
|
19
|
+
* @param healthFactor Current health factor in percentage-factor units.
|
|
20
|
+
* @param debt Current debt amount.
|
|
21
|
+
* @param underlyingLT Liquidation threshold of underlying token.
|
|
22
|
+
* @param minHf Target minimum health factor (defaults to `PERCENTAGE_FACTOR`).
|
|
23
|
+
* @returns Maximum non-negative debt increase.
|
|
24
|
+
*/
|
|
25
|
+
export declare function calcMaxDebtIncrease(healthFactor: number, debt: bigint, underlyingLT: number, minHf?: number): bigint;
|
|
26
|
+
/**
|
|
27
|
+
* Calculates maximum debt capacity for lending based on collateral mix.
|
|
28
|
+
*
|
|
29
|
+
* It computes liquidation-threshold-weighted collateral value and converts
|
|
30
|
+
* it into underlying-token debt units at the requested target health factor.
|
|
31
|
+
*
|
|
32
|
+
* @param props Asset balances, prices, thresholds, target HF, and token metadata.
|
|
33
|
+
* @returns Maximum borrowable debt amount in underlying token units.
|
|
34
|
+
*/
|
|
35
|
+
export declare function calcMaxLendingDebt({ assets, liquidationThresholds, underlyingToken, prices, tokensList, targetHF, }: CalcMaxLendingDebtProps): bigint;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface TimeToLiquidationProps {
|
|
2
|
+
totalBorrowRate_debt: bigint;
|
|
3
|
+
healthFactor: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Estimates time remaining until health factor reaches liquidation level.
|
|
7
|
+
*
|
|
8
|
+
* Assumes linear debt growth under current aggregate borrow-rate exposure
|
|
9
|
+
* and returns a millisecond duration until `healthFactor` decays to `1.0`
|
|
10
|
+
* (`PERCENTAGE_FACTOR` in internal scale).
|
|
11
|
+
*
|
|
12
|
+
* @param props Current health factor and `totalBorrowRate * debt` term.
|
|
13
|
+
* @returns Milliseconds to liquidation as `bigint`, or `null` when already at/under
|
|
14
|
+
* liquidation threshold or when borrow-rate exposure is zero.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getTimeToLiquidation({ healthFactor, totalBorrowRate_debt, }: TimeToLiquidationProps): bigint | null;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./calcHealthFactor.js";
|
|
2
|
+
export * from "./calcQuotaBorrowRate.js";
|
|
3
|
+
export * from "./calcQuotaBorrowRate.js";
|
|
4
|
+
export * from "./calcRelativeBaseBorrowRate.js";
|
|
5
|
+
export * from "./debt.js";
|
|
6
|
+
export * from "./getTimeToLiquidation.js";
|
|
7
|
+
export * from "./liquidationPrice.js";
|
|
8
|
+
export * from "./quotaUtils.js";
|
|
9
|
+
export * from "./sort.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import type { Asset } from "../../../sdk/index.js";
|
|
3
|
+
import type { TokenDataSlice } from "./types.js";
|
|
4
|
+
interface LiquidationPriceProps {
|
|
5
|
+
liquidationThresholds: Record<Address, bigint>;
|
|
6
|
+
debt: bigint;
|
|
7
|
+
underlyingToken: Address;
|
|
8
|
+
targetToken: Address;
|
|
9
|
+
assets: Record<Address, Asset>;
|
|
10
|
+
tokensList: Record<Address, TokenDataSlice>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Calculates target token liquidation price for a credit account.
|
|
14
|
+
*
|
|
15
|
+
* The formula uses:
|
|
16
|
+
* - effective debt adjusted by underlying-token collateral contribution
|
|
17
|
+
* - target token effective balance
|
|
18
|
+
* - target token liquidation threshold
|
|
19
|
+
*
|
|
20
|
+
* @param props Debt context, assets, thresholds, and token metadata.
|
|
21
|
+
* @returns Target token price in `PRICE_DECIMALS` precision that corresponds
|
|
22
|
+
* to liquidation boundary; returns `0n` when target balance or LT is non-positive.
|
|
23
|
+
*/
|
|
24
|
+
export declare function liquidationPrice({ liquidationThresholds, debt, underlyingToken, targetToken, assets, tokensList, }: LiquidationPriceProps): bigint;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import { type Asset } from "../../../sdk/index.js";
|
|
3
|
+
import type { AssetWithAmountInTarget, QuotaInfoTokenSlice } from "./types.js";
|
|
4
|
+
export interface CalcDefaultQuotaProps {
|
|
5
|
+
amount: bigint;
|
|
6
|
+
lt: bigint;
|
|
7
|
+
quotaReserve: bigint;
|
|
8
|
+
}
|
|
9
|
+
export interface CalcRecommendedQuotaProps {
|
|
10
|
+
amount: bigint;
|
|
11
|
+
debt: bigint;
|
|
12
|
+
lt: bigint;
|
|
13
|
+
quotaReserve: bigint;
|
|
14
|
+
}
|
|
15
|
+
export interface CalcQuotaUpdateProps {
|
|
16
|
+
quotas: Record<Address, QuotaInfoTokenSlice>;
|
|
17
|
+
initialQuotas: Record<Address, {
|
|
18
|
+
quota: bigint;
|
|
19
|
+
}>;
|
|
20
|
+
liquidationThresholds: Record<Address, bigint>;
|
|
21
|
+
assetsAfterUpdate: Record<Address, AssetWithAmountInTarget>;
|
|
22
|
+
maxDebt: bigint;
|
|
23
|
+
calcModification?: {
|
|
24
|
+
type: "recommendedQuota";
|
|
25
|
+
debt: bigint;
|
|
26
|
+
};
|
|
27
|
+
allowedToSpend: Record<Address, object>;
|
|
28
|
+
allowedToObtain: Record<Address, object>;
|
|
29
|
+
quotaReserve: bigint;
|
|
30
|
+
}
|
|
31
|
+
interface CalcQuotaUpdateReturnType {
|
|
32
|
+
desiredQuota: Record<Address, Asset>;
|
|
33
|
+
quotaIncrease: Array<Asset>;
|
|
34
|
+
quotaDecrease: Array<Asset>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Rounds quota deltas to protocol precision step (`PERCENTAGE_FACTOR`).
|
|
38
|
+
*
|
|
39
|
+
* The `MIN_INT96` sentinel is preserved as-is because it encodes
|
|
40
|
+
* "reset quota" semantics and must not be transformed.
|
|
41
|
+
*
|
|
42
|
+
* @param quotaChange Raw quota delta.
|
|
43
|
+
* @returns Rounded quota delta, or untouched `MIN_INT96` sentinel.
|
|
44
|
+
*/
|
|
45
|
+
export declare function roundUpQuota(quotaChange: bigint): bigint;
|
|
46
|
+
/**
|
|
47
|
+
* Computes recommended quota for a token when debt-aware capping is enabled.
|
|
48
|
+
*
|
|
49
|
+
* Base quota is `min(debt, amount * LT)`, then reserve buffer is applied.
|
|
50
|
+
* Final value is rounded to protocol quota step.
|
|
51
|
+
*
|
|
52
|
+
* @param props Token amount in target units, debt, liquidation threshold, reserve.
|
|
53
|
+
* @returns Recommended rounded quota.
|
|
54
|
+
*/
|
|
55
|
+
export declare function calcRecommendedQuota({ amount, debt, lt, quotaReserve, }: CalcRecommendedQuotaProps): bigint;
|
|
56
|
+
/**
|
|
57
|
+
* Computes default quota for a token without debt capping.
|
|
58
|
+
*
|
|
59
|
+
* Base quota is `amount * LT`, then reserve buffer is applied.
|
|
60
|
+
* Final value is rounded to protocol quota step.
|
|
61
|
+
*
|
|
62
|
+
* @param props Token amount in target units, liquidation threshold, reserve.
|
|
63
|
+
* @returns Default rounded quota.
|
|
64
|
+
*/
|
|
65
|
+
export declare function calcDefaultQuota({ amount, lt, quotaReserve, }: CalcDefaultQuotaProps): bigint;
|
|
66
|
+
/**
|
|
67
|
+
* Produces desired quota state and explicit increase/decrease actions.
|
|
68
|
+
*
|
|
69
|
+
* The function:
|
|
70
|
+
* - computes decreases for spendable tokens
|
|
71
|
+
* - computes increase capacity based on global quota cap
|
|
72
|
+
* - computes increases for obtainable tokens within remaining capacity
|
|
73
|
+
* - applies changes on top of initial quotas for active quota tokens
|
|
74
|
+
*
|
|
75
|
+
* `MIN_INT96` is treated as a full reset marker for a token quota.
|
|
76
|
+
*
|
|
77
|
+
* @param props Current quotas, initial quotas, permissions, and calculation context.
|
|
78
|
+
* @returns Desired quota map plus separate increase/decrease instruction lists.
|
|
79
|
+
*/
|
|
80
|
+
export declare function calcQuotaUpdate(props: CalcQuotaUpdateProps): CalcQuotaUpdateReturnType;
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import { type Asset } from "../../../sdk/index.js";
|
|
3
|
+
import type { TokenDataSlice } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Sorts balance record entries by descending total value.
|
|
6
|
+
*
|
|
7
|
+
* Ordering uses `assetComparator` (value-desc, then amount-desc, then symbol-asc).
|
|
8
|
+
*
|
|
9
|
+
* @param balances Token balance map.
|
|
10
|
+
* @param prices Token prices map.
|
|
11
|
+
* @param tokens Token metadata map.
|
|
12
|
+
* @returns Sorted `[token, balance]` tuples.
|
|
13
|
+
*/
|
|
14
|
+
export declare function sortBalances(balances: Record<Address, bigint>, prices: Record<Address, bigint>, tokens: Record<Address, TokenDataSlice>): Array<[Address, bigint]>;
|
|
15
|
+
/**
|
|
16
|
+
* Sorts an asset array by descending total value.
|
|
17
|
+
*
|
|
18
|
+
* Ordering uses `assetComparator` (value-desc, then amount-desc, then symbol-asc).
|
|
19
|
+
*
|
|
20
|
+
* @param balances Asset list to sort.
|
|
21
|
+
* @param prices Token prices map.
|
|
22
|
+
* @param tokens Token metadata map.
|
|
23
|
+
* @returns New sorted array (input is not mutated).
|
|
24
|
+
*/
|
|
25
|
+
export declare function sortAssets<T extends Asset>(balances: Array<T>, prices: Record<Address, bigint>, tokens: Record<Address, TokenDataSlice>): T[];
|
|
26
|
+
/**
|
|
27
|
+
* Compares two assets for deterministic sorting.
|
|
28
|
+
*
|
|
29
|
+
* Priority:
|
|
30
|
+
* 1) higher total value (price * amount) first
|
|
31
|
+
* 2) higher raw balance first when values are equal
|
|
32
|
+
* 3) token symbol alphabetical order when balances are equal
|
|
33
|
+
*
|
|
34
|
+
* Missing prices fall back to `PRICE_DECIMALS`; missing token metadata
|
|
35
|
+
* is tolerated.
|
|
36
|
+
*
|
|
37
|
+
* @returns Comparator result suitable for `Array.prototype.sort`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function assetComparator<T extends Asset>(t1: T, t2: T, prices1: Record<Address, bigint> | undefined, prices2: Record<Address, bigint> | undefined, tokens1: Record<Address, TokenDataSlice> | undefined, tokens2: Record<Address, TokenDataSlice> | undefined): 1 | 0 | -1;
|
|
40
|
+
/**
|
|
41
|
+
* Alphabetical comparator for token symbols (case-insensitive).
|
|
42
|
+
*
|
|
43
|
+
* @param t1 First token metadata.
|
|
44
|
+
* @param t2 Second token metadata.
|
|
45
|
+
* @returns `-1`, `0`, or `1` for ascending symbol order.
|
|
46
|
+
*/
|
|
47
|
+
export declare function tokensAbcComparator(t1?: TokenDataSlice, t2?: TokenDataSlice): 1 | 0 | -1;
|
|
48
|
+
/**
|
|
49
|
+
* Descending numeric comparator for bigint amounts.
|
|
50
|
+
*
|
|
51
|
+
* @param t1 First amount.
|
|
52
|
+
* @param t2 Second amount.
|
|
53
|
+
* @returns `-1` when `t1` should come before `t2`, otherwise `1`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function amountAbcComparator(t1: bigint, t2: bigint): 1 | -1;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Address } from "viem";
|
|
2
|
+
import type { Asset } from "../../../sdk/index.js";
|
|
3
|
+
export interface TokenDataSlice {
|
|
4
|
+
symbol: string;
|
|
5
|
+
decimals: number;
|
|
6
|
+
}
|
|
7
|
+
export interface QuotaInfoIsActiveSlice {
|
|
8
|
+
isActive: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface QuotaInfoTokenSlice extends QuotaInfoIsActiveSlice {
|
|
11
|
+
token: Address;
|
|
12
|
+
}
|
|
13
|
+
export interface QuotaInfoSlice extends QuotaInfoIsActiveSlice {
|
|
14
|
+
rate: bigint;
|
|
15
|
+
}
|
|
16
|
+
export interface AssetWithAmountInTarget extends Asset {
|
|
17
|
+
amountInTarget: bigint;
|
|
18
|
+
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
|
-
import { type NetworkType } from "
|
|
2
|
+
import { type NetworkType } from "../../sdk/index.js";
|
|
3
3
|
type ChartsPriceSource = "chainlink" | "spot";
|
|
4
|
+
/**
|
|
5
|
+
* Centralized endpoint builder for Gearbox backend resources.
|
|
6
|
+
*
|
|
7
|
+
* This utility exposes static helpers that compose canonical URLs for:
|
|
8
|
+
* - charts backend API routes
|
|
9
|
+
* - static token metadata
|
|
10
|
+
* - rewards and NFT merkle proof files
|
|
11
|
+
* - APY state cache snapshots
|
|
12
|
+
*
|
|
13
|
+
* The class is intentionally non-instantiable and is used as a pure
|
|
14
|
+
* namespace of URL-construction functions.
|
|
15
|
+
*/
|
|
4
16
|
export declare class GearboxBackendApi {
|
|
5
17
|
private constructor();
|
|
6
18
|
static getChartsUrl: (url: string, chainId: number, options?: Options, priceSource?: ChartsPriceSource) => string;
|
|
@@ -12,8 +24,4 @@ export declare class GearboxBackendApi {
|
|
|
12
24
|
interface Options {
|
|
13
25
|
params?: Record<string, string | number>;
|
|
14
26
|
}
|
|
15
|
-
export declare class URLApi {
|
|
16
|
-
private constructor();
|
|
17
|
-
static getRelativeUrl: (url: string, options?: Options) => string;
|
|
18
|
-
}
|
|
19
27
|
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
interface Target {
|
|
2
|
+
price: bigint;
|
|
3
|
+
decimals: number | undefined;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Static utility namespace for converting between token amounts and
|
|
7
|
+
* normalized value using oracle-like prices.
|
|
8
|
+
*
|
|
9
|
+
* All helpers rely on shared fixed-point constants (`WAD` and
|
|
10
|
+
* `PRICE_DECIMALS`) to keep arithmetic consistent across assets
|
|
11
|
+
* with different token decimal precisions.
|
|
12
|
+
*/
|
|
13
|
+
export declare class PriceUtils {
|
|
14
|
+
/**
|
|
15
|
+
* This class is intentionally non-instantiable.
|
|
16
|
+
*/
|
|
17
|
+
private constructor();
|
|
18
|
+
/**
|
|
19
|
+
* Calculates normalized total value for a token amount at a given price.
|
|
20
|
+
*
|
|
21
|
+
* Formula:
|
|
22
|
+
* `(amount * WAD * price) / 10^decimals / PRICE_DECIMALS`
|
|
23
|
+
*
|
|
24
|
+
* @param price Token unit price in `PRICE_DECIMALS` precision.
|
|
25
|
+
* @param amount Token amount in raw token units.
|
|
26
|
+
* @param decimals Token decimals used to normalize `amount` (defaults to `18`).
|
|
27
|
+
* @returns Total value in WAD-normalized units.
|
|
28
|
+
*/
|
|
29
|
+
static calcTotalPrice: (price: bigint, amount: bigint, decimals?: number | undefined) => bigint;
|
|
30
|
+
/**
|
|
31
|
+
* Converts a normalized monetary value into target token amount by price.
|
|
32
|
+
*
|
|
33
|
+
* Formula:
|
|
34
|
+
* `(totalMoney * 10^targetDecimals * PRICE_DECIMALS) / targetPrice / WAD`
|
|
35
|
+
*
|
|
36
|
+
* Safety behavior:
|
|
37
|
+
* - returns `0n` when `targetPrice <= 0n` to avoid invalid division
|
|
38
|
+
*
|
|
39
|
+
* @param totalMoney Value to convert, expected in WAD-normalized units.
|
|
40
|
+
* @param target Conversion target configuration:
|
|
41
|
+
* - `price`: target token unit price in `PRICE_DECIMALS` precision
|
|
42
|
+
* - `decimals`: target token decimals (defaults to `18`)
|
|
43
|
+
* @returns Target token amount in raw token units.
|
|
44
|
+
*/
|
|
45
|
+
static convertByPrice(totalMoney: bigint, { price: targetPrice, decimals: targetDecimals }: Target): bigint;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
export * from "./AddressMap.js";
|
|
2
2
|
export * from "./AddressSet.js";
|
|
3
3
|
export * from "./abi-decode.js";
|
|
4
|
-
export * from "./assetsMath.js";
|
|
5
|
-
export * from "./bigintMath.js";
|
|
6
4
|
export * from "./bytes32ToString.js";
|
|
7
5
|
export * from "./childLogger.js";
|
|
8
6
|
export * from "./createRawTx.js";
|
|
9
|
-
export * from "./creditAccount.js";
|
|
10
|
-
export * from "./endpoints.js";
|
|
11
7
|
export * from "./etherscan.js";
|
|
12
8
|
export * from "./filterDust.js";
|
|
13
9
|
export * from "./formatter.js";
|
|
@@ -15,7 +11,6 @@ export * from "./hex.js";
|
|
|
15
11
|
export * from "./isDust.js";
|
|
16
12
|
export * from "./json.js";
|
|
17
13
|
export * from "./mappers.js";
|
|
18
|
-
export * from "./priceMath.js";
|
|
19
14
|
export * from "./retry.js";
|
|
20
15
|
export * from "./toAddress.js";
|
|
21
16
|
export * from "./type-utils.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gearbox-protocol/sdk",
|
|
3
|
-
"version": "13.0.0-beta.
|
|
3
|
+
"version": "13.0.0-beta.5",
|
|
4
4
|
"description": "Gearbox SDK",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./dist/cjs/sdk/index.js",
|
|
@@ -39,6 +39,11 @@
|
|
|
39
39
|
"types": "./dist/types/permissionless/index.d.ts",
|
|
40
40
|
"import": "./dist/esm/permissionless/index.js",
|
|
41
41
|
"default": "./dist/cjs/permissionless/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./common-utils": {
|
|
44
|
+
"types": "./dist/types/common-utils/index.d.ts",
|
|
45
|
+
"import": "./dist/esm/common-utils/index.js",
|
|
46
|
+
"default": "./dist/cjs/common-utils/index.js"
|
|
42
47
|
}
|
|
43
48
|
},
|
|
44
49
|
"files": [
|