@morpho-org/blue-sdk 1.0.0 → 1.0.1
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/lib/addresses.d.ts +168 -0
- package/lib/addresses.js +169 -0
- package/lib/chain/chain.constants.d.ts +3 -0
- package/lib/chain/chain.constants.js +232 -0
- package/lib/chain/chain.types.d.ts +20 -0
- package/lib/chain/chain.types.js +30 -0
- package/lib/chain/chain.utils.d.ts +14 -0
- package/lib/chain/chain.utils.js +30 -0
- package/lib/chain/index.js +18 -0
- package/lib/constants.d.ts +8 -0
- package/lib/constants.js +13 -0
- package/lib/errors.d.ts +37 -0
- package/lib/errors.js +71 -0
- package/lib/ethers/index.js +18 -0
- package/lib/ethers/safeGetAddress.d.ts +1 -0
- package/lib/ethers/safeGetAddress.js +6 -0
- package/lib/ethers/safeParseUnits.d.ts +2 -0
- package/lib/ethers/safeParseUnits.js +25 -0
- package/lib/evm.d.ts +36 -0
- package/lib/evm.js +113 -0
- package/lib/helpers/format/format.d.ts +98 -0
- package/lib/helpers/format/format.js +301 -0
- package/lib/helpers/format/index.js +17 -0
- package/lib/helpers/getChecksumedAddress.d.ts +7 -0
- package/lib/helpers/getChecksumedAddress.js +17 -0
- package/lib/helpers/index.js +20 -0
- package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
- package/lib/helpers/isZeroAddressOrUnset.js +14 -0
- package/lib/helpers/locale.d.ts +36 -0
- package/lib/helpers/locale.js +86 -0
- package/lib/holding/Holding.d.ts +60 -0
- package/lib/holding/Holding.js +31 -0
- package/lib/holding/index.js +17 -0
- package/lib/index.d.ts +33 -0
- package/lib/index.js +62 -0
- package/lib/market/Market.d.ts +159 -0
- package/lib/market/Market.js +240 -0
- package/lib/market/MarketConfig.d.ts +44 -0
- package/lib/market/MarketConfig.js +56 -0
- package/lib/market/MarketUtils.d.ts +165 -0
- package/lib/market/MarketUtils.js +182 -0
- package/lib/market/index.js +19 -0
- package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
- package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
- package/lib/maths/MathLib.d.ts +94 -0
- package/lib/maths/MathLib.js +153 -0
- package/lib/maths/MathUtils.d.ts +15 -0
- package/lib/maths/MathUtils.js +33 -0
- package/lib/maths/SharesMath.d.ts +12 -0
- package/lib/maths/SharesMath.js +22 -0
- package/lib/maths/index.js +20 -0
- package/lib/notifications.d.ts +98 -0
- package/lib/notifications.js +52 -0
- package/lib/position/Position.d.ts +118 -0
- package/lib/position/Position.js +145 -0
- package/lib/position/index.js +17 -0
- package/lib/signatures/index.d.ts +12 -0
- package/lib/signatures/index.js +39 -0
- package/lib/signatures/manager.d.ts +10 -0
- package/lib/signatures/manager.js +37 -0
- package/lib/signatures/permit.d.ts +21 -0
- package/lib/signatures/permit.js +101 -0
- package/lib/signatures/permit2.d.ts +20 -0
- package/lib/signatures/permit2.js +91 -0
- package/lib/signatures/types.d.ts +13 -0
- package/lib/signatures/types.js +2 -0
- package/lib/signatures/utils.d.ts +6 -0
- package/lib/signatures/utils.js +44 -0
- package/lib/tests/mocks/markets.d.ts +17 -0
- package/lib/tests/mocks/markets.js +108 -0
- package/lib/token/ERC20Metadata.d.ts +249 -0
- package/lib/token/ERC20Metadata.js +81 -0
- package/lib/token/Token.d.ts +45 -0
- package/lib/token/Token.js +39 -0
- package/lib/token/TokenNamespace.d.ts +18 -0
- package/lib/token/TokenNamespace.js +55 -0
- package/lib/token/WrappedToken.d.ts +42 -0
- package/lib/token/WrappedToken.js +87 -0
- package/lib/token/index.js +18 -0
- package/lib/types.d.ts +29 -0
- package/lib/types.js +23 -0
- package/lib/user/User.d.ts +20 -0
- package/lib/user/User.js +11 -0
- package/lib/user/index.js +18 -0
- package/lib/user/user.types.d.ts +18 -0
- package/lib/user/user.types.js +2 -0
- package/lib/vault/Vault.d.ts +167 -0
- package/lib/vault/Vault.js +156 -0
- package/lib/vault/VaultAllocation.d.ts +38 -0
- package/lib/vault/VaultAllocation.js +18 -0
- package/lib/vault/VaultConfig.d.ts +23 -0
- package/lib/vault/VaultConfig.js +26 -0
- package/lib/vault/VaultUtils.d.ts +17 -0
- package/lib/vault/VaultUtils.js +17 -0
- package/lib/vault/index.js +20 -0
- package/package.json +5 -5
- package/src/addresses.ts +0 -261
- package/src/chain/chain.constants.ts +0 -235
- package/src/chain/chain.test.ts +0 -51
- package/src/chain/chain.types.ts +0 -42
- package/src/chain/chain.utils.ts +0 -44
- package/src/constants.ts +0 -18
- package/src/errors.ts +0 -75
- package/src/ethers/ethers.test.ts +0 -17
- package/src/ethers/safeGetAddress.ts +0 -4
- package/src/ethers/safeParseUnits.ts +0 -29
- package/src/evm.ts +0 -172
- package/src/helpers/format/format.test.ts +0 -340
- package/src/helpers/format/format.ts +0 -416
- package/src/helpers/getChecksumedAddress.ts +0 -15
- package/src/helpers/locale.ts +0 -108
- package/src/holding/Holding.ts +0 -109
- package/src/market/Market.ts +0 -479
- package/src/market/MarketConfig.ts +0 -108
- package/src/market/MarketUtils.test.ts +0 -25
- package/src/market/MarketUtils.ts +0 -467
- package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
- package/src/maths/MathLib.ts +0 -208
- package/src/maths/MathUtils.ts +0 -31
- package/src/maths/SharesMath.ts +0 -40
- package/src/notifications.ts +0 -167
- package/src/position/Position.ts +0 -251
- package/src/signatures/index.ts +0 -18
- package/src/signatures/manager.ts +0 -50
- package/src/signatures/permit.ts +0 -126
- package/src/signatures/permit2.ts +0 -120
- package/src/signatures/types.ts +0 -18
- package/src/signatures/utils.ts +0 -83
- package/src/tests/mocks/markets.ts +0 -110
- package/src/token/ERC20Metadata.ts +0 -124
- package/src/token/Token.ts +0 -83
- package/src/token/TokenNamespace.ts +0 -76
- package/src/token/WrappedToken.ts +0 -142
- package/src/types.ts +0 -37
- package/src/user/User.ts +0 -32
- package/src/user/user.types.ts +0 -23
- package/src/vault/Vault.ts +0 -370
- package/src/vault/VaultAllocation.ts +0 -58
- package/src/vault/VaultConfig.ts +0 -55
- package/src/vault/VaultUtils.ts +0 -47
- /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
- /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
- /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
- /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
- /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
- /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
- /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
- /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
- /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
- /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
- /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.vault = exports.ethers = exports.helpers = exports.user = exports.signatures = exports.notifications = exports.maths = exports.types = exports.holding = exports.position = exports.token = exports.chain = exports.market = exports.errors = exports.constants = exports.evm = void 0;
|
|
30
|
+
__exportStar(require("./addresses"), exports);
|
|
31
|
+
__exportStar(require("./evm"), exports);
|
|
32
|
+
__exportStar(require("./constants"), exports);
|
|
33
|
+
__exportStar(require("./errors"), exports);
|
|
34
|
+
__exportStar(require("./market"), exports);
|
|
35
|
+
__exportStar(require("./chain"), exports);
|
|
36
|
+
__exportStar(require("./token"), exports);
|
|
37
|
+
__exportStar(require("./types"), exports);
|
|
38
|
+
__exportStar(require("./maths"), exports);
|
|
39
|
+
__exportStar(require("./notifications"), exports);
|
|
40
|
+
__exportStar(require("./signatures"), exports);
|
|
41
|
+
__exportStar(require("./user"), exports);
|
|
42
|
+
__exportStar(require("./holding"), exports);
|
|
43
|
+
__exportStar(require("./position"), exports);
|
|
44
|
+
__exportStar(require("./helpers"), exports);
|
|
45
|
+
__exportStar(require("./ethers"), exports);
|
|
46
|
+
__exportStar(require("./vault"), exports);
|
|
47
|
+
exports.evm = __importStar(require("./evm"));
|
|
48
|
+
exports.constants = __importStar(require("./constants"));
|
|
49
|
+
exports.errors = __importStar(require("./errors"));
|
|
50
|
+
exports.market = __importStar(require("./market"));
|
|
51
|
+
exports.chain = __importStar(require("./chain"));
|
|
52
|
+
exports.token = __importStar(require("./token"));
|
|
53
|
+
exports.position = __importStar(require("./position"));
|
|
54
|
+
exports.holding = __importStar(require("./holding"));
|
|
55
|
+
exports.types = __importStar(require("./types"));
|
|
56
|
+
exports.maths = __importStar(require("./maths"));
|
|
57
|
+
exports.notifications = __importStar(require("./notifications"));
|
|
58
|
+
exports.signatures = __importStar(require("./signatures"));
|
|
59
|
+
exports.user = __importStar(require("./user"));
|
|
60
|
+
exports.helpers = __importStar(require("./helpers"));
|
|
61
|
+
exports.ethers = __importStar(require("./ethers"));
|
|
62
|
+
exports.vault = __importStar(require("./vault"));
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { BigNumberish, Provider } from "ethers";
|
|
2
|
+
import { ViewOverrides } from "ethers-types/dist/common";
|
|
3
|
+
import { ChainId } from "../chain";
|
|
4
|
+
import { RoundingDirection } from "../maths";
|
|
5
|
+
import { MarketId } from "../types";
|
|
6
|
+
import { MarketConfig } from "./MarketConfig";
|
|
7
|
+
export declare enum CapacityLimitReason {
|
|
8
|
+
liquidity = "Liquidity",
|
|
9
|
+
balance = "Balance",
|
|
10
|
+
position = "Position",
|
|
11
|
+
collateral = "Collateral",
|
|
12
|
+
cap = "Cap"
|
|
13
|
+
}
|
|
14
|
+
export interface CapacityLimit {
|
|
15
|
+
value: bigint;
|
|
16
|
+
limiter: CapacityLimitReason;
|
|
17
|
+
}
|
|
18
|
+
export interface MaxPositionCapacities {
|
|
19
|
+
supply: CapacityLimit;
|
|
20
|
+
withdraw: CapacityLimit;
|
|
21
|
+
borrow: CapacityLimit;
|
|
22
|
+
repay: CapacityLimit;
|
|
23
|
+
supplyCollateral: CapacityLimit;
|
|
24
|
+
withdrawCollateral: CapacityLimit;
|
|
25
|
+
}
|
|
26
|
+
export interface InputMarket {
|
|
27
|
+
config: MarketConfig;
|
|
28
|
+
totalSupplyAssets: BigNumberish;
|
|
29
|
+
totalBorrowAssets: BigNumberish;
|
|
30
|
+
totalSupplyShares: BigNumberish;
|
|
31
|
+
totalBorrowShares: BigNumberish;
|
|
32
|
+
lastUpdate: BigNumberish;
|
|
33
|
+
fee: BigNumberish;
|
|
34
|
+
price: BigNumberish;
|
|
35
|
+
rateAtTarget?: BigNumberish;
|
|
36
|
+
}
|
|
37
|
+
export declare class Market implements InputMarket {
|
|
38
|
+
static fetchFromId(id: MarketId, runner: {
|
|
39
|
+
provider: Provider;
|
|
40
|
+
}, { chainId, overrides, }?: {
|
|
41
|
+
chainId?: ChainId;
|
|
42
|
+
overrides?: ViewOverrides;
|
|
43
|
+
}): Promise<Market>;
|
|
44
|
+
static fetchFromConfig(config: MarketConfig, runner: {
|
|
45
|
+
provider: Provider;
|
|
46
|
+
}, { chainId, overrides, }?: {
|
|
47
|
+
chainId?: ChainId;
|
|
48
|
+
overrides?: ViewOverrides;
|
|
49
|
+
}): Promise<Market>;
|
|
50
|
+
/**
|
|
51
|
+
* The market's config.
|
|
52
|
+
*/
|
|
53
|
+
readonly config: MarketConfig;
|
|
54
|
+
/**
|
|
55
|
+
* The amount of loan assets supplied in total on the market.
|
|
56
|
+
*/
|
|
57
|
+
totalSupplyAssets: bigint;
|
|
58
|
+
/**
|
|
59
|
+
* The amount of loan assets supplied in total on the market.
|
|
60
|
+
*/
|
|
61
|
+
totalBorrowAssets: bigint;
|
|
62
|
+
/**
|
|
63
|
+
* The amount of loan assets supplied in total on the market.
|
|
64
|
+
*/
|
|
65
|
+
totalSupplyShares: bigint;
|
|
66
|
+
/**
|
|
67
|
+
* The amount of loan assets supplied in total on the market.
|
|
68
|
+
*/
|
|
69
|
+
totalBorrowShares: bigint;
|
|
70
|
+
/**
|
|
71
|
+
* The block timestamp (in __seconds__) when the interest was last accrued.
|
|
72
|
+
*/
|
|
73
|
+
lastUpdate: bigint;
|
|
74
|
+
/**
|
|
75
|
+
* The fee percentage of the market, scaled by WAD.
|
|
76
|
+
*/
|
|
77
|
+
fee: bigint;
|
|
78
|
+
/**
|
|
79
|
+
* The price as returned by the market's oracle.
|
|
80
|
+
*/
|
|
81
|
+
price: bigint;
|
|
82
|
+
/**
|
|
83
|
+
* If the market uses the Adaptive Curve IRM, the rate at target utilization.
|
|
84
|
+
* Undefined otherwise.
|
|
85
|
+
*/
|
|
86
|
+
rateAtTarget?: bigint;
|
|
87
|
+
constructor({ config, totalSupplyAssets, totalBorrowAssets, totalSupplyShares, totalBorrowShares, lastUpdate, fee, price, rateAtTarget, }: InputMarket);
|
|
88
|
+
get id(): MarketId;
|
|
89
|
+
get isIdle(): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* @warning Cannot be used to calculate the liquidity available inside a callback,
|
|
92
|
+
* because the balance of Blue may be lower than the market's liquidity due to assets being transferred out prior to the callback.
|
|
93
|
+
*/
|
|
94
|
+
get liquidity(): bigint;
|
|
95
|
+
get utilization(): bigint;
|
|
96
|
+
get apyAtTarget(): bigint | undefined;
|
|
97
|
+
get supplyRate(): bigint;
|
|
98
|
+
get borrowRate(): bigint;
|
|
99
|
+
get supplyApy(): bigint;
|
|
100
|
+
get borrowApy(): bigint;
|
|
101
|
+
accrueInterest(timestamp: BigNumberish): Market;
|
|
102
|
+
getLiquidityToUtilization(utilization: BigNumberish): bigint;
|
|
103
|
+
getCollateralValue(collateral: BigNumberish): bigint;
|
|
104
|
+
getMaxBorrowAssets(collateral: BigNumberish): bigint;
|
|
105
|
+
getMaxBorrowableAssets(position: {
|
|
106
|
+
collateral: BigNumberish;
|
|
107
|
+
borrowShares: BigNumberish;
|
|
108
|
+
}): bigint;
|
|
109
|
+
getLiquidationSeizedAssets(repaidShares: BigNumberish): bigint;
|
|
110
|
+
getLiquidationRepaidShares(seizedAssets: BigNumberish): bigint;
|
|
111
|
+
getSeizableCollateral(position: {
|
|
112
|
+
collateral: BigNumberish;
|
|
113
|
+
borrowShares: BigNumberish;
|
|
114
|
+
}): bigint;
|
|
115
|
+
getWithdrawableCollateral(position: {
|
|
116
|
+
collateral: BigNumberish;
|
|
117
|
+
borrowShares: BigNumberish;
|
|
118
|
+
}): bigint;
|
|
119
|
+
isHealthy(position: {
|
|
120
|
+
collateral: BigNumberish;
|
|
121
|
+
borrowShares: BigNumberish;
|
|
122
|
+
}): boolean;
|
|
123
|
+
getLiquidationPrice(position: {
|
|
124
|
+
collateral: BigNumberish;
|
|
125
|
+
borrowShares: BigNumberish;
|
|
126
|
+
}): bigint | null;
|
|
127
|
+
getPriceVariationToLiquidation(position: {
|
|
128
|
+
collateral: BigNumberish;
|
|
129
|
+
borrowShares: BigNumberish;
|
|
130
|
+
}): bigint | null;
|
|
131
|
+
getHealthFactor(position: {
|
|
132
|
+
collateral: BigNumberish;
|
|
133
|
+
borrowShares: BigNumberish;
|
|
134
|
+
}): bigint | null;
|
|
135
|
+
getLtv(position: {
|
|
136
|
+
collateral: BigNumberish;
|
|
137
|
+
borrowShares: BigNumberish;
|
|
138
|
+
}): bigint | null;
|
|
139
|
+
getBorrowCapacityUsage(position: {
|
|
140
|
+
collateral: BigNumberish;
|
|
141
|
+
borrowShares: BigNumberish;
|
|
142
|
+
}): bigint | null;
|
|
143
|
+
toSupplyAssets(shares: BigNumberish, rounding?: RoundingDirection): bigint;
|
|
144
|
+
toSupplyShares(assets: BigNumberish, rounding?: RoundingDirection): bigint;
|
|
145
|
+
toBorrowAssets(shares: BigNumberish, rounding?: RoundingDirection): bigint;
|
|
146
|
+
toBorrowShares(assets: BigNumberish, rounding?: RoundingDirection): bigint;
|
|
147
|
+
getBorrowCapacityLimit(collateral: bigint): CapacityLimit;
|
|
148
|
+
getRepayCapacityLimit(borrowShares: bigint, loanTokenBalance: bigint): CapacityLimit;
|
|
149
|
+
getWithdrawCapacityLimit(supplyShares: bigint): CapacityLimit;
|
|
150
|
+
getWithdrawCollateralCapacityLimit(position: {
|
|
151
|
+
collateral: bigint;
|
|
152
|
+
borrowShares: bigint;
|
|
153
|
+
}): CapacityLimit;
|
|
154
|
+
getMaxCapacities(position: {
|
|
155
|
+
collateral: bigint;
|
|
156
|
+
supplyShares: bigint;
|
|
157
|
+
borrowShares: bigint;
|
|
158
|
+
}, loanTokenBalance: bigint, collateralTokenBalance: bigint): MaxPositionCapacities;
|
|
159
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Market = exports.CapacityLimitReason = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const ethers_types_1 = require("ethers-types");
|
|
6
|
+
const addresses_1 = require("../addresses");
|
|
7
|
+
const chain_1 = require("../chain");
|
|
8
|
+
const errors_1 = require("../errors");
|
|
9
|
+
const maths_1 = require("../maths");
|
|
10
|
+
const MarketConfig_1 = require("./MarketConfig");
|
|
11
|
+
const MarketUtils_1 = require("./MarketUtils");
|
|
12
|
+
var CapacityLimitReason;
|
|
13
|
+
(function (CapacityLimitReason) {
|
|
14
|
+
CapacityLimitReason["liquidity"] = "Liquidity";
|
|
15
|
+
CapacityLimitReason["balance"] = "Balance";
|
|
16
|
+
CapacityLimitReason["position"] = "Position";
|
|
17
|
+
CapacityLimitReason["collateral"] = "Collateral";
|
|
18
|
+
CapacityLimitReason["cap"] = "Cap";
|
|
19
|
+
})(CapacityLimitReason || (exports.CapacityLimitReason = CapacityLimitReason = {}));
|
|
20
|
+
class Market {
|
|
21
|
+
static async fetchFromId(id, runner, { chainId, overrides = {}, } = {}) {
|
|
22
|
+
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
23
|
+
const config = await MarketConfig_1.MarketConfig.fetch(id, runner, chainId);
|
|
24
|
+
return Market.fetchFromConfig(config, runner, { chainId, overrides });
|
|
25
|
+
}
|
|
26
|
+
static async fetchFromConfig(config, runner, { chainId, overrides = {}, } = {}) {
|
|
27
|
+
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
28
|
+
const { morpho, adaptiveCurveIrm } = (0, addresses_1.getChainAddresses)(chainId);
|
|
29
|
+
const [{ totalSupplyAssets, totalSupplyShares, totalBorrowShares, totalBorrowAssets, lastUpdate, fee, }, price, rateAtTarget,] = await Promise.all([
|
|
30
|
+
ethers_types_1.MorphoBlue__factory.connect(morpho, runner).market(config.id, overrides),
|
|
31
|
+
config.oracle !== ethers_1.ZeroAddress
|
|
32
|
+
? ethers_types_1.BlueOracle__factory.connect(config.oracle, runner).price(overrides)
|
|
33
|
+
: 0n,
|
|
34
|
+
config.irm === adaptiveCurveIrm
|
|
35
|
+
? await ethers_types_1.AdaptiveCurveIrm__factory.connect(config.irm, runner).rateAtTarget(config.id, overrides)
|
|
36
|
+
: undefined,
|
|
37
|
+
]);
|
|
38
|
+
return new Market({
|
|
39
|
+
config,
|
|
40
|
+
totalSupplyAssets,
|
|
41
|
+
totalBorrowAssets,
|
|
42
|
+
totalSupplyShares,
|
|
43
|
+
totalBorrowShares,
|
|
44
|
+
lastUpdate,
|
|
45
|
+
fee,
|
|
46
|
+
price,
|
|
47
|
+
rateAtTarget,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
constructor({ config, totalSupplyAssets, totalBorrowAssets, totalSupplyShares, totalBorrowShares, lastUpdate, fee, price, rateAtTarget, }) {
|
|
51
|
+
this.config = config;
|
|
52
|
+
this.totalSupplyAssets = (0, ethers_1.toBigInt)(totalSupplyAssets);
|
|
53
|
+
this.totalBorrowAssets = (0, ethers_1.toBigInt)(totalBorrowAssets);
|
|
54
|
+
this.totalSupplyShares = (0, ethers_1.toBigInt)(totalSupplyShares);
|
|
55
|
+
this.totalBorrowShares = (0, ethers_1.toBigInt)(totalBorrowShares);
|
|
56
|
+
this.lastUpdate = (0, ethers_1.toBigInt)(lastUpdate);
|
|
57
|
+
this.fee = (0, ethers_1.toBigInt)(fee);
|
|
58
|
+
this.price = (0, ethers_1.toBigInt)(price);
|
|
59
|
+
if (rateAtTarget != null)
|
|
60
|
+
this.rateAtTarget = (0, ethers_1.toBigInt)(rateAtTarget);
|
|
61
|
+
}
|
|
62
|
+
get id() {
|
|
63
|
+
return this.config.id;
|
|
64
|
+
}
|
|
65
|
+
get isIdle() {
|
|
66
|
+
return this.config.collateralToken === ethers_1.ZeroAddress;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @warning Cannot be used to calculate the liquidity available inside a callback,
|
|
70
|
+
* because the balance of Blue may be lower than the market's liquidity due to assets being transferred out prior to the callback.
|
|
71
|
+
*/
|
|
72
|
+
get liquidity() {
|
|
73
|
+
return this.totalSupplyAssets - this.totalBorrowAssets;
|
|
74
|
+
}
|
|
75
|
+
get utilization() {
|
|
76
|
+
return MarketUtils_1.MarketUtils.getUtilization(this);
|
|
77
|
+
}
|
|
78
|
+
get apyAtTarget() {
|
|
79
|
+
if (this.rateAtTarget == null)
|
|
80
|
+
return;
|
|
81
|
+
return MarketUtils_1.MarketUtils.getApy(this.rateAtTarget);
|
|
82
|
+
}
|
|
83
|
+
get supplyRate() {
|
|
84
|
+
return MarketUtils_1.MarketUtils.getSupplyRate(this.borrowRate, this);
|
|
85
|
+
}
|
|
86
|
+
get borrowRate() {
|
|
87
|
+
if (this.rateAtTarget == null)
|
|
88
|
+
return 0n;
|
|
89
|
+
return maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, 0n).avgBorrowRate;
|
|
90
|
+
}
|
|
91
|
+
get supplyApy() {
|
|
92
|
+
return MarketUtils_1.MarketUtils.getApy(this.supplyRate);
|
|
93
|
+
}
|
|
94
|
+
get borrowApy() {
|
|
95
|
+
return MarketUtils_1.MarketUtils.getApy(this.borrowRate);
|
|
96
|
+
}
|
|
97
|
+
accrueInterest(timestamp) {
|
|
98
|
+
timestamp = (0, ethers_1.toBigInt)(timestamp);
|
|
99
|
+
const elapsed = timestamp - this.lastUpdate;
|
|
100
|
+
if (elapsed < 0n)
|
|
101
|
+
throw new errors_1.InvalidInterestAccrualError(this.id, timestamp, this.lastUpdate);
|
|
102
|
+
let borrowRate = 0n;
|
|
103
|
+
let { rateAtTarget } = this;
|
|
104
|
+
if (this.rateAtTarget != null) {
|
|
105
|
+
const { avgBorrowRate, endRateAtTarget } = maths_1.AdaptiveCurveIrmLib.getBorrowRate(this.utilization, this.rateAtTarget, elapsed);
|
|
106
|
+
borrowRate = avgBorrowRate;
|
|
107
|
+
rateAtTarget = endRateAtTarget;
|
|
108
|
+
}
|
|
109
|
+
const { interest, feeShares } = MarketUtils_1.MarketUtils.getAccruedInterest(borrowRate, this, elapsed);
|
|
110
|
+
return new Market({
|
|
111
|
+
...this,
|
|
112
|
+
totalSupplyAssets: this.totalSupplyAssets + interest,
|
|
113
|
+
totalBorrowAssets: this.totalBorrowAssets + interest,
|
|
114
|
+
totalSupplyShares: this.totalSupplyShares + feeShares,
|
|
115
|
+
lastUpdate: timestamp,
|
|
116
|
+
rateAtTarget,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
getLiquidityToUtilization(utilization) {
|
|
120
|
+
return MarketUtils_1.MarketUtils.getLiquidityToUtilization(this, utilization);
|
|
121
|
+
}
|
|
122
|
+
getCollateralValue(collateral) {
|
|
123
|
+
return MarketUtils_1.MarketUtils.getCollateralValue(collateral, this);
|
|
124
|
+
}
|
|
125
|
+
getMaxBorrowAssets(collateral) {
|
|
126
|
+
return MarketUtils_1.MarketUtils.getMaxBorrowAssets(collateral, this, this.config);
|
|
127
|
+
}
|
|
128
|
+
getMaxBorrowableAssets(position) {
|
|
129
|
+
return MarketUtils_1.MarketUtils.getMaxBorrowableAssets(position, this, this.config);
|
|
130
|
+
}
|
|
131
|
+
getLiquidationSeizedAssets(repaidShares) {
|
|
132
|
+
return MarketUtils_1.MarketUtils.getLiquidationSeizedAssets(repaidShares, this, this.config);
|
|
133
|
+
}
|
|
134
|
+
getLiquidationRepaidShares(seizedAssets) {
|
|
135
|
+
return MarketUtils_1.MarketUtils.getLiquidationRepaidShares(seizedAssets, this, this.config);
|
|
136
|
+
}
|
|
137
|
+
getSeizableCollateral(position) {
|
|
138
|
+
return MarketUtils_1.MarketUtils.getSeizableCollateral(position, this, this.config);
|
|
139
|
+
}
|
|
140
|
+
getWithdrawableCollateral(position) {
|
|
141
|
+
return MarketUtils_1.MarketUtils.getWithdrawableCollateral(position, this, this.config);
|
|
142
|
+
}
|
|
143
|
+
isHealthy(position) {
|
|
144
|
+
return MarketUtils_1.MarketUtils.isHealthy(position, this, this.config);
|
|
145
|
+
}
|
|
146
|
+
getLiquidationPrice(position) {
|
|
147
|
+
return MarketUtils_1.MarketUtils.getLiquidationPrice(position, this, this.config);
|
|
148
|
+
}
|
|
149
|
+
getPriceVariationToLiquidation(position) {
|
|
150
|
+
return MarketUtils_1.MarketUtils.getPriceVariationToLiquidation(position, this, this.config);
|
|
151
|
+
}
|
|
152
|
+
getHealthFactor(position) {
|
|
153
|
+
return MarketUtils_1.MarketUtils.getHealthFactor(position, this, this.config);
|
|
154
|
+
}
|
|
155
|
+
getLtv(position) {
|
|
156
|
+
return MarketUtils_1.MarketUtils.getLtv(position, this);
|
|
157
|
+
}
|
|
158
|
+
getBorrowCapacityUsage(position) {
|
|
159
|
+
return MarketUtils_1.MarketUtils.getBorrowCapacityUsage(position, this, this.config);
|
|
160
|
+
}
|
|
161
|
+
toSupplyAssets(shares, rounding) {
|
|
162
|
+
return MarketUtils_1.MarketUtils.toSupplyAssets(shares, this, rounding);
|
|
163
|
+
}
|
|
164
|
+
toSupplyShares(assets, rounding) {
|
|
165
|
+
return MarketUtils_1.MarketUtils.toSupplyShares(assets, this, rounding);
|
|
166
|
+
}
|
|
167
|
+
toBorrowAssets(shares, rounding) {
|
|
168
|
+
return MarketUtils_1.MarketUtils.toBorrowAssets(shares, this, rounding);
|
|
169
|
+
}
|
|
170
|
+
toBorrowShares(assets, rounding) {
|
|
171
|
+
return MarketUtils_1.MarketUtils.toBorrowShares(assets, this, rounding);
|
|
172
|
+
}
|
|
173
|
+
getBorrowCapacityLimit(collateral) {
|
|
174
|
+
const maxBorrowableAssets = this.getMaxBorrowAssets(collateral);
|
|
175
|
+
const { liquidity } = this;
|
|
176
|
+
if (maxBorrowableAssets > liquidity)
|
|
177
|
+
return {
|
|
178
|
+
value: liquidity,
|
|
179
|
+
limiter: CapacityLimitReason.liquidity,
|
|
180
|
+
};
|
|
181
|
+
return {
|
|
182
|
+
value: maxBorrowableAssets,
|
|
183
|
+
limiter: CapacityLimitReason.collateral,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
getRepayCapacityLimit(borrowShares, loanTokenBalance) {
|
|
187
|
+
const borrowAssets = this.toBorrowAssets(borrowShares);
|
|
188
|
+
if (borrowAssets > loanTokenBalance)
|
|
189
|
+
return {
|
|
190
|
+
value: loanTokenBalance,
|
|
191
|
+
limiter: CapacityLimitReason.balance,
|
|
192
|
+
};
|
|
193
|
+
return {
|
|
194
|
+
value: borrowAssets,
|
|
195
|
+
limiter: CapacityLimitReason.position,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
getWithdrawCapacityLimit(supplyShares) {
|
|
199
|
+
const supplyAssets = this.toSupplyAssets(supplyShares);
|
|
200
|
+
const { liquidity } = this;
|
|
201
|
+
if (supplyAssets > liquidity)
|
|
202
|
+
return {
|
|
203
|
+
value: liquidity,
|
|
204
|
+
limiter: CapacityLimitReason.liquidity,
|
|
205
|
+
};
|
|
206
|
+
return {
|
|
207
|
+
value: supplyAssets,
|
|
208
|
+
limiter: CapacityLimitReason.position,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
getWithdrawCollateralCapacityLimit(position) {
|
|
212
|
+
const withdrawableCollateral = this.getWithdrawableCollateral(position);
|
|
213
|
+
if (position.collateral > withdrawableCollateral)
|
|
214
|
+
return {
|
|
215
|
+
value: withdrawableCollateral,
|
|
216
|
+
limiter: CapacityLimitReason.collateral,
|
|
217
|
+
};
|
|
218
|
+
return {
|
|
219
|
+
value: position.collateral,
|
|
220
|
+
limiter: CapacityLimitReason.position,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
getMaxCapacities(position, loanTokenBalance, collateralTokenBalance) {
|
|
224
|
+
return {
|
|
225
|
+
supply: {
|
|
226
|
+
value: loanTokenBalance,
|
|
227
|
+
limiter: CapacityLimitReason.balance,
|
|
228
|
+
},
|
|
229
|
+
withdraw: this.getWithdrawCapacityLimit(position.supplyShares),
|
|
230
|
+
borrow: this.getBorrowCapacityLimit(position.collateral),
|
|
231
|
+
repay: this.getRepayCapacityLimit(position.borrowShares, loanTokenBalance),
|
|
232
|
+
supplyCollateral: {
|
|
233
|
+
value: collateralTokenBalance,
|
|
234
|
+
limiter: CapacityLimitReason.balance,
|
|
235
|
+
},
|
|
236
|
+
withdrawCollateral: this.getWithdrawCollateralCapacityLimit(position),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.Market = Market;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Provider } from "ethers";
|
|
2
|
+
import { MarketParamsStruct } from "ethers-types/dist/protocols/morpho/blue/MorphoBlue";
|
|
3
|
+
import { ChainId } from "../chain";
|
|
4
|
+
import { Address, MarketId } from "../types";
|
|
5
|
+
export interface MarketParams extends MarketParamsStruct {
|
|
6
|
+
loanToken: Address;
|
|
7
|
+
collateralToken: Address;
|
|
8
|
+
oracle: Address;
|
|
9
|
+
irm: Address;
|
|
10
|
+
}
|
|
11
|
+
export declare class MarketConfig implements MarketParams {
|
|
12
|
+
private static readonly _CACHE;
|
|
13
|
+
static get(id: MarketId): MarketConfig;
|
|
14
|
+
static fetch(id: MarketId, runner: {
|
|
15
|
+
provider: Provider;
|
|
16
|
+
}, chainId?: ChainId): Promise<MarketConfig>;
|
|
17
|
+
static idle(token: Address): MarketConfig;
|
|
18
|
+
/**
|
|
19
|
+
* The market's collateral token address.
|
|
20
|
+
*/
|
|
21
|
+
readonly collateralToken: Address;
|
|
22
|
+
/**
|
|
23
|
+
* The market's loan token address.
|
|
24
|
+
*/
|
|
25
|
+
readonly loanToken: Address;
|
|
26
|
+
/**
|
|
27
|
+
* The market's oracle address.
|
|
28
|
+
*/
|
|
29
|
+
readonly oracle: Address;
|
|
30
|
+
/**
|
|
31
|
+
* The market's interest rate model address.
|
|
32
|
+
*/
|
|
33
|
+
readonly irm: Address;
|
|
34
|
+
/**
|
|
35
|
+
* The market's liquidation Loan-To-Value (scaled by WAD).
|
|
36
|
+
*/
|
|
37
|
+
readonly lltv: bigint;
|
|
38
|
+
constructor({ collateralToken, loanToken, oracle, irm, lltv }: MarketParams);
|
|
39
|
+
/**
|
|
40
|
+
* The market's hex-encoded id, defined as the hash of the market's params.
|
|
41
|
+
*/
|
|
42
|
+
get id(): MarketId;
|
|
43
|
+
get liquidationIncentiveFactor(): bigint;
|
|
44
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MarketConfig = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const ethers_types_1 = require("ethers-types");
|
|
6
|
+
const addresses_1 = require("../addresses");
|
|
7
|
+
const chain_1 = require("../chain");
|
|
8
|
+
const errors_1 = require("../errors");
|
|
9
|
+
const MarketUtils_1 = require("./MarketUtils");
|
|
10
|
+
class MarketConfig {
|
|
11
|
+
static get(id) {
|
|
12
|
+
const marketConfig = MarketConfig._CACHE[id];
|
|
13
|
+
if (!marketConfig)
|
|
14
|
+
throw new errors_1.UnknownMarketConfigError(id);
|
|
15
|
+
return marketConfig;
|
|
16
|
+
}
|
|
17
|
+
static async fetch(id, runner, chainId) {
|
|
18
|
+
let config = (0, errors_1._try)(() => MarketConfig.get(id), errors_1.UnknownMarketConfigError);
|
|
19
|
+
if (!config) {
|
|
20
|
+
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
21
|
+
const { morpho } = (0, addresses_1.getChainAddresses)(chainId);
|
|
22
|
+
config = new MarketConfig(
|
|
23
|
+
// Always fetch at latest block because config is immutable.
|
|
24
|
+
await ethers_types_1.MorphoBlue__factory.connect(morpho, runner).idToMarketParams(id));
|
|
25
|
+
}
|
|
26
|
+
return config;
|
|
27
|
+
}
|
|
28
|
+
static idle(token) {
|
|
29
|
+
return new MarketConfig({
|
|
30
|
+
collateralToken: ethers_1.ZeroAddress,
|
|
31
|
+
loanToken: token,
|
|
32
|
+
oracle: ethers_1.ZeroAddress,
|
|
33
|
+
irm: ethers_1.ZeroAddress,
|
|
34
|
+
lltv: 0n,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
constructor({ collateralToken, loanToken, oracle, irm, lltv }) {
|
|
38
|
+
this.collateralToken = collateralToken;
|
|
39
|
+
this.loanToken = loanToken;
|
|
40
|
+
this.oracle = oracle;
|
|
41
|
+
this.irm = irm;
|
|
42
|
+
this.lltv = (0, ethers_1.toBigInt)(lltv);
|
|
43
|
+
MarketConfig._CACHE[this.id] = this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The market's hex-encoded id, defined as the hash of the market's params.
|
|
47
|
+
*/
|
|
48
|
+
get id() {
|
|
49
|
+
return MarketUtils_1.MarketUtils.getMarketId(this);
|
|
50
|
+
}
|
|
51
|
+
get liquidationIncentiveFactor() {
|
|
52
|
+
return MarketUtils_1.MarketUtils.getLiquidationIncentiveFactor(this);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.MarketConfig = MarketConfig;
|
|
56
|
+
MarketConfig._CACHE = {};
|