@gearbox-protocol/sdk 13.0.0-beta.3 → 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/abi/iPriceFeed.js +84 -0
- 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 +88 -21
- 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/permissionless/bindings/cross-chain-multisig.js +3 -3
- package/dist/cjs/permissionless/bindings/instance-manager.js +2 -2
- package/dist/cjs/plugins/adapters/contracts/MellowDepositQueueAdapterContract.js +0 -6
- package/dist/cjs/plugins/adapters/contracts/MellowRedeemQueueAdapterContract.js +0 -6
- package/dist/cjs/sdk/GearboxSDK.js +0 -2
- package/dist/cjs/sdk/base/BaseContract.js +5 -2
- package/dist/cjs/sdk/base/ChainContractsRegister.js +2 -10
- package/dist/cjs/sdk/base/Construct.js +25 -19
- package/dist/cjs/sdk/base/SDKConstruct.js +1 -1
- package/dist/cjs/sdk/market/adapters/PlaceholderAdapterContracts.js +16 -0
- package/dist/cjs/sdk/market/pricefeeds/updates/PriceUpdatesCache.js +0 -17
- package/dist/cjs/sdk/market/pricefeeds/updates/PythUpdater.js +1 -1
- package/dist/cjs/sdk/market/pricefeeds/updates/RedstoneUpdater.js +1 -1
- package/dist/cjs/sdk/market/pricefeeds/updates/fetchPythPayloads.js +2 -2
- package/dist/cjs/sdk/utils/formatter.js +3 -3
- package/dist/cjs/sdk/utils/index.js +0 -10
- package/dist/esm/abi/iPriceFeed.js +60 -0
- 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 +80 -13
- 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/permissionless/bindings/cross-chain-multisig.js +3 -3
- package/dist/esm/permissionless/bindings/instance-manager.js +2 -2
- package/dist/esm/plugins/adapters/contracts/MellowDepositQueueAdapterContract.js +0 -6
- package/dist/esm/plugins/adapters/contracts/MellowRedeemQueueAdapterContract.js +0 -6
- package/dist/esm/sdk/GearboxSDK.js +0 -2
- package/dist/esm/sdk/base/BaseContract.js +5 -2
- package/dist/esm/sdk/base/ChainContractsRegister.js +2 -10
- package/dist/esm/sdk/base/Construct.js +25 -19
- package/dist/esm/sdk/base/SDKConstruct.js +1 -1
- package/dist/esm/sdk/market/adapters/PlaceholderAdapterContracts.js +16 -0
- package/dist/esm/sdk/market/pricefeeds/updates/PriceUpdatesCache.js +0 -17
- package/dist/esm/sdk/market/pricefeeds/updates/PythUpdater.js +1 -1
- package/dist/esm/sdk/market/pricefeeds/updates/RedstoneUpdater.js +1 -1
- package/dist/esm/sdk/market/pricefeeds/updates/fetchPythPayloads.js +2 -2
- package/dist/esm/sdk/utils/formatter.js +1 -1
- package/dist/esm/sdk/utils/index.js +0 -5
- package/dist/types/abi/iPriceFeed.d.ts +87 -0
- 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/permissionless/bindings/cross-chain-multisig.d.ts +3 -3
- package/dist/types/permissionless/bindings/instance-manager.d.ts +3 -3
- package/dist/types/plugins/adapters/contracts/MellowDepositQueueAdapterContract.d.ts +1 -4
- package/dist/types/plugins/adapters/contracts/MellowRedeemQueueAdapterContract.d.ts +1 -4
- package/dist/types/sdk/base/ChainContractsRegister.d.ts +0 -2
- package/dist/types/sdk/base/Construct.d.ts +7 -10
- package/dist/types/sdk/market/adapters/PlaceholderAdapterContracts.d.ts +2 -1
- package/dist/types/sdk/market/pricefeeds/updates/PriceUpdatesCache.d.ts +1 -8
- package/dist/types/sdk/utils/index.d.ts +0 -5
- package/package.json +11 -2
- package/dist/cjs/sdk/utils/creditAccount.js +0 -409
- 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,84 @@
|
|
|
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 iPriceFeed_exports = {};
|
|
20
|
+
__export(iPriceFeed_exports, {
|
|
21
|
+
iPriceFeedAbi: () => iPriceFeedAbi
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(iPriceFeed_exports);
|
|
24
|
+
const iPriceFeedAbi = [
|
|
25
|
+
{
|
|
26
|
+
type: "function",
|
|
27
|
+
name: "contractType",
|
|
28
|
+
inputs: [],
|
|
29
|
+
outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
|
|
30
|
+
stateMutability: "view"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
type: "function",
|
|
34
|
+
name: "decimals",
|
|
35
|
+
inputs: [],
|
|
36
|
+
outputs: [{ name: "", type: "uint8", internalType: "uint8" }],
|
|
37
|
+
stateMutability: "view"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
type: "function",
|
|
41
|
+
name: "description",
|
|
42
|
+
inputs: [],
|
|
43
|
+
outputs: [{ name: "", type: "string", internalType: "string" }],
|
|
44
|
+
stateMutability: "view"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: "function",
|
|
48
|
+
name: "latestRoundData",
|
|
49
|
+
inputs: [],
|
|
50
|
+
outputs: [
|
|
51
|
+
{ name: "", type: "uint80", internalType: "uint80" },
|
|
52
|
+
{ name: "answer", type: "int256", internalType: "int256" },
|
|
53
|
+
{ name: "", type: "uint256", internalType: "uint256" },
|
|
54
|
+
{ name: "updatedAt", type: "uint256", internalType: "uint256" },
|
|
55
|
+
{ name: "", type: "uint80", internalType: "uint80" }
|
|
56
|
+
],
|
|
57
|
+
stateMutability: "view"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
type: "function",
|
|
61
|
+
name: "serialize",
|
|
62
|
+
inputs: [],
|
|
63
|
+
outputs: [{ name: "serializedData", type: "bytes", internalType: "bytes" }],
|
|
64
|
+
stateMutability: "view"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
type: "function",
|
|
68
|
+
name: "skipPriceCheck",
|
|
69
|
+
inputs: [],
|
|
70
|
+
outputs: [{ name: "", type: "bool", internalType: "bool" }],
|
|
71
|
+
stateMutability: "view"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
type: "function",
|
|
75
|
+
name: "version",
|
|
76
|
+
inputs: [],
|
|
77
|
+
outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
|
|
78
|
+
stateMutability: "view"
|
|
79
|
+
}
|
|
80
|
+
];
|
|
81
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
82
|
+
0 && (module.exports = {
|
|
83
|
+
iPriceFeedAbi
|
|
84
|
+
});
|
|
@@ -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"}
|
|
@@ -21,9 +21,25 @@ __export(assetsMath_exports, {
|
|
|
21
21
|
AssetUtils: () => AssetUtils
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(assetsMath_exports);
|
|
24
|
-
var
|
|
25
|
-
var
|
|
24
|
+
var import_bigintMath = require("./bigintMath.js");
|
|
25
|
+
var import_sort = require("./creditAccount/sort.js");
|
|
26
|
+
var import_priceMath = require("./priceMath.js");
|
|
26
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
|
+
*/
|
|
27
43
|
static nextAsset({
|
|
28
44
|
allowedTokens,
|
|
29
45
|
selectedAssets,
|
|
@@ -42,7 +58,7 @@ class AssetUtils {
|
|
|
42
58
|
const alreadySelected = selectedRecord[allowedToken.toLowerCase()];
|
|
43
59
|
return !alreadySelected;
|
|
44
60
|
});
|
|
45
|
-
const sorted =
|
|
61
|
+
const sorted = (0, import_sort.sortBalances)(
|
|
46
62
|
AssetUtils.getBalances(notSelected, balances),
|
|
47
63
|
prices,
|
|
48
64
|
tokensList
|
|
@@ -50,15 +66,30 @@ class AssetUtils {
|
|
|
50
66
|
const [address] = sorted[0] || [];
|
|
51
67
|
return address;
|
|
52
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
|
+
*/
|
|
53
78
|
static getBalances(allowedTokens, externalBalances) {
|
|
54
79
|
return allowedTokens.reduce((acc, address) => {
|
|
55
80
|
const addressLc = address.toLowerCase();
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
[addressLc]: externalBalances[addressLc] || 0n
|
|
59
|
-
};
|
|
81
|
+
acc[addressLc] = externalBalances[addressLc] || 0n;
|
|
82
|
+
return acc;
|
|
60
83
|
}, {});
|
|
61
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
|
+
*/
|
|
62
93
|
static constructAssetRecord(a) {
|
|
63
94
|
const record = a.reduce((acc, asset) => {
|
|
64
95
|
acc[asset.token] = asset;
|
|
@@ -66,6 +97,18 @@ class AssetUtils {
|
|
|
66
97
|
}, {});
|
|
67
98
|
return record;
|
|
68
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
|
+
*/
|
|
69
112
|
static memoWrap = (unwrappedAddress, wrappedAddress, prices, tokensList) => function wrap(assets) {
|
|
70
113
|
const assetsRecord = AssetUtils.constructAssetRecord(assets);
|
|
71
114
|
const unwrapped = assetsRecord[unwrappedAddress];
|
|
@@ -77,10 +120,10 @@ class AssetUtils {
|
|
|
77
120
|
const unwrappedPrice = prices[unwrappedAddress] || 0n;
|
|
78
121
|
const wrappedToken = tokensList[wrappedAddress];
|
|
79
122
|
const wrappedPrice = prices[wrappedAddress] || 0n;
|
|
80
|
-
const unwrappedInWrapped =
|
|
81
|
-
|
|
123
|
+
const unwrappedInWrapped = import_priceMath.PriceUtils.convertByPrice(
|
|
124
|
+
import_priceMath.PriceUtils.calcTotalPrice(
|
|
82
125
|
unwrappedPrice,
|
|
83
|
-
|
|
126
|
+
import_bigintMath.BigIntMath.max(0n, unwrappedAmount),
|
|
84
127
|
unwrappedToken.decimals
|
|
85
128
|
),
|
|
86
129
|
{
|
|
@@ -90,7 +133,7 @@ class AssetUtils {
|
|
|
90
133
|
);
|
|
91
134
|
assetsRecord[wrappedAddress] = {
|
|
92
135
|
token: wrappedAddress,
|
|
93
|
-
balance:
|
|
136
|
+
balance: import_bigintMath.BigIntMath.max(0n, wrappedAmount) + unwrappedInWrapped
|
|
94
137
|
};
|
|
95
138
|
delete assetsRecord[unwrappedAddress];
|
|
96
139
|
return [Object.values(assetsRecord), unwrappedInWrapped, wrappedAmount];
|
|
@@ -98,15 +141,23 @@ class AssetUtils {
|
|
|
98
141
|
return [Object.values(assetsRecord), 0n, wrappedAmount];
|
|
99
142
|
};
|
|
100
143
|
/**
|
|
101
|
-
*
|
|
102
|
-
*
|
|
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.
|
|
103
154
|
*/
|
|
104
155
|
static sumAssets(a, b) {
|
|
105
156
|
const aRecord = AssetUtils.constructAssetRecord(a);
|
|
106
157
|
const resRecord = b.reduce((acc, bAsset) => {
|
|
107
158
|
const aAsset = acc[bAsset.token];
|
|
108
159
|
const { balance: amount = 0n } = aAsset || {};
|
|
109
|
-
const amountSum =
|
|
160
|
+
const amountSum = import_bigintMath.BigIntMath.max(0n, bAsset.balance) + import_bigintMath.BigIntMath.max(0n, amount);
|
|
110
161
|
const aOrB = aAsset || bAsset;
|
|
111
162
|
acc[bAsset.token] = {
|
|
112
163
|
...aOrB,
|
|
@@ -117,29 +168,45 @@ class AssetUtils {
|
|
|
117
168
|
return Object.values(resRecord);
|
|
118
169
|
}
|
|
119
170
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
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.
|
|
122
181
|
*/
|
|
123
182
|
static addBalances(a, b) {
|
|
124
183
|
const bRecord = AssetUtils.constructAssetRecord(b);
|
|
125
184
|
return a.map((asset) => {
|
|
126
185
|
const bAsset = bRecord[asset.token];
|
|
127
186
|
const { balance: bAmount = 0n } = bAsset || {};
|
|
128
|
-
const amountSum =
|
|
187
|
+
const amountSum = import_bigintMath.BigIntMath.max(0n, asset.balance) + import_bigintMath.BigIntMath.max(0n, bAmount);
|
|
129
188
|
return { ...asset, balance: amountSum };
|
|
130
189
|
});
|
|
131
190
|
}
|
|
132
191
|
/**
|
|
133
|
-
* Subtracts
|
|
134
|
-
*
|
|
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.
|
|
135
202
|
*/
|
|
136
203
|
static subAssets(a, b) {
|
|
137
204
|
const bRecord = AssetUtils.constructAssetRecord(b);
|
|
138
205
|
return a.map((asset) => {
|
|
139
206
|
const bAsset = bRecord[asset.token];
|
|
140
207
|
const { balance: bAmount = 0n } = bAsset || {};
|
|
141
|
-
const amountSub =
|
|
142
|
-
return { ...asset, balance:
|
|
208
|
+
const amountSub = import_bigintMath.BigIntMath.max(0n, asset.balance) - import_bigintMath.BigIntMath.max(0n, bAmount);
|
|
209
|
+
return { ...asset, balance: import_bigintMath.BigIntMath.max(0n, amountSub) };
|
|
143
210
|
});
|
|
144
211
|
}
|
|
145
212
|
}
|
|
@@ -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
|
+
});
|