@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.
- package/LICENSE +21 -0
- package/README.md +46 -26
- package/lib/addresses.d.ts +59 -55
- package/lib/addresses.js +42 -43
- package/lib/chain.d.ts +1 -1
- package/lib/chain.js +3 -3
- package/lib/constants.d.ts +0 -9
- package/lib/constants.js +1 -10
- package/lib/errors.d.ts +7 -8
- package/lib/errors.js +21 -19
- package/lib/holding/AssetBalances.d.ts +3 -3
- package/lib/holding/Holding.d.ts +10 -5
- package/lib/holding/Holding.js +14 -4
- package/lib/holding/index.d.ts +2 -2
- package/lib/holding/index.js +2 -2
- package/lib/index.d.ts +12 -25
- package/lib/index.js +12 -38
- package/lib/market/Market.d.ts +48 -34
- package/lib/market/Market.js +76 -57
- package/lib/market/{MarketConfig.d.ts → MarketParams.d.ts} +8 -7
- package/lib/market/{MarketConfig.js → MarketParams.js} +20 -21
- package/lib/market/MarketUtils.d.ts +94 -42
- package/lib/market/MarketUtils.js +145 -67
- package/lib/market/index.d.ts +3 -3
- package/lib/market/index.js +3 -3
- package/lib/{maths → math}/AdaptiveCurveIrmLib.d.ts +1 -1
- package/lib/{maths → math}/AdaptiveCurveIrmLib.js +20 -20
- package/lib/{maths → math}/MathLib.d.ts +24 -42
- package/lib/{maths → math}/MathLib.js +41 -54
- package/lib/{maths → math}/SharesMath.d.ts +2 -2
- package/lib/{maths → math}/SharesMath.js +3 -3
- package/lib/math/index.d.ts +3 -0
- package/lib/{helpers/format → math}/index.js +3 -1
- package/lib/position/Position.d.ts +35 -25
- package/lib/position/Position.js +46 -44
- package/lib/position/index.d.ts +1 -1
- package/lib/position/index.js +1 -1
- package/lib/token/ConstantWrappedToken.d.ts +6 -7
- package/lib/token/ConstantWrappedToken.js +8 -10
- package/lib/token/ExchangeRateWrappedToken.d.ts +5 -5
- package/lib/token/ExchangeRateWrappedToken.js +5 -5
- package/lib/token/Token.d.ts +18 -18
- package/lib/token/Token.js +24 -27
- package/lib/token/VaultToken.d.ts +20 -11
- package/lib/token/VaultToken.js +15 -7
- package/lib/token/WrappedToken.d.ts +4 -4
- package/lib/token/WrappedToken.js +7 -7
- package/lib/token/index.d.ts +5 -5
- package/lib/token/index.js +5 -5
- package/lib/types.d.ts +3 -6
- package/lib/user/User.d.ts +1 -1
- package/lib/user/index.d.ts +1 -1
- package/lib/user/index.js +1 -1
- package/lib/vault/Vault.d.ts +22 -28
- package/lib/vault/Vault.js +43 -51
- package/lib/vault/VaultConfig.d.ts +6 -7
- package/lib/vault/VaultConfig.js +5 -5
- package/lib/vault/VaultMarketAllocation.d.ts +8 -8
- package/lib/vault/VaultMarketAllocation.js +3 -3
- package/lib/vault/VaultMarketConfig.d.ts +10 -10
- package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +6 -6
- package/lib/vault/VaultUser.d.ts +26 -0
- package/lib/vault/VaultUser.js +28 -0
- package/lib/vault/VaultUtils.d.ts +4 -6
- package/lib/vault/VaultUtils.js +6 -6
- package/lib/vault/index.d.ts +7 -6
- package/lib/vault/index.js +7 -6
- package/package.json +25 -56
- package/lib/chain.test.d.ts +0 -1
- package/lib/chain.test.js +0 -21
- package/lib/helpers/format/format.d.ts +0 -122
- package/lib/helpers/format/format.js +0 -286
- package/lib/helpers/format/format.test.d.ts +0 -1
- package/lib/helpers/format/format.test.js +0 -255
- package/lib/helpers/format/index.d.ts +0 -1
- package/lib/helpers/index.d.ts +0 -2
- package/lib/helpers/index.js +0 -18
- package/lib/helpers/locale.d.ts +0 -46
- package/lib/helpers/locale.js +0 -96
- package/lib/market/MarketUtils.test.d.ts +0 -1
- package/lib/market/MarketUtils.test.js +0 -38
- package/lib/maths/index.d.ts +0 -3
- package/lib/maths/index.js +0 -19
- package/lib/tests/mocks/markets.d.ts +0 -19
- package/lib/tests/mocks/markets.js +0 -121
package/lib/position/Position.js
CHANGED
|
@@ -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
|
|
5
|
-
const
|
|
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
|
|
95
|
-
*
|
|
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
|
|
98
|
-
return this.market.
|
|
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
|
|
141
|
-
|
|
142
|
-
|
|
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
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
|
165
|
+
return new AccrualPosition(this, new index_js_1.Market(this.market));
|
|
162
166
|
}
|
|
163
167
|
withdrawCollateral(assets, timestamp) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (
|
|
169
|
-
throw new
|
|
170
|
-
|
|
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
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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);
|
package/lib/position/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./Position";
|
|
1
|
+
export * from "./Position.js";
|
package/lib/position/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("./Position"), exports);
|
|
17
|
+
__exportStar(require("./Position.js"), exports);
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { RoundingDirection } from "../
|
|
2
|
-
import { Address } from "../types";
|
|
3
|
-
import {
|
|
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
|
|
7
|
-
|
|
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
|
|
5
|
-
const
|
|
6
|
-
class ConstantWrappedToken extends
|
|
7
|
-
|
|
8
|
-
|
|
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.
|
|
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
|
|
28
|
+
return index_js_1.MathLib.mulDivDown(amount, 10n ** BigInt(this.decimals), 10n ** this.underlyingDecimals);
|
|
31
29
|
}
|
|
32
30
|
_unwrap(amount) {
|
|
33
|
-
return
|
|
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 "../
|
|
2
|
-
import { Address } from "../types";
|
|
3
|
-
import {
|
|
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:
|
|
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
|
|
5
|
-
const
|
|
6
|
-
class ExchangeRateWrappedToken extends
|
|
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
|
|
15
|
+
return index_js_1.MathLib.wDiv(amount, this.wrappedTokenExchangeRate, rounding);
|
|
16
16
|
}
|
|
17
17
|
_unwrap(amount, rounding) {
|
|
18
|
-
return
|
|
18
|
+
return index_js_1.MathLib.wMul(amount, this.wrappedTokenExchangeRate, rounding);
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
exports.ExchangeRateWrappedToken = ExchangeRateWrappedToken;
|
package/lib/token/Token.d.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import { ChainId } from "../chain";
|
|
2
|
-
import { RoundingDirection } from "../
|
|
3
|
-
import { Address } from "../types";
|
|
4
|
-
export interface
|
|
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
|
|
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
|
|
18
|
+
* The token's name.
|
|
18
19
|
*/
|
|
19
|
-
readonly
|
|
20
|
+
readonly name?: string;
|
|
20
21
|
/**
|
|
21
22
|
* The token's symbol.
|
|
22
23
|
*/
|
|
23
|
-
readonly symbol
|
|
24
|
+
readonly symbol?: string;
|
|
24
25
|
/**
|
|
25
|
-
* The
|
|
26
|
+
* The token's number of decimals. Defaults to 0.
|
|
26
27
|
*/
|
|
27
|
-
readonly
|
|
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(
|
|
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 |
|
|
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 |
|
|
45
|
+
toUsd(amount: bigint, rounding?: RoundingDirection): bigint | undefined;
|
|
46
46
|
}
|
package/lib/token/Token.js
CHANGED
|
@@ -1,64 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
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 =
|
|
10
|
-
return new Token({ ...currency, 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
|
|
17
|
+
* The token's name.
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
name;
|
|
20
20
|
/**
|
|
21
21
|
* The token's symbol.
|
|
22
22
|
*/
|
|
23
23
|
symbol;
|
|
24
24
|
/**
|
|
25
|
-
* The
|
|
25
|
+
* The token's number of decimals. Defaults to 0.
|
|
26
26
|
*/
|
|
27
|
-
|
|
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(
|
|
42
|
-
|
|
43
|
-
this.
|
|
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
|
|
52
|
-
return
|
|
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
|
|
61
|
-
return
|
|
57
|
+
return;
|
|
58
|
+
return index_js_1.MathLib.mulDiv(amount, this.price, 10n ** BigInt(this.decimals), rounding);
|
|
62
59
|
}
|
|
63
60
|
}
|
|
64
|
-
exports.
|
|
61
|
+
exports.Token = Token;
|
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
import { RoundingDirection } from "../
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
}
|
package/lib/token/VaultToken.js
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.VaultToken = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
class VaultToken extends
|
|
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
|
-
|
|
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
|
|
25
|
+
return VaultUtils_js_1.VaultUtils.toShares(amount, this, rounding);
|
|
18
26
|
}
|
|
19
27
|
_unwrap(amount, rounding) {
|
|
20
|
-
return
|
|
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 "../
|
|
2
|
-
import { Address } from "../types";
|
|
3
|
-
import {
|
|
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:
|
|
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
|
|
5
|
-
const
|
|
6
|
-
class WrappedToken extends
|
|
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
|
|
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 =
|
|
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
|
|
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 =
|
|
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
|
}
|
package/lib/token/index.d.ts
CHANGED
|
@@ -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";
|
package/lib/token/index.js
CHANGED
|
@@ -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${
|
|
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:
|
|
27
|
+
export declare const isMarketId: (value: unknown) => value is MarketId;
|
package/lib/user/User.d.ts
CHANGED
package/lib/user/index.d.ts
CHANGED
|
@@ -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);
|