@gearbox-protocol/sdk 14.10.1 → 14.10.2-next.2
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/test-utils/index.js +139 -0
- package/dist/cjs/common-utils/utils/apy/calculate-borrow-rate-from-utilization.js +59 -0
- package/dist/cjs/common-utils/utils/apy/calculate-borrow-rate-from-utilization.spec.js +32 -0
- package/dist/cjs/common-utils/utils/apy/calculate-earnings.js +59 -0
- package/dist/cjs/common-utils/utils/apy/calculate-earnings.spec.js +62 -0
- package/dist/cjs/common-utils/utils/apy/calculate-safe-borrow-rate.js +55 -0
- package/dist/cjs/common-utils/utils/apy/calculate-safe-borrow-rate.spec.js +39 -0
- package/dist/cjs/common-utils/utils/apy/get-safe-base-borrow-rate.js +38 -0
- package/dist/cjs/common-utils/utils/apy/index.js +8 -0
- package/dist/cjs/common-utils/utils/constants.js +34 -0
- package/dist/cjs/common-utils/utils/index.js +5 -1
- package/dist/cjs/common-utils/utils/strategies/assets/assets.js +30 -0
- package/dist/cjs/common-utils/utils/strategies/assets/index.js +22 -0
- package/dist/cjs/common-utils/utils/strategies/availability/index.js +1 -5
- package/dist/cjs/common-utils/utils/strategies/credit-managers/get-cm-allowed-collaterals.js +62 -0
- package/dist/cjs/common-utils/utils/strategies/credit-managers/get-cm-allowed-collaterals.spec.js +109 -0
- package/dist/cjs/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.js +75 -0
- package/dist/cjs/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.spec.js +93 -0
- package/dist/cjs/common-utils/utils/strategies/credit-managers/index.js +4 -0
- package/dist/cjs/common-utils/utils/strategies/index.js +2 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +63 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.spec.js +330 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/get-collateral-by-debt.js +34 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/get-collateral-by-debt.spec.js +30 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/get-recommended-debt.js +85 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/get-recommended-debt.spec.js +227 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/get-recommended-leverage-factor.js +33 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/get-recommended-leverage-factor.spec.js +23 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/index.js +8 -0
- package/dist/cjs/common-utils/utils/strategies/leverage/max-leverage.js +2 -2
- package/dist/cjs/common-utils/utils/strategies/strategy-info/calculate-total-apy.js +112 -0
- package/dist/cjs/common-utils/utils/strategies/strategy-info/calculate-total-apy.spec.js +183 -0
- package/dist/cjs/common-utils/utils/strategies/strategy-info/calculate-total-points.js +74 -0
- package/dist/cjs/common-utils/utils/strategies/strategy-info/calculate-total-points.spec.js +131 -0
- package/dist/cjs/common-utils/utils/strategies/strategy-info/cm-availability-condition.js +5 -4
- package/dist/cjs/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +410 -0
- package/dist/cjs/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.spec.js +274 -0
- package/dist/cjs/common-utils/utils/strategies/strategy-info/get-strategy-you-can-earn.js +108 -0
- package/dist/cjs/common-utils/utils/strategies/strategy-info/index.js +8 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/add-amount-in-target.js +41 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/add-amount-in-target.spec.js +38 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/get-chain-phantom-tokens.js +35 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/get-list-with-amount-in-target.js +42 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/get-list-with-amount-in-target.spec.js +32 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/get-native-token-address.js +31 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/index.js +19 -1
- package/dist/cjs/common-utils/utils/strategies/tokens/is-apy-with-points-exception.js +36 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/is-collateral-token.js +59 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/is-collateral-token.spec.js +135 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/is-obtainable-token.js +42 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/is-obtainable-token.spec.js +162 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/is-valid-extra-collateral-token.js +51 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/wrap-token-address.js +33 -0
- package/dist/cjs/common-utils/utils/strategies/tokens/wrap-token-address.spec.js +13 -0
- package/dist/cjs/common-utils/utils/strategies/types/index.js +4 -0
- package/dist/cjs/common-utils/utils/strategies/types/strategy-data.js +16 -0
- package/dist/cjs/common-utils/utils/strategies/types/strategy-earnings.js +16 -0
- package/dist/cjs/common-utils/utils/validation/index.js +38 -0
- package/dist/cjs/common-utils/utils/validation/is-zero-balance.js +30 -0
- package/dist/cjs/common-utils/utils/validation/validate-balance.js +47 -0
- package/dist/cjs/common-utils/utils/validation/validate-balance.spec.js +52 -0
- package/dist/cjs/common-utils/utils/validation/validate-balances.js +44 -0
- package/dist/cjs/common-utils/utils/validation/validate-balances.spec.js +37 -0
- package/dist/cjs/common-utils/utils/validation/validate-credit-manager.js +34 -0
- package/dist/cjs/common-utils/utils/validation/validate-credit-manager.spec.js +36 -0
- package/dist/cjs/common-utils/utils/validation/validate-hf.js +38 -0
- package/dist/cjs/common-utils/utils/validation/validate-hf.spec.js +24 -0
- package/dist/cjs/common-utils/utils/{strategies/availability → validation}/validate-open-account-pool-quota-status.js +1 -4
- package/dist/cjs/common-utils/utils/validation/validate-open-account-pool-quota-status.spec.js +43 -0
- package/dist/cjs/common-utils/utils/{strategies/availability → validation}/validate-open-account-pool-status.js +1 -1
- package/dist/cjs/common-utils/utils/validation/validate-open-account-pool-status.spec.js +98 -0
- package/dist/cjs/common-utils/utils/validation/validate-open-account.js +41 -0
- package/dist/cjs/common-utils/utils/validation/validate-open-account.spec.js +54 -0
- package/dist/cjs/common-utils/utils/validation/validate-quota.js +49 -0
- package/dist/cjs/common-utils/utils/validation/validate-quota.spec.js +79 -0
- package/dist/cjs/common-utils/utils/validation/validate-token-to-obtain.js +42 -0
- package/dist/cjs/common-utils/utils/validation/validate-token-to-obtain.spec.js +35 -0
- package/dist/cjs/common-utils/utils/validation/validation-types.js +16 -0
- package/dist/cjs/plugins/apy/strategy-data-source.js +1 -0
- package/dist/cjs/sdk/constants/math.js +3 -0
- package/dist/esm/common-utils/test-utils/index.js +104 -0
- package/dist/esm/common-utils/utils/apy/calculate-borrow-rate-from-utilization.js +35 -0
- package/dist/esm/common-utils/utils/apy/calculate-borrow-rate-from-utilization.spec.js +31 -0
- package/dist/esm/common-utils/utils/apy/calculate-earnings.js +35 -0
- package/dist/esm/common-utils/utils/apy/calculate-earnings.spec.js +65 -0
- package/dist/esm/common-utils/utils/apy/calculate-safe-borrow-rate.js +34 -0
- package/dist/esm/common-utils/utils/apy/calculate-safe-borrow-rate.spec.js +38 -0
- package/dist/esm/common-utils/utils/apy/get-safe-base-borrow-rate.js +16 -0
- package/dist/esm/common-utils/utils/apy/index.js +4 -0
- package/dist/esm/common-utils/utils/constants.js +8 -0
- package/dist/esm/common-utils/utils/index.js +2 -0
- package/dist/esm/common-utils/utils/strategies/assets/assets.js +6 -0
- package/dist/esm/common-utils/utils/strategies/assets/index.js +1 -0
- package/dist/esm/common-utils/utils/strategies/availability/index.js +0 -2
- package/dist/esm/common-utils/utils/strategies/credit-managers/get-cm-allowed-collaterals.js +38 -0
- package/dist/esm/common-utils/utils/strategies/credit-managers/get-cm-allowed-collaterals.spec.js +115 -0
- package/dist/esm/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.js +51 -0
- package/dist/esm/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.spec.js +96 -0
- package/dist/esm/common-utils/utils/strategies/credit-managers/index.js +2 -0
- package/dist/esm/common-utils/utils/strategies/index.js +1 -0
- package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.js +42 -0
- package/dist/esm/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.spec.js +332 -0
- package/dist/esm/common-utils/utils/strategies/leverage/get-collateral-by-debt.js +10 -0
- package/dist/esm/common-utils/utils/strategies/leverage/get-collateral-by-debt.spec.js +29 -0
- package/dist/esm/common-utils/utils/strategies/leverage/get-recommended-debt.js +61 -0
- package/dist/esm/common-utils/utils/strategies/leverage/get-recommended-debt.spec.js +226 -0
- package/dist/esm/common-utils/utils/strategies/leverage/get-recommended-leverage-factor.js +9 -0
- package/dist/esm/common-utils/utils/strategies/leverage/get-recommended-leverage-factor.spec.js +22 -0
- package/dist/esm/common-utils/utils/strategies/leverage/index.js +4 -0
- package/dist/esm/common-utils/utils/strategies/leverage/max-leverage.js +2 -2
- package/dist/esm/common-utils/utils/strategies/strategy-info/calculate-total-apy.js +88 -0
- package/dist/esm/common-utils/utils/strategies/strategy-info/calculate-total-apy.spec.js +187 -0
- package/dist/esm/common-utils/utils/strategies/strategy-info/calculate-total-points.js +54 -0
- package/dist/esm/common-utils/utils/strategies/strategy-info/calculate-total-points.spec.js +139 -0
- package/dist/esm/common-utils/utils/strategies/strategy-info/cm-availability-condition.js +2 -5
- package/dist/esm/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.js +389 -0
- package/dist/esm/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.spec.js +278 -0
- package/dist/esm/common-utils/utils/strategies/strategy-info/get-strategy-you-can-earn.js +84 -0
- package/dist/esm/common-utils/utils/strategies/strategy-info/index.js +4 -0
- package/dist/esm/common-utils/utils/strategies/tokens/add-amount-in-target.js +17 -0
- package/dist/esm/common-utils/utils/strategies/tokens/add-amount-in-target.spec.js +41 -0
- package/dist/esm/common-utils/utils/strategies/tokens/get-chain-phantom-tokens.js +11 -0
- package/dist/esm/common-utils/utils/strategies/tokens/get-list-with-amount-in-target.js +18 -0
- package/dist/esm/common-utils/utils/strategies/tokens/get-list-with-amount-in-target.spec.js +36 -0
- package/dist/esm/common-utils/utils/strategies/tokens/get-native-token-address.js +7 -0
- package/dist/esm/common-utils/utils/strategies/tokens/index.js +9 -0
- package/dist/esm/common-utils/utils/strategies/tokens/is-apy-with-points-exception.js +11 -0
- package/dist/esm/common-utils/utils/strategies/tokens/is-collateral-token.js +35 -0
- package/dist/esm/common-utils/utils/strategies/tokens/is-collateral-token.spec.js +137 -0
- package/dist/esm/common-utils/utils/strategies/tokens/is-obtainable-token.js +18 -0
- package/dist/esm/common-utils/utils/strategies/tokens/is-obtainable-token.spec.js +161 -0
- package/dist/esm/common-utils/utils/strategies/tokens/is-valid-extra-collateral-token.js +27 -0
- package/dist/esm/common-utils/utils/strategies/tokens/wrap-token-address.js +9 -0
- package/dist/esm/common-utils/utils/strategies/tokens/wrap-token-address.spec.js +12 -0
- package/dist/esm/common-utils/utils/strategies/types/index.js +2 -0
- package/dist/esm/common-utils/utils/strategies/types/strategy-data.js +0 -0
- package/dist/esm/common-utils/utils/strategies/types/strategy-earnings.js +0 -0
- package/dist/esm/common-utils/utils/validation/index.js +9 -0
- package/dist/esm/common-utils/utils/validation/is-zero-balance.js +6 -0
- package/dist/esm/common-utils/utils/validation/validate-balance.js +23 -0
- package/dist/esm/common-utils/utils/validation/validate-balance.spec.js +51 -0
- package/dist/esm/common-utils/utils/validation/validate-balances.js +20 -0
- package/dist/esm/common-utils/utils/validation/validate-balances.spec.js +36 -0
- package/dist/esm/common-utils/utils/validation/validate-credit-manager.js +10 -0
- package/dist/esm/common-utils/utils/validation/validate-credit-manager.spec.js +35 -0
- package/dist/esm/common-utils/utils/validation/validate-hf.js +13 -0
- package/dist/esm/common-utils/utils/validation/validate-hf.spec.js +23 -0
- package/dist/esm/common-utils/utils/{strategies/availability → validation}/validate-open-account-pool-quota-status.js +1 -4
- package/dist/esm/common-utils/utils/validation/validate-open-account-pool-quota-status.spec.js +46 -0
- package/dist/esm/common-utils/utils/{strategies/availability → validation}/validate-open-account-pool-status.js +4 -2
- package/dist/esm/common-utils/utils/validation/validate-open-account-pool-status.spec.js +102 -0
- package/dist/esm/common-utils/utils/validation/validate-open-account.js +17 -0
- package/dist/esm/common-utils/utils/validation/validate-open-account.spec.js +56 -0
- package/dist/esm/common-utils/utils/validation/validate-quota.js +25 -0
- package/dist/esm/common-utils/utils/validation/validate-quota.spec.js +83 -0
- package/dist/esm/common-utils/utils/validation/validate-token-to-obtain.js +18 -0
- package/dist/esm/common-utils/utils/validation/validate-token-to-obtain.spec.js +34 -0
- package/dist/esm/common-utils/utils/validation/validation-types.js +0 -0
- package/dist/esm/plugins/apy/strategy-data-source.js +1 -0
- package/dist/esm/sdk/constants/math.js +2 -0
- package/dist/types/common-utils/test-utils/index.d.ts +16 -0
- package/dist/types/common-utils/utils/apy/calculate-borrow-rate-from-utilization.d.ts +2 -0
- package/dist/types/common-utils/utils/apy/calculate-borrow-rate-from-utilization.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/apy/calculate-earnings.d.ts +16 -0
- package/dist/types/common-utils/utils/apy/calculate-earnings.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/apy/calculate-safe-borrow-rate.d.ts +11 -0
- package/dist/types/common-utils/utils/apy/calculate-safe-borrow-rate.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/apy/get-safe-base-borrow-rate.d.ts +4 -0
- package/dist/types/common-utils/utils/apy/index.d.ts +4 -0
- package/dist/types/common-utils/utils/constants.d.ts +4 -0
- package/dist/types/common-utils/utils/index.d.ts +2 -0
- package/dist/types/common-utils/utils/strategies/assets/assets.d.ts +3 -0
- package/dist/types/common-utils/utils/strategies/assets/index.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/availability/index.d.ts +0 -2
- package/dist/types/common-utils/utils/strategies/credit-managers/get-cm-allowed-collaterals.d.ts +20 -0
- package/dist/types/common-utils/utils/strategies/credit-managers/get-cm-allowed-collaterals.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.d.ts +13 -0
- package/dist/types/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/credit-managers/index.d.ts +2 -0
- package/dist/types/common-utils/utils/strategies/index.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.d.ts +11 -0
- package/dist/types/common-utils/utils/strategies/leverage/calculate-max-strategy-debt.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/leverage/get-collateral-by-debt.d.ts +2 -0
- package/dist/types/common-utils/utils/strategies/leverage/get-collateral-by-debt.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/leverage/get-recommended-debt.d.ts +21 -0
- package/dist/types/common-utils/utils/strategies/leverage/get-recommended-debt.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/leverage/get-recommended-leverage-factor.d.ts +4 -0
- package/dist/types/common-utils/utils/strategies/leverage/get-recommended-leverage-factor.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/leverage/index.d.ts +4 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/calculate-total-apy.d.ts +28 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/calculate-total-apy.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/calculate-total-points.d.ts +22 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/calculate-total-points.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.d.ts +19 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/get-cm-you-can-earn.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/get-strategy-you-can-earn.d.ts +29 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/index.d.ts +4 -0
- package/dist/types/common-utils/utils/strategies/strategy-info/types.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/tokens/add-amount-in-target.d.ts +6 -0
- package/dist/types/common-utils/utils/strategies/tokens/add-amount-in-target.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/tokens/get-chain-phantom-tokens.d.ts +6 -0
- package/dist/types/common-utils/utils/strategies/tokens/get-list-with-amount-in-target.d.ts +12 -0
- package/dist/types/common-utils/utils/strategies/tokens/get-list-with-amount-in-target.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/tokens/get-native-token-address.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/tokens/index.d.ts +9 -0
- package/dist/types/common-utils/utils/strategies/tokens/is-apy-with-points-exception.d.ts +3 -0
- package/dist/types/common-utils/utils/strategies/tokens/is-collateral-token.d.ts +16 -0
- package/dist/types/common-utils/utils/strategies/tokens/is-collateral-token.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/tokens/is-obtainable-token.d.ts +14 -0
- package/dist/types/common-utils/utils/strategies/tokens/is-obtainable-token.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/tokens/is-valid-extra-collateral-token.d.ts +20 -0
- package/dist/types/common-utils/utils/strategies/tokens/wrap-token-address.d.ts +2 -0
- package/dist/types/common-utils/utils/strategies/tokens/wrap-token-address.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/strategies/types/index.d.ts +4 -1
- package/dist/types/common-utils/utils/strategies/types/strategy-data.d.ts +64 -0
- package/dist/types/common-utils/utils/strategies/types/strategy-earnings.d.ts +43 -0
- package/dist/types/common-utils/utils/validation/index.d.ts +9 -0
- package/dist/types/common-utils/utils/validation/is-zero-balance.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-balance.d.ts +22 -0
- package/dist/types/common-utils/utils/validation/validate-balance.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-balances.d.ts +9 -0
- package/dist/types/common-utils/utils/validation/validate-balances.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-credit-manager.d.ts +10 -0
- package/dist/types/common-utils/utils/validation/validate-credit-manager.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-hf.d.ts +9 -0
- package/dist/types/common-utils/utils/validation/validate-hf.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-open-account-pool-quota-status.d.ts +7 -0
- package/dist/types/common-utils/utils/validation/validate-open-account-pool-quota-status.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-open-account-pool-status.d.ts +23 -0
- package/dist/types/common-utils/utils/validation/validate-open-account-pool-status.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-open-account.d.ts +21 -0
- package/dist/types/common-utils/utils/validation/validate-open-account.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-quota.d.ts +20 -0
- package/dist/types/common-utils/utils/validation/validate-quota.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validate-token-to-obtain.d.ts +13 -0
- package/dist/types/common-utils/utils/validation/validate-token-to-obtain.spec.d.ts +1 -0
- package/dist/types/common-utils/utils/validation/validation-types.d.ts +14 -0
- package/dist/types/dev/RevolverTransport.d.ts +4 -4
- package/dist/types/sdk/constants/math.d.ts +1 -0
- package/package.json +1 -1
- package/dist/cjs/common-utils/utils/strategies/__test-utils.js +0 -95
- package/dist/esm/common-utils/utils/strategies/__test-utils.js +0 -65
- package/dist/types/common-utils/utils/strategies/__test-utils.d.ts +0 -11
- package/dist/types/common-utils/utils/strategies/availability/validate-open-account-pool-quota-status.d.ts +0 -6
- package/dist/types/common-utils/utils/strategies/availability/validate-open-account-pool-status.d.ts +0 -24
package/dist/esm/common-utils/utils/strategies/credit-managers/get-cm-allowed-collaterals.spec.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { TokenData } from "../../../charts/token-data.js";
|
|
3
|
+
import {
|
|
4
|
+
buildCreditManager,
|
|
5
|
+
mockNativeTokenAddress,
|
|
6
|
+
mockToken1,
|
|
7
|
+
mockToken2,
|
|
8
|
+
mockUnderlyingToken,
|
|
9
|
+
mockWrappedNativeTokenAddress
|
|
10
|
+
} from "../../../test-utils/index.js";
|
|
11
|
+
import { isCollateralToken } from "../tokens/is-collateral-token.js";
|
|
12
|
+
import { getCMAllowedCollaterals } from "./get-cm-allowed-collaterals.js";
|
|
13
|
+
vi.mock("./is-collateral-token");
|
|
14
|
+
const mockIsCollateralToken = vi.mocked(isCollateralToken);
|
|
15
|
+
describe("getCMAllowedCollaterals", () => {
|
|
16
|
+
const underlying = mockUnderlyingToken;
|
|
17
|
+
const allowed = mockToken1;
|
|
18
|
+
const rejected = mockToken2;
|
|
19
|
+
const tokensList = {
|
|
20
|
+
[underlying]: new TokenData({
|
|
21
|
+
addr: underlying,
|
|
22
|
+
symbol: "DAI",
|
|
23
|
+
name: "Underlying",
|
|
24
|
+
decimals: 18,
|
|
25
|
+
isPhantom: false
|
|
26
|
+
}),
|
|
27
|
+
[allowed]: new TokenData({
|
|
28
|
+
addr: allowed,
|
|
29
|
+
symbol: "USDC",
|
|
30
|
+
name: "Allowed",
|
|
31
|
+
decimals: 6,
|
|
32
|
+
isPhantom: false
|
|
33
|
+
}),
|
|
34
|
+
[rejected]: new TokenData({
|
|
35
|
+
addr: rejected,
|
|
36
|
+
symbol: "WBTC",
|
|
37
|
+
name: "Rejected",
|
|
38
|
+
decimals: 8,
|
|
39
|
+
isPhantom: false
|
|
40
|
+
}),
|
|
41
|
+
[mockWrappedNativeTokenAddress]: new TokenData({
|
|
42
|
+
addr: mockWrappedNativeTokenAddress,
|
|
43
|
+
symbol: "WETH",
|
|
44
|
+
name: "WrappedNative",
|
|
45
|
+
decimals: 18,
|
|
46
|
+
isPhantom: false
|
|
47
|
+
})
|
|
48
|
+
};
|
|
49
|
+
const baseCm = buildCreditManager({
|
|
50
|
+
address: "0x0000000000000000000000000000000000000CAA",
|
|
51
|
+
underlyingToken: underlying,
|
|
52
|
+
collateralTokens: [
|
|
53
|
+
underlying,
|
|
54
|
+
allowed,
|
|
55
|
+
rejected,
|
|
56
|
+
mockWrappedNativeTokenAddress
|
|
57
|
+
]
|
|
58
|
+
});
|
|
59
|
+
afterEach(() => {
|
|
60
|
+
vi.restoreAllMocks();
|
|
61
|
+
vi.clearAllMocks();
|
|
62
|
+
});
|
|
63
|
+
it("returns empty record when credit manager missing", () => {
|
|
64
|
+
const result = getCMAllowedCollaterals({
|
|
65
|
+
creditManager: void 0,
|
|
66
|
+
tokensList,
|
|
67
|
+
delayedPhantoms: {},
|
|
68
|
+
nativeTokenAddress: mockNativeTokenAddress,
|
|
69
|
+
wrappedNativeTokenAddress: mockWrappedNativeTokenAddress,
|
|
70
|
+
extraCollaterals: void 0
|
|
71
|
+
});
|
|
72
|
+
expect(result).toEqual({});
|
|
73
|
+
});
|
|
74
|
+
it("keeps allowed collaterals and mirrors wrapped native to native token", () => {
|
|
75
|
+
mockIsCollateralToken.mockImplementation(
|
|
76
|
+
({ address }) => address !== rejected
|
|
77
|
+
);
|
|
78
|
+
const result = getCMAllowedCollaterals({
|
|
79
|
+
creditManager: baseCm,
|
|
80
|
+
tokensList,
|
|
81
|
+
delayedPhantoms: {},
|
|
82
|
+
nativeTokenAddress: mockNativeTokenAddress,
|
|
83
|
+
wrappedNativeTokenAddress: mockWrappedNativeTokenAddress,
|
|
84
|
+
extraCollaterals: void 0
|
|
85
|
+
});
|
|
86
|
+
expect(result).toEqual({
|
|
87
|
+
[underlying]: underlying,
|
|
88
|
+
[allowed]: allowed,
|
|
89
|
+
[mockWrappedNativeTokenAddress]: mockWrappedNativeTokenAddress,
|
|
90
|
+
[mockNativeTokenAddress]: mockNativeTokenAddress
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
it("does not mirror wrapped native when credit manager is paused", () => {
|
|
94
|
+
mockIsCollateralToken.mockReturnValue(true);
|
|
95
|
+
const pausedCm = buildCreditManager({
|
|
96
|
+
...baseCm,
|
|
97
|
+
isPaused: true
|
|
98
|
+
});
|
|
99
|
+
const result = getCMAllowedCollaterals({
|
|
100
|
+
creditManager: pausedCm,
|
|
101
|
+
tokensList,
|
|
102
|
+
delayedPhantoms: {},
|
|
103
|
+
nativeTokenAddress: mockNativeTokenAddress,
|
|
104
|
+
wrappedNativeTokenAddress: mockWrappedNativeTokenAddress,
|
|
105
|
+
extraCollaterals: void 0
|
|
106
|
+
});
|
|
107
|
+
expect(result).toEqual({
|
|
108
|
+
[underlying]: underlying,
|
|
109
|
+
[allowed]: allowed,
|
|
110
|
+
[rejected]: rejected,
|
|
111
|
+
[mockWrappedNativeTokenAddress]: mockWrappedNativeTokenAddress
|
|
112
|
+
});
|
|
113
|
+
expect(result[mockNativeTokenAddress]).toBeUndefined();
|
|
114
|
+
});
|
|
115
|
+
});
|
package/dist/esm/common-utils/utils/strategies/credit-managers/get-wallet-balances-allowed-on-cm.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { sortBalances } from "../../creditAccount/sort.js";
|
|
2
|
+
import { wrapTokenAddress } from "../tokens/wrap-token-address.js";
|
|
3
|
+
function getWalletBalancesAllowedOnCM({
|
|
4
|
+
walletBalances,
|
|
5
|
+
collateralRecord,
|
|
6
|
+
nativeTokenAddress,
|
|
7
|
+
wrappedNativeTokenAddress,
|
|
8
|
+
prices,
|
|
9
|
+
tokensList,
|
|
10
|
+
isPaused = false
|
|
11
|
+
}) {
|
|
12
|
+
const walletBalancesSorted = sortBalances(
|
|
13
|
+
walletBalances || {},
|
|
14
|
+
prices,
|
|
15
|
+
tokensList
|
|
16
|
+
);
|
|
17
|
+
const { list, record } = walletBalancesSorted.reduce(
|
|
18
|
+
(acc, [token, balance]) => {
|
|
19
|
+
if (isPaused && token === nativeTokenAddress) return acc;
|
|
20
|
+
const wrappedAddress = wrapTokenAddress(
|
|
21
|
+
token,
|
|
22
|
+
nativeTokenAddress,
|
|
23
|
+
wrappedNativeTokenAddress
|
|
24
|
+
);
|
|
25
|
+
if (collateralRecord[wrappedAddress]) {
|
|
26
|
+
const asset = {
|
|
27
|
+
token,
|
|
28
|
+
balance
|
|
29
|
+
};
|
|
30
|
+
acc.list.push(asset);
|
|
31
|
+
acc.record[wrappedAddress] = asset;
|
|
32
|
+
}
|
|
33
|
+
return acc;
|
|
34
|
+
},
|
|
35
|
+
{ list: [], record: {} }
|
|
36
|
+
);
|
|
37
|
+
const collateralList = Object.values(collateralRecord);
|
|
38
|
+
const missingAssets = collateralList.filter(
|
|
39
|
+
(token) => isPaused ? token !== nativeTokenAddress && !record[token] : !record[token]
|
|
40
|
+
).map(
|
|
41
|
+
(token) => ({
|
|
42
|
+
token,
|
|
43
|
+
balance: 0n
|
|
44
|
+
})
|
|
45
|
+
);
|
|
46
|
+
const r = [...list, ...missingAssets];
|
|
47
|
+
return r;
|
|
48
|
+
}
|
|
49
|
+
export {
|
|
50
|
+
getWalletBalancesAllowedOnCM
|
|
51
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { TokenData } from "../../../charts/token-data.js";
|
|
3
|
+
import {
|
|
4
|
+
mockNativeTokenAddress,
|
|
5
|
+
mockToken1,
|
|
6
|
+
mockWrappedNativeTokenAddress
|
|
7
|
+
} from "../../../test-utils/index.js";
|
|
8
|
+
import { sortBalances } from "../../creditAccount/sort.js";
|
|
9
|
+
import { getWalletBalancesAllowedOnCM } from "./get-wallet-balances-allowed-on-cm.js";
|
|
10
|
+
vi.mock("../../utils/creditAccount/sort.js", async () => {
|
|
11
|
+
const actual = await vi.importActual(
|
|
12
|
+
"../../utils/creditAccount/sort.js"
|
|
13
|
+
);
|
|
14
|
+
return {
|
|
15
|
+
...actual,
|
|
16
|
+
sortBalances: vi.fn()
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
const mockSortBalances = vi.mocked(sortBalances);
|
|
20
|
+
describe("getWalletBalancesAllowedOnCM", () => {
|
|
21
|
+
const allowed = mockToken1;
|
|
22
|
+
const prices = {};
|
|
23
|
+
const tokensList = {
|
|
24
|
+
[mockNativeTokenAddress]: new TokenData({
|
|
25
|
+
addr: mockNativeTokenAddress,
|
|
26
|
+
symbol: "ETH",
|
|
27
|
+
name: "Native",
|
|
28
|
+
decimals: 18,
|
|
29
|
+
isPhantom: false
|
|
30
|
+
}),
|
|
31
|
+
[mockWrappedNativeTokenAddress]: new TokenData({
|
|
32
|
+
addr: mockWrappedNativeTokenAddress,
|
|
33
|
+
symbol: "WETH",
|
|
34
|
+
name: "Wrapped",
|
|
35
|
+
decimals: 18,
|
|
36
|
+
isPhantom: false
|
|
37
|
+
}),
|
|
38
|
+
[allowed]: new TokenData({
|
|
39
|
+
addr: allowed,
|
|
40
|
+
symbol: "DAI",
|
|
41
|
+
name: "Allowed",
|
|
42
|
+
decimals: 18,
|
|
43
|
+
isPhantom: false
|
|
44
|
+
})
|
|
45
|
+
};
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
vi.restoreAllMocks();
|
|
48
|
+
vi.clearAllMocks();
|
|
49
|
+
});
|
|
50
|
+
it("wraps native token when allowed and keeps sorted balances", () => {
|
|
51
|
+
const collateralRecord = {
|
|
52
|
+
[mockWrappedNativeTokenAddress]: mockWrappedNativeTokenAddress,
|
|
53
|
+
[allowed]: allowed
|
|
54
|
+
};
|
|
55
|
+
mockSortBalances.mockReturnValue([
|
|
56
|
+
[mockNativeTokenAddress, 2n],
|
|
57
|
+
[allowed, 1n]
|
|
58
|
+
]);
|
|
59
|
+
const result = getWalletBalancesAllowedOnCM({
|
|
60
|
+
walletBalances: {
|
|
61
|
+
[mockNativeTokenAddress]: 2n,
|
|
62
|
+
[allowed]: 1n
|
|
63
|
+
},
|
|
64
|
+
collateralRecord,
|
|
65
|
+
nativeTokenAddress: mockNativeTokenAddress,
|
|
66
|
+
wrappedNativeTokenAddress: mockWrappedNativeTokenAddress,
|
|
67
|
+
prices,
|
|
68
|
+
tokensList
|
|
69
|
+
});
|
|
70
|
+
expect(result).toEqual([
|
|
71
|
+
{ token: mockNativeTokenAddress, balance: 2n },
|
|
72
|
+
{ token: allowed, balance: 1n }
|
|
73
|
+
]);
|
|
74
|
+
});
|
|
75
|
+
it("skips native token when paused and fills missing collaterals with zero", () => {
|
|
76
|
+
const collateralRecord = {
|
|
77
|
+
[mockWrappedNativeTokenAddress]: mockWrappedNativeTokenAddress,
|
|
78
|
+
[allowed]: allowed,
|
|
79
|
+
[mockNativeTokenAddress]: mockNativeTokenAddress
|
|
80
|
+
};
|
|
81
|
+
mockSortBalances.mockReturnValue([[mockNativeTokenAddress, 5n]]);
|
|
82
|
+
const result = getWalletBalancesAllowedOnCM({
|
|
83
|
+
walletBalances: { [mockNativeTokenAddress]: 5n },
|
|
84
|
+
collateralRecord,
|
|
85
|
+
nativeTokenAddress: mockNativeTokenAddress,
|
|
86
|
+
wrappedNativeTokenAddress: mockWrappedNativeTokenAddress,
|
|
87
|
+
prices,
|
|
88
|
+
tokensList,
|
|
89
|
+
isPaused: true
|
|
90
|
+
});
|
|
91
|
+
expect(result).toEqual([
|
|
92
|
+
{ token: mockWrappedNativeTokenAddress, balance: 0n },
|
|
93
|
+
{ token: allowed, balance: 0n }
|
|
94
|
+
]);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export * from "./get-cm-allowed-collaterals.js";
|
|
1
2
|
export * from "./get-strategy-credit-managers.js";
|
|
2
3
|
export * from "./get-strategy-credit-managers-list.js";
|
|
3
4
|
export * from "./get-strategy-credit-managers-list-core.js";
|
|
5
|
+
export * from "./get-wallet-balances-allowed-on-cm.js";
|
|
4
6
|
export * from "./is-credit-manager-usable.js";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LEVERAGE_DECIMALS,
|
|
3
|
+
PERCENTAGE_FACTOR
|
|
4
|
+
} from "../../../../sdk/constants/math.js";
|
|
5
|
+
import { BigIntMath } from "../../bigint-math.js";
|
|
6
|
+
import { calculateLossCoefficient } from "./calculate-loss-coefficient.js";
|
|
7
|
+
import { maxLeverage } from "./max-leverage.js";
|
|
8
|
+
const DEFAULT_DEBT_CONSTANT_LOSS = 1500n;
|
|
9
|
+
function calculateMaxStrategyDebt({
|
|
10
|
+
targetToken,
|
|
11
|
+
amount,
|
|
12
|
+
creditManager,
|
|
13
|
+
slippage,
|
|
14
|
+
constantLoss = DEFAULT_DEBT_CONSTANT_LOSS,
|
|
15
|
+
swapCollateral = true
|
|
16
|
+
}) {
|
|
17
|
+
const targetQuota = creditManager.quotas[targetToken];
|
|
18
|
+
const quotaLeft = swapCollateral && targetQuota ? BigIntMath.max(0n, targetQuota.limit - targetQuota.totalQuoted) : void 0;
|
|
19
|
+
if (quotaLeft === 0n) {
|
|
20
|
+
return creditManager.minDebt;
|
|
21
|
+
}
|
|
22
|
+
const availableLiquidity = quotaLeft !== void 0 ? BigIntMath.min(quotaLeft, creditManager.availableToBorrow) : creditManager.availableToBorrow;
|
|
23
|
+
const coefficient = calculateLossCoefficient(slippage, constantLoss);
|
|
24
|
+
const effectiveMaxDebt = BigIntMath.max(
|
|
25
|
+
BigIntMath.min(availableLiquidity, creditManager.maxDebt),
|
|
26
|
+
creditManager.minDebt
|
|
27
|
+
);
|
|
28
|
+
if (amount === 0n) {
|
|
29
|
+
return effectiveMaxDebt;
|
|
30
|
+
}
|
|
31
|
+
const maxPossibleLeverage = maxLeverage(targetToken, [creditManager]);
|
|
32
|
+
const debt = amount * BigInt(maxPossibleLeverage) / LEVERAGE_DECIMALS;
|
|
33
|
+
const effectiveDebt = debt * coefficient / PERCENTAGE_FACTOR;
|
|
34
|
+
const result = BigIntMath.max(
|
|
35
|
+
BigIntMath.min(effectiveDebt, effectiveMaxDebt),
|
|
36
|
+
creditManager.minDebt
|
|
37
|
+
);
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
calculateMaxStrategyDebt
|
|
42
|
+
};
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
LEVERAGE_DECIMALS,
|
|
4
|
+
PERCENTAGE_FACTOR
|
|
5
|
+
} from "../../../../sdk/constants/math.js";
|
|
6
|
+
import { buildCreditManager, mockToken1 } from "../../../test-utils/index.js";
|
|
7
|
+
import { calculateLossCoefficient } from "./calculate-loss-coefficient.js";
|
|
8
|
+
import { calculateMaxStrategyDebt } from "./calculate-max-strategy-debt.js";
|
|
9
|
+
import { maxLeverage } from "./max-leverage.js";
|
|
10
|
+
vi.mock("./max-leverage");
|
|
11
|
+
vi.mock(
|
|
12
|
+
"../../utils/strategies/leverage/calculate-loss-coefficient.js",
|
|
13
|
+
async () => {
|
|
14
|
+
const actual = await vi.importActual(
|
|
15
|
+
"../../utils/strategies/leverage/calculate-loss-coefficient.js"
|
|
16
|
+
);
|
|
17
|
+
return {
|
|
18
|
+
...actual,
|
|
19
|
+
calculateLossCoefficient: vi.fn(
|
|
20
|
+
actual.calculateLossCoefficient
|
|
21
|
+
)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
const mockMaxLeverage = vi.mocked(maxLeverage);
|
|
26
|
+
const mockCalculateLossCoefficient = vi.mocked(calculateLossCoefficient);
|
|
27
|
+
describe("calculateMaxStrategyDebt", () => {
|
|
28
|
+
const mockCreditManager = buildCreditManager({});
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
mockMaxLeverage.mockReturnValue(
|
|
31
|
+
10n * LEVERAGE_DECIMALS
|
|
32
|
+
);
|
|
33
|
+
vi.clearAllMocks();
|
|
34
|
+
});
|
|
35
|
+
it("should return effectiveMaxDebt when amount is zero without calling maxLeverage", () => {
|
|
36
|
+
const result = calculateMaxStrategyDebt({
|
|
37
|
+
targetToken: mockToken1,
|
|
38
|
+
amount: 0n,
|
|
39
|
+
creditManager: mockCreditManager,
|
|
40
|
+
slippage: 100,
|
|
41
|
+
constantLoss: 30n
|
|
42
|
+
});
|
|
43
|
+
expect(mockMaxLeverage).not.toHaveBeenCalled();
|
|
44
|
+
expect(result).toEqual(900000n);
|
|
45
|
+
});
|
|
46
|
+
it("should calculate max debt correctly with mocked maxLeverage", () => {
|
|
47
|
+
const maxLeverageValue = 5n * LEVERAGE_DECIMALS;
|
|
48
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
49
|
+
const targetToken = mockToken1;
|
|
50
|
+
const amount = 10000n;
|
|
51
|
+
const slippage = 100;
|
|
52
|
+
const constantLoss = 30n;
|
|
53
|
+
const result = calculateMaxStrategyDebt({
|
|
54
|
+
targetToken: mockToken1,
|
|
55
|
+
amount,
|
|
56
|
+
creditManager: mockCreditManager,
|
|
57
|
+
slippage,
|
|
58
|
+
constantLoss
|
|
59
|
+
});
|
|
60
|
+
expect(mockMaxLeverage).toHaveBeenCalledWith(targetToken, [
|
|
61
|
+
mockCreditManager
|
|
62
|
+
]);
|
|
63
|
+
expect(mockMaxLeverage).toHaveBeenCalledTimes(1);
|
|
64
|
+
expect(result).toEqual(49350n);
|
|
65
|
+
});
|
|
66
|
+
it("should handle case where effectiveDebt exceeds effectiveMaxDebt", () => {
|
|
67
|
+
const veryHighLeverage = 100n * LEVERAGE_DECIMALS;
|
|
68
|
+
mockMaxLeverage.mockReturnValue(veryHighLeverage);
|
|
69
|
+
const amount = 100000000000000000000000n;
|
|
70
|
+
const creditManager = buildCreditManager({
|
|
71
|
+
availableToBorrow: 1000n
|
|
72
|
+
});
|
|
73
|
+
const result = calculateMaxStrategyDebt({
|
|
74
|
+
targetToken: mockToken1,
|
|
75
|
+
amount,
|
|
76
|
+
creditManager,
|
|
77
|
+
slippage: 100,
|
|
78
|
+
constantLoss: 30n
|
|
79
|
+
});
|
|
80
|
+
expect(result).toEqual(creditManager.availableToBorrow);
|
|
81
|
+
});
|
|
82
|
+
it("should ensure result is at least minDebt", () => {
|
|
83
|
+
mockMaxLeverage.mockReturnValue(1n);
|
|
84
|
+
const amount = 10000n;
|
|
85
|
+
const result = calculateMaxStrategyDebt({
|
|
86
|
+
targetToken: mockToken1,
|
|
87
|
+
amount,
|
|
88
|
+
creditManager: mockCreditManager,
|
|
89
|
+
slippage: 100,
|
|
90
|
+
constantLoss: 30n
|
|
91
|
+
});
|
|
92
|
+
expect(result).toEqual(mockCreditManager.minDebt);
|
|
93
|
+
});
|
|
94
|
+
it("should handle different slippage values correctly", () => {
|
|
95
|
+
const maxLeverageValue = 10n * LEVERAGE_DECIMALS;
|
|
96
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
97
|
+
const amount = 10000n;
|
|
98
|
+
const highSlippage = 500;
|
|
99
|
+
const result = calculateMaxStrategyDebt({
|
|
100
|
+
targetToken: mockToken1,
|
|
101
|
+
amount,
|
|
102
|
+
creditManager: mockCreditManager,
|
|
103
|
+
slippage: highSlippage,
|
|
104
|
+
constantLoss: 30n
|
|
105
|
+
});
|
|
106
|
+
expect(result).toEqual(94700n);
|
|
107
|
+
});
|
|
108
|
+
it("should handle different constantLoss values correctly", () => {
|
|
109
|
+
const maxLeverageValue = 10n * LEVERAGE_DECIMALS;
|
|
110
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
111
|
+
const amount = 10000n;
|
|
112
|
+
const highConstantLoss = 500n;
|
|
113
|
+
const result = calculateMaxStrategyDebt({
|
|
114
|
+
targetToken: mockToken1,
|
|
115
|
+
amount,
|
|
116
|
+
creditManager: mockCreditManager,
|
|
117
|
+
slippage: 100,
|
|
118
|
+
constantLoss: highConstantLoss
|
|
119
|
+
});
|
|
120
|
+
expect(result).toEqual(94000n);
|
|
121
|
+
});
|
|
122
|
+
it("should respect quota limit when quotaLeft is less than availableToBorrow", () => {
|
|
123
|
+
const maxLeverageValue = 10n * LEVERAGE_DECIMALS;
|
|
124
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
125
|
+
const quotaLimit = 50000n;
|
|
126
|
+
const totalQuoted = 40000n;
|
|
127
|
+
const amount = 10000n;
|
|
128
|
+
const availableToBorrow = 100000n;
|
|
129
|
+
const creditManagerWithQuota = buildCreditManager({
|
|
130
|
+
availableToBorrow,
|
|
131
|
+
quotas: {
|
|
132
|
+
...mockCreditManager.quotas,
|
|
133
|
+
[mockToken1]: {
|
|
134
|
+
...mockCreditManager.quotas[mockToken1],
|
|
135
|
+
limit: quotaLimit,
|
|
136
|
+
totalQuoted
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
const result = calculateMaxStrategyDebt({
|
|
141
|
+
targetToken: mockToken1,
|
|
142
|
+
amount,
|
|
143
|
+
creditManager: creditManagerWithQuota,
|
|
144
|
+
slippage: 100,
|
|
145
|
+
constantLoss: 30n
|
|
146
|
+
});
|
|
147
|
+
expect(result).toEqual(quotaLimit - totalQuoted);
|
|
148
|
+
});
|
|
149
|
+
it("should use availableToBorrow when quota does not exist for target token", () => {
|
|
150
|
+
const maxLeverageValue = 10n * LEVERAGE_DECIMALS;
|
|
151
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
152
|
+
const availableToBorrow = 75000n;
|
|
153
|
+
const tokenWithoutQuota = "0x9999999999999999999999999999999999999999";
|
|
154
|
+
const creditManagerWithoutQuota = buildCreditManager({
|
|
155
|
+
availableToBorrow
|
|
156
|
+
});
|
|
157
|
+
const result = calculateMaxStrategyDebt({
|
|
158
|
+
targetToken: tokenWithoutQuota,
|
|
159
|
+
amount: 10000n,
|
|
160
|
+
creditManager: creditManagerWithoutQuota,
|
|
161
|
+
slippage: 100,
|
|
162
|
+
constantLoss: 30n
|
|
163
|
+
});
|
|
164
|
+
expect(result).toEqual(creditManagerWithoutQuota.availableToBorrow);
|
|
165
|
+
});
|
|
166
|
+
it("should respect quotaLeft", () => {
|
|
167
|
+
const maxLeverageValue = 10n * LEVERAGE_DECIMALS;
|
|
168
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
169
|
+
const quotaLimit = 50000n;
|
|
170
|
+
const totalQuoted = 10000n;
|
|
171
|
+
const amount = 20000n;
|
|
172
|
+
const availableToBorrow = 100000n;
|
|
173
|
+
const creditManagerWithQuota = buildCreditManager({
|
|
174
|
+
availableToBorrow,
|
|
175
|
+
quotas: {
|
|
176
|
+
...mockCreditManager.quotas,
|
|
177
|
+
[mockToken1]: {
|
|
178
|
+
...mockCreditManager.quotas[mockToken1],
|
|
179
|
+
limit: quotaLimit,
|
|
180
|
+
totalQuoted
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
const result = calculateMaxStrategyDebt({
|
|
185
|
+
targetToken: mockToken1,
|
|
186
|
+
amount,
|
|
187
|
+
creditManager: creditManagerWithQuota,
|
|
188
|
+
slippage: 100,
|
|
189
|
+
constantLoss: 30n
|
|
190
|
+
});
|
|
191
|
+
expect(result).toEqual(quotaLimit - totalQuoted);
|
|
192
|
+
});
|
|
193
|
+
it("returns minDebt when quotaLeft would be negative (clamped to 0)", () => {
|
|
194
|
+
const maxLeverageValue = 10n * LEVERAGE_DECIMALS;
|
|
195
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
196
|
+
const quotaLimit = 50000n;
|
|
197
|
+
const totalQuoted = 50001n;
|
|
198
|
+
const amount = 20000n;
|
|
199
|
+
const availableToBorrow = 100000n;
|
|
200
|
+
const creditManagerWithQuota = buildCreditManager({
|
|
201
|
+
availableToBorrow,
|
|
202
|
+
quotas: {
|
|
203
|
+
...mockCreditManager.quotas,
|
|
204
|
+
[mockToken1]: {
|
|
205
|
+
...mockCreditManager.quotas[mockToken1],
|
|
206
|
+
limit: quotaLimit,
|
|
207
|
+
totalQuoted
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
const result = calculateMaxStrategyDebt({
|
|
212
|
+
targetToken: mockToken1,
|
|
213
|
+
amount,
|
|
214
|
+
creditManager: creditManagerWithQuota,
|
|
215
|
+
slippage: 100,
|
|
216
|
+
constantLoss: 30n
|
|
217
|
+
});
|
|
218
|
+
expect(result).toEqual(mockCreditManager.minDebt);
|
|
219
|
+
expect(mockMaxLeverage).not.toHaveBeenCalled();
|
|
220
|
+
});
|
|
221
|
+
it("returns minDebt when quotaLimit is zero", () => {
|
|
222
|
+
const maxLeverageValue = 10n * LEVERAGE_DECIMALS;
|
|
223
|
+
mockMaxLeverage.mockReturnValue(maxLeverageValue);
|
|
224
|
+
const quotaLimit = 0n;
|
|
225
|
+
const totalQuoted = 30000n;
|
|
226
|
+
const amount = 20000n;
|
|
227
|
+
const availableToBorrow = 100000n;
|
|
228
|
+
const creditManagerWithQuota = buildCreditManager({
|
|
229
|
+
availableToBorrow,
|
|
230
|
+
quotas: {
|
|
231
|
+
...mockCreditManager.quotas,
|
|
232
|
+
[mockToken1]: {
|
|
233
|
+
...mockCreditManager.quotas[mockToken1],
|
|
234
|
+
limit: quotaLimit,
|
|
235
|
+
totalQuoted
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
const result = calculateMaxStrategyDebt({
|
|
240
|
+
targetToken: mockToken1,
|
|
241
|
+
amount,
|
|
242
|
+
creditManager: creditManagerWithQuota,
|
|
243
|
+
slippage: 100,
|
|
244
|
+
constantLoss: 30n
|
|
245
|
+
});
|
|
246
|
+
expect(result).toEqual(mockCreditManager.minDebt);
|
|
247
|
+
expect(mockMaxLeverage).not.toHaveBeenCalled();
|
|
248
|
+
});
|
|
249
|
+
it("returns minDebt when effectiveMaxDebt is below minDebt", () => {
|
|
250
|
+
mockCalculateLossCoefficient.mockReturnValueOnce(PERCENTAGE_FACTOR);
|
|
251
|
+
const creditManager = buildCreditManager({
|
|
252
|
+
minDebt: 500n,
|
|
253
|
+
availableToBorrow: 50n,
|
|
254
|
+
maxDebt: 200n
|
|
255
|
+
});
|
|
256
|
+
const result = calculateMaxStrategyDebt({
|
|
257
|
+
targetToken: mockToken1,
|
|
258
|
+
amount: 0n,
|
|
259
|
+
creditManager,
|
|
260
|
+
slippage: 0,
|
|
261
|
+
constantLoss: 0n
|
|
262
|
+
});
|
|
263
|
+
expect(result).toEqual(creditManager.minDebt);
|
|
264
|
+
expect(mockMaxLeverage).not.toHaveBeenCalled();
|
|
265
|
+
});
|
|
266
|
+
it("clamps leveraged result to minDebt when effective debt is smaller", () => {
|
|
267
|
+
mockCalculateLossCoefficient.mockReturnValueOnce(PERCENTAGE_FACTOR);
|
|
268
|
+
const creditManager = buildCreditManager({
|
|
269
|
+
minDebt: 500n,
|
|
270
|
+
availableToBorrow: 2000n,
|
|
271
|
+
maxDebt: 2000n
|
|
272
|
+
});
|
|
273
|
+
mockMaxLeverage.mockReturnValue(1n);
|
|
274
|
+
const result = calculateMaxStrategyDebt({
|
|
275
|
+
targetToken: mockToken1,
|
|
276
|
+
amount: 100n,
|
|
277
|
+
creditManager,
|
|
278
|
+
slippage: 0,
|
|
279
|
+
constantLoss: 0n
|
|
280
|
+
});
|
|
281
|
+
expect(result).toEqual(creditManager.minDebt);
|
|
282
|
+
expect(mockMaxLeverage).toHaveBeenCalledTimes(1);
|
|
283
|
+
});
|
|
284
|
+
it("ignores quota when swapCollateral is false", () => {
|
|
285
|
+
const creditManagerWithQuota = buildCreditManager({
|
|
286
|
+
availableToBorrow: 100000n,
|
|
287
|
+
quotas: {
|
|
288
|
+
...mockCreditManager.quotas,
|
|
289
|
+
[mockToken1]: {
|
|
290
|
+
...mockCreditManager.quotas[mockToken1],
|
|
291
|
+
limit: 50000n,
|
|
292
|
+
totalQuoted: 40000n
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
const result = calculateMaxStrategyDebt({
|
|
297
|
+
targetToken: mockToken1,
|
|
298
|
+
amount: 0n,
|
|
299
|
+
creditManager: creditManagerWithQuota,
|
|
300
|
+
slippage: 100,
|
|
301
|
+
constantLoss: 30n,
|
|
302
|
+
swapCollateral: false
|
|
303
|
+
});
|
|
304
|
+
expect(result).toEqual(creditManagerWithQuota.availableToBorrow);
|
|
305
|
+
expect(mockMaxLeverage).not.toHaveBeenCalled();
|
|
306
|
+
});
|
|
307
|
+
it("should respect quota limit when amount is zero", () => {
|
|
308
|
+
const quotaLimit = 50000n;
|
|
309
|
+
const totalQuoted = 20000n;
|
|
310
|
+
const availableToBorrow = 100000n;
|
|
311
|
+
const creditManagerWithQuota = buildCreditManager({
|
|
312
|
+
availableToBorrow,
|
|
313
|
+
quotas: {
|
|
314
|
+
...mockCreditManager.quotas,
|
|
315
|
+
[mockToken1]: {
|
|
316
|
+
...mockCreditManager.quotas[mockToken1],
|
|
317
|
+
limit: quotaLimit,
|
|
318
|
+
totalQuoted
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
const result = calculateMaxStrategyDebt({
|
|
323
|
+
targetToken: mockToken1,
|
|
324
|
+
amount: 0n,
|
|
325
|
+
creditManager: creditManagerWithQuota,
|
|
326
|
+
slippage: 100,
|
|
327
|
+
constantLoss: 30n
|
|
328
|
+
});
|
|
329
|
+
expect(result).toBe(30000n);
|
|
330
|
+
expect(mockMaxLeverage).not.toHaveBeenCalled();
|
|
331
|
+
});
|
|
332
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PERCENTAGE_FACTOR } from "../../../../sdk/constants/math.js";
|
|
2
|
+
function getCollateralByDebt(debt, lt, targetHF = PERCENTAGE_FACTOR) {
|
|
3
|
+
if (lt === 0n) return 0n;
|
|
4
|
+
if (targetHF === lt) return 0n;
|
|
5
|
+
const a = debt * (targetHF - lt) / lt;
|
|
6
|
+
return a;
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
getCollateralByDebt
|
|
10
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { PERCENTAGE_FACTOR, WAD } from "../../../../sdk/constants/math.js";
|
|
3
|
+
import { toBN } from "../../../../sdk/utils/formatter.js";
|
|
4
|
+
import { getCollateralByDebt } from "./get-collateral-by-debt.js";
|
|
5
|
+
describe("getCollateralByDebt", () => {
|
|
6
|
+
it("returns full debt when target HF equals 1 and LT is 0.5", () => {
|
|
7
|
+
const debt = 1000000n;
|
|
8
|
+
const lt = toBN("0.5", 4);
|
|
9
|
+
const result = getCollateralByDebt(debt, lt);
|
|
10
|
+
expect(result).toBe(debt);
|
|
11
|
+
});
|
|
12
|
+
it("returns 0 when lt = hf", () => {
|
|
13
|
+
const debt = 1000000n;
|
|
14
|
+
const result = getCollateralByDebt(debt, PERCENTAGE_FACTOR);
|
|
15
|
+
expect(result).toBe(0n);
|
|
16
|
+
});
|
|
17
|
+
it("returns when lt = 0", () => {
|
|
18
|
+
const debt = 1000000n;
|
|
19
|
+
const result = getCollateralByDebt(debt, 0n);
|
|
20
|
+
expect(result).toBe(0n);
|
|
21
|
+
});
|
|
22
|
+
it("handles fractional LT and target HF values", () => {
|
|
23
|
+
const debt = 35n * WAD / 2n;
|
|
24
|
+
const lt = toBN("0.93", 4);
|
|
25
|
+
const targetHF = toBN("1.035", 4);
|
|
26
|
+
const result = getCollateralByDebt(debt, lt, targetHF);
|
|
27
|
+
expect(result).toBe(1975806451612903225n);
|
|
28
|
+
});
|
|
29
|
+
});
|