@morpho-org/blue-sdk 2.0.0-next.1 → 2.0.0-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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -9
  3. package/lib/addresses.d.ts +117 -0
  4. package/lib/addresses.js +156 -0
  5. package/lib/chain.d.ts +29 -0
  6. package/lib/chain.js +286 -0
  7. package/lib/constants.d.ts +29 -0
  8. package/lib/constants.js +30 -0
  9. package/lib/errors.d.ts +52 -0
  10. package/lib/errors.js +97 -0
  11. package/lib/holding/AssetBalances.d.ts +48 -0
  12. package/lib/holding/AssetBalances.js +38 -0
  13. package/lib/holding/Holding.d.ts +64 -0
  14. package/lib/holding/Holding.js +65 -0
  15. package/lib/holding/index.d.ts +2 -0
  16. package/lib/holding/index.js +2 -0
  17. package/lib/index.js +12 -0
  18. package/lib/market/Market.d.ts +332 -0
  19. package/lib/market/Market.js +459 -0
  20. package/lib/market/MarketConfig.d.ts +52 -0
  21. package/lib/market/MarketConfig.js +73 -0
  22. package/lib/market/MarketUtils.d.ts +233 -0
  23. package/lib/market/MarketUtils.js +257 -0
  24. package/lib/market/index.d.ts +3 -0
  25. package/lib/market/index.js +3 -0
  26. package/lib/math/AdaptiveCurveIrmLib.d.ts +39 -0
  27. package/lib/math/AdaptiveCurveIrmLib.js +131 -0
  28. package/lib/math/MathLib.d.ts +111 -0
  29. package/lib/math/MathLib.js +185 -0
  30. package/lib/math/SharesMath.d.ts +12 -0
  31. package/lib/math/SharesMath.js +18 -0
  32. package/lib/math/index.d.ts +3 -0
  33. package/lib/math/index.js +3 -0
  34. package/lib/position/Position.d.ts +127 -0
  35. package/lib/position/Position.js +199 -0
  36. package/lib/position/index.d.ts +1 -0
  37. package/lib/position/index.js +1 -0
  38. package/lib/token/ConstantWrappedToken.d.ts +17 -0
  39. package/lib/token/ConstantWrappedToken.js +30 -0
  40. package/lib/token/ExchangeRateWrappedToken.d.ts +11 -0
  41. package/lib/token/ExchangeRateWrappedToken.js +17 -0
  42. package/lib/token/Token.d.ts +46 -0
  43. package/lib/token/Token.js +59 -0
  44. package/lib/token/VaultToken.d.ts +23 -0
  45. package/lib/token/VaultToken.js +27 -0
  46. package/lib/token/WrappedToken.d.ts +17 -0
  47. package/lib/token/WrappedToken.js +29 -0
  48. package/lib/token/index.d.ts +5 -0
  49. package/lib/token/index.js +5 -0
  50. package/lib/types.d.ts +27 -0
  51. package/lib/types.js +17 -0
  52. package/lib/user/User.d.ts +20 -0
  53. package/lib/user/User.js +19 -0
  54. package/lib/user/index.d.ts +1 -0
  55. package/lib/user/index.js +1 -0
  56. package/lib/vault/Vault.d.ts +152 -0
  57. package/lib/vault/Vault.js +221 -0
  58. package/lib/vault/VaultConfig.d.ts +22 -0
  59. package/lib/vault/VaultConfig.js +28 -0
  60. package/lib/vault/VaultMarketAllocation.d.ts +20 -0
  61. package/lib/vault/VaultMarketAllocation.js +26 -0
  62. package/lib/vault/VaultMarketConfig.d.ts +43 -0
  63. package/lib/vault/VaultMarketConfig.js +39 -0
  64. package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
  65. package/lib/vault/VaultMarketPublicAllocatorConfig.js +24 -0
  66. package/lib/vault/VaultUser.d.ts +26 -0
  67. package/lib/vault/VaultUser.js +24 -0
  68. package/lib/vault/VaultUtils.d.ts +16 -0
  69. package/lib/vault/VaultUtils.js +17 -0
  70. package/lib/vault/index.d.ts +7 -0
  71. package/lib/vault/index.js +7 -0
  72. package/package.json +33 -25
  73. /package/{src/index.ts → lib/index.d.ts} +0 -0
package/lib/chain.js ADDED
@@ -0,0 +1,286 @@
1
+ import { UnsupportedChainIdError } from "./errors.js";
2
+ export var ChainId;
3
+ (function (ChainId) {
4
+ ChainId[ChainId["EthMainnet"] = 1] = "EthMainnet";
5
+ ChainId[ChainId["BaseMainnet"] = 8453] = "BaseMainnet";
6
+ // EthGoerliTestnet = 5,
7
+ // PolygonMainnet = 137,
8
+ // MumbaiTestnet = 80001,
9
+ // OptimismMainnet = 10,
10
+ // BscMainnet = 56,
11
+ // ArbitrumMainnet = 42161,
12
+ // ArbitrumTestnet = 421611,
13
+ // GnosisChain = 100,
14
+ // FantomMainnet = 250,
15
+ // FantomTestnet = 4002,
16
+ // HarmonyMainnet = 128,
17
+ // HarmonyTestnet = 1666600000,
18
+ // BscTestnet = 97,
19
+ // OptimismTestnet = 69,
20
+ // EthRopstenTestnet = 3,
21
+ // EthRinkebyTestnet = 4,
22
+ // EthKovanTestnet = 42,
23
+ // GnosisChainTestnet = 10200,
24
+ // AvalancheMainnet = 43114,
25
+ // AvalancheFujiTestnet = 43113,
26
+ // MoonbaseAlphaTestnet = 1287,
27
+ // BaseGoerliTestnet = 84531,
28
+ })(ChainId || (ChainId = {}));
29
+ export var ChainUtils;
30
+ (function (ChainUtils) {
31
+ ChainUtils.toHexChainId = (chainId) => {
32
+ return `0x${chainId.toString(16)}`;
33
+ };
34
+ ChainUtils.getExplorerUrl = (chainId) => {
35
+ return ChainUtils.CHAIN_METADATA[chainId].explorerUrl;
36
+ };
37
+ ChainUtils.getExplorerAddressUrl = (chainId, address) => {
38
+ return `${ChainUtils.getExplorerUrl(chainId)}/address/${address}`;
39
+ };
40
+ ChainUtils.getExplorerTransactionUrl = (chainId, tx) => {
41
+ return `${ChainUtils.getExplorerUrl(chainId)}/tx/${tx}`;
42
+ };
43
+ function isSupported(chainId) {
44
+ return ChainUtils.BLUE_AVAILABLE_CHAINS.includes(chainId);
45
+ }
46
+ ChainUtils.isSupported = isSupported;
47
+ function parseSupportedChainId(candidate) {
48
+ const chainId = Number.parseInt(candidate); // Force cast to string to silence TS because it works.
49
+ if (!isSupported(chainId))
50
+ throw new UnsupportedChainIdError(chainId);
51
+ return chainId;
52
+ }
53
+ ChainUtils.parseSupportedChainId = parseSupportedChainId;
54
+ ChainUtils.BLUE_AVAILABLE_CHAINS = [
55
+ ChainId.EthMainnet,
56
+ ChainId.BaseMainnet,
57
+ ];
58
+ ChainUtils.CHAIN_METADATA = {
59
+ [ChainId.EthMainnet]: {
60
+ name: "Ethereum",
61
+ id: ChainId.EthMainnet,
62
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
63
+ defaultRpcUrl: "https://mainnet.infura.io/V3/84842078b09946638c03157f83405213",
64
+ explorerUrl: "https://etherscan.io",
65
+ isTestnet: false,
66
+ shortName: "ETH",
67
+ logoSrc: "https://cdn.morpho.org/assets/chains/eth.svg",
68
+ identifier: "mainnet",
69
+ },
70
+ [ChainId.BaseMainnet]: {
71
+ name: "Base",
72
+ id: ChainId.BaseMainnet,
73
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
74
+ defaultRpcUrl: "https://rpc.baseprotocol.org",
75
+ explorerUrl: "https://basescan.org",
76
+ isTestnet: false,
77
+ shortName: "Base",
78
+ logoSrc: "https://cdn.morpho.org/assets/chains/base.png",
79
+ identifier: "base",
80
+ },
81
+ // [ChainId.EthGoerliTestnet]: {
82
+ // name: "Ethereum Goerli Testnet",
83
+ // id: ChainId.EthGoerliTestnet,
84
+ // nativeCurrency: { name: "Goerli Ether", symbol: "ETH", decimals: 18 },
85
+ // defaultRpcUrl:
86
+ // "https://goerli.infura.io/V3/84842078b09946638c03157f83405213",
87
+ // explorerUrl: "https://goerli.etherscan.io",
88
+ // isTestnet: true,
89
+ // shortName: "Goerli",
90
+ // logoSrc: "https://cdn.morpho.org/assets/chains/eth.svg",
91
+ // identifier: "goerli",
92
+ // },
93
+ // [ChainId.PolygonMainnet]: {
94
+ // name: "Polygon Mainnet",
95
+ // id: ChainId.PolygonMainnet,
96
+ // nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18n },
97
+ // defaultRpcUrl: "https://rpc-mainnet.maticvigil.com",
98
+ // explorerUrl: "https://polygonscan.com",
99
+ // isTestnet: false,
100
+ // shortName: "Polygon",
101
+ // },
102
+ // [ChainId.MumbaiTestnet]: {
103
+ // name: "Mumbai Testnet",
104
+ // id: ChainId.MumbaiTestnet,
105
+ // nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18n },
106
+ // defaultRpcUrl: "https://rpc-mumbai.maticvigil.com",
107
+ // explorerUrl: "https://mumbai.polygonscan.com",
108
+ // isTestnet: true,
109
+ // shortName: "Mumbai",
110
+ // },
111
+ /* see https://chainid.network/chains.json */
112
+ // [ChainId.PolygonMainnet]: {
113
+ // name: "Polygon Mainnet",
114
+ // id: ChainId.PolygonMainnet,
115
+ // nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18n },
116
+ // defaultRpcUrl: "https://rpc-mainnet.maticvigil.com",
117
+ // explorerUrl: "https://polygonscan.com",
118
+ // isTestnet: false,
119
+ // shortName: "Polygon",
120
+ // },
121
+ // [ChainId.MumbaiTestnet]: {
122
+ // name: "Mumbai Testnet",
123
+ // id: ChainId.MumbaiTestnet,
124
+ // nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18n },
125
+ // defaultRpcUrl: "https://rpc-mumbai.maticvigil.com",
126
+ // explorerUrl: "https://mumbai.polygonscan.com",
127
+ // isTestnet: true,
128
+ // shortName: "Mumbai",
129
+ // },
130
+ // [ChainId.OptimismMainnet]: {
131
+ // name: "Optimism Mainnet",
132
+ // id: ChainId.OptimismMainnet,
133
+ // baseCurrency: "ETH",
134
+ // defaultRpcUrl: "https://mainnet.optimism.io",
135
+ // explorerUrl: "https://optimistic.etherscan.io",
136
+ // isTestnet: false,
137
+ // },
138
+ // [ChainId.BscMainnet]: {
139
+ // name: "Binance Smart Chain Mainnet",
140
+ // id: ChainId.BscMainnet,
141
+ // baseCurrency: "BNB",
142
+ // defaultRpcUrl: "https://bsc-dataseed.binance.org",
143
+ // explorerUrl: "https://bscscan.com",
144
+ // isTestnet: false,
145
+ // },
146
+ // [ChainId.ArbitrumMainnet]: {
147
+ // name: "Arbitrum Mainnet",
148
+ // id: ChainId.ArbitrumMainnet,
149
+ // baseCurrency: "ETH",
150
+ // defaultRpcUrl: "https://arb1.arbitrum.io/rpc",
151
+ // explorerUrl: "https://arbiscan.io",
152
+ // isTestnet: false,
153
+ // },
154
+ // [ChainId.ArbitrumTestnet]: {
155
+ // name: "Arbitrum Testnet",
156
+ // id: ChainId.ArbitrumTestnet,
157
+ // baseCurrency: "ETH",
158
+ // defaultRpcUrl: "https://rinkeby.arbitrum.io/rpc",
159
+ // explorerUrl: "https://rinkeby-explorer.arbitrum.io",
160
+ // isTestnet: true,
161
+ // },
162
+ // [ChainId.GnosisChain]: {
163
+ // name: "Gnosis Chain",
164
+ // id: ChainId.GnosisChain,
165
+ // baseCurrency: "XDAI",
166
+ // defaultRpcUrl: "https://rpc.xdaichain.com",
167
+ // explorerUrl: "https://gnosis.blockscout.com",
168
+ // isTestnet: false,
169
+ // },
170
+ // [ChainId.FantomMainnet]: {
171
+ // name: "Fantom Mainnet",
172
+ // id: ChainId.FantomMainnet,
173
+ // baseCurrency: "FTM",
174
+ // defaultRpcUrl: "https://rpcapi.fantom.network",
175
+ // explorerUrl: "https://ftmscan.com",
176
+ // isTestnet: false,
177
+ // },
178
+ // [ChainId.FantomTestnet]: {
179
+ // name: "Fantom Testnet",
180
+ // id: ChainId.FantomTestnet,
181
+ // baseCurrency: "FTM",
182
+ // defaultRpcUrl: "https://rpc.testnet.fantom.network",
183
+ // explorerUrl: "https://explorer.testnet.fantom.network",
184
+ // isTestnet: true,
185
+ // },
186
+ // [ChainId.HarmonyMainnet]: {
187
+ // name: "Harmony Mainnet",
188
+ // id: ChainId.HarmonyMainnet,
189
+ // baseCurrency: "ONE",
190
+ // defaultRpcUrl: "https://api.harmony.one",
191
+ // explorerUrl: "https://explorer.harmony.one",
192
+ // isTestnet: false,
193
+ // },
194
+ // [ChainId.HarmonyTestnet]: {
195
+ // name: "Harmony Testnet",
196
+ // id: ChainId.HarmonyTestnet,
197
+ // baseCurrency: "ONE",
198
+ // defaultRpcUrl: "https://api.s0.b.hmny.io",
199
+ // explorerUrl: "https://explorer.testnet.harmony.one",
200
+ // isTestnet: true,
201
+ // },
202
+ // [ChainId.BscTestnet]: {
203
+ // name: "Binance Smart Chain Testnet",
204
+ // id: ChainId.BscTestnet,
205
+ // baseCurrency: "BNB",
206
+ // defaultRpcUrl: "https://data-seed-prebsc-1-s1.binance.org:8545",
207
+ // explorerUrl: "https://testnet.bscscan.com",
208
+ // isTestnet: true,
209
+ // },
210
+ // [ChainId.OptimismTestnet]: {
211
+ // name: "Optimism Testnet",
212
+ // id: ChainId.OptimismTestnet,
213
+ // baseCurrency: "ETH",
214
+ // defaultRpcUrl: "https://kovan.optimism.io",
215
+ // explorerUrl: "https://kovan-optimistic.etherscan.io",
216
+ // isTestnet: true,
217
+ // },
218
+ // [ChainId.EthRopstenTestnet]: {
219
+ // name: "Ethereum Ropsten Testnet",
220
+ // id: ChainId.EthRopstenTestnet,
221
+ // baseCurrency: "ETH",
222
+ // defaultRpcUrl:
223
+ // "https://ropsten.infura.io/V4/84842078b09946638c03157f83405213",
224
+ // explorerUrl: "https://ropsten.etherscan.io",
225
+ // isTestnet: true,
226
+ // },
227
+ // [ChainId.EthRinkebyTestnet]: {
228
+ // name: "Ethereum Rinkeby Testnet",
229
+ // id: ChainId.EthRinkebyTestnet,
230
+ // baseCurrency: "ETH",
231
+ // defaultRpcUrl:
232
+ // "https://rinkeby.infura.io/V4/84842078b09946638c03157f83405213",
233
+ // explorerUrl: "https://rinkeby.etherscan.io",
234
+ // isTestnet: true,
235
+ // },
236
+ // [ChainId.EthKovanTestnet]: {
237
+ // name: "Ethereum Kovan Testnet",
238
+ // id: ChainId.EthKovanTestnet,
239
+ // baseCurrency: "ETH",
240
+ // defaultRpcUrl:
241
+ // "https://kovan.infura.io/V4/84842078b09946638c03157f83405213",
242
+ // explorerUrl: "https://kovan.etherscan.io",
243
+ // isTestnet: true,
244
+ // },
245
+ // [ChainId.BaseGoerliTestnet]: {
246
+ // name: "Base Goerli Testnet",
247
+ // id: ChainId.BaseGoerliTestnet,
248
+ // baseCurrency: "ETH",
249
+ // defaultRpcUrl: "https://rpc-goerli.baseprotocol.org",
250
+ // explorerUrl: "https://goerli.basescan.org",
251
+ // isTestnet: true,
252
+ // },
253
+ // [ChainId.GnosisChainTestnet]: {
254
+ // name: "Gnosis Chain Testnet",
255
+ // id: ChainId.GnosisChainTestnet,
256
+ // baseCurrency: "XDAI",
257
+ // defaultRpcUrl: "https://rpc.chiado.gnosis.gateway.fm",
258
+ // explorerUrl: "https://gnosis-chiado.blockscout.com/",
259
+ // isTestnet: true,
260
+ // },
261
+ // [ChainId.AvalancheMainnet]: {
262
+ // name: "Avalanche Mainnet",
263
+ // id: ChainId.AvalancheMainnet,
264
+ // baseCurrency: "AVAX",
265
+ // defaultRpcUrl: "https://api.avax.network/ext/bc/C/rpc",
266
+ // explorerUrl: "https://cchain.explorer.avax.network",
267
+ // isTestnet: false,
268
+ // },
269
+ // [ChainId.AvalancheFujiTestnet]: {
270
+ // name: "Avalanche Fuji Testnet",
271
+ // id: ChainId.AvalancheFujiTestnet,
272
+ // baseCurrency: "AVAX",
273
+ // defaultRpcUrl: "https://api.avax-test.network/ext/bc/C/rpc",
274
+ // explorerUrl: "https://cchain.explorer.avax-test.network",
275
+ // isTestnet: true,
276
+ // },
277
+ // [ChainId.MoonbaseAlphaTestnet]: {
278
+ // name: "Moonbase Alpha Testnet",
279
+ // id: ChainId.MoonbaseAlphaTestnet,
280
+ // baseCurrency: "DEV",
281
+ // defaultRpcUrl: "https://rpc.testnet.moonbeam.network",
282
+ // explorerUrl: "https://moonbase-blockscout.testnet.moonbeam.network",
283
+ // isTestnet: true,
284
+ // },
285
+ };
286
+ })(ChainUtils || (ChainUtils = {}));
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The liquidation cursor used to calculate the liquidation incentive. Hardcoded to 30%.
3
+ */
4
+ export declare const LIQUIDATION_CURSOR = 300000000000000000n;
5
+ /**
6
+ * The maximum liquidation incentive factor. Hardcoded to 115%.
7
+ */
8
+ export declare const MAX_LIQUIDATION_INCENTIVE_FACTOR = 1150000000000000000n;
9
+ export declare const ORACLE_PRICE_OFFSET = 36;
10
+ /**
11
+ * The scale of the oracle price. Hardcoded to 1e36.
12
+ */
13
+ export declare const ORACLE_PRICE_SCALE = 1000000000000000000000000000000000000n;
14
+ /**
15
+ * The default slippage tolerance used in the SDK. Hardcoded to 0.03%.
16
+ */
17
+ export declare const DEFAULT_SLIPPAGE_TOLERANCE = 300000000000000n;
18
+ /**
19
+ * The default withdrawal target utilization used by the shared liquidity algorithm. Hardcoded to 92%.
20
+ */
21
+ export declare const DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = 920000000000000000n;
22
+ /**
23
+ * The default supply target utilization used by the shared liquidity algorithm. Hardcoded to 90.5%.
24
+ */
25
+ export declare const DEFAULT_SUPPLY_TARGET_UTILIZATION = 905000000000000000n;
26
+ /**
27
+ * The number of seconds in a year.
28
+ */
29
+ export declare const SECONDS_PER_YEAR: bigint;
@@ -0,0 +1,30 @@
1
+ import { Time } from "@morpho-org/morpho-ts";
2
+ /**
3
+ * The liquidation cursor used to calculate the liquidation incentive. Hardcoded to 30%.
4
+ */
5
+ export const LIQUIDATION_CURSOR = 300000000000000000n;
6
+ /**
7
+ * The maximum liquidation incentive factor. Hardcoded to 115%.
8
+ */
9
+ export const MAX_LIQUIDATION_INCENTIVE_FACTOR = 1150000000000000000n;
10
+ export const ORACLE_PRICE_OFFSET = 36;
11
+ /**
12
+ * The scale of the oracle price. Hardcoded to 1e36.
13
+ */
14
+ export const ORACLE_PRICE_SCALE = 1000000000000000000000000000000000000n;
15
+ /**
16
+ * The default slippage tolerance used in the SDK. Hardcoded to 0.03%.
17
+ */
18
+ export const DEFAULT_SLIPPAGE_TOLERANCE = 300000000000000n;
19
+ /**
20
+ * The default withdrawal target utilization used by the shared liquidity algorithm. Hardcoded to 92%.
21
+ */
22
+ export const DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = 920000000000000000n;
23
+ /**
24
+ * The default supply target utilization used by the shared liquidity algorithm. Hardcoded to 90.5%.
25
+ */
26
+ export const DEFAULT_SUPPLY_TARGET_UTILIZATION = 905000000000000000n;
27
+ /**
28
+ * The number of seconds in a year.
29
+ */
30
+ export const SECONDS_PER_YEAR = Time.s.from.y(1n);
@@ -0,0 +1,52 @@
1
+ import type { Address, MarketId } from "./types.js";
2
+ export declare class UnknownDataError extends Error {
3
+ }
4
+ export declare class UnknownTokenError extends UnknownDataError {
5
+ readonly address: Address;
6
+ constructor(address: Address);
7
+ }
8
+ export declare class UnknownTokenPriceError extends UnknownDataError {
9
+ readonly address: Address;
10
+ constructor(address: Address);
11
+ }
12
+ export declare class UnknownMarketConfigError extends UnknownDataError {
13
+ readonly marketId: MarketId;
14
+ constructor(marketId: MarketId);
15
+ }
16
+ export declare class UnknownVaultConfigError extends UnknownDataError {
17
+ readonly vault: Address;
18
+ constructor(vault: Address);
19
+ }
20
+ export declare class UnsupportedChainIdError extends Error {
21
+ readonly chainId: number;
22
+ constructor(chainId: number);
23
+ }
24
+ export declare namespace BlueErrors {
25
+ class InvalidInterestAccrual extends Error {
26
+ readonly marketId: MarketId;
27
+ readonly timestamp: bigint;
28
+ readonly lastUpdate: bigint;
29
+ constructor(marketId: MarketId, timestamp: bigint, lastUpdate: bigint);
30
+ }
31
+ class InconsistentInput extends Error {
32
+ constructor();
33
+ }
34
+ class InsufficientLiquidity extends Error {
35
+ readonly marketId: MarketId;
36
+ constructor(marketId: MarketId);
37
+ }
38
+ class InsufficientPosition extends Error {
39
+ readonly user: Address;
40
+ readonly marketId: MarketId;
41
+ constructor(user: Address, marketId: MarketId);
42
+ }
43
+ class InsufficientCollateral extends Error {
44
+ readonly user: Address;
45
+ readonly marketId: MarketId;
46
+ constructor(user: Address, marketId: MarketId);
47
+ }
48
+ }
49
+ export interface ErrorClass<E extends Error> {
50
+ new (...args: any[]): E;
51
+ }
52
+ export declare function _try<T, E extends Error>(accessor: () => T, ...errorClasses: ErrorClass<E>[]): T | undefined;
package/lib/errors.js ADDED
@@ -0,0 +1,97 @@
1
+ export class UnknownDataError extends Error {
2
+ }
3
+ export class UnknownTokenError extends UnknownDataError {
4
+ address;
5
+ constructor(address) {
6
+ super(`unknown token ${address}`);
7
+ this.address = address;
8
+ }
9
+ }
10
+ export class UnknownTokenPriceError extends UnknownDataError {
11
+ address;
12
+ constructor(address) {
13
+ super(`unknown price of token ${address}`);
14
+ this.address = address;
15
+ }
16
+ }
17
+ export class UnknownMarketConfigError extends UnknownDataError {
18
+ marketId;
19
+ constructor(marketId) {
20
+ super(`unknown config for market ${marketId}`);
21
+ this.marketId = marketId;
22
+ }
23
+ }
24
+ export class UnknownVaultConfigError extends UnknownDataError {
25
+ vault;
26
+ constructor(vault) {
27
+ super(`unknown config for vault ${vault}`);
28
+ this.vault = vault;
29
+ }
30
+ }
31
+ export class UnsupportedChainIdError extends Error {
32
+ chainId;
33
+ constructor(chainId) {
34
+ super(`unsupported chain ${chainId}`);
35
+ this.chainId = chainId;
36
+ }
37
+ }
38
+ export var BlueErrors;
39
+ (function (BlueErrors) {
40
+ class InvalidInterestAccrual extends Error {
41
+ marketId;
42
+ timestamp;
43
+ lastUpdate;
44
+ constructor(marketId, timestamp, lastUpdate) {
45
+ super(`invalid interest accrual on market ${marketId}: accrual timestamp ${timestamp} can't be prior to last update ${lastUpdate}`);
46
+ this.marketId = marketId;
47
+ this.timestamp = timestamp;
48
+ this.lastUpdate = lastUpdate;
49
+ }
50
+ }
51
+ BlueErrors.InvalidInterestAccrual = InvalidInterestAccrual;
52
+ class InconsistentInput extends Error {
53
+ constructor() {
54
+ super(`inconsistent input: assets & shares cannot both be zero`);
55
+ }
56
+ }
57
+ BlueErrors.InconsistentInput = InconsistentInput;
58
+ class InsufficientLiquidity extends Error {
59
+ marketId;
60
+ constructor(marketId) {
61
+ super(`insufficient liquidity on market ${marketId}`);
62
+ this.marketId = marketId;
63
+ }
64
+ }
65
+ BlueErrors.InsufficientLiquidity = InsufficientLiquidity;
66
+ class InsufficientPosition extends Error {
67
+ user;
68
+ marketId;
69
+ constructor(user, marketId) {
70
+ super(`insufficient position for user ${user} on market ${marketId}`);
71
+ this.user = user;
72
+ this.marketId = marketId;
73
+ }
74
+ }
75
+ BlueErrors.InsufficientPosition = InsufficientPosition;
76
+ class InsufficientCollateral extends Error {
77
+ user;
78
+ marketId;
79
+ constructor(user, marketId) {
80
+ super(`insufficient collateral for user ${user} on market ${marketId}`);
81
+ this.user = user;
82
+ this.marketId = marketId;
83
+ }
84
+ }
85
+ BlueErrors.InsufficientCollateral = InsufficientCollateral;
86
+ })(BlueErrors || (BlueErrors = {}));
87
+ export function _try(accessor, ...errorClasses) {
88
+ try {
89
+ return accessor();
90
+ }
91
+ catch (error) {
92
+ if (errorClasses.length === 0 ||
93
+ errorClasses.some((errorClass) => error instanceof errorClass))
94
+ return;
95
+ throw error;
96
+ }
97
+ }
@@ -0,0 +1,48 @@
1
+ import type { Token } from "../token/Token.js";
2
+ export type PeripheralBalanceType = "base" | "wrapped" | "staked-wrapped" | "vault" | "wrapped-vault" | "unwrapped-staked-wrapped";
3
+ /**
4
+ * Represents the balance of a requested token and the balance quoted in the corresponding source token:
5
+ * ```{
6
+ * type: "staked-wrapped",
7
+ * srcToken: ETH,
8
+ * srcAmount: 1 ETH,
9
+ * dstAmount: 1.2 wstETH
10
+ * }```
11
+ */
12
+ export interface PeripheralBalance {
13
+ /**
14
+ * The type of balance conversion.
15
+ */
16
+ type: PeripheralBalanceType;
17
+ /**
18
+ * The source token held corresponding to the type of balance conversion.
19
+ */
20
+ srcToken: Token;
21
+ /**
22
+ * The source amount of source token held.
23
+ */
24
+ srcAmount: bigint;
25
+ /**
26
+ * The corresponding amount of token held after conversion of the whole balance `srcAmount`.
27
+ */
28
+ dstAmount: bigint;
29
+ }
30
+ export interface InputAssetBalances extends Omit<PeripheralBalance, "type"> {
31
+ }
32
+ export declare class AssetBalances {
33
+ /**
34
+ * The total balance of all types of related tokens.
35
+ */
36
+ total: bigint;
37
+ /**
38
+ * The balance of each type of related tokens and the corresponding underlying balance.
39
+ */
40
+ allocations: {
41
+ base: PeripheralBalance;
42
+ } & {
43
+ [T in Exclude<PeripheralBalanceType, "base">]?: PeripheralBalance;
44
+ };
45
+ constructor(balance: InputAssetBalances);
46
+ add(balance: PeripheralBalance): this;
47
+ sub(balance: PeripheralBalance): this;
48
+ }
@@ -0,0 +1,38 @@
1
+ export class AssetBalances {
2
+ /**
3
+ * The total balance of all types of related tokens.
4
+ */
5
+ total;
6
+ /**
7
+ * The balance of each type of related tokens and the corresponding underlying balance.
8
+ */
9
+ allocations;
10
+ constructor(balance) {
11
+ this.total = balance.dstAmount;
12
+ this.allocations = {
13
+ base: { ...balance, type: "base" },
14
+ };
15
+ }
16
+ add(balance) {
17
+ this.total += balance.dstAmount;
18
+ const allocation = (this.allocations[balance.type] ??= {
19
+ ...balance,
20
+ srcAmount: 0n,
21
+ dstAmount: 0n,
22
+ });
23
+ allocation.srcAmount += balance.srcAmount;
24
+ allocation.dstAmount += balance.dstAmount;
25
+ return this;
26
+ }
27
+ sub(balance) {
28
+ this.total -= balance.dstAmount;
29
+ const allocation = (this.allocations[balance.type] ??= {
30
+ ...balance,
31
+ srcAmount: 0n,
32
+ dstAmount: 0n,
33
+ });
34
+ allocation.srcAmount -= balance.srcAmount;
35
+ allocation.dstAmount -= balance.dstAmount;
36
+ return this;
37
+ }
38
+ }
@@ -0,0 +1,64 @@
1
+ import type { Address, BigIntish } from "../types.js";
2
+ export declare const ERC20_ALLOWANCE_RECIPIENTS: readonly ["morpho", "permit2", "bundler"];
3
+ export declare const PERMIT2_ALLOWANCE_RECIPIENTS: readonly ["morpho", "bundler"];
4
+ export type Erc20AllowanceRecipient = (typeof ERC20_ALLOWANCE_RECIPIENTS)[number];
5
+ export type Permit2AllowanceRecipient = (typeof PERMIT2_ALLOWANCE_RECIPIENTS)[number];
6
+ export interface Permit2Allowance {
7
+ amount: bigint;
8
+ expiration: bigint;
9
+ nonce: bigint;
10
+ }
11
+ export interface InputPermit2Allowance {
12
+ amount: BigIntish;
13
+ expiration: BigIntish;
14
+ nonce: BigIntish;
15
+ }
16
+ export interface InputHolding {
17
+ user: Address;
18
+ token: Address;
19
+ erc20Allowances: {
20
+ [key in Erc20AllowanceRecipient]: bigint;
21
+ };
22
+ permit2Allowances: {
23
+ [key in Permit2AllowanceRecipient]: InputPermit2Allowance;
24
+ };
25
+ erc2612Nonce?: bigint;
26
+ canTransfer?: boolean;
27
+ balance: bigint;
28
+ }
29
+ export declare class Holding implements InputHolding {
30
+ /**
31
+ * The user of this holding.
32
+ */
33
+ readonly user: Address;
34
+ /**
35
+ * The token in which this holding is denominated.
36
+ */
37
+ readonly token: Address;
38
+ /**
39
+ * The balance of the user for this token.
40
+ */
41
+ balance: bigint;
42
+ /**
43
+ * Whether the user is allowed to transfer this holding's balance.
44
+ */
45
+ canTransfer?: boolean;
46
+ /**
47
+ * ERC20 allowance for this token from the user to the allowance recipient.
48
+ */
49
+ readonly erc20Allowances: {
50
+ [key in Erc20AllowanceRecipient]: bigint;
51
+ };
52
+ /**
53
+ * Permit2 allowance for this token from the user to the allowance recipient.
54
+ */
55
+ readonly permit2Allowances: {
56
+ [key in Permit2AllowanceRecipient]: Permit2Allowance;
57
+ };
58
+ /**
59
+ * ERC-2612 Permit nonce of the user for this token.
60
+ * `undefined` if the token does not support ERC-2612.
61
+ */
62
+ erc2612Nonce?: bigint;
63
+ constructor({ user, token, erc20Allowances, permit2Allowances, balance, erc2612Nonce, canTransfer, }: InputHolding);
64
+ }