@morpho-org/blue-sdk 2.0.0-test.1 → 2.1.0

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 (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +46 -26
  3. package/lib/addresses.d.ts +59 -55
  4. package/lib/addresses.js +42 -43
  5. package/lib/chain.d.ts +1 -1
  6. package/lib/chain.js +3 -3
  7. package/lib/constants.d.ts +0 -9
  8. package/lib/constants.js +1 -10
  9. package/lib/errors.d.ts +7 -8
  10. package/lib/errors.js +21 -19
  11. package/lib/holding/AssetBalances.d.ts +3 -3
  12. package/lib/holding/Holding.d.ts +10 -5
  13. package/lib/holding/Holding.js +14 -4
  14. package/lib/holding/index.d.ts +2 -2
  15. package/lib/holding/index.js +2 -2
  16. package/lib/index.d.ts +12 -25
  17. package/lib/index.js +12 -38
  18. package/lib/market/Market.d.ts +48 -34
  19. package/lib/market/Market.js +76 -57
  20. package/lib/market/{MarketConfig.d.ts → MarketParams.d.ts} +8 -7
  21. package/lib/market/{MarketConfig.js → MarketParams.js} +20 -21
  22. package/lib/market/MarketUtils.d.ts +94 -42
  23. package/lib/market/MarketUtils.js +145 -67
  24. package/lib/market/index.d.ts +3 -3
  25. package/lib/market/index.js +3 -3
  26. package/lib/{maths → math}/AdaptiveCurveIrmLib.d.ts +1 -1
  27. package/lib/{maths → math}/AdaptiveCurveIrmLib.js +20 -20
  28. package/lib/{maths → math}/MathLib.d.ts +24 -42
  29. package/lib/{maths → math}/MathLib.js +41 -54
  30. package/lib/{maths → math}/SharesMath.d.ts +2 -2
  31. package/lib/{maths → math}/SharesMath.js +3 -3
  32. package/lib/math/index.d.ts +3 -0
  33. package/lib/{helpers/format → math}/index.js +3 -1
  34. package/lib/position/Position.d.ts +35 -25
  35. package/lib/position/Position.js +46 -44
  36. package/lib/position/index.d.ts +1 -1
  37. package/lib/position/index.js +1 -1
  38. package/lib/token/ConstantWrappedToken.d.ts +6 -7
  39. package/lib/token/ConstantWrappedToken.js +8 -10
  40. package/lib/token/ExchangeRateWrappedToken.d.ts +5 -5
  41. package/lib/token/ExchangeRateWrappedToken.js +5 -5
  42. package/lib/token/Token.d.ts +18 -18
  43. package/lib/token/Token.js +24 -27
  44. package/lib/token/VaultToken.d.ts +20 -11
  45. package/lib/token/VaultToken.js +15 -7
  46. package/lib/token/WrappedToken.d.ts +4 -4
  47. package/lib/token/WrappedToken.js +7 -7
  48. package/lib/token/index.d.ts +5 -5
  49. package/lib/token/index.js +5 -5
  50. package/lib/types.d.ts +3 -6
  51. package/lib/user/User.d.ts +1 -1
  52. package/lib/user/index.d.ts +1 -1
  53. package/lib/user/index.js +1 -1
  54. package/lib/vault/Vault.d.ts +22 -28
  55. package/lib/vault/Vault.js +43 -51
  56. package/lib/vault/VaultConfig.d.ts +6 -7
  57. package/lib/vault/VaultConfig.js +5 -5
  58. package/lib/vault/VaultMarketAllocation.d.ts +8 -8
  59. package/lib/vault/VaultMarketAllocation.js +3 -3
  60. package/lib/vault/VaultMarketConfig.d.ts +10 -10
  61. package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +6 -6
  62. package/lib/vault/VaultUser.d.ts +26 -0
  63. package/lib/vault/VaultUser.js +28 -0
  64. package/lib/vault/VaultUtils.d.ts +4 -6
  65. package/lib/vault/VaultUtils.js +6 -6
  66. package/lib/vault/index.d.ts +7 -6
  67. package/lib/vault/index.js +7 -6
  68. package/package.json +25 -56
  69. package/lib/chain.test.d.ts +0 -1
  70. package/lib/chain.test.js +0 -21
  71. package/lib/helpers/format/format.d.ts +0 -122
  72. package/lib/helpers/format/format.js +0 -286
  73. package/lib/helpers/format/format.test.d.ts +0 -1
  74. package/lib/helpers/format/format.test.js +0 -255
  75. package/lib/helpers/format/index.d.ts +0 -1
  76. package/lib/helpers/index.d.ts +0 -2
  77. package/lib/helpers/index.js +0 -18
  78. package/lib/helpers/locale.d.ts +0 -46
  79. package/lib/helpers/locale.js +0 -96
  80. package/lib/market/MarketUtils.test.d.ts +0 -1
  81. package/lib/market/MarketUtils.test.js +0 -38
  82. package/lib/maths/index.d.ts +0 -3
  83. package/lib/maths/index.js +0 -19
  84. package/lib/tests/mocks/markets.d.ts +0 -19
  85. package/lib/tests/mocks/markets.js +0 -121
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AccrualPosition = exports.Position = void 0;
4
- const errors_1 = require("../errors");
5
- const market_1 = require("../market");
4
+ const errors_js_1 = require("../errors.js");
5
+ const index_js_1 = require("../market/index.js");
6
6
  class Position {
7
7
  /**
8
8
  * The user holding this position.
@@ -50,36 +50,42 @@ class AccrualPosition extends Position {
50
50
  }
51
51
  /**
52
52
  * The value of this position's collateral quoted in loan assets.
53
+ * `undefined` iff the market's oracle is undefined or reverts.
53
54
  */
54
55
  get collateralValue() {
55
56
  return this.market.getCollateralValue(this.collateral);
56
57
  }
57
58
  /**
58
59
  * The maximum amount of loan assets that can be borrowed against this position's collateral.
60
+ * `undefined` iff the market's oracle is undefined or reverts.
59
61
  */
60
62
  get maxBorrowAssets() {
61
63
  return this.market.getMaxBorrowAssets(this.collateral);
62
64
  }
63
65
  /**
64
66
  * The maximum additional amount of assets that can be borrowed against this position's collateral.
67
+ * `undefined` iff the market's oracle is undefined or reverts.
65
68
  */
66
69
  get maxBorrowableAssets() {
67
70
  return this.market.getMaxBorrowableAssets(this);
68
71
  }
69
72
  /**
70
73
  * The maximum amount of collateral that can be seized in exchange for the outstanding debt.
74
+ * `undefined` iff the market's oracle is undefined or reverts.
71
75
  */
72
76
  get seizableCollateral() {
73
77
  return this.market.getSeizableCollateral(this);
74
78
  }
75
79
  /**
76
80
  * The maximum amount of collateral that can be withdrawn.
81
+ * `undefined` iff the market's oracle is undefined or reverts.
77
82
  */
78
83
  get withdrawableCollateral() {
79
84
  return this.market.getWithdrawableCollateral(this);
80
85
  }
81
86
  /**
82
87
  * Whether this position is healthy.
88
+ * `undefined` iff the market's oracle is undefined or reverts.
83
89
  */
84
90
  get isHealthy() {
85
91
  return this.market.isHealthy(this);
@@ -91,15 +97,18 @@ class AccrualPosition extends Position {
91
97
  return this.market.getLiquidationPrice(this);
92
98
  }
93
99
  /**
94
- * The variation of the price of the collateral quoted in loan assets that would allow this position to be liquidated,
95
- * relative to the current collateral price (scaled by WAD).
100
+ * The price variation required for the position to reach its liquidation threshold (scaled by WAD).
101
+ * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
102
+ * `undefined` iff the market's oracle is undefined or reverts.
103
+ * Null if the position is not a borrow.
96
104
  */
97
- get priceVariationToLiquidation() {
98
- return this.market.getPriceVariationToLiquidation(this);
105
+ get priceVariationToLiquidationPrice() {
106
+ return this.market.getPriceVariationToLiquidationPrice(this);
99
107
  }
100
108
  /**
101
109
  * This position's Loan-To-Value (debt over collateral power, scaled by WAD).
102
110
  * If the collateral price is 0, LTV is `MaxUint256`.
111
+ * `undefined` iff the market's oracle is undefined or reverts.
103
112
  */
104
113
  get ltv() {
105
114
  return this.market.getLtv(this);
@@ -107,6 +116,7 @@ class AccrualPosition extends Position {
107
116
  /**
108
117
  * This position's health factor (collateral power over debt, scaled by WAD).
109
118
  * If the debt is 0, health factor is `MaxUint256`.
119
+ * `undefined` iff the market's oracle is undefined or reverts.
110
120
  */
111
121
  get healthFactor() {
112
122
  return this.market.getHealthFactor(this);
@@ -137,61 +147,53 @@ class AccrualPosition extends Position {
137
147
  supply(assets, shares, timestamp) {
138
148
  let { market } = this;
139
149
  ({ market, assets, shares } = market.supply(assets, shares, timestamp));
140
- this.supplyShares += shares;
141
- return {
142
- position: new AccrualPosition(this, market),
143
- assets,
144
- shares,
145
- };
150
+ const position = new AccrualPosition(this, market);
151
+ position.supplyShares += shares;
152
+ return { position, assets, shares };
146
153
  }
147
154
  withdraw(assets, shares, timestamp) {
148
155
  let { market } = this;
149
156
  ({ market, assets, shares } = market.withdraw(assets, shares, timestamp));
150
- this.supplyShares -= shares;
151
- if (this.supplyShares < 0n)
152
- throw new errors_1.BlueErrors.InsufficientPosition(this.user, this.marketId);
153
- return {
154
- position: new AccrualPosition(this, market),
155
- assets,
156
- shares,
157
- };
157
+ const position = new AccrualPosition(this, market);
158
+ position.supplyShares -= shares;
159
+ if (position.supplyShares < 0n)
160
+ throw new errors_js_1.BlueErrors.InsufficientPosition(position.user, position.marketId);
161
+ return { position, assets, shares };
158
162
  }
159
163
  supplyCollateral(assets) {
160
164
  this.collateral += assets;
161
- return new AccrualPosition(this, new market_1.Market(this.market));
165
+ return new AccrualPosition(this, new index_js_1.Market(this.market));
162
166
  }
163
167
  withdrawCollateral(assets, timestamp) {
164
- const market = this.market.accrueInterest(timestamp);
165
- this.collateral -= assets;
166
- if (this.collateral < 0n)
167
- throw new errors_1.BlueErrors.InsufficientPosition(this.user, this.marketId);
168
- if (!market.isHealthy(this))
169
- throw new errors_1.BlueErrors.InsufficientCollateral(this.user, this.marketId);
170
- return new AccrualPosition(this, market);
168
+ if (this.market.price == null)
169
+ throw new errors_js_1.BlueErrors.UnknownOraclePrice(this.marketId);
170
+ const position = this.accrueInterest(timestamp);
171
+ position.collateral -= assets;
172
+ if (position.collateral < 0n)
173
+ throw new errors_js_1.BlueErrors.InsufficientPosition(position.user, position.marketId);
174
+ if (!position.isHealthy)
175
+ throw new errors_js_1.BlueErrors.InsufficientCollateral(position.user, position.marketId);
176
+ return position;
171
177
  }
172
178
  borrow(assets, shares, timestamp) {
173
179
  let { market } = this;
180
+ if (market.price == null)
181
+ throw new errors_js_1.BlueErrors.UnknownOraclePrice(market.id);
174
182
  ({ market, assets, shares } = market.borrow(assets, shares, timestamp));
175
- this.borrowShares += shares;
176
- if (!market.isHealthy(this))
177
- throw new errors_1.BlueErrors.InsufficientCollateral(this.user, this.marketId);
178
- return {
179
- position: new AccrualPosition(this, market),
180
- assets,
181
- shares,
182
- };
183
+ const position = new AccrualPosition(this, market);
184
+ position.borrowShares += shares;
185
+ if (!position.isHealthy)
186
+ throw new errors_js_1.BlueErrors.InsufficientCollateral(this.user, this.marketId);
187
+ return { position, assets, shares };
183
188
  }
184
189
  repay(assets, shares, timestamp) {
185
190
  let { market } = this;
186
191
  ({ market, assets, shares } = market.repay(assets, shares, timestamp));
187
- this.borrowShares -= shares;
188
- if (this.borrowShares < 0n)
189
- throw new errors_1.BlueErrors.InsufficientPosition(this.user, this.marketId);
190
- return {
191
- position: new AccrualPosition(this, market),
192
- assets,
193
- shares,
194
- };
192
+ const position = new AccrualPosition(this, market);
193
+ position.borrowShares -= shares;
194
+ if (position.borrowShares < 0n)
195
+ throw new errors_js_1.BlueErrors.InsufficientPosition(position.user, position.marketId);
196
+ return { position, assets, shares };
195
197
  }
196
198
  getRepayCapacityLimit(loanTokenBalance) {
197
199
  return this.market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
@@ -1 +1 @@
1
- export * from "./Position";
1
+ export * from "./Position.js";
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./Position"), exports);
17
+ __exportStar(require("./Position.js"), exports);
@@ -1,11 +1,10 @@
1
- import { RoundingDirection } from "../maths";
2
- import { Address } from "../types";
3
- import { InputToken } from "./Token";
4
- import { WrappedToken } from "./WrappedToken";
1
+ import { type RoundingDirection } from "../math/index.js";
2
+ import type { Address, BigIntish } from "../types.js";
3
+ import type { IToken } from "./Token.js";
4
+ import { WrappedToken } from "./WrappedToken.js";
5
5
  export declare class ConstantWrappedToken extends WrappedToken {
6
- readonly underlying: Address;
7
- private readonly _underlyingDecimals;
8
- constructor(token: InputToken, underlying: Address, _underlyingDecimals?: number);
6
+ readonly underlyingDecimals: bigint;
7
+ constructor(token: IToken, underlying: Address, underlyingDecimals?: BigIntish);
9
8
  toWrappedExactAmountIn(unwrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
10
9
  /** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
11
10
  toWrappedExactAmountOut(wrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
@@ -1,15 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstantWrappedToken = void 0;
4
- const maths_1 = require("../maths");
5
- const WrappedToken_1 = require("./WrappedToken");
6
- class ConstantWrappedToken extends WrappedToken_1.WrappedToken {
7
- underlying;
8
- _underlyingDecimals;
9
- constructor(token, underlying, _underlyingDecimals = 18) {
4
+ const index_js_1 = require("../math/index.js");
5
+ const WrappedToken_js_1 = require("./WrappedToken.js");
6
+ class ConstantWrappedToken extends WrappedToken_js_1.WrappedToken {
7
+ underlyingDecimals;
8
+ constructor(token, underlying, underlyingDecimals = 0) {
10
9
  super(token, underlying);
11
- this.underlying = underlying;
12
- this._underlyingDecimals = _underlyingDecimals;
10
+ this.underlyingDecimals = BigInt(underlyingDecimals);
13
11
  }
14
12
  toWrappedExactAmountIn(unwrappedAmount, _slippage, rounding = "Down") {
15
13
  return super.toWrappedExactAmountIn(unwrappedAmount, 0n, rounding);
@@ -27,10 +25,10 @@ class ConstantWrappedToken extends WrappedToken_1.WrappedToken {
27
25
  return super.toUnwrappedExactAmountOut(unwrappedAmount, 0n, rounding);
28
26
  }
29
27
  _wrap(amount) {
30
- return maths_1.MathLib.mulDivDown(amount, 10n ** BigInt(this.decimals), 10n ** BigInt(this._underlyingDecimals));
28
+ return index_js_1.MathLib.mulDivDown(amount, 10n ** BigInt(this.decimals), 10n ** this.underlyingDecimals);
31
29
  }
32
30
  _unwrap(amount) {
33
- return maths_1.MathLib.mulDivDown(amount, 10n ** BigInt(this._underlyingDecimals), 10n ** BigInt(this.decimals));
31
+ return index_js_1.MathLib.mulDivDown(amount, 10n ** this.underlyingDecimals, 10n ** BigInt(this.decimals));
34
32
  }
35
33
  }
36
34
  exports.ConstantWrappedToken = ConstantWrappedToken;
@@ -1,11 +1,11 @@
1
- import { RoundingDirection } from "../maths";
2
- import { Address } from "../types";
3
- import { InputToken } from "./Token";
4
- import { WrappedToken } from "./WrappedToken";
1
+ import { type RoundingDirection } from "../math/index.js";
2
+ import type { Address } from "../types.js";
3
+ import type { IToken } from "./Token.js";
4
+ import { WrappedToken } from "./WrappedToken.js";
5
5
  export declare class ExchangeRateWrappedToken extends WrappedToken {
6
6
  readonly underlying: Address;
7
7
  wrappedTokenExchangeRate: bigint;
8
- constructor(token: InputToken, underlying: Address, wrappedTokenExchangeRate: bigint);
8
+ constructor(token: IToken, underlying: Address, wrappedTokenExchangeRate: bigint);
9
9
  protected _wrap(amount: bigint, rounding: RoundingDirection): bigint;
10
10
  protected _unwrap(amount: bigint, rounding: RoundingDirection): bigint;
11
11
  }
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ExchangeRateWrappedToken = void 0;
4
- const maths_1 = require("../maths");
5
- const WrappedToken_1 = require("./WrappedToken");
6
- class ExchangeRateWrappedToken extends WrappedToken_1.WrappedToken {
4
+ const index_js_1 = require("../math/index.js");
5
+ const WrappedToken_js_1 = require("./WrappedToken.js");
6
+ class ExchangeRateWrappedToken extends WrappedToken_js_1.WrappedToken {
7
7
  underlying;
8
8
  wrappedTokenExchangeRate;
9
9
  constructor(token, underlying, wrappedTokenExchangeRate) {
@@ -12,10 +12,10 @@ class ExchangeRateWrappedToken extends WrappedToken_1.WrappedToken {
12
12
  this.wrappedTokenExchangeRate = wrappedTokenExchangeRate;
13
13
  }
14
14
  _wrap(amount, rounding) {
15
- return maths_1.MathLib.wDiv(amount, this.wrappedTokenExchangeRate, rounding);
15
+ return index_js_1.MathLib.wDiv(amount, this.wrappedTokenExchangeRate, rounding);
16
16
  }
17
17
  _unwrap(amount, rounding) {
18
- return maths_1.MathLib.wMul(amount, this.wrappedTokenExchangeRate, rounding);
18
+ return index_js_1.MathLib.wMul(amount, this.wrappedTokenExchangeRate, rounding);
19
19
  }
20
20
  }
21
21
  exports.ExchangeRateWrappedToken = ExchangeRateWrappedToken;
@@ -1,46 +1,46 @@
1
- import { ChainId } from "../chain";
2
- import { RoundingDirection } from "../maths";
3
- import { Address } from "../types";
4
- export interface InputToken {
1
+ import { type ChainId } from "../chain.js";
2
+ import { type RoundingDirection } from "../math/index.js";
3
+ import type { Address, BigIntish } from "../types.js";
4
+ export interface IToken {
5
5
  address: Address;
6
- decimals: number;
7
- symbol: string;
8
6
  name?: string;
7
+ symbol?: string;
8
+ decimals?: BigIntish;
9
+ price?: BigIntish;
9
10
  }
10
- export declare class Token implements InputToken {
11
+ export declare class Token implements IToken {
11
12
  static native(chainId: ChainId): Token;
12
13
  /**
13
14
  * The token's address.
14
15
  */
15
16
  readonly address: Address;
16
17
  /**
17
- * The token's number of decimals.
18
+ * The token's name.
18
19
  */
19
- readonly decimals: number;
20
+ readonly name?: string;
20
21
  /**
21
22
  * The token's symbol.
22
23
  */
23
- readonly symbol: string;
24
+ readonly symbol?: string;
24
25
  /**
25
- * The name of the token (defaults to the symbol).
26
+ * The token's number of decimals. Defaults to 0.
26
27
  */
27
- readonly name: string;
28
- constructor({ address, decimals, symbol, name }: InputToken);
29
- }
30
- export declare class TokenWithPrice extends Token {
28
+ readonly decimals: number;
31
29
  /**
32
30
  * Price of the token in USD (scaled by WAD).
33
31
  */
34
32
  price?: bigint;
35
- constructor(token: InputToken, price?: bigint);
33
+ constructor({ address, name, symbol, decimals, price }: IToken);
36
34
  /**
37
35
  * Quotes an amount in USD (scaled by WAD) in this token.
36
+ * Returns `undefined` iff the token's price is undefined.
38
37
  * @param amount The amount of USD to quote.
39
38
  */
40
- fromUsd(amount: bigint, rounding?: RoundingDirection): bigint | null;
39
+ fromUsd(amount: bigint, rounding?: RoundingDirection): bigint | undefined;
41
40
  /**
42
41
  * Quotes an amount of tokens in USD (scaled by WAD).
42
+ * Returns `undefined` iff the token's price is undefined.
43
43
  * @param amount The amount of tokens to quote.
44
44
  */
45
- toUsd(amount: bigint, rounding?: RoundingDirection): bigint | null;
45
+ toUsd(amount: bigint, rounding?: RoundingDirection): bigint | undefined;
46
46
  }
@@ -1,64 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TokenWithPrice = exports.Token = void 0;
4
- const addresses_1 = require("../addresses");
5
- const chain_1 = require("../chain");
6
- const maths_1 = require("../maths");
3
+ exports.Token = void 0;
4
+ const addresses_js_1 = require("../addresses.js");
5
+ const chain_js_1 = require("../chain.js");
6
+ const index_js_1 = require("../math/index.js");
7
7
  class Token {
8
8
  static native(chainId) {
9
- const currency = chain_1.ChainUtils.CHAIN_METADATA[chainId].nativeCurrency;
10
- return new Token({ ...currency, address: addresses_1.NATIVE_ADDRESS });
9
+ const currency = chain_js_1.ChainUtils.CHAIN_METADATA[chainId].nativeCurrency;
10
+ return new Token({ ...currency, address: addresses_js_1.NATIVE_ADDRESS });
11
11
  }
12
12
  /**
13
13
  * The token's address.
14
14
  */
15
15
  address;
16
16
  /**
17
- * The token's number of decimals.
17
+ * The token's name.
18
18
  */
19
- decimals;
19
+ name;
20
20
  /**
21
21
  * The token's symbol.
22
22
  */
23
23
  symbol;
24
24
  /**
25
- * The name of the token (defaults to the symbol).
25
+ * The token's number of decimals. Defaults to 0.
26
26
  */
27
- name;
28
- constructor({ address, decimals, symbol, name }) {
29
- this.address = address;
30
- this.decimals = decimals;
31
- this.symbol = symbol;
32
- this.name = name ?? symbol;
33
- }
34
- }
35
- exports.Token = Token;
36
- class TokenWithPrice extends Token {
27
+ decimals;
37
28
  /**
38
29
  * Price of the token in USD (scaled by WAD).
39
30
  */
40
31
  price;
41
- constructor(token, price) {
42
- super(token);
43
- this.price = price;
32
+ constructor({ address, name, symbol, decimals = 0, price }) {
33
+ this.address = address;
34
+ this.name = name;
35
+ this.symbol = symbol;
36
+ this.decimals = Number(decimals);
37
+ if (price != null)
38
+ this.price = BigInt(price);
44
39
  }
45
40
  /**
46
41
  * Quotes an amount in USD (scaled by WAD) in this token.
42
+ * Returns `undefined` iff the token's price is undefined.
47
43
  * @param amount The amount of USD to quote.
48
44
  */
49
45
  fromUsd(amount, rounding = "Down") {
50
46
  if (this.price == null)
51
- return null;
52
- return maths_1.MathLib.mulDiv(amount, 10n ** BigInt(this.decimals), this.price, rounding);
47
+ return;
48
+ return index_js_1.MathLib.mulDiv(amount, 10n ** BigInt(this.decimals), this.price, rounding);
53
49
  }
54
50
  /**
55
51
  * Quotes an amount of tokens in USD (scaled by WAD).
52
+ * Returns `undefined` iff the token's price is undefined.
56
53
  * @param amount The amount of tokens to quote.
57
54
  */
58
55
  toUsd(amount, rounding = "Down") {
59
56
  if (this.price == null)
60
- return null;
61
- return maths_1.MathLib.mulDiv(amount, this.price, 10n ** BigInt(this.decimals), rounding);
57
+ return;
58
+ return index_js_1.MathLib.mulDiv(amount, this.price, 10n ** BigInt(this.decimals), rounding);
62
59
  }
63
60
  }
64
- exports.TokenWithPrice = TokenWithPrice;
61
+ exports.Token = Token;
@@ -1,14 +1,23 @@
1
- import { RoundingDirection } from "../maths";
2
- import { InputVaultConfig } from "../vault/VaultConfig";
3
- import { WrappedToken } from "./WrappedToken";
4
- export declare class VaultToken extends WrappedToken {
5
- decimalsOffset: bigint;
1
+ import type { RoundingDirection } from "../math/index.js";
2
+ import type { Address } from "../types.js";
3
+ import type { IVaultConfig } from "../vault/VaultConfig.js";
4
+ import { WrappedToken } from "./WrappedToken.js";
5
+ export interface IVaultToken {
6
6
  totalAssets: bigint;
7
7
  totalSupply: bigint;
8
- constructor(config: InputVaultConfig, { totalAssets, totalSupply }: {
9
- totalAssets: bigint;
10
- totalSupply: bigint;
11
- });
12
- protected _wrap(amount: bigint, rounding: RoundingDirection): bigint;
13
- protected _unwrap(amount: bigint, rounding: RoundingDirection): bigint;
8
+ }
9
+ export declare class VaultToken extends WrappedToken implements IVaultToken {
10
+ readonly asset: Address;
11
+ readonly decimalsOffset: bigint;
12
+ /**
13
+ * The ERC4626 vault's total supply of shares.
14
+ */
15
+ totalSupply: bigint;
16
+ /**
17
+ * The ERC4626 vault's total assets.
18
+ */
19
+ totalAssets: bigint;
20
+ constructor(config: IVaultConfig, { totalAssets, totalSupply }: IVaultToken);
21
+ protected _wrap(amount: bigint, rounding?: RoundingDirection): bigint;
22
+ protected _unwrap(amount: bigint, rounding?: RoundingDirection): bigint;
14
23
  }
@@ -1,23 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VaultToken = void 0;
4
- const VaultUtils_1 = require("../vault/VaultUtils");
5
- const WrappedToken_1 = require("./WrappedToken");
6
- class VaultToken extends WrappedToken_1.WrappedToken {
4
+ const VaultUtils_js_1 = require("../vault/VaultUtils.js");
5
+ const WrappedToken_js_1 = require("./WrappedToken.js");
6
+ class VaultToken extends WrappedToken_js_1.WrappedToken {
7
+ asset;
7
8
  decimalsOffset;
8
- totalAssets;
9
+ /**
10
+ * The ERC4626 vault's total supply of shares.
11
+ */
9
12
  totalSupply;
13
+ /**
14
+ * The ERC4626 vault's total assets.
15
+ */
16
+ totalAssets;
10
17
  constructor(config, { totalAssets, totalSupply }) {
11
18
  super(config, config.asset);
19
+ this.asset = config.asset;
12
20
  this.totalAssets = totalAssets;
13
21
  this.totalSupply = totalSupply;
14
- this.decimalsOffset = config.decimalsOffset;
22
+ this.decimalsOffset = BigInt(config.decimalsOffset);
15
23
  }
16
24
  _wrap(amount, rounding) {
17
- return VaultUtils_1.VaultUtils.toShares(amount, this, this, rounding);
25
+ return VaultUtils_js_1.VaultUtils.toShares(amount, this, rounding);
18
26
  }
19
27
  _unwrap(amount, rounding) {
20
- return VaultUtils_1.VaultUtils.toAssets(amount, this, this, rounding);
28
+ return VaultUtils_js_1.VaultUtils.toAssets(amount, this, rounding);
21
29
  }
22
30
  }
23
31
  exports.VaultToken = VaultToken;
@@ -1,9 +1,9 @@
1
- import { RoundingDirection } from "../maths";
2
- import { Address } from "../types";
3
- import { InputToken, Token } from "./Token";
1
+ import { type RoundingDirection } from "../math/index.js";
2
+ import type { Address } from "../types.js";
3
+ import { type IToken, Token } from "./Token.js";
4
4
  export declare abstract class WrappedToken extends Token {
5
5
  readonly underlying: Address;
6
- constructor(token: InputToken, underlying: Address);
6
+ constructor(token: IToken, underlying: Address);
7
7
  /** The expected amount when wrapping `unwrappedAmount` */
8
8
  toWrappedExactAmountIn(unwrappedAmount: bigint, slippage?: bigint, rounding?: RoundingDirection): bigint;
9
9
  /** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WrappedToken = void 0;
4
- const maths_1 = require("../maths");
5
- const Token_1 = require("./Token");
6
- class WrappedToken extends Token_1.Token {
4
+ const index_js_1 = require("../math/index.js");
5
+ const Token_js_1 = require("./Token.js");
6
+ class WrappedToken extends Token_js_1.Token {
7
7
  underlying;
8
8
  constructor(token, underlying) {
9
9
  super(token);
@@ -12,21 +12,21 @@ class WrappedToken extends Token_1.Token {
12
12
  /** The expected amount when wrapping `unwrappedAmount` */
13
13
  toWrappedExactAmountIn(unwrappedAmount, slippage = 0n, rounding = "Down") {
14
14
  const wrappedAmount = this._wrap(unwrappedAmount, rounding);
15
- return maths_1.MathLib.wMul(wrappedAmount, maths_1.MathLib.WAD - slippage, "Down");
15
+ return index_js_1.MathLib.wMul(wrappedAmount, index_js_1.MathLib.WAD - slippage, "Down");
16
16
  }
17
17
  /** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
18
18
  toWrappedExactAmountOut(wrappedAmount, slippage = 0n, rounding = "Up") {
19
- const wAmountTarget = maths_1.MathLib.wDiv(wrappedAmount, maths_1.MathLib.WAD - slippage, rounding);
19
+ const wAmountTarget = index_js_1.MathLib.wDiv(wrappedAmount, index_js_1.MathLib.WAD - slippage, rounding);
20
20
  return this._unwrap(wAmountTarget, rounding);
21
21
  }
22
22
  /** The expected amount when unwrapping `wrappedAmount` */
23
23
  toUnwrappedExactAmountIn(wrappedAmount, slippage = 0n, rounding = "Down") {
24
24
  const unwrappedAmount = this._unwrap(wrappedAmount, rounding);
25
- return maths_1.MathLib.wMul(unwrappedAmount, maths_1.MathLib.WAD - slippage, "Up");
25
+ return index_js_1.MathLib.wMul(unwrappedAmount, index_js_1.MathLib.WAD - slippage, "Up");
26
26
  }
27
27
  /** The amount of wrappedTokens that should be unwrapped to receive `unwrappedAmount` */
28
28
  toUnwrappedExactAmountOut(unwrappedAmount, slippage = 0n, rounding = "Up") {
29
- const unwrappedAmountToTarget = maths_1.MathLib.wDiv(unwrappedAmount, maths_1.MathLib.WAD - slippage, rounding);
29
+ const unwrappedAmountToTarget = index_js_1.MathLib.wDiv(unwrappedAmount, index_js_1.MathLib.WAD - slippage, rounding);
30
30
  return this._wrap(unwrappedAmountToTarget, rounding);
31
31
  }
32
32
  }
@@ -1,5 +1,5 @@
1
- export * from "./Token";
2
- export * from "./WrappedToken";
3
- export * from "./ConstantWrappedToken";
4
- export * from "./ExchangeRateWrappedToken";
5
- export * from "./VaultToken";
1
+ export * from "./Token.js";
2
+ export * from "./WrappedToken.js";
3
+ export * from "./ConstantWrappedToken.js";
4
+ export * from "./ExchangeRateWrappedToken.js";
5
+ export * from "./VaultToken.js";
@@ -14,8 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./Token"), exports);
18
- __exportStar(require("./WrappedToken"), exports);
19
- __exportStar(require("./ConstantWrappedToken"), exports);
20
- __exportStar(require("./ExchangeRateWrappedToken"), exports);
21
- __exportStar(require("./VaultToken"), exports);
17
+ __exportStar(require("./Token.js"), exports);
18
+ __exportStar(require("./WrappedToken.js"), exports);
19
+ __exportStar(require("./ConstantWrappedToken.js"), exports);
20
+ __exportStar(require("./ExchangeRateWrappedToken.js"), exports);
21
+ __exportStar(require("./VaultToken.js"), exports);
package/lib/types.d.ts CHANGED
@@ -1,14 +1,11 @@
1
- export type Hex64 = string & {
2
- __LENGTH__: 64;
3
- };
4
1
  /**
5
2
  * The address of a Contract, or an EOA
6
3
  */
7
- export type Address = string;
4
+ export type Address = `0x${string}`;
8
5
  /**
9
6
  * The id of a market used on the Blue contract
10
7
  */
11
- export type MarketId = `0x${Hex64}` & {
8
+ export type MarketId = `0x${string}` & {
12
9
  __TYPE__: "marketId";
13
10
  };
14
11
  export type BigIntish = bigint | string | number | boolean;
@@ -27,4 +24,4 @@ export type Loadable<T> = T | undefined;
27
24
  export type Failable<T> = T | null;
28
25
  export type Fetchable<T> = Failable<Loadable<T>>;
29
26
  export declare function isFetched<T>(v: Fetchable<T>): v is T;
30
- export declare const isMarketId: (value: any) => value is MarketId;
27
+ export declare const isMarketId: (value: unknown) => value is MarketId;
@@ -1,4 +1,4 @@
1
- import { Address } from "../types";
1
+ import type { Address } from "../types.js";
2
2
  export declare class User {
3
3
  /**
4
4
  * The user's address.
@@ -1 +1 @@
1
- export * from "./User";
1
+ export * from "./User.js";
package/lib/user/index.js CHANGED
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./User"), exports);
17
+ __exportStar(require("./User.js"), exports);