@gearbox-protocol/sdk 13.0.0-beta.4 → 13.0.0-beta.6
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 +40 -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 +10 -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 +10 -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,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var common_utils_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(common_utils_exports);
|
|
18
|
+
__reExport(common_utils_exports, require("./utils/index.js"), module.exports);
|
|
19
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
20
|
+
0 && (module.exports = {
|
|
21
|
+
...require("./utils/index.js")
|
|
22
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -22,9 +22,24 @@ __export(assetsMath_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(assetsMath_exports);
|
|
24
24
|
var import_bigintMath = require("./bigintMath.js");
|
|
25
|
-
var
|
|
25
|
+
var import_sort = require("./creditAccount/sort.js");
|
|
26
26
|
var import_priceMath = require("./priceMath.js");
|
|
27
27
|
class AssetUtils {
|
|
28
|
+
constructor() {
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Selects the next candidate token to add to a selected-asset list.
|
|
32
|
+
*
|
|
33
|
+
* Flow:
|
|
34
|
+
* 1) Removes tokens already present in `selectedAssets`
|
|
35
|
+
* 2) Builds balances for the remaining allowed tokens
|
|
36
|
+
* 3) Sorts the balances using `CreditAccountDataUtils.sortBalances`
|
|
37
|
+
* 4) Returns the highest-priority token address, if any
|
|
38
|
+
*
|
|
39
|
+
* Addresses are normalized to lowercase for matching.
|
|
40
|
+
*
|
|
41
|
+
* @returns The next token address to select, or `undefined` if no candidates remain.
|
|
42
|
+
*/
|
|
28
43
|
static nextAsset({
|
|
29
44
|
allowedTokens,
|
|
30
45
|
selectedAssets,
|
|
@@ -43,7 +58,7 @@ class AssetUtils {
|
|
|
43
58
|
const alreadySelected = selectedRecord[allowedToken.toLowerCase()];
|
|
44
59
|
return !alreadySelected;
|
|
45
60
|
});
|
|
46
|
-
const sorted =
|
|
61
|
+
const sorted = (0, import_sort.sortBalances)(
|
|
47
62
|
AssetUtils.getBalances(notSelected, balances),
|
|
48
63
|
prices,
|
|
49
64
|
tokensList
|
|
@@ -51,15 +66,30 @@ class AssetUtils {
|
|
|
51
66
|
const [address] = sorted[0] || [];
|
|
52
67
|
return address;
|
|
53
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Builds a normalized balance record for a specific token subset.
|
|
71
|
+
*
|
|
72
|
+
* Missing balances are defaulted to `0n`.
|
|
73
|
+
*
|
|
74
|
+
* @param allowedTokens Tokens to include in the output record.
|
|
75
|
+
* @param externalBalances Source balances keyed by lowercase address.
|
|
76
|
+
* @returns A record that contains only `allowedTokens`.
|
|
77
|
+
*/
|
|
54
78
|
static getBalances(allowedTokens, externalBalances) {
|
|
55
79
|
return allowedTokens.reduce((acc, address) => {
|
|
56
80
|
const addressLc = address.toLowerCase();
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
[addressLc]: externalBalances[addressLc] || 0n
|
|
60
|
-
};
|
|
81
|
+
acc[addressLc] = externalBalances[addressLc] || 0n;
|
|
82
|
+
return acc;
|
|
61
83
|
}, {});
|
|
62
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Converts an asset array into a token-address keyed record.
|
|
87
|
+
*
|
|
88
|
+
* If duplicate token addresses are present, the last occurrence wins.
|
|
89
|
+
*
|
|
90
|
+
* @param a Source asset list.
|
|
91
|
+
* @returns Record keyed by `asset.token`.
|
|
92
|
+
*/
|
|
63
93
|
static constructAssetRecord(a) {
|
|
64
94
|
const record = a.reduce((acc, asset) => {
|
|
65
95
|
acc[asset.token] = asset;
|
|
@@ -67,6 +97,18 @@ class AssetUtils {
|
|
|
67
97
|
}, {});
|
|
68
98
|
return record;
|
|
69
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Creates a reusable wrapper function that merges "unwrapped" and "wrapped"
|
|
102
|
+
* token balances into the wrapped token representation.
|
|
103
|
+
*
|
|
104
|
+
* The returned function:
|
|
105
|
+
* - converts non-negative unwrapped balance into wrapped units by price
|
|
106
|
+
* - adds the converted amount to the wrapped token balance
|
|
107
|
+
* - removes the unwrapped token from the result list
|
|
108
|
+
* - leaves input untouched when no unwrapped token is present
|
|
109
|
+
*
|
|
110
|
+
* @returns A function producing `[assets, convertedUnwrapped, originalWrapped]`.
|
|
111
|
+
*/
|
|
70
112
|
static memoWrap = (unwrappedAddress, wrappedAddress, prices, tokensList) => function wrap(assets) {
|
|
71
113
|
const assetsRecord = AssetUtils.constructAssetRecord(assets);
|
|
72
114
|
const unwrapped = assetsRecord[unwrappedAddress];
|
|
@@ -99,8 +141,16 @@ class AssetUtils {
|
|
|
99
141
|
return [Object.values(assetsRecord), 0n, wrappedAmount];
|
|
100
142
|
};
|
|
101
143
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
144
|
+
* Adds balances from the second asset list into the first list.
|
|
145
|
+
*
|
|
146
|
+
* Behavior:
|
|
147
|
+
* - balances are clamped to non-negative before summation
|
|
148
|
+
* - tokens found only in `b` are created in the output
|
|
149
|
+
* - existing asset metadata is preserved from `a` when possible
|
|
150
|
+
*
|
|
151
|
+
* @param a Base asset list.
|
|
152
|
+
* @param b Asset deltas to add.
|
|
153
|
+
* @returns A merged list containing assets from both inputs.
|
|
104
154
|
*/
|
|
105
155
|
static sumAssets(a, b) {
|
|
106
156
|
const aRecord = AssetUtils.constructAssetRecord(a);
|
|
@@ -118,8 +168,16 @@ class AssetUtils {
|
|
|
118
168
|
return Object.values(resRecord);
|
|
119
169
|
}
|
|
120
170
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
171
|
+
* Adds balances from the second list to matching assets in the first list.
|
|
172
|
+
*
|
|
173
|
+
* Behavior:
|
|
174
|
+
* - balances are clamped to non-negative before summation
|
|
175
|
+
* - only assets already present in `a` are returned
|
|
176
|
+
* - no new token entries are created
|
|
177
|
+
*
|
|
178
|
+
* @param a Base asset list.
|
|
179
|
+
* @param b Asset deltas keyed by token.
|
|
180
|
+
* @returns Updated version of `a` with adjusted balances.
|
|
123
181
|
*/
|
|
124
182
|
static addBalances(a, b) {
|
|
125
183
|
const bRecord = AssetUtils.constructAssetRecord(b);
|
|
@@ -131,8 +189,16 @@ class AssetUtils {
|
|
|
131
189
|
});
|
|
132
190
|
}
|
|
133
191
|
/**
|
|
134
|
-
* Subtracts
|
|
135
|
-
*
|
|
192
|
+
* Subtracts balances in the second list from matching assets in the first list.
|
|
193
|
+
*
|
|
194
|
+
* Behavior:
|
|
195
|
+
* - both operands are clamped to non-negative before subtraction
|
|
196
|
+
* - output balances are clamped to non-negative after subtraction
|
|
197
|
+
* - only assets already present in `a` are returned
|
|
198
|
+
*
|
|
199
|
+
* @param a Base asset list.
|
|
200
|
+
* @param b Asset amounts to subtract by token.
|
|
201
|
+
* @returns Updated `a` list with non-negative post-subtraction balances.
|
|
136
202
|
*/
|
|
137
203
|
static subAssets(a, b) {
|
|
138
204
|
const bRecord = AssetUtils.constructAssetRecord(b);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var bigintMath_exports = {};
|
|
20
|
+
__export(bigintMath_exports, {
|
|
21
|
+
BigIntMath: () => BigIntMath
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(bigintMath_exports);
|
|
24
|
+
class BigIntMath {
|
|
25
|
+
constructor() {
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns the absolute (non-negative) value of a bigint.
|
|
29
|
+
*
|
|
30
|
+
* @param x Input value.
|
|
31
|
+
* @returns `x` when `x` is already non-negative, otherwise `-x`.
|
|
32
|
+
*/
|
|
33
|
+
static abs = (x) => x < 0n ? -x : x;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the greater of two bigint values.
|
|
36
|
+
*
|
|
37
|
+
* @param a First candidate value.
|
|
38
|
+
* @param b Second candidate value.
|
|
39
|
+
* @returns The larger value between `a` and `b`.
|
|
40
|
+
*/
|
|
41
|
+
static max = (a, b) => a > b ? a : b;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the smaller of two bigint values.
|
|
44
|
+
*
|
|
45
|
+
* @param a First candidate value.
|
|
46
|
+
* @param b Second candidate value.
|
|
47
|
+
* @returns The smaller value between `a` and `b`.
|
|
48
|
+
*/
|
|
49
|
+
static min = (a, b) => a < b ? a : b;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the negative form of a bigint if it is currently positive.
|
|
52
|
+
*
|
|
53
|
+
* Useful when a value should be represented as an outflow/debit:
|
|
54
|
+
* - positive values become negative
|
|
55
|
+
* - zero and negative values are returned unchanged
|
|
56
|
+
*
|
|
57
|
+
* @param a Input value.
|
|
58
|
+
* @returns A non-positive bigint representation of `a`.
|
|
59
|
+
*/
|
|
60
|
+
static neg = (a) => a > 0 ? a * -1n : a;
|
|
61
|
+
}
|
|
62
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
63
|
+
0 && (module.exports = {
|
|
64
|
+
BigIntMath
|
|
65
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var calcHealthFactor_exports = {};
|
|
20
|
+
__export(calcHealthFactor_exports, {
|
|
21
|
+
calcHealthFactor: () => calcHealthFactor
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(calcHealthFactor_exports);
|
|
24
|
+
var import_sdk = require("../../../sdk/index.js");
|
|
25
|
+
var import_bigintMath = require("../bigintMath.js");
|
|
26
|
+
var import_priceMath = require("../priceMath.js");
|
|
27
|
+
const MAX_UINT16 = 65535;
|
|
28
|
+
function calcHealthFactor({
|
|
29
|
+
assets,
|
|
30
|
+
quotas,
|
|
31
|
+
quotasInfo,
|
|
32
|
+
liquidationThresholds,
|
|
33
|
+
underlyingToken,
|
|
34
|
+
debt,
|
|
35
|
+
prices,
|
|
36
|
+
tokensList
|
|
37
|
+
}) {
|
|
38
|
+
if (debt === 0n) return MAX_UINT16;
|
|
39
|
+
const underlyingDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
40
|
+
const underlyingPrice = prices[underlyingToken] || 0n;
|
|
41
|
+
const assetMoney = assets.reduce(
|
|
42
|
+
(acc, { token: tokenAddress, balance: amount }) => {
|
|
43
|
+
const tokenDecimals = tokensList[tokenAddress]?.decimals || 18;
|
|
44
|
+
const lt = liquidationThresholds[tokenAddress] || 0n;
|
|
45
|
+
const price = prices[tokenAddress] || 0n;
|
|
46
|
+
const tokenMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
47
|
+
price,
|
|
48
|
+
amount,
|
|
49
|
+
tokenDecimals
|
|
50
|
+
);
|
|
51
|
+
const tokenLtMoney = tokenMoney * lt / import_sdk.PERCENTAGE_FACTOR;
|
|
52
|
+
const { isActive = false } = quotasInfo?.[tokenAddress] || {};
|
|
53
|
+
const quota = quotas[tokenAddress];
|
|
54
|
+
const quotaBalance = isActive ? quota?.balance || 0n : 0n;
|
|
55
|
+
const quotaMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
56
|
+
underlyingPrice,
|
|
57
|
+
quotaBalance,
|
|
58
|
+
underlyingDecimals
|
|
59
|
+
);
|
|
60
|
+
const money = quota ? import_bigintMath.BigIntMath.min(quotaMoney, tokenLtMoney) : tokenLtMoney;
|
|
61
|
+
return acc + money;
|
|
62
|
+
},
|
|
63
|
+
0n
|
|
64
|
+
);
|
|
65
|
+
const borrowedMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
66
|
+
underlyingPrice || import_sdk.PRICE_DECIMALS,
|
|
67
|
+
debt,
|
|
68
|
+
underlyingDecimals
|
|
69
|
+
);
|
|
70
|
+
const hfInPercent = borrowedMoney > 0n ? assetMoney * import_sdk.PERCENTAGE_FACTOR / borrowedMoney : 0n;
|
|
71
|
+
return Number(hfInPercent);
|
|
72
|
+
}
|
|
73
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
74
|
+
0 && (module.exports = {
|
|
75
|
+
calcHealthFactor
|
|
76
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var calcOverallAPY_exports = {};
|
|
20
|
+
__export(calcOverallAPY_exports, {
|
|
21
|
+
calcOverallAPY: () => calcOverallAPY
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(calcOverallAPY_exports);
|
|
24
|
+
var import_sdk = require("../../../sdk/index.js");
|
|
25
|
+
var import_priceMath = require("../priceMath.js");
|
|
26
|
+
function calcOverallAPY({
|
|
27
|
+
caAssets,
|
|
28
|
+
lpAPY,
|
|
29
|
+
prices,
|
|
30
|
+
quotas,
|
|
31
|
+
quotaRates,
|
|
32
|
+
feeInterest,
|
|
33
|
+
totalValue,
|
|
34
|
+
debt,
|
|
35
|
+
baseRateWithFee,
|
|
36
|
+
underlyingToken,
|
|
37
|
+
tokensList
|
|
38
|
+
}) {
|
|
39
|
+
if (!lpAPY || !totalValue || totalValue <= 0n || !debt || totalValue <= debt)
|
|
40
|
+
return void 0;
|
|
41
|
+
const underlyingTokenDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
42
|
+
const underlyingPrice = prices[underlyingToken];
|
|
43
|
+
const assetAPYMoney = caAssets.reduce(
|
|
44
|
+
(acc, { token: tokenAddress, balance: amount }) => {
|
|
45
|
+
const apy = lpAPY[tokenAddress] || 0;
|
|
46
|
+
const tokenDecimals = tokensList[tokenAddress]?.decimals || 18;
|
|
47
|
+
const price = prices[tokenAddress] || 0n;
|
|
48
|
+
const money = import_priceMath.PriceUtils.calcTotalPrice(price, amount, tokenDecimals);
|
|
49
|
+
const apyMoney = money * BigInt(apy);
|
|
50
|
+
const { rate: quotaAPY = 0n, isActive = false } = quotaRates?.[tokenAddress] || {};
|
|
51
|
+
const { balance: quotaBalance = 0n } = quotas[tokenAddress] || {};
|
|
52
|
+
const quotaAmount = isActive ? quotaBalance : 0n;
|
|
53
|
+
const quotaMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
54
|
+
underlyingPrice || 0n,
|
|
55
|
+
quotaAmount,
|
|
56
|
+
underlyingTokenDecimals
|
|
57
|
+
);
|
|
58
|
+
const quotaRate = quotaAPY * (BigInt(feeInterest) + import_sdk.PERCENTAGE_FACTOR) / import_sdk.PERCENTAGE_FACTOR;
|
|
59
|
+
const quotaAPYMoney = quotaMoney * quotaRate;
|
|
60
|
+
return acc + apyMoney - quotaAPYMoney;
|
|
61
|
+
},
|
|
62
|
+
0n
|
|
63
|
+
);
|
|
64
|
+
const debtMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
65
|
+
underlyingPrice || 0n,
|
|
66
|
+
debt,
|
|
67
|
+
underlyingTokenDecimals
|
|
68
|
+
);
|
|
69
|
+
const debtAPYMoney = debtMoney * BigInt(baseRateWithFee);
|
|
70
|
+
const yourAssetsMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
71
|
+
underlyingPrice || import_sdk.PRICE_DECIMALS,
|
|
72
|
+
totalValue - debt,
|
|
73
|
+
underlyingTokenDecimals
|
|
74
|
+
);
|
|
75
|
+
const apyInPercent = (assetAPYMoney - debtAPYMoney) / yourAssetsMoney;
|
|
76
|
+
return apyInPercent;
|
|
77
|
+
}
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
calcOverallAPY
|
|
81
|
+
});
|
package/dist/cjs/{sdk/utils/priceMath.js → common-utils/utils/creditAccount/calcQuotaBorrowRate.js}
RENAMED
|
@@ -16,20 +16,27 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var
|
|
20
|
-
__export(
|
|
21
|
-
|
|
19
|
+
var calcQuotaBorrowRate_exports = {};
|
|
20
|
+
__export(calcQuotaBorrowRate_exports, {
|
|
21
|
+
calcQuotaBorrowRate: () => calcQuotaBorrowRate
|
|
22
22
|
});
|
|
23
|
-
module.exports = __toCommonJS(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
module.exports = __toCommonJS(calcQuotaBorrowRate_exports);
|
|
24
|
+
function calcQuotaBorrowRate({
|
|
25
|
+
quotas,
|
|
26
|
+
quotaRates
|
|
27
|
+
}) {
|
|
28
|
+
const totalRateBalance = Object.values(quotas).reduce(
|
|
29
|
+
(acc, { token, balance }) => {
|
|
30
|
+
const { rate = 0, isActive = false } = quotaRates?.[token] || {};
|
|
31
|
+
const quotaBalance = isActive ? balance : 0n;
|
|
32
|
+
const rateBalance = quotaBalance * BigInt(rate);
|
|
33
|
+
return acc + rateBalance;
|
|
34
|
+
},
|
|
35
|
+
0n
|
|
36
|
+
);
|
|
37
|
+
return totalRateBalance;
|
|
31
38
|
}
|
|
32
39
|
// Annotate the CommonJS export names for ESM import in node:
|
|
33
40
|
0 && (module.exports = {
|
|
34
|
-
|
|
41
|
+
calcQuotaBorrowRate
|
|
35
42
|
});
|
|
@@ -16,18 +16,19 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var
|
|
20
|
-
__export(
|
|
21
|
-
|
|
19
|
+
var calcRelativeBaseBorrowRate_exports = {};
|
|
20
|
+
__export(calcRelativeBaseBorrowRate_exports, {
|
|
21
|
+
calcRelativeBaseBorrowRate: () => calcRelativeBaseBorrowRate
|
|
22
22
|
});
|
|
23
|
-
module.exports = __toCommonJS(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
module.exports = __toCommonJS(calcRelativeBaseBorrowRate_exports);
|
|
24
|
+
function calcRelativeBaseBorrowRate({
|
|
25
|
+
debt,
|
|
26
|
+
baseRateWithFee,
|
|
27
|
+
assetAmountInUnderlying
|
|
28
|
+
}) {
|
|
29
|
+
return debt * BigInt(baseRateWithFee) * assetAmountInUnderlying;
|
|
29
30
|
}
|
|
30
31
|
// Annotate the CommonJS export names for ESM import in node:
|
|
31
32
|
0 && (module.exports = {
|
|
32
|
-
|
|
33
|
+
calcRelativeBaseBorrowRate
|
|
33
34
|
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var debt_exports = {};
|
|
20
|
+
__export(debt_exports, {
|
|
21
|
+
calcMaxDebtIncrease: () => calcMaxDebtIncrease,
|
|
22
|
+
calcMaxLendingDebt: () => calcMaxLendingDebt
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(debt_exports);
|
|
25
|
+
var import_sdk = require("../../../sdk/index.js");
|
|
26
|
+
var import_bigintMath = require("../bigintMath.js");
|
|
27
|
+
var import_priceMath = require("../priceMath.js");
|
|
28
|
+
function calcMaxDebtIncrease(healthFactor, debt, underlyingLT, minHf = Number(import_sdk.PERCENTAGE_FACTOR)) {
|
|
29
|
+
const result = debt * BigInt(healthFactor - minHf) / BigInt(minHf - underlyingLT);
|
|
30
|
+
return import_bigintMath.BigIntMath.max(0n, result);
|
|
31
|
+
}
|
|
32
|
+
function calcMaxLendingDebt({
|
|
33
|
+
assets,
|
|
34
|
+
liquidationThresholds,
|
|
35
|
+
underlyingToken,
|
|
36
|
+
prices,
|
|
37
|
+
tokensList,
|
|
38
|
+
targetHF = import_sdk.PERCENTAGE_FACTOR
|
|
39
|
+
}) {
|
|
40
|
+
const assetsLTMoney = assets.reduce(
|
|
41
|
+
(acc, { token: tokenAddress, balance: amount }) => {
|
|
42
|
+
const tokenDecimals = tokensList[tokenAddress]?.decimals || 18;
|
|
43
|
+
const lt = liquidationThresholds[tokenAddress] || 0n;
|
|
44
|
+
const price = prices[tokenAddress] || 0n;
|
|
45
|
+
const tokenMoney = import_priceMath.PriceUtils.calcTotalPrice(
|
|
46
|
+
price,
|
|
47
|
+
amount,
|
|
48
|
+
tokenDecimals
|
|
49
|
+
);
|
|
50
|
+
const tokenLtMoney = tokenMoney * lt;
|
|
51
|
+
return acc + tokenLtMoney;
|
|
52
|
+
},
|
|
53
|
+
0n
|
|
54
|
+
);
|
|
55
|
+
const underlyingPrice = prices[underlyingToken] || 0n;
|
|
56
|
+
const underlyingDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
57
|
+
const max = underlyingPrice > 0 ? assetsLTMoney * 10n ** BigInt(underlyingDecimals) / underlyingPrice / targetHF / 10n ** BigInt(import_sdk.WAD_DECIMALS_POW - import_sdk.PRICE_DECIMALS_POW) : 0n;
|
|
58
|
+
return max;
|
|
59
|
+
}
|
|
60
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
61
|
+
0 && (module.exports = {
|
|
62
|
+
calcMaxDebtIncrease,
|
|
63
|
+
calcMaxLendingDebt
|
|
64
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var getTimeToLiquidation_exports = {};
|
|
20
|
+
__export(getTimeToLiquidation_exports, {
|
|
21
|
+
getTimeToLiquidation: () => getTimeToLiquidation
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(getTimeToLiquidation_exports);
|
|
24
|
+
var import_sdk = require("../../../sdk/index.js");
|
|
25
|
+
function getTimeToLiquidation({
|
|
26
|
+
healthFactor,
|
|
27
|
+
totalBorrowRate_debt
|
|
28
|
+
}) {
|
|
29
|
+
if (healthFactor <= import_sdk.PERCENTAGE_FACTOR || totalBorrowRate_debt === 0n)
|
|
30
|
+
return null;
|
|
31
|
+
const HF_1 = BigInt(healthFactor) - import_sdk.PERCENTAGE_FACTOR;
|
|
32
|
+
const brPerYear = BigInt(import_sdk.SECONDS_PER_YEAR) * import_sdk.PERCENTAGE_FACTOR * import_sdk.PERCENTAGE_DECIMALS / totalBorrowRate_debt;
|
|
33
|
+
return HF_1 * brPerYear * 1000n / import_sdk.PERCENTAGE_FACTOR;
|
|
34
|
+
}
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
getTimeToLiquidation
|
|
38
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var creditAccount_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(creditAccount_exports);
|
|
18
|
+
__reExport(creditAccount_exports, require("./calcHealthFactor.js"), module.exports);
|
|
19
|
+
__reExport(creditAccount_exports, require("./calcOverallAPY.js"), module.exports);
|
|
20
|
+
__reExport(creditAccount_exports, require("./calcQuotaBorrowRate.js"), module.exports);
|
|
21
|
+
__reExport(creditAccount_exports, require("./calcQuotaBorrowRate.js"), module.exports);
|
|
22
|
+
__reExport(creditAccount_exports, require("./calcRelativeBaseBorrowRate.js"), module.exports);
|
|
23
|
+
__reExport(creditAccount_exports, require("./debt.js"), module.exports);
|
|
24
|
+
__reExport(creditAccount_exports, require("./getTimeToLiquidation.js"), module.exports);
|
|
25
|
+
__reExport(creditAccount_exports, require("./liquidationPrice.js"), module.exports);
|
|
26
|
+
__reExport(creditAccount_exports, require("./quotaUtils.js"), module.exports);
|
|
27
|
+
__reExport(creditAccount_exports, require("./sort.js"), module.exports);
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
...require("./calcHealthFactor.js"),
|
|
31
|
+
...require("./calcOverallAPY.js"),
|
|
32
|
+
...require("./calcQuotaBorrowRate.js"),
|
|
33
|
+
...require("./calcQuotaBorrowRate.js"),
|
|
34
|
+
...require("./calcRelativeBaseBorrowRate.js"),
|
|
35
|
+
...require("./debt.js"),
|
|
36
|
+
...require("./getTimeToLiquidation.js"),
|
|
37
|
+
...require("./liquidationPrice.js"),
|
|
38
|
+
...require("./quotaUtils.js"),
|
|
39
|
+
...require("./sort.js")
|
|
40
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var liquidationPrice_exports = {};
|
|
20
|
+
__export(liquidationPrice_exports, {
|
|
21
|
+
liquidationPrice: () => liquidationPrice
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(liquidationPrice_exports);
|
|
24
|
+
var import_sdk = require("../../../sdk/index.js");
|
|
25
|
+
function liquidationPrice({
|
|
26
|
+
liquidationThresholds,
|
|
27
|
+
debt,
|
|
28
|
+
underlyingToken,
|
|
29
|
+
targetToken,
|
|
30
|
+
assets,
|
|
31
|
+
tokensList
|
|
32
|
+
}) {
|
|
33
|
+
const underlyingDecimals = tokensList[underlyingToken]?.decimals || 18;
|
|
34
|
+
const { balance: underlyingBalance = 0n } = assets[underlyingToken] || {};
|
|
35
|
+
const ltUnderlying = liquidationThresholds[underlyingToken] || 0n;
|
|
36
|
+
const effectiveDebt = (debt - underlyingBalance * ltUnderlying / import_sdk.PERCENTAGE_FACTOR) * import_sdk.WAD / 10n ** BigInt(underlyingDecimals);
|
|
37
|
+
const targetDecimals = tokensList[targetToken]?.decimals || 18;
|
|
38
|
+
const { balance: targetBalance = 0n } = assets[targetToken] || {};
|
|
39
|
+
const effectiveTargetBalance = targetBalance * import_sdk.WAD / 10n ** BigInt(targetDecimals);
|
|
40
|
+
const lpLT = liquidationThresholds[targetToken] || 0n;
|
|
41
|
+
if (targetBalance <= 0n || lpLT <= 0n) return 0n;
|
|
42
|
+
return effectiveDebt * import_sdk.PRICE_DECIMALS * import_sdk.PERCENTAGE_FACTOR / (effectiveTargetBalance * lpLT);
|
|
43
|
+
}
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
liquidationPrice
|
|
47
|
+
});
|