@morpho-org/blue-sdk 2.0.0-next.1 → 2.0.0-next.4
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 +10 -9
- package/lib/addresses.d.ts +117 -0
- package/lib/addresses.js +156 -0
- package/lib/chain.d.ts +29 -0
- package/lib/chain.js +286 -0
- package/lib/constants.d.ts +29 -0
- package/lib/constants.js +30 -0
- package/lib/errors.d.ts +52 -0
- package/lib/errors.js +97 -0
- package/lib/holding/AssetBalances.d.ts +48 -0
- package/lib/holding/AssetBalances.js +38 -0
- package/lib/holding/Holding.d.ts +64 -0
- package/lib/holding/Holding.js +65 -0
- package/lib/holding/index.d.ts +2 -0
- package/lib/holding/index.js +2 -0
- package/lib/index.js +12 -0
- package/lib/market/Market.d.ts +332 -0
- package/lib/market/Market.js +459 -0
- package/lib/market/MarketConfig.d.ts +52 -0
- package/lib/market/MarketConfig.js +73 -0
- package/lib/market/MarketUtils.d.ts +233 -0
- package/lib/market/MarketUtils.js +257 -0
- package/lib/market/index.d.ts +3 -0
- package/lib/market/index.js +3 -0
- package/lib/math/AdaptiveCurveIrmLib.d.ts +39 -0
- package/lib/math/AdaptiveCurveIrmLib.js +131 -0
- package/lib/math/MathLib.d.ts +111 -0
- package/lib/math/MathLib.js +185 -0
- package/lib/math/SharesMath.d.ts +12 -0
- package/lib/math/SharesMath.js +18 -0
- package/lib/math/index.d.ts +3 -0
- package/lib/math/index.js +3 -0
- package/lib/position/Position.d.ts +127 -0
- package/lib/position/Position.js +199 -0
- package/lib/position/index.d.ts +1 -0
- package/lib/position/index.js +1 -0
- package/lib/token/ConstantWrappedToken.d.ts +17 -0
- package/lib/token/ConstantWrappedToken.js +30 -0
- package/lib/token/ExchangeRateWrappedToken.d.ts +11 -0
- package/lib/token/ExchangeRateWrappedToken.js +17 -0
- package/lib/token/Token.d.ts +46 -0
- package/lib/token/Token.js +59 -0
- package/lib/token/VaultToken.d.ts +23 -0
- package/lib/token/VaultToken.js +27 -0
- package/lib/token/WrappedToken.d.ts +17 -0
- package/lib/token/WrappedToken.js +29 -0
- package/lib/token/index.d.ts +5 -0
- package/lib/token/index.js +5 -0
- package/lib/types.d.ts +27 -0
- package/lib/types.js +17 -0
- package/lib/user/User.d.ts +20 -0
- package/lib/user/User.js +19 -0
- package/lib/user/index.d.ts +1 -0
- package/lib/user/index.js +1 -0
- package/lib/vault/Vault.d.ts +152 -0
- package/lib/vault/Vault.js +221 -0
- package/lib/vault/VaultConfig.d.ts +22 -0
- package/lib/vault/VaultConfig.js +28 -0
- package/lib/vault/VaultMarketAllocation.d.ts +20 -0
- package/lib/vault/VaultMarketAllocation.js +26 -0
- package/lib/vault/VaultMarketConfig.d.ts +43 -0
- package/lib/vault/VaultMarketConfig.js +39 -0
- package/lib/vault/VaultMarketPublicAllocatorConfig.d.ts +29 -0
- package/lib/vault/VaultMarketPublicAllocatorConfig.js +24 -0
- package/lib/vault/VaultUser.d.ts +26 -0
- package/lib/vault/VaultUser.js +24 -0
- package/lib/vault/VaultUtils.d.ts +16 -0
- package/lib/vault/VaultUtils.js +17 -0
- package/lib/vault/index.d.ts +7 -0
- package/lib/vault/index.js +7 -0
- package/package.json +30 -34
- /package/{src/index.ts → lib/index.d.ts} +0 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Time } from "@morpho-org/morpho-ts";
|
|
2
|
+
import type { BigIntish } from "../types.js";
|
|
3
|
+
export type RoundingDirection = "Up" | "Down";
|
|
4
|
+
/**
|
|
5
|
+
* Library to manage fixed-point arithmetic.
|
|
6
|
+
* https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
|
|
7
|
+
*/
|
|
8
|
+
export declare namespace MathLib {
|
|
9
|
+
const WAD = 1000000000000000000n;
|
|
10
|
+
const MAX_UINT_256: bigint;
|
|
11
|
+
const MAX_UINT_160: bigint;
|
|
12
|
+
const MAX_UINT_128: bigint;
|
|
13
|
+
const MAX_UINT_48: bigint;
|
|
14
|
+
function maxUint(nBits: number): bigint;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the absolute value of a number
|
|
17
|
+
* @param a The number
|
|
18
|
+
*/
|
|
19
|
+
function abs(a: BigIntish): bigint;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the smallest number given as param
|
|
22
|
+
* @param x The first number
|
|
23
|
+
* @param y The second number
|
|
24
|
+
*/
|
|
25
|
+
function min(...xs: BigIntish[]): bigint;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the greatest number given as param
|
|
28
|
+
* @param x The first number
|
|
29
|
+
* @param y The second number
|
|
30
|
+
*/
|
|
31
|
+
function max(...xs: BigIntish[]): bigint;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the subtraction of b from a, floored to zero if negative
|
|
34
|
+
* @param x The first number
|
|
35
|
+
* @param y The second number
|
|
36
|
+
*/
|
|
37
|
+
function zeroFloorSub(x: BigIntish, y: BigIntish): bigint;
|
|
38
|
+
/**
|
|
39
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded down
|
|
40
|
+
* @param x The first number
|
|
41
|
+
* @param y The second number
|
|
42
|
+
*/
|
|
43
|
+
function wMulDown(x: BigIntish, y: BigIntish): bigint;
|
|
44
|
+
/**
|
|
45
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
46
|
+
* @param x The first number
|
|
47
|
+
* @param y The second number
|
|
48
|
+
*/
|
|
49
|
+
function wMulUp(x: BigIntish, y: BigIntish): bigint;
|
|
50
|
+
/**
|
|
51
|
+
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
52
|
+
* @param x The first number
|
|
53
|
+
* @param y The second number
|
|
54
|
+
*/
|
|
55
|
+
function wMul(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
|
|
56
|
+
/**
|
|
57
|
+
* Perform the WAD-based division of 2 numbers, rounded down
|
|
58
|
+
* @param x The first number
|
|
59
|
+
* @param y The second number
|
|
60
|
+
*/
|
|
61
|
+
function wDivDown(x: BigIntish, y: BigIntish): bigint;
|
|
62
|
+
/**
|
|
63
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
64
|
+
* @param x The first number
|
|
65
|
+
* @param y The second number
|
|
66
|
+
*/
|
|
67
|
+
function wDivUp(x: BigIntish, y: BigIntish): bigint;
|
|
68
|
+
/**
|
|
69
|
+
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
70
|
+
* @param x The first number
|
|
71
|
+
* @param y The second number
|
|
72
|
+
*/
|
|
73
|
+
function wDiv(x: BigIntish, y: BigIntish, rounding: RoundingDirection): bigint;
|
|
74
|
+
/**
|
|
75
|
+
* Multiply two numbers and divide by a denominator, rounding down the result
|
|
76
|
+
* @param x The first number
|
|
77
|
+
* @param y The second number
|
|
78
|
+
* @param denominator The denominator
|
|
79
|
+
*/
|
|
80
|
+
function mulDivDown(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
|
|
81
|
+
/**
|
|
82
|
+
* Multiply two numbers and divide by a denominator, rounding up the result
|
|
83
|
+
* @param x The first number
|
|
84
|
+
* @param y The second number
|
|
85
|
+
* @param denominator The denominator
|
|
86
|
+
*/
|
|
87
|
+
function mulDivUp(x: BigIntish, y: BigIntish, denominator: BigIntish): bigint;
|
|
88
|
+
function mulDiv(x: BigIntish, y: BigIntish, denominator: BigIntish, rounding: RoundingDirection): bigint;
|
|
89
|
+
/**
|
|
90
|
+
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
91
|
+
* to approximate a continuous compound interest rate.
|
|
92
|
+
*
|
|
93
|
+
* @param x The base of the exponent
|
|
94
|
+
* @param n The exponent
|
|
95
|
+
*/
|
|
96
|
+
function wTaylorCompounded(x: BigIntish, n: BigIntish): bigint;
|
|
97
|
+
/**
|
|
98
|
+
* Converts an rate to compounded apy
|
|
99
|
+
*
|
|
100
|
+
* @param rate The rate to convert (in WAD)
|
|
101
|
+
* @param period The compounding basis
|
|
102
|
+
*/
|
|
103
|
+
function rateToApy(rate: BigIntish, period: Time.PeriodLike): number;
|
|
104
|
+
/**
|
|
105
|
+
* Converts an apr to compounded apy
|
|
106
|
+
*
|
|
107
|
+
* @param apr The apr to convert (in WAD)
|
|
108
|
+
* @param compounding The compounding basis
|
|
109
|
+
*/
|
|
110
|
+
function aprToApy(apr: BigIntish, compounding: Time.PeriodLike): number;
|
|
111
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Time, format } from "@morpho-org/morpho-ts";
|
|
2
|
+
/**
|
|
3
|
+
* Library to manage fixed-point arithmetic.
|
|
4
|
+
* https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/MathLib.sol
|
|
5
|
+
*/
|
|
6
|
+
export var MathLib;
|
|
7
|
+
(function (MathLib) {
|
|
8
|
+
MathLib.WAD = 1000000000000000000n;
|
|
9
|
+
MathLib.MAX_UINT_256 = maxUint(256);
|
|
10
|
+
MathLib.MAX_UINT_160 = maxUint(160);
|
|
11
|
+
MathLib.MAX_UINT_128 = maxUint(128);
|
|
12
|
+
MathLib.MAX_UINT_48 = maxUint(48);
|
|
13
|
+
function maxUint(nBits) {
|
|
14
|
+
if (nBits % 4 !== 0)
|
|
15
|
+
throw new Error(`Invalid number of bits: ${nBits}`);
|
|
16
|
+
return BigInt(`0x${"f".repeat(nBits / 4)}`);
|
|
17
|
+
}
|
|
18
|
+
MathLib.maxUint = maxUint;
|
|
19
|
+
/**
|
|
20
|
+
* Returns the absolute value of a number
|
|
21
|
+
* @param a The number
|
|
22
|
+
*/
|
|
23
|
+
function abs(a) {
|
|
24
|
+
a = BigInt(a);
|
|
25
|
+
return a >= 0 ? a : -a;
|
|
26
|
+
}
|
|
27
|
+
MathLib.abs = abs;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the smallest number given as param
|
|
30
|
+
* @param x The first number
|
|
31
|
+
* @param y The second number
|
|
32
|
+
*/
|
|
33
|
+
function min(...xs) {
|
|
34
|
+
return xs.map(BigInt).reduce((x, y) => (x <= y ? x : y));
|
|
35
|
+
}
|
|
36
|
+
MathLib.min = min;
|
|
37
|
+
/**
|
|
38
|
+
* Returns the greatest number given as param
|
|
39
|
+
* @param x The first number
|
|
40
|
+
* @param y The second number
|
|
41
|
+
*/
|
|
42
|
+
function max(...xs) {
|
|
43
|
+
return xs.map(BigInt).reduce((x, y) => (x <= y ? y : x));
|
|
44
|
+
}
|
|
45
|
+
MathLib.max = max;
|
|
46
|
+
/**
|
|
47
|
+
* Returns the subtraction of b from a, floored to zero if negative
|
|
48
|
+
* @param x The first number
|
|
49
|
+
* @param y The second number
|
|
50
|
+
*/
|
|
51
|
+
function zeroFloorSub(x, y) {
|
|
52
|
+
x = BigInt(x);
|
|
53
|
+
y = BigInt(y);
|
|
54
|
+
return x <= y ? 0n : x - y;
|
|
55
|
+
}
|
|
56
|
+
MathLib.zeroFloorSub = zeroFloorSub;
|
|
57
|
+
/**
|
|
58
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded down
|
|
59
|
+
* @param x The first number
|
|
60
|
+
* @param y The second number
|
|
61
|
+
*/
|
|
62
|
+
function wMulDown(x, y) {
|
|
63
|
+
return MathLib.wMul(x, y, "Down");
|
|
64
|
+
}
|
|
65
|
+
MathLib.wMulDown = wMulDown;
|
|
66
|
+
/**
|
|
67
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
68
|
+
* @param x The first number
|
|
69
|
+
* @param y The second number
|
|
70
|
+
*/
|
|
71
|
+
function wMulUp(x, y) {
|
|
72
|
+
return MathLib.wMul(x, y, "Up");
|
|
73
|
+
}
|
|
74
|
+
MathLib.wMulUp = wMulUp;
|
|
75
|
+
/**
|
|
76
|
+
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
77
|
+
* @param x The first number
|
|
78
|
+
* @param y The second number
|
|
79
|
+
*/
|
|
80
|
+
function wMul(x, y, rounding) {
|
|
81
|
+
return MathLib.mulDiv(x, y, MathLib.WAD, rounding);
|
|
82
|
+
}
|
|
83
|
+
MathLib.wMul = wMul;
|
|
84
|
+
/**
|
|
85
|
+
* Perform the WAD-based division of 2 numbers, rounded down
|
|
86
|
+
* @param x The first number
|
|
87
|
+
* @param y The second number
|
|
88
|
+
*/
|
|
89
|
+
function wDivDown(x, y) {
|
|
90
|
+
return MathLib.wDiv(x, y, "Down");
|
|
91
|
+
}
|
|
92
|
+
MathLib.wDivDown = wDivDown;
|
|
93
|
+
/**
|
|
94
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
95
|
+
* @param x The first number
|
|
96
|
+
* @param y The second number
|
|
97
|
+
*/
|
|
98
|
+
function wDivUp(x, y) {
|
|
99
|
+
return MathLib.wDiv(x, y, "Up");
|
|
100
|
+
}
|
|
101
|
+
MathLib.wDivUp = wDivUp;
|
|
102
|
+
/**
|
|
103
|
+
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
104
|
+
* @param x The first number
|
|
105
|
+
* @param y The second number
|
|
106
|
+
*/
|
|
107
|
+
function wDiv(x, y, rounding) {
|
|
108
|
+
return MathLib.mulDiv(x, MathLib.WAD, y, rounding);
|
|
109
|
+
}
|
|
110
|
+
MathLib.wDiv = wDiv;
|
|
111
|
+
/**
|
|
112
|
+
* Multiply two numbers and divide by a denominator, rounding down the result
|
|
113
|
+
* @param x The first number
|
|
114
|
+
* @param y The second number
|
|
115
|
+
* @param denominator The denominator
|
|
116
|
+
*/
|
|
117
|
+
function mulDivDown(x, y, denominator) {
|
|
118
|
+
x = BigInt(x);
|
|
119
|
+
y = BigInt(y);
|
|
120
|
+
denominator = BigInt(denominator);
|
|
121
|
+
if (denominator === 0n)
|
|
122
|
+
throw Error("MathLib: DIVISION_BY_ZERO");
|
|
123
|
+
return (x * y) / denominator;
|
|
124
|
+
}
|
|
125
|
+
MathLib.mulDivDown = mulDivDown;
|
|
126
|
+
/**
|
|
127
|
+
* Multiply two numbers and divide by a denominator, rounding up the result
|
|
128
|
+
* @param x The first number
|
|
129
|
+
* @param y The second number
|
|
130
|
+
* @param denominator The denominator
|
|
131
|
+
*/
|
|
132
|
+
function mulDivUp(x, y, denominator) {
|
|
133
|
+
x = BigInt(x);
|
|
134
|
+
y = BigInt(y);
|
|
135
|
+
denominator = BigInt(denominator);
|
|
136
|
+
if (denominator === 0n)
|
|
137
|
+
throw Error("MathLib: DIVISION_BY_ZERO");
|
|
138
|
+
const roundup = (x * y) % denominator > 0 ? 1n : 0n;
|
|
139
|
+
return (x * y) / denominator + roundup;
|
|
140
|
+
}
|
|
141
|
+
MathLib.mulDivUp = mulDivUp;
|
|
142
|
+
function mulDiv(x, y, denominator, rounding) {
|
|
143
|
+
return MathLib[`mulDiv${rounding}`](x, y, denominator);
|
|
144
|
+
}
|
|
145
|
+
MathLib.mulDiv = mulDiv;
|
|
146
|
+
/**
|
|
147
|
+
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
148
|
+
* to approximate a continuous compound interest rate.
|
|
149
|
+
*
|
|
150
|
+
* @param x The base of the exponent
|
|
151
|
+
* @param n The exponent
|
|
152
|
+
*/
|
|
153
|
+
function wTaylorCompounded(x, n) {
|
|
154
|
+
const firstTerm = BigInt(x) * BigInt(n);
|
|
155
|
+
const secondTerm = MathLib.mulDivDown(firstTerm, firstTerm, 2n * MathLib.WAD);
|
|
156
|
+
const thirdTerm = MathLib.mulDivDown(secondTerm, firstTerm, 3n * MathLib.WAD);
|
|
157
|
+
return firstTerm + secondTerm + thirdTerm;
|
|
158
|
+
}
|
|
159
|
+
MathLib.wTaylorCompounded = wTaylorCompounded;
|
|
160
|
+
/**
|
|
161
|
+
* Converts an rate to compounded apy
|
|
162
|
+
*
|
|
163
|
+
* @param rate The rate to convert (in WAD)
|
|
164
|
+
* @param period The compounding basis
|
|
165
|
+
*/
|
|
166
|
+
function rateToApy(rate, period) {
|
|
167
|
+
const { unit, duration } = Time.toPeriod(period);
|
|
168
|
+
const factor = Time[unit].from.y(1) / duration;
|
|
169
|
+
return ((1 + Number(format.number.locale("en").of(BigInt(rate), 18))) ** factor -
|
|
170
|
+
1);
|
|
171
|
+
}
|
|
172
|
+
MathLib.rateToApy = rateToApy;
|
|
173
|
+
/**
|
|
174
|
+
* Converts an apr to compounded apy
|
|
175
|
+
*
|
|
176
|
+
* @param apr The apr to convert (in WAD)
|
|
177
|
+
* @param compounding The compounding basis
|
|
178
|
+
*/
|
|
179
|
+
function aprToApy(apr, compounding) {
|
|
180
|
+
const { unit, duration } = Time.toPeriod(compounding);
|
|
181
|
+
const rate = (BigInt(apr) * BigInt(duration)) / Time[unit].from.y(1n);
|
|
182
|
+
return rateToApy(rate, compounding);
|
|
183
|
+
}
|
|
184
|
+
MathLib.aprToApy = aprToApy;
|
|
185
|
+
})(MathLib || (MathLib = {}));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BigIntish } from "../types.js";
|
|
2
|
+
import { type RoundingDirection } from "./MathLib.js";
|
|
3
|
+
/**
|
|
4
|
+
* JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
|
|
5
|
+
* & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
|
|
6
|
+
*/
|
|
7
|
+
export declare namespace SharesMath {
|
|
8
|
+
const VIRTUAL_SHARES = 1000000n;
|
|
9
|
+
const VIRTUAL_ASSETS = 1n;
|
|
10
|
+
function toAssets(shares: BigIntish, totalAssets: BigIntish, totalShares: BigIntish, rounding: RoundingDirection): bigint;
|
|
11
|
+
function toShares(assets: BigIntish, totalAssets: BigIntish, totalShares: BigIntish, rounding: RoundingDirection): bigint;
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { MathLib } from "./MathLib.js";
|
|
2
|
+
/**
|
|
3
|
+
* JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
|
|
4
|
+
* & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
|
|
5
|
+
*/
|
|
6
|
+
export var SharesMath;
|
|
7
|
+
(function (SharesMath) {
|
|
8
|
+
SharesMath.VIRTUAL_SHARES = 1000000n;
|
|
9
|
+
SharesMath.VIRTUAL_ASSETS = 1n;
|
|
10
|
+
function toAssets(shares, totalAssets, totalShares, rounding) {
|
|
11
|
+
return MathLib.mulDiv(shares, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
|
|
12
|
+
}
|
|
13
|
+
SharesMath.toAssets = toAssets;
|
|
14
|
+
function toShares(assets, totalAssets, totalShares, rounding) {
|
|
15
|
+
return MathLib.mulDiv(assets, BigInt(totalShares) + SharesMath.VIRTUAL_SHARES, BigInt(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
|
|
16
|
+
}
|
|
17
|
+
SharesMath.toShares = toShares;
|
|
18
|
+
})(SharesMath || (SharesMath = {}));
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Market, type MaxBorrowOptions, type MaxWithdrawCollateralOptions } from "../market/index.js";
|
|
2
|
+
import type { Address, BigIntish, MarketId } from "../types.js";
|
|
3
|
+
export interface InputPosition {
|
|
4
|
+
user: Address;
|
|
5
|
+
marketId: MarketId;
|
|
6
|
+
supplyShares: bigint;
|
|
7
|
+
borrowShares: bigint;
|
|
8
|
+
collateral: bigint;
|
|
9
|
+
}
|
|
10
|
+
export declare class Position implements InputPosition {
|
|
11
|
+
/**
|
|
12
|
+
* The user holding this position.
|
|
13
|
+
*/
|
|
14
|
+
readonly user: Address;
|
|
15
|
+
/**
|
|
16
|
+
* The id of the market on which this position is held.
|
|
17
|
+
*/
|
|
18
|
+
readonly marketId: MarketId;
|
|
19
|
+
/**
|
|
20
|
+
* The amount of supply shares held with this position.
|
|
21
|
+
*/
|
|
22
|
+
supplyShares: bigint;
|
|
23
|
+
/**
|
|
24
|
+
* The amount of borrow shares held with this position.
|
|
25
|
+
*/
|
|
26
|
+
borrowShares: bigint;
|
|
27
|
+
/**
|
|
28
|
+
* The amount of collateral assets held with this position.
|
|
29
|
+
*/
|
|
30
|
+
collateral: bigint;
|
|
31
|
+
constructor({ user, marketId, supplyShares, borrowShares, collateral, }: InputPosition);
|
|
32
|
+
}
|
|
33
|
+
export interface InputAccrualPosition extends Omit<InputPosition, "marketId"> {
|
|
34
|
+
}
|
|
35
|
+
export declare class AccrualPosition extends Position implements InputAccrualPosition {
|
|
36
|
+
/**
|
|
37
|
+
* The market on which this position is held.
|
|
38
|
+
*/
|
|
39
|
+
readonly market: Market;
|
|
40
|
+
constructor(position: InputAccrualPosition, market: Market);
|
|
41
|
+
get supplyAssets(): bigint;
|
|
42
|
+
get borrowAssets(): bigint;
|
|
43
|
+
/**
|
|
44
|
+
* The value of this position's collateral quoted in loan assets.
|
|
45
|
+
*/
|
|
46
|
+
get collateralValue(): bigint;
|
|
47
|
+
/**
|
|
48
|
+
* The maximum amount of loan assets that can be borrowed against this position's collateral.
|
|
49
|
+
*/
|
|
50
|
+
get maxBorrowAssets(): bigint;
|
|
51
|
+
/**
|
|
52
|
+
* The maximum additional amount of assets that can be borrowed against this position's collateral.
|
|
53
|
+
*/
|
|
54
|
+
get maxBorrowableAssets(): bigint;
|
|
55
|
+
/**
|
|
56
|
+
* The maximum amount of collateral that can be seized in exchange for the outstanding debt.
|
|
57
|
+
*/
|
|
58
|
+
get seizableCollateral(): bigint;
|
|
59
|
+
/**
|
|
60
|
+
* The maximum amount of collateral that can be withdrawn.
|
|
61
|
+
*/
|
|
62
|
+
get withdrawableCollateral(): bigint;
|
|
63
|
+
/**
|
|
64
|
+
* Whether this position is healthy.
|
|
65
|
+
*/
|
|
66
|
+
get isHealthy(): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* The price of the collateral quoted in loan assets that would allow this position to be liquidated.
|
|
69
|
+
*/
|
|
70
|
+
get liquidationPrice(): bigint | null;
|
|
71
|
+
/**
|
|
72
|
+
* The price variation required for the position to reach its liquidation threshold (scaled by WAD).
|
|
73
|
+
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
74
|
+
* Returns null if the position is not a borrow.
|
|
75
|
+
*/
|
|
76
|
+
get priceVariationToLiquidationPrice(): bigint | null;
|
|
77
|
+
/**
|
|
78
|
+
* This position's Loan-To-Value (debt over collateral power, scaled by WAD).
|
|
79
|
+
* If the collateral price is 0, LTV is `MaxUint256`.
|
|
80
|
+
*/
|
|
81
|
+
get ltv(): bigint | null;
|
|
82
|
+
/**
|
|
83
|
+
* This position's health factor (collateral power over debt, scaled by WAD).
|
|
84
|
+
* If the debt is 0, health factor is `MaxUint256`.
|
|
85
|
+
*/
|
|
86
|
+
get healthFactor(): bigint | null;
|
|
87
|
+
/**
|
|
88
|
+
* The percentage of this position's borrow power currently used (scaled by WAD).
|
|
89
|
+
* If the collateral price is 0, usage is `MaxUint256`.
|
|
90
|
+
*/
|
|
91
|
+
get borrowCapacityUsage(): bigint | null;
|
|
92
|
+
get borrowCapacityLimit(): import("../market/Market.js").CapacityLimit;
|
|
93
|
+
get withdrawCapacityLimit(): import("../market/Market.js").CapacityLimit;
|
|
94
|
+
get withdrawCollateralCapacityLimit(): import("../market/Market.js").CapacityLimit;
|
|
95
|
+
/**
|
|
96
|
+
* Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
|
|
97
|
+
* @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
|
|
98
|
+
*/
|
|
99
|
+
accrueInterest(timestamp?: BigIntish): AccrualPosition;
|
|
100
|
+
supply(assets: bigint, shares: bigint, timestamp?: BigIntish): {
|
|
101
|
+
position: AccrualPosition;
|
|
102
|
+
assets: bigint;
|
|
103
|
+
shares: bigint;
|
|
104
|
+
};
|
|
105
|
+
withdraw(assets: bigint, shares: bigint, timestamp?: BigIntish): {
|
|
106
|
+
position: AccrualPosition;
|
|
107
|
+
assets: bigint;
|
|
108
|
+
shares: bigint;
|
|
109
|
+
};
|
|
110
|
+
supplyCollateral(assets: bigint): AccrualPosition;
|
|
111
|
+
withdrawCollateral(assets: bigint, timestamp?: BigIntish): AccrualPosition;
|
|
112
|
+
borrow(assets: bigint, shares: bigint, timestamp?: BigIntish): {
|
|
113
|
+
position: AccrualPosition;
|
|
114
|
+
assets: bigint;
|
|
115
|
+
shares: bigint;
|
|
116
|
+
};
|
|
117
|
+
repay(assets: bigint, shares: bigint, timestamp?: BigIntish): {
|
|
118
|
+
position: AccrualPosition;
|
|
119
|
+
assets: bigint;
|
|
120
|
+
shares: bigint;
|
|
121
|
+
};
|
|
122
|
+
getRepayCapacityLimit(loanTokenBalance: bigint): import("../market/Market.js").CapacityLimit;
|
|
123
|
+
getMaxCapacities(loanTokenBalance: bigint, collateralTokenBalance: bigint, options?: {
|
|
124
|
+
borrow?: MaxBorrowOptions;
|
|
125
|
+
withdrawCollateral?: MaxWithdrawCollateralOptions;
|
|
126
|
+
}): import("../market/Market.js").MaxPositionCapacities;
|
|
127
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { BlueErrors } from "../errors.js";
|
|
2
|
+
import { Market, } from "../market/index.js";
|
|
3
|
+
export class Position {
|
|
4
|
+
/**
|
|
5
|
+
* The user holding this position.
|
|
6
|
+
*/
|
|
7
|
+
user;
|
|
8
|
+
/**
|
|
9
|
+
* The id of the market on which this position is held.
|
|
10
|
+
*/
|
|
11
|
+
marketId;
|
|
12
|
+
/**
|
|
13
|
+
* The amount of supply shares held with this position.
|
|
14
|
+
*/
|
|
15
|
+
supplyShares;
|
|
16
|
+
/**
|
|
17
|
+
* The amount of borrow shares held with this position.
|
|
18
|
+
*/
|
|
19
|
+
borrowShares;
|
|
20
|
+
/**
|
|
21
|
+
* The amount of collateral assets held with this position.
|
|
22
|
+
*/
|
|
23
|
+
collateral;
|
|
24
|
+
constructor({ user, marketId, supplyShares, borrowShares, collateral, }) {
|
|
25
|
+
this.user = user;
|
|
26
|
+
this.marketId = marketId;
|
|
27
|
+
this.supplyShares = supplyShares;
|
|
28
|
+
this.borrowShares = borrowShares;
|
|
29
|
+
this.collateral = collateral;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class AccrualPosition extends Position {
|
|
33
|
+
/**
|
|
34
|
+
* The market on which this position is held.
|
|
35
|
+
*/
|
|
36
|
+
market;
|
|
37
|
+
constructor(position, market) {
|
|
38
|
+
super({ ...position, marketId: market.id });
|
|
39
|
+
this.market = market;
|
|
40
|
+
}
|
|
41
|
+
get supplyAssets() {
|
|
42
|
+
return this.market.toSupplyAssets(this.supplyShares);
|
|
43
|
+
}
|
|
44
|
+
get borrowAssets() {
|
|
45
|
+
return this.market.toBorrowAssets(this.borrowShares);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The value of this position's collateral quoted in loan assets.
|
|
49
|
+
*/
|
|
50
|
+
get collateralValue() {
|
|
51
|
+
return this.market.getCollateralValue(this.collateral);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The maximum amount of loan assets that can be borrowed against this position's collateral.
|
|
55
|
+
*/
|
|
56
|
+
get maxBorrowAssets() {
|
|
57
|
+
return this.market.getMaxBorrowAssets(this.collateral);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The maximum additional amount of assets that can be borrowed against this position's collateral.
|
|
61
|
+
*/
|
|
62
|
+
get maxBorrowableAssets() {
|
|
63
|
+
return this.market.getMaxBorrowableAssets(this);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The maximum amount of collateral that can be seized in exchange for the outstanding debt.
|
|
67
|
+
*/
|
|
68
|
+
get seizableCollateral() {
|
|
69
|
+
return this.market.getSeizableCollateral(this);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The maximum amount of collateral that can be withdrawn.
|
|
73
|
+
*/
|
|
74
|
+
get withdrawableCollateral() {
|
|
75
|
+
return this.market.getWithdrawableCollateral(this);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Whether this position is healthy.
|
|
79
|
+
*/
|
|
80
|
+
get isHealthy() {
|
|
81
|
+
return this.market.isHealthy(this);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The price of the collateral quoted in loan assets that would allow this position to be liquidated.
|
|
85
|
+
*/
|
|
86
|
+
get liquidationPrice() {
|
|
87
|
+
return this.market.getLiquidationPrice(this);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The price variation required for the position to reach its liquidation threshold (scaled by WAD).
|
|
91
|
+
* Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%).
|
|
92
|
+
* Returns null if the position is not a borrow.
|
|
93
|
+
*/
|
|
94
|
+
get priceVariationToLiquidationPrice() {
|
|
95
|
+
return this.market.getPriceVariationToLiquidationPrice(this);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* This position's Loan-To-Value (debt over collateral power, scaled by WAD).
|
|
99
|
+
* If the collateral price is 0, LTV is `MaxUint256`.
|
|
100
|
+
*/
|
|
101
|
+
get ltv() {
|
|
102
|
+
return this.market.getLtv(this);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* This position's health factor (collateral power over debt, scaled by WAD).
|
|
106
|
+
* If the debt is 0, health factor is `MaxUint256`.
|
|
107
|
+
*/
|
|
108
|
+
get healthFactor() {
|
|
109
|
+
return this.market.getHealthFactor(this);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The percentage of this position's borrow power currently used (scaled by WAD).
|
|
113
|
+
* If the collateral price is 0, usage is `MaxUint256`.
|
|
114
|
+
*/
|
|
115
|
+
get borrowCapacityUsage() {
|
|
116
|
+
return this.market.getBorrowCapacityUsage(this);
|
|
117
|
+
}
|
|
118
|
+
get borrowCapacityLimit() {
|
|
119
|
+
return this.market.getBorrowCapacityLimit(this);
|
|
120
|
+
}
|
|
121
|
+
get withdrawCapacityLimit() {
|
|
122
|
+
return this.market.getWithdrawCapacityLimit(this);
|
|
123
|
+
}
|
|
124
|
+
get withdrawCollateralCapacityLimit() {
|
|
125
|
+
return this.market.getWithdrawCollateralCapacityLimit(this);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns a new position derived from this position, whose interest has been accrued up to the given timestamp.
|
|
129
|
+
* @param timestamp The timestamp at which to accrue interest. Must be greater than or equal to the market's `lastUpdate`.
|
|
130
|
+
*/
|
|
131
|
+
accrueInterest(timestamp) {
|
|
132
|
+
return new AccrualPosition(this, this.market.accrueInterest(timestamp));
|
|
133
|
+
}
|
|
134
|
+
supply(assets, shares, timestamp) {
|
|
135
|
+
let { market } = this;
|
|
136
|
+
({ market, assets, shares } = market.supply(assets, shares, timestamp));
|
|
137
|
+
this.supplyShares += shares;
|
|
138
|
+
return {
|
|
139
|
+
position: new AccrualPosition(this, market),
|
|
140
|
+
assets,
|
|
141
|
+
shares,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
withdraw(assets, shares, timestamp) {
|
|
145
|
+
let { market } = this;
|
|
146
|
+
({ market, assets, shares } = market.withdraw(assets, shares, timestamp));
|
|
147
|
+
this.supplyShares -= shares;
|
|
148
|
+
if (this.supplyShares < 0n)
|
|
149
|
+
throw new BlueErrors.InsufficientPosition(this.user, this.marketId);
|
|
150
|
+
return {
|
|
151
|
+
position: new AccrualPosition(this, market),
|
|
152
|
+
assets,
|
|
153
|
+
shares,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
supplyCollateral(assets) {
|
|
157
|
+
this.collateral += assets;
|
|
158
|
+
return new AccrualPosition(this, new Market(this.market));
|
|
159
|
+
}
|
|
160
|
+
withdrawCollateral(assets, timestamp) {
|
|
161
|
+
const market = this.market.accrueInterest(timestamp);
|
|
162
|
+
this.collateral -= assets;
|
|
163
|
+
if (this.collateral < 0n)
|
|
164
|
+
throw new BlueErrors.InsufficientPosition(this.user, this.marketId);
|
|
165
|
+
if (!market.isHealthy(this))
|
|
166
|
+
throw new BlueErrors.InsufficientCollateral(this.user, this.marketId);
|
|
167
|
+
return new AccrualPosition(this, market);
|
|
168
|
+
}
|
|
169
|
+
borrow(assets, shares, timestamp) {
|
|
170
|
+
let { market } = this;
|
|
171
|
+
({ market, assets, shares } = market.borrow(assets, shares, timestamp));
|
|
172
|
+
this.borrowShares += shares;
|
|
173
|
+
if (!market.isHealthy(this))
|
|
174
|
+
throw new BlueErrors.InsufficientCollateral(this.user, this.marketId);
|
|
175
|
+
return {
|
|
176
|
+
position: new AccrualPosition(this, market),
|
|
177
|
+
assets,
|
|
178
|
+
shares,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
repay(assets, shares, timestamp) {
|
|
182
|
+
let { market } = this;
|
|
183
|
+
({ market, assets, shares } = market.repay(assets, shares, timestamp));
|
|
184
|
+
this.borrowShares -= shares;
|
|
185
|
+
if (this.borrowShares < 0n)
|
|
186
|
+
throw new BlueErrors.InsufficientPosition(this.user, this.marketId);
|
|
187
|
+
return {
|
|
188
|
+
position: new AccrualPosition(this, market),
|
|
189
|
+
assets,
|
|
190
|
+
shares,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
getRepayCapacityLimit(loanTokenBalance) {
|
|
194
|
+
return this.market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
|
|
195
|
+
}
|
|
196
|
+
getMaxCapacities(loanTokenBalance, collateralTokenBalance, options) {
|
|
197
|
+
return this.market.getMaxCapacities(this, loanTokenBalance, collateralTokenBalance, options);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Position.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Position.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type RoundingDirection } from "../math/index.js";
|
|
2
|
+
import type { Address, BigIntish } from "../types.js";
|
|
3
|
+
import type { InputToken } from "./Token.js";
|
|
4
|
+
import { WrappedToken } from "./WrappedToken.js";
|
|
5
|
+
export declare class ConstantWrappedToken extends WrappedToken {
|
|
6
|
+
readonly underlyingDecimals: bigint;
|
|
7
|
+
constructor(token: InputToken, underlying: Address, underlyingDecimals?: BigIntish);
|
|
8
|
+
toWrappedExactAmountIn(unwrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
|
|
9
|
+
/** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
|
|
10
|
+
toWrappedExactAmountOut(wrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
|
|
11
|
+
/** The expected amount when unwrapping `wrappedAmount` */
|
|
12
|
+
toUnwrappedExactAmountIn(wrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
|
|
13
|
+
/** The amount of wrappedTokens that should be unwrapped to receive `unwrappedAmount` */
|
|
14
|
+
toUnwrappedExactAmountOut(unwrappedAmount: bigint, _slippage?: bigint, rounding?: RoundingDirection): bigint;
|
|
15
|
+
protected _wrap(amount: bigint): bigint;
|
|
16
|
+
protected _unwrap(amount: bigint): bigint;
|
|
17
|
+
}
|