@morpho-org/blue-sdk 1.0.1 → 1.0.3

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.
Files changed (150) hide show
  1. package/package.json +10 -7
  2. package/src/addresses.ts +247 -0
  3. package/src/chain/chain.constants.ts +235 -0
  4. package/src/chain/chain.test.ts +51 -0
  5. package/src/chain/chain.types.ts +42 -0
  6. package/src/chain/chain.utils.ts +44 -0
  7. package/src/constants.ts +18 -0
  8. package/src/errors.ts +75 -0
  9. package/src/ethers/ethers.test.ts +17 -0
  10. package/src/ethers/safeGetAddress.ts +4 -0
  11. package/src/ethers/safeParseUnits.ts +29 -0
  12. package/src/evm.ts +172 -0
  13. package/src/helpers/format/format.test.ts +340 -0
  14. package/src/helpers/format/format.ts +416 -0
  15. package/src/helpers/getChecksumedAddress.ts +15 -0
  16. package/{lib/helpers/isZeroAddressOrUnset.d.ts → src/helpers/isZeroAddressOrUnset.ts} +7 -1
  17. package/src/helpers/locale.ts +108 -0
  18. package/src/holding/Holding.ts +109 -0
  19. package/src/market/Market.ts +479 -0
  20. package/src/market/MarketConfig.ts +108 -0
  21. package/src/market/MarketUtils.test.ts +25 -0
  22. package/src/market/MarketUtils.ts +467 -0
  23. package/src/maths/AdaptiveCurveIrmLib.ts +143 -0
  24. package/src/maths/MathLib.ts +208 -0
  25. package/src/maths/MathUtils.ts +31 -0
  26. package/src/maths/SharesMath.ts +40 -0
  27. package/src/notifications.ts +167 -0
  28. package/src/position/Position.ts +251 -0
  29. package/src/signatures/index.ts +18 -0
  30. package/src/signatures/manager.ts +50 -0
  31. package/src/signatures/permit.ts +126 -0
  32. package/src/signatures/permit2.ts +120 -0
  33. package/src/signatures/types.ts +18 -0
  34. package/src/signatures/utils.ts +83 -0
  35. package/src/tests/mocks/markets.ts +110 -0
  36. package/src/token/ERC20Metadata.ts +124 -0
  37. package/src/token/Token.ts +83 -0
  38. package/src/token/TokenNamespace.ts +76 -0
  39. package/src/token/WrappedToken.ts +142 -0
  40. package/src/types.ts +37 -0
  41. package/src/user/User.ts +32 -0
  42. package/src/user/user.types.ts +25 -0
  43. package/src/vault/Vault.ts +370 -0
  44. package/src/vault/VaultAllocation.ts +58 -0
  45. package/src/vault/VaultConfig.ts +55 -0
  46. package/src/vault/VaultUtils.ts +47 -0
  47. package/lib/addresses.d.ts +0 -168
  48. package/lib/addresses.js +0 -169
  49. package/lib/chain/chain.constants.d.ts +0 -3
  50. package/lib/chain/chain.constants.js +0 -232
  51. package/lib/chain/chain.types.d.ts +0 -20
  52. package/lib/chain/chain.types.js +0 -30
  53. package/lib/chain/chain.utils.d.ts +0 -14
  54. package/lib/chain/chain.utils.js +0 -30
  55. package/lib/chain/index.js +0 -18
  56. package/lib/constants.d.ts +0 -8
  57. package/lib/constants.js +0 -13
  58. package/lib/errors.d.ts +0 -37
  59. package/lib/errors.js +0 -71
  60. package/lib/ethers/index.js +0 -18
  61. package/lib/ethers/safeGetAddress.d.ts +0 -1
  62. package/lib/ethers/safeGetAddress.js +0 -6
  63. package/lib/ethers/safeParseUnits.d.ts +0 -2
  64. package/lib/ethers/safeParseUnits.js +0 -25
  65. package/lib/evm.d.ts +0 -36
  66. package/lib/evm.js +0 -113
  67. package/lib/helpers/format/format.d.ts +0 -98
  68. package/lib/helpers/format/format.js +0 -301
  69. package/lib/helpers/format/index.js +0 -17
  70. package/lib/helpers/getChecksumedAddress.d.ts +0 -7
  71. package/lib/helpers/getChecksumedAddress.js +0 -17
  72. package/lib/helpers/index.js +0 -20
  73. package/lib/helpers/isZeroAddressOrUnset.js +0 -14
  74. package/lib/helpers/locale.d.ts +0 -36
  75. package/lib/helpers/locale.js +0 -86
  76. package/lib/holding/Holding.d.ts +0 -60
  77. package/lib/holding/Holding.js +0 -31
  78. package/lib/holding/index.js +0 -17
  79. package/lib/index.d.ts +0 -33
  80. package/lib/market/Market.d.ts +0 -159
  81. package/lib/market/Market.js +0 -240
  82. package/lib/market/MarketConfig.d.ts +0 -44
  83. package/lib/market/MarketConfig.js +0 -56
  84. package/lib/market/MarketUtils.d.ts +0 -165
  85. package/lib/market/MarketUtils.js +0 -182
  86. package/lib/market/index.js +0 -19
  87. package/lib/maths/AdaptiveCurveIrmLib.d.ts +0 -37
  88. package/lib/maths/AdaptiveCurveIrmLib.js +0 -116
  89. package/lib/maths/MathLib.d.ts +0 -94
  90. package/lib/maths/MathLib.js +0 -153
  91. package/lib/maths/MathUtils.d.ts +0 -15
  92. package/lib/maths/MathUtils.js +0 -33
  93. package/lib/maths/SharesMath.d.ts +0 -12
  94. package/lib/maths/SharesMath.js +0 -22
  95. package/lib/maths/index.js +0 -20
  96. package/lib/notifications.d.ts +0 -98
  97. package/lib/notifications.js +0 -52
  98. package/lib/position/Position.d.ts +0 -118
  99. package/lib/position/Position.js +0 -145
  100. package/lib/position/index.js +0 -17
  101. package/lib/signatures/index.d.ts +0 -12
  102. package/lib/signatures/index.js +0 -39
  103. package/lib/signatures/manager.d.ts +0 -10
  104. package/lib/signatures/manager.js +0 -37
  105. package/lib/signatures/permit.d.ts +0 -21
  106. package/lib/signatures/permit.js +0 -101
  107. package/lib/signatures/permit2.d.ts +0 -20
  108. package/lib/signatures/permit2.js +0 -91
  109. package/lib/signatures/types.d.ts +0 -13
  110. package/lib/signatures/types.js +0 -2
  111. package/lib/signatures/utils.d.ts +0 -6
  112. package/lib/signatures/utils.js +0 -44
  113. package/lib/tests/mocks/markets.d.ts +0 -17
  114. package/lib/tests/mocks/markets.js +0 -108
  115. package/lib/token/ERC20Metadata.d.ts +0 -249
  116. package/lib/token/ERC20Metadata.js +0 -81
  117. package/lib/token/Token.d.ts +0 -45
  118. package/lib/token/Token.js +0 -39
  119. package/lib/token/TokenNamespace.d.ts +0 -18
  120. package/lib/token/TokenNamespace.js +0 -55
  121. package/lib/token/WrappedToken.d.ts +0 -42
  122. package/lib/token/WrappedToken.js +0 -87
  123. package/lib/token/index.js +0 -18
  124. package/lib/types.d.ts +0 -29
  125. package/lib/types.js +0 -23
  126. package/lib/user/User.d.ts +0 -20
  127. package/lib/user/User.js +0 -11
  128. package/lib/user/index.js +0 -18
  129. package/lib/user/user.types.d.ts +0 -18
  130. package/lib/user/user.types.js +0 -2
  131. package/lib/vault/Vault.d.ts +0 -167
  132. package/lib/vault/Vault.js +0 -156
  133. package/lib/vault/VaultAllocation.d.ts +0 -38
  134. package/lib/vault/VaultAllocation.js +0 -18
  135. package/lib/vault/VaultConfig.d.ts +0 -23
  136. package/lib/vault/VaultConfig.js +0 -26
  137. package/lib/vault/VaultUtils.d.ts +0 -17
  138. package/lib/vault/VaultUtils.js +0 -17
  139. package/lib/vault/index.js +0 -20
  140. /package/{lib/chain/index.d.ts → src/chain/index.ts} +0 -0
  141. /package/{lib/ethers/index.d.ts → src/ethers/index.ts} +0 -0
  142. /package/{lib/helpers/format/index.d.ts → src/helpers/format/index.ts} +0 -0
  143. /package/{lib/helpers/index.d.ts → src/helpers/index.ts} +0 -0
  144. /package/{lib/holding/index.d.ts → src/holding/index.ts} +0 -0
  145. /package/{lib/market/index.d.ts → src/market/index.ts} +0 -0
  146. /package/{lib/maths/index.d.ts → src/maths/index.ts} +0 -0
  147. /package/{lib/position/index.d.ts → src/position/index.ts} +0 -0
  148. /package/{lib/token/index.d.ts → src/token/index.ts} +0 -0
  149. /package/{lib/user/index.d.ts → src/user/index.ts} +0 -0
  150. /package/{lib/vault/index.d.ts → src/vault/index.ts} +0 -0
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isZeroAddressOrUnset = void 0;
4
- const ethers_1 = require("ethers");
5
- /**
6
- * Check if an address is the zero address, null, or undefined.
7
- *
8
- * @param address - The address to check, which can be a valid address, null, or undefined.
9
- * @returns True if the address is zero, null, or undefined; otherwise, false.
10
- */
11
- function isZeroAddressOrUnset(address) {
12
- return address === undefined || address === null || address === ethers_1.ZeroAddress;
13
- }
14
- exports.isZeroAddressOrUnset = isZeroAddressOrUnset;
@@ -1,36 +0,0 @@
1
- type LocaleSymbols = {
2
- decimalSymbol: string;
3
- groupSymbol: string;
4
- locale: string;
5
- };
6
- export type LocaleParts = LocaleSymbols & {
7
- value: string;
8
- };
9
- /**
10
- * @returns the locale symbols for the given locale defaulting to en-US
11
- */
12
- export declare const getLocaleSymbols: (locale: string) => LocaleSymbols;
13
- /**
14
- * @returns the effective browser locale
15
- */
16
- export declare const getEffectiveLocale: () => string;
17
- /**
18
- * @returns the value as a string with the given locale symbols
19
- * @param numStr the number as a string in the "from" locale (e.g. "1,2345.6" for en-US)
20
- * @param from the locale the numStr is in (e.g. "en-US")
21
- * @param to the locale to convert to (e.g. "fr-FR")
22
- */
23
- export declare const convertNumStrToLocal: (numStr: string, from: string, to: string) => string;
24
- /**
25
- * @returns the value as a string in the effective browser locale
26
- * @param numStr the number as a string in the effective browser locale (e.g. "1,2345.6" for en-US)
27
- * @param to the locale to use (e.g. "fr-FR")
28
- */
29
- export declare const convertNumStrFromEffectiveTo: (numStr: string, to: string) => string;
30
- /**
31
- * @returns the value as a string with the given locale symbols either from the given locale or the effective browser locale
32
- * @param numStr the number as a string in english format (e.g. "1,2345.6")
33
- * @param locale optional - the locale to use (e.g. "fr-FR")
34
- */
35
- export declare const getEnUSNumberToLocalParts: (numStr: string, locale?: string) => LocaleParts;
36
- export {};
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEnUSNumberToLocalParts = exports.convertNumStrFromEffectiveTo = exports.convertNumStrToLocal = exports.getEffectiveLocale = exports.getLocaleSymbols = void 0;
4
- const _convertEnNumStrToLocale = (numStr, localSymbols) => {
5
- return numStr
6
- .replaceAll(",", "#TEMP#")
7
- .replaceAll(".", localSymbols.decimalSymbol)
8
- .replaceAll("#TEMP#", localSymbols.groupSymbol);
9
- };
10
- /**
11
- * @returns the locale symbols for the given locale defaulting to en-US
12
- */
13
- const getLocaleSymbols = (locale) => {
14
- let formatter;
15
- const formatterOptions = {
16
- useGrouping: true,
17
- maximumFractionDigits: 1,
18
- minimumFractionDigits: 1,
19
- };
20
- try {
21
- formatter = new Intl.NumberFormat(locale, formatterOptions);
22
- }
23
- catch {
24
- formatter = new Intl.NumberFormat("en-US", formatterOptions);
25
- }
26
- const parts = formatter.formatToParts(12345.6);
27
- const decimalSymbol = parts.find((part) => part.type === "decimal").value;
28
- const groupSymbol = parts.find((part) => part.type === "group").value;
29
- return { decimalSymbol, groupSymbol, locale };
30
- };
31
- exports.getLocaleSymbols = getLocaleSymbols;
32
- /**
33
- * @returns the effective browser locale
34
- */
35
- const getEffectiveLocale = () => {
36
- if (typeof window !== "undefined") {
37
- try {
38
- const locale = navigator?.language || document?.documentElement?.lang || "en-US";
39
- new Intl.NumberFormat(locale);
40
- return locale;
41
- }
42
- catch (e) {
43
- return "en-US";
44
- }
45
- }
46
- return "en-US";
47
- };
48
- exports.getEffectiveLocale = getEffectiveLocale;
49
- /**
50
- * @returns the value as a string with the given locale symbols
51
- * @param numStr the number as a string in the "from" locale (e.g. "1,2345.6" for en-US)
52
- * @param from the locale the numStr is in (e.g. "en-US")
53
- * @param to the locale to convert to (e.g. "fr-FR")
54
- */
55
- const convertNumStrToLocal = (numStr, from, to) => {
56
- const fromSymbols = (0, exports.getLocaleSymbols)(from);
57
- const toSymbols = (0, exports.getLocaleSymbols)(to);
58
- return numStr
59
- .replaceAll(fromSymbols.groupSymbol, "#GROUP#")
60
- .replaceAll(fromSymbols.decimalSymbol, "#DECIMAL#")
61
- .replaceAll("#GROUP#", toSymbols.groupSymbol)
62
- .replaceAll("#DECIMAL#", toSymbols.decimalSymbol);
63
- };
64
- exports.convertNumStrToLocal = convertNumStrToLocal;
65
- /**
66
- * @returns the value as a string in the effective browser locale
67
- * @param numStr the number as a string in the effective browser locale (e.g. "1,2345.6" for en-US)
68
- * @param to the locale to use (e.g. "fr-FR")
69
- */
70
- const convertNumStrFromEffectiveTo = (numStr, to) => {
71
- const from = (0, exports.getEffectiveLocale)();
72
- return (0, exports.convertNumStrToLocal)(numStr, from, to);
73
- };
74
- exports.convertNumStrFromEffectiveTo = convertNumStrFromEffectiveTo;
75
- /**
76
- * @returns the value as a string with the given locale symbols either from the given locale or the effective browser locale
77
- * @param numStr the number as a string in english format (e.g. "1,2345.6")
78
- * @param locale optional - the locale to use (e.g. "fr-FR")
79
- */
80
- const getEnUSNumberToLocalParts = (numStr, locale) => {
81
- const _locale = locale || (0, exports.getEffectiveLocale)();
82
- const localSymbols = (0, exports.getLocaleSymbols)(_locale);
83
- const value = _convertEnNumStrToLocale(numStr, localSymbols);
84
- return { ...localSymbols, value };
85
- };
86
- exports.getEnUSNumberToLocalParts = getEnUSNumberToLocalParts;
@@ -1,60 +0,0 @@
1
- import { BigNumberish } from "ethers";
2
- import { Address } from "../types";
3
- export declare const ERC20_ALLOWANCE_RECIPIENTS: readonly ["morpho", "permit2", "bundler"];
4
- export declare const PERMIT2_ALLOWANCE_RECIPIENTS: readonly ["morpho", "bundler"];
5
- export type Erc20AllowanceRecipient = (typeof ERC20_ALLOWANCE_RECIPIENTS)[number];
6
- export type Permit2AllowanceRecipient = (typeof PERMIT2_ALLOWANCE_RECIPIENTS)[number];
7
- export interface Permit2Allowance {
8
- amount: bigint;
9
- expiration: bigint;
10
- nonce: bigint;
11
- }
12
- export interface InputHolding {
13
- user: Address;
14
- token: Address;
15
- erc20Allowances: {
16
- [key in Erc20AllowanceRecipient]: BigNumberish;
17
- };
18
- permit2Allowances: {
19
- [key in Permit2AllowanceRecipient]: Permit2Allowance;
20
- };
21
- erc2612Nonce?: BigNumberish;
22
- canTransfer?: boolean;
23
- balance: BigNumberish;
24
- }
25
- export declare class Holding implements InputHolding {
26
- /**
27
- * The user of this holding.
28
- */
29
- readonly user: Address;
30
- /**
31
- * The token in which this holding is denominated.
32
- */
33
- readonly token: Address;
34
- /**
35
- * The balance of the user for this token.
36
- */
37
- balance: bigint;
38
- /**
39
- * Whether the user is allowed to transfer this holding's balance.
40
- */
41
- canTransfer?: boolean;
42
- /**
43
- * ERC20 allowance for this token from the user to the allowance recipient.
44
- */
45
- readonly erc20Allowances: {
46
- [key in Erc20AllowanceRecipient]: bigint;
47
- };
48
- /**
49
- * Permit2 allowance for this token from the user to the allowance recipient.
50
- */
51
- readonly permit2Allowances: {
52
- [key in Permit2AllowanceRecipient]: Permit2Allowance;
53
- };
54
- /**
55
- * ERC-2612 Permit nonce of the user for this token.
56
- * `undefined` if the token does not support ERC-2612.
57
- */
58
- erc2612Nonce?: bigint;
59
- constructor({ user, token, erc20Allowances, permit2Allowances, balance, erc2612Nonce, canTransfer, }: InputHolding);
60
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Holding = exports.PERMIT2_ALLOWANCE_RECIPIENTS = exports.ERC20_ALLOWANCE_RECIPIENTS = void 0;
4
- const ethers_1 = require("ethers");
5
- const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
- const addresses_1 = require("../addresses");
7
- exports.ERC20_ALLOWANCE_RECIPIENTS = [
8
- "morpho",
9
- "permit2",
10
- "bundler",
11
- ];
12
- exports.PERMIT2_ALLOWANCE_RECIPIENTS = [
13
- "morpho",
14
- "bundler",
15
- ];
16
- class Holding {
17
- constructor({ user, token, erc20Allowances, permit2Allowances, balance, erc2612Nonce, canTransfer = true, }) {
18
- this.user = user;
19
- this.token = token;
20
- this.balance = (0, ethers_1.toBigInt)(balance);
21
- this.canTransfer = token === addresses_1.NATIVE_ADDRESS || canTransfer;
22
- this.erc20Allowances = (0, morpho_ts_1.fromEntries)((0, morpho_ts_1.entries)(erc20Allowances).map(([address, allowance]) => [
23
- address,
24
- (0, ethers_1.toBigInt)(allowance),
25
- ]));
26
- this.permit2Allowances = permit2Allowances;
27
- if (erc2612Nonce != null)
28
- this.erc2612Nonce = (0, ethers_1.toBigInt)(erc2612Nonce);
29
- }
30
- }
31
- exports.Holding = Holding;
@@ -1,17 +0,0 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./Holding"), exports);
package/lib/index.d.ts DELETED
@@ -1,33 +0,0 @@
1
- export * from "./addresses";
2
- export * from "./evm";
3
- export * from "./constants";
4
- export * from "./errors";
5
- export * from "./market";
6
- export * from "./chain";
7
- export * from "./token";
8
- export * from "./types";
9
- export * from "./maths";
10
- export * from "./notifications";
11
- export * from "./signatures";
12
- export * from "./user";
13
- export * from "./holding";
14
- export * from "./position";
15
- export * from "./helpers";
16
- export * from "./ethers";
17
- export * from "./vault";
18
- export * as evm from "./evm";
19
- export * as constants from "./constants";
20
- export * as errors from "./errors";
21
- export * as market from "./market";
22
- export * as chain from "./chain";
23
- export * as token from "./token";
24
- export * as position from "./position";
25
- export * as holding from "./holding";
26
- export * as types from "./types";
27
- export * as maths from "./maths";
28
- export * as notifications from "./notifications";
29
- export * as signatures from "./signatures";
30
- export * as user from "./user";
31
- export * as helpers from "./helpers";
32
- export * as ethers from "./ethers";
33
- export * as vault from "./vault";
@@ -1,159 +0,0 @@
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
- }
@@ -1,240 +0,0 @@
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;