@morpho-org/blue-sdk 1.0.0 → 1.0.1
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/lib/addresses.d.ts +168 -0
- package/lib/addresses.js +169 -0
- package/lib/chain/chain.constants.d.ts +3 -0
- package/lib/chain/chain.constants.js +232 -0
- package/lib/chain/chain.types.d.ts +20 -0
- package/lib/chain/chain.types.js +30 -0
- package/lib/chain/chain.utils.d.ts +14 -0
- package/lib/chain/chain.utils.js +30 -0
- package/lib/chain/index.js +18 -0
- package/lib/constants.d.ts +8 -0
- package/lib/constants.js +13 -0
- package/lib/errors.d.ts +37 -0
- package/lib/errors.js +71 -0
- package/lib/ethers/index.js +18 -0
- package/lib/ethers/safeGetAddress.d.ts +1 -0
- package/lib/ethers/safeGetAddress.js +6 -0
- package/lib/ethers/safeParseUnits.d.ts +2 -0
- package/lib/ethers/safeParseUnits.js +25 -0
- package/lib/evm.d.ts +36 -0
- package/lib/evm.js +113 -0
- package/lib/helpers/format/format.d.ts +98 -0
- package/lib/helpers/format/format.js +301 -0
- package/lib/helpers/format/index.js +17 -0
- package/lib/helpers/getChecksumedAddress.d.ts +7 -0
- package/lib/helpers/getChecksumedAddress.js +17 -0
- package/lib/helpers/index.js +20 -0
- package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
- package/lib/helpers/isZeroAddressOrUnset.js +14 -0
- package/lib/helpers/locale.d.ts +36 -0
- package/lib/helpers/locale.js +86 -0
- package/lib/holding/Holding.d.ts +60 -0
- package/lib/holding/Holding.js +31 -0
- package/lib/holding/index.js +17 -0
- package/lib/index.d.ts +33 -0
- package/lib/index.js +62 -0
- package/lib/market/Market.d.ts +159 -0
- package/lib/market/Market.js +240 -0
- package/lib/market/MarketConfig.d.ts +44 -0
- package/lib/market/MarketConfig.js +56 -0
- package/lib/market/MarketUtils.d.ts +165 -0
- package/lib/market/MarketUtils.js +182 -0
- package/lib/market/index.js +19 -0
- package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
- package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
- package/lib/maths/MathLib.d.ts +94 -0
- package/lib/maths/MathLib.js +153 -0
- package/lib/maths/MathUtils.d.ts +15 -0
- package/lib/maths/MathUtils.js +33 -0
- package/lib/maths/SharesMath.d.ts +12 -0
- package/lib/maths/SharesMath.js +22 -0
- package/lib/maths/index.js +20 -0
- package/lib/notifications.d.ts +98 -0
- package/lib/notifications.js +52 -0
- package/lib/position/Position.d.ts +118 -0
- package/lib/position/Position.js +145 -0
- package/lib/position/index.js +17 -0
- package/lib/signatures/index.d.ts +12 -0
- package/lib/signatures/index.js +39 -0
- package/lib/signatures/manager.d.ts +10 -0
- package/lib/signatures/manager.js +37 -0
- package/lib/signatures/permit.d.ts +21 -0
- package/lib/signatures/permit.js +101 -0
- package/lib/signatures/permit2.d.ts +20 -0
- package/lib/signatures/permit2.js +91 -0
- package/lib/signatures/types.d.ts +13 -0
- package/lib/signatures/types.js +2 -0
- package/lib/signatures/utils.d.ts +6 -0
- package/lib/signatures/utils.js +44 -0
- package/lib/tests/mocks/markets.d.ts +17 -0
- package/lib/tests/mocks/markets.js +108 -0
- package/lib/token/ERC20Metadata.d.ts +249 -0
- package/lib/token/ERC20Metadata.js +81 -0
- package/lib/token/Token.d.ts +45 -0
- package/lib/token/Token.js +39 -0
- package/lib/token/TokenNamespace.d.ts +18 -0
- package/lib/token/TokenNamespace.js +55 -0
- package/lib/token/WrappedToken.d.ts +42 -0
- package/lib/token/WrappedToken.js +87 -0
- package/lib/token/index.js +18 -0
- package/lib/types.d.ts +29 -0
- package/lib/types.js +23 -0
- package/lib/user/User.d.ts +20 -0
- package/lib/user/User.js +11 -0
- package/lib/user/index.js +18 -0
- package/lib/user/user.types.d.ts +18 -0
- package/lib/user/user.types.js +2 -0
- package/lib/vault/Vault.d.ts +167 -0
- package/lib/vault/Vault.js +156 -0
- package/lib/vault/VaultAllocation.d.ts +38 -0
- package/lib/vault/VaultAllocation.js +18 -0
- package/lib/vault/VaultConfig.d.ts +23 -0
- package/lib/vault/VaultConfig.js +26 -0
- package/lib/vault/VaultUtils.d.ts +17 -0
- package/lib/vault/VaultUtils.js +17 -0
- package/lib/vault/index.js +20 -0
- package/package.json +5 -5
- package/src/addresses.ts +0 -261
- package/src/chain/chain.constants.ts +0 -235
- package/src/chain/chain.test.ts +0 -51
- package/src/chain/chain.types.ts +0 -42
- package/src/chain/chain.utils.ts +0 -44
- package/src/constants.ts +0 -18
- package/src/errors.ts +0 -75
- package/src/ethers/ethers.test.ts +0 -17
- package/src/ethers/safeGetAddress.ts +0 -4
- package/src/ethers/safeParseUnits.ts +0 -29
- package/src/evm.ts +0 -172
- package/src/helpers/format/format.test.ts +0 -340
- package/src/helpers/format/format.ts +0 -416
- package/src/helpers/getChecksumedAddress.ts +0 -15
- package/src/helpers/locale.ts +0 -108
- package/src/holding/Holding.ts +0 -109
- package/src/market/Market.ts +0 -479
- package/src/market/MarketConfig.ts +0 -108
- package/src/market/MarketUtils.test.ts +0 -25
- package/src/market/MarketUtils.ts +0 -467
- package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
- package/src/maths/MathLib.ts +0 -208
- package/src/maths/MathUtils.ts +0 -31
- package/src/maths/SharesMath.ts +0 -40
- package/src/notifications.ts +0 -167
- package/src/position/Position.ts +0 -251
- package/src/signatures/index.ts +0 -18
- package/src/signatures/manager.ts +0 -50
- package/src/signatures/permit.ts +0 -126
- package/src/signatures/permit2.ts +0 -120
- package/src/signatures/types.ts +0 -18
- package/src/signatures/utils.ts +0 -83
- package/src/tests/mocks/markets.ts +0 -110
- package/src/token/ERC20Metadata.ts +0 -124
- package/src/token/Token.ts +0 -83
- package/src/token/TokenNamespace.ts +0 -76
- package/src/token/WrappedToken.ts +0 -142
- package/src/types.ts +0 -37
- package/src/user/User.ts +0 -32
- package/src/user/user.types.ts +0 -23
- package/src/vault/Vault.ts +0 -370
- package/src/vault/VaultAllocation.ts +0 -58
- package/src/vault/VaultConfig.ts +0 -55
- package/src/vault/VaultUtils.ts +0 -47
- /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
- /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
- /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
- /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
- /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
- /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
- /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
- /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
- /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
- /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
- /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MathLib = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
/**
|
|
7
|
+
* Library to manage fixed-point arithmetic.
|
|
8
|
+
* This library reproduces the behaviour of the solidity library MathLib
|
|
9
|
+
* TODO: add library link
|
|
10
|
+
* @category Maths
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
class MathLib {
|
|
14
|
+
/**
|
|
15
|
+
* Returns the smallest number given as param
|
|
16
|
+
* @param x The first number
|
|
17
|
+
* @param y The second number
|
|
18
|
+
*/
|
|
19
|
+
static min(x, y) {
|
|
20
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
21
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
22
|
+
return x <= y ? x : y;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns the greatest number given as param
|
|
26
|
+
* @param x The first number
|
|
27
|
+
* @param y The second number
|
|
28
|
+
*/
|
|
29
|
+
static max(x, y) {
|
|
30
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
31
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
32
|
+
return x <= y ? y : x;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded down
|
|
36
|
+
* @param x The first number
|
|
37
|
+
* @param y The second number
|
|
38
|
+
*/
|
|
39
|
+
static wMulDown(x, y) {
|
|
40
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
41
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
42
|
+
return MathLib.wMul(x, y, "Down");
|
|
43
|
+
}
|
|
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
|
+
static wMulUp(x, y) {
|
|
50
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
51
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
52
|
+
return MathLib.wMul(x, y, "Up");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
56
|
+
* @param x The first number
|
|
57
|
+
* @param y The second number
|
|
58
|
+
*/
|
|
59
|
+
static wMul(x, y, rounding) {
|
|
60
|
+
return MathLib.mulDiv(x, y, MathLib.WAD, rounding);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Perform the WAD-based division of 2 numbers, rounded down
|
|
64
|
+
* @param x The first number
|
|
65
|
+
* @param y The second number
|
|
66
|
+
*/
|
|
67
|
+
static wDivDown(x, y) {
|
|
68
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
69
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
70
|
+
return MathLib.wDiv(x, y, "Down");
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Perform the WAD-based multiplication of 2 numbers, rounded up
|
|
74
|
+
* @param x The first number
|
|
75
|
+
* @param y The second number
|
|
76
|
+
*/
|
|
77
|
+
static wDivUp(x, y) {
|
|
78
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
79
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
80
|
+
return MathLib.wDiv(x, y, "Up");
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Perform the WAD-based multiplication of 2 numbers with a provided rounding direction
|
|
84
|
+
* @param x The first number
|
|
85
|
+
* @param y The second number
|
|
86
|
+
*/
|
|
87
|
+
static wDiv(x, y, rounding) {
|
|
88
|
+
return MathLib.mulDiv(x, MathLib.WAD, y, rounding);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Multiply two numbers and divide by a denominator, rounding down the result
|
|
92
|
+
* @param x The first number
|
|
93
|
+
* @param y The second number
|
|
94
|
+
* @param denominator The denominator
|
|
95
|
+
*/
|
|
96
|
+
static mulDivDown(x, y, denominator) {
|
|
97
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
98
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
99
|
+
denominator = (0, ethers_1.toBigInt)(denominator);
|
|
100
|
+
if (denominator === 0n)
|
|
101
|
+
throw Error("MathLib: DIVISION_BY_ZERO");
|
|
102
|
+
return (x * y) / denominator;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Multiply two numbers and divide by a denominator, rounding up the result
|
|
106
|
+
* @param x The first number
|
|
107
|
+
* @param y The second number
|
|
108
|
+
* @param denominator The denominator
|
|
109
|
+
*/
|
|
110
|
+
static mulDivUp(x, y, denominator) {
|
|
111
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
112
|
+
y = (0, ethers_1.toBigInt)(y);
|
|
113
|
+
denominator = (0, ethers_1.toBigInt)(denominator);
|
|
114
|
+
if (denominator === 0n)
|
|
115
|
+
throw Error("MathLib: DIVISION_BY_ZERO");
|
|
116
|
+
const roundup = (x * y) % denominator > 0 ? 1n : 0n;
|
|
117
|
+
return (x * y) / denominator + roundup;
|
|
118
|
+
}
|
|
119
|
+
static mulDiv(x, y, denominator, rounding) {
|
|
120
|
+
return MathLib[`mulDiv${rounding}`](x, y, denominator);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The sum of the first three non-zero terms of a Taylor expansion of e^(nx) - 1,
|
|
124
|
+
* to approximate a continuous compound interest rate.
|
|
125
|
+
*
|
|
126
|
+
* @param x The base of the exponent
|
|
127
|
+
* @param n The exponent
|
|
128
|
+
*/
|
|
129
|
+
static wTaylorCompounded(x, n) {
|
|
130
|
+
x = (0, ethers_1.toBigInt)(x);
|
|
131
|
+
n = (0, ethers_1.toBigInt)(n);
|
|
132
|
+
const firstTerm = x * n;
|
|
133
|
+
const secondTerm = MathLib.mulDivDown(firstTerm, firstTerm, 2n * MathLib.WAD);
|
|
134
|
+
const thirdTerm = MathLib.mulDivDown(secondTerm, firstTerm, 3n * MathLib.WAD);
|
|
135
|
+
return firstTerm + secondTerm + thirdTerm;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Converts an apr to compounded apy
|
|
139
|
+
*
|
|
140
|
+
* @param apr The apr to convert (in WAD)
|
|
141
|
+
* @param compounding The compounding basis
|
|
142
|
+
*/
|
|
143
|
+
static aprToApy(apr, compounding) {
|
|
144
|
+
const factor = morpho_ts_1.Time[compounding].from.y(1n);
|
|
145
|
+
const rate = apr / factor;
|
|
146
|
+
return ((0, ethers_1.parseUnits)(((+(0, ethers_1.formatUnits)(this.WAD + rate)) ** Number(factor)).toFixed(18)) - this.WAD);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.MathLib = MathLib;
|
|
150
|
+
MathLib.WAD = (0, ethers_1.parseUnits)("1");
|
|
151
|
+
MathLib.MAX_UINT_160 = BigInt("0x" + "f".repeat(40));
|
|
152
|
+
MathLib.MAX_UINT_128 = BigInt("0x" + "f".repeat(32));
|
|
153
|
+
MathLib.MAX_UINT_48 = BigInt("0x" + "f".repeat(12));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Math utilities
|
|
3
|
+
* This library reproduces the behaviour of the solidity library MathUtils
|
|
4
|
+
* TODO: add library link
|
|
5
|
+
* @category Maths
|
|
6
|
+
*/
|
|
7
|
+
export declare class MathUtils {
|
|
8
|
+
static zeroFloorSub(a: bigint, b: bigint): bigint;
|
|
9
|
+
static min(a: bigint, b: bigint): bigint;
|
|
10
|
+
static mins(...args: bigint[]): bigint;
|
|
11
|
+
static max(a: bigint, b: bigint): bigint;
|
|
12
|
+
static pos(a: bigint): bigint;
|
|
13
|
+
static neg(a: bigint): bigint;
|
|
14
|
+
static abs(a: bigint): bigint;
|
|
15
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MathUtils = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Math utilities
|
|
6
|
+
* This library reproduces the behaviour of the solidity library MathUtils
|
|
7
|
+
* TODO: add library link
|
|
8
|
+
* @category Maths
|
|
9
|
+
*/
|
|
10
|
+
class MathUtils {
|
|
11
|
+
static zeroFloorSub(a, b) {
|
|
12
|
+
return a - b < 0n ? 0n : a - b;
|
|
13
|
+
}
|
|
14
|
+
static min(a, b) {
|
|
15
|
+
return a < b ? a : b;
|
|
16
|
+
}
|
|
17
|
+
static mins(...args) {
|
|
18
|
+
return args.reduce((a, b) => this.min(a, b));
|
|
19
|
+
}
|
|
20
|
+
static max(a, b) {
|
|
21
|
+
return a > b ? a : b;
|
|
22
|
+
}
|
|
23
|
+
static pos(a) {
|
|
24
|
+
return a >= 0n ? a : 0n;
|
|
25
|
+
}
|
|
26
|
+
static neg(a) {
|
|
27
|
+
return a < 0n ? -a : 0n;
|
|
28
|
+
}
|
|
29
|
+
static abs(a) {
|
|
30
|
+
return a >= 0 ? a : -a;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.MathUtils = MathUtils;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BigNumberish } from "ethers";
|
|
2
|
+
import { RoundingDirection } from "./MathLib";
|
|
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: BigNumberish, totalAssets: BigNumberish, totalShares: BigNumberish, rounding: RoundingDirection): bigint;
|
|
11
|
+
function toShares(assets: BigNumberish, totalAssets: BigNumberish, totalShares: BigNumberish, rounding: RoundingDirection): bigint;
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SharesMath = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const MathLib_1 = require("./MathLib");
|
|
6
|
+
/**
|
|
7
|
+
* JS implementation of {@link https://github.com/morpho-org/morpho-blue/blob/main/src/libraries/SharesMathLib.sol SharesMathLib} used by Morpho Blue
|
|
8
|
+
* & MetaMorpho (via {@link https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol ERC4626}).
|
|
9
|
+
*/
|
|
10
|
+
var SharesMath;
|
|
11
|
+
(function (SharesMath) {
|
|
12
|
+
SharesMath.VIRTUAL_SHARES = 1000000n;
|
|
13
|
+
SharesMath.VIRTUAL_ASSETS = 1n;
|
|
14
|
+
function toAssets(shares, totalAssets, totalShares, rounding) {
|
|
15
|
+
return MathLib_1.MathLib.mulDiv(shares, (0, ethers_1.toBigInt)(totalAssets) + SharesMath.VIRTUAL_ASSETS, (0, ethers_1.toBigInt)(totalShares) + SharesMath.VIRTUAL_SHARES, rounding);
|
|
16
|
+
}
|
|
17
|
+
SharesMath.toAssets = toAssets;
|
|
18
|
+
function toShares(assets, totalAssets, totalShares, rounding) {
|
|
19
|
+
return MathLib_1.MathLib.mulDiv(assets, (0, ethers_1.toBigInt)(totalShares) + SharesMath.VIRTUAL_SHARES, (0, ethers_1.toBigInt)(totalAssets) + SharesMath.VIRTUAL_ASSETS, rounding);
|
|
20
|
+
}
|
|
21
|
+
SharesMath.toShares = toShares;
|
|
22
|
+
})(SharesMath || (exports.SharesMath = SharesMath = {}));
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./MathLib"), exports);
|
|
18
|
+
__exportStar(require("./SharesMath"), exports);
|
|
19
|
+
__exportStar(require("./MathUtils"), exports);
|
|
20
|
+
__exportStar(require("./AdaptiveCurveIrmLib"), exports);
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Signature, TransactionReceipt, TransactionRequest, TransactionResponse } from "ethers";
|
|
2
|
+
import { Observable, Observer, ReplaySubject } from "rxjs";
|
|
3
|
+
import { SignatureMessage } from "./signatures/types";
|
|
4
|
+
export declare enum NotificationStatus {
|
|
5
|
+
signing = "signing",
|
|
6
|
+
success = "success",
|
|
7
|
+
error = "error",
|
|
8
|
+
pending = "pending"
|
|
9
|
+
}
|
|
10
|
+
export declare enum NotificationTopic {
|
|
11
|
+
tx = "tx",
|
|
12
|
+
signature = "signature"
|
|
13
|
+
}
|
|
14
|
+
export interface NotificationContext {
|
|
15
|
+
[NotificationTopic.tx]: {
|
|
16
|
+
[NotificationStatus.signing]: {
|
|
17
|
+
tx: TransactionRequest;
|
|
18
|
+
};
|
|
19
|
+
[NotificationStatus.pending]: {
|
|
20
|
+
args: Record<PropertyKey, any>;
|
|
21
|
+
tx: TransactionRequest;
|
|
22
|
+
response?: TransactionResponse;
|
|
23
|
+
};
|
|
24
|
+
[NotificationStatus.success]: {
|
|
25
|
+
args: Record<PropertyKey, any>;
|
|
26
|
+
tx: TransactionRequest;
|
|
27
|
+
response?: TransactionResponse;
|
|
28
|
+
receipt: TransactionReceipt | null;
|
|
29
|
+
};
|
|
30
|
+
[NotificationStatus.error]: {
|
|
31
|
+
args: Record<PropertyKey, any>;
|
|
32
|
+
tx: TransactionRequest;
|
|
33
|
+
response?: TransactionResponse;
|
|
34
|
+
error: Error;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
[NotificationTopic.signature]: {
|
|
38
|
+
[NotificationStatus.signing]: never;
|
|
39
|
+
[NotificationStatus.pending]: {
|
|
40
|
+
message: SignatureMessage;
|
|
41
|
+
args: Record<PropertyKey, any>;
|
|
42
|
+
};
|
|
43
|
+
[NotificationStatus.success]: {
|
|
44
|
+
message: SignatureMessage;
|
|
45
|
+
args: Record<PropertyKey, any>;
|
|
46
|
+
signature: Signature;
|
|
47
|
+
};
|
|
48
|
+
[NotificationStatus.error]: {
|
|
49
|
+
message: SignatureMessage;
|
|
50
|
+
args: Record<PropertyKey, any>;
|
|
51
|
+
error: Error;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export interface NotificationBody<Topic extends NotificationTopic = NotificationTopic, Status extends NotificationStatus = NotificationStatus> {
|
|
56
|
+
id: string;
|
|
57
|
+
topic: Topic;
|
|
58
|
+
status: Status;
|
|
59
|
+
}
|
|
60
|
+
export type Notifications = {
|
|
61
|
+
[Topic in NotificationTopic]: {
|
|
62
|
+
[Status in NotificationStatus]: NotificationBody<Topic, Status> & (NotificationContext[Topic][Status] extends never ? unknown : {
|
|
63
|
+
context: NotificationContext[Topic][Status];
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
export type TxNotification = Notifications[NotificationTopic.tx][NotificationStatus];
|
|
68
|
+
export type SignatureNotification = Notifications[NotificationTopic.signature][NotificationStatus];
|
|
69
|
+
export type Notification = Notifications[NotificationTopic][NotificationStatus];
|
|
70
|
+
export type NotificationsWithoutTopic = {
|
|
71
|
+
[Topic in NotificationTopic]: {
|
|
72
|
+
[Status in NotificationStatus]: Omit<NotificationBody<Topic, Status>, "topic"> & (NotificationContext[Topic][Status] extends never ? unknown : {
|
|
73
|
+
context: NotificationContext[Topic][Status];
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
export interface NotificationConsumer<Topic extends NotificationTopic> {
|
|
78
|
+
notifications$: Observable<Notifications[Topic][NotificationStatus]>;
|
|
79
|
+
wait: (observerOrNext?: Partial<Observer<Notifications[Topic][NotificationStatus]>> | ((value: Notifications[Topic][NotificationStatus]) => void)) => Promise<Notifications[Topic][NotificationStatus.success | NotificationStatus.error]>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* MUST receive a success or error notification at some point to ensure the result promise resolves.
|
|
83
|
+
*/
|
|
84
|
+
export declare class NotificationProducer<Topic extends NotificationTopic> {
|
|
85
|
+
readonly topic: Topic;
|
|
86
|
+
protected readonly _notifications$: ReplaySubject<Notifications[Topic][NotificationStatus]>;
|
|
87
|
+
readonly notifications$: Observable<Notifications[Topic][NotificationStatus]>;
|
|
88
|
+
protected readonly _result: Promise<Notifications[Topic][NotificationStatus]>;
|
|
89
|
+
readonly consumer: NotificationConsumer<Topic>;
|
|
90
|
+
/**
|
|
91
|
+
* Creates a notification producer.
|
|
92
|
+
* MUST receive a success or error notification at some point to ensure the result promise resolves.
|
|
93
|
+
* @param topic The notification topic to populate each notification with.
|
|
94
|
+
*/
|
|
95
|
+
constructor(topic: Topic);
|
|
96
|
+
wait(observerOrNext?: Partial<Observer<Notifications[Topic][NotificationStatus]>> | ((value: Notifications[Topic][NotificationStatus]) => void)): Promise<Notifications[Topic][NotificationStatus.success | NotificationStatus.error]>;
|
|
97
|
+
next(notification: NotificationsWithoutTopic[Topic][NotificationStatus]): void;
|
|
98
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NotificationProducer = exports.NotificationTopic = exports.NotificationStatus = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
var NotificationStatus;
|
|
6
|
+
(function (NotificationStatus) {
|
|
7
|
+
NotificationStatus["signing"] = "signing";
|
|
8
|
+
NotificationStatus["success"] = "success";
|
|
9
|
+
NotificationStatus["error"] = "error";
|
|
10
|
+
NotificationStatus["pending"] = "pending";
|
|
11
|
+
})(NotificationStatus || (exports.NotificationStatus = NotificationStatus = {}));
|
|
12
|
+
var NotificationTopic;
|
|
13
|
+
(function (NotificationTopic) {
|
|
14
|
+
NotificationTopic["tx"] = "tx";
|
|
15
|
+
NotificationTopic["signature"] = "signature";
|
|
16
|
+
})(NotificationTopic || (exports.NotificationTopic = NotificationTopic = {}));
|
|
17
|
+
/**
|
|
18
|
+
* MUST receive a success or error notification at some point to ensure the result promise resolves.
|
|
19
|
+
*/
|
|
20
|
+
class NotificationProducer {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a notification producer.
|
|
23
|
+
* MUST receive a success or error notification at some point to ensure the result promise resolves.
|
|
24
|
+
* @param topic The notification topic to populate each notification with.
|
|
25
|
+
*/
|
|
26
|
+
constructor(topic) {
|
|
27
|
+
this.topic = topic;
|
|
28
|
+
this._notifications$ = new rxjs_1.ReplaySubject(Infinity);
|
|
29
|
+
this.notifications$ = this._notifications$.asObservable();
|
|
30
|
+
this._result = (0, rxjs_1.lastValueFrom)(this._notifications$);
|
|
31
|
+
this.consumer = {
|
|
32
|
+
notifications$: this.notifications$,
|
|
33
|
+
wait: this.wait.bind(this),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async wait(observerOrNext) {
|
|
37
|
+
const subscription = this.notifications$.subscribe(observerOrNext);
|
|
38
|
+
const notification = (await this._result);
|
|
39
|
+
subscription.unsubscribe();
|
|
40
|
+
return notification;
|
|
41
|
+
}
|
|
42
|
+
next(notification) {
|
|
43
|
+
this._notifications$.next({
|
|
44
|
+
...notification,
|
|
45
|
+
topic: this.topic,
|
|
46
|
+
});
|
|
47
|
+
if (notification.status === NotificationStatus.success ||
|
|
48
|
+
notification.status === NotificationStatus.error)
|
|
49
|
+
this._notifications$.complete();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.NotificationProducer = NotificationProducer;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { BigNumberish, Provider } from "ethers";
|
|
2
|
+
import { ViewOverrides } from "ethers-types/dist/common";
|
|
3
|
+
import { ChainId } from "../chain";
|
|
4
|
+
import { Market, MarketConfig } from "../market";
|
|
5
|
+
import { Address, MarketId } from "../types";
|
|
6
|
+
export interface InputPosition {
|
|
7
|
+
user: Address;
|
|
8
|
+
marketId: MarketId;
|
|
9
|
+
supplyShares: BigNumberish;
|
|
10
|
+
borrowShares: BigNumberish;
|
|
11
|
+
collateral: BigNumberish;
|
|
12
|
+
}
|
|
13
|
+
export declare class Position implements InputPosition {
|
|
14
|
+
static fetch(user: Address, marketId: MarketId, runner: {
|
|
15
|
+
provider: Provider;
|
|
16
|
+
}, { chainId, overrides, }?: {
|
|
17
|
+
chainId?: ChainId;
|
|
18
|
+
overrides?: ViewOverrides;
|
|
19
|
+
}): Promise<Position>;
|
|
20
|
+
/**
|
|
21
|
+
* The user holding this position.
|
|
22
|
+
*/
|
|
23
|
+
readonly user: Address;
|
|
24
|
+
/**
|
|
25
|
+
* The id of the market on which this position is held.
|
|
26
|
+
*/
|
|
27
|
+
readonly marketId: MarketId;
|
|
28
|
+
/**
|
|
29
|
+
* The amount of supply shares held with this position.
|
|
30
|
+
*/
|
|
31
|
+
supplyShares: bigint;
|
|
32
|
+
/**
|
|
33
|
+
* The amount of borrow shares held with this position.
|
|
34
|
+
*/
|
|
35
|
+
borrowShares: bigint;
|
|
36
|
+
/**
|
|
37
|
+
* The amount of collateral assets held with this position.
|
|
38
|
+
*/
|
|
39
|
+
collateral: bigint;
|
|
40
|
+
constructor({ user, marketId, supplyShares, borrowShares, collateral, }: InputPosition);
|
|
41
|
+
}
|
|
42
|
+
export interface InputAccrualPosition extends Omit<InputPosition, "marketId"> {
|
|
43
|
+
}
|
|
44
|
+
export declare class AccrualPosition extends Position implements InputAccrualPosition {
|
|
45
|
+
static fetchFromId(user: Address, marketId: MarketId, runner: {
|
|
46
|
+
provider: Provider;
|
|
47
|
+
}, options: {
|
|
48
|
+
chainId?: ChainId;
|
|
49
|
+
overrides?: ViewOverrides;
|
|
50
|
+
}): Promise<AccrualPosition>;
|
|
51
|
+
static fetchFromConfig(user: Address, config: MarketConfig, runner: {
|
|
52
|
+
provider: Provider;
|
|
53
|
+
}, options: {
|
|
54
|
+
chainId?: ChainId;
|
|
55
|
+
overrides?: ViewOverrides;
|
|
56
|
+
}): Promise<AccrualPosition>;
|
|
57
|
+
/**
|
|
58
|
+
* The market on which this position is held.
|
|
59
|
+
*/
|
|
60
|
+
readonly market: Market;
|
|
61
|
+
constructor(position: InputAccrualPosition, market: Market);
|
|
62
|
+
get supplyAssets(): bigint;
|
|
63
|
+
get borrowAssets(): bigint;
|
|
64
|
+
/**
|
|
65
|
+
* The value of this position's collateral quoted in loan assets.
|
|
66
|
+
*/
|
|
67
|
+
get collateralValue(): bigint;
|
|
68
|
+
/**
|
|
69
|
+
* The maximum amount of loan assets that can be borrowed against this position's collateral.
|
|
70
|
+
*/
|
|
71
|
+
get maxBorrowAssets(): bigint;
|
|
72
|
+
/**
|
|
73
|
+
* The maximum additional amount of assets that can be borrowed against this position's collateral.
|
|
74
|
+
*/
|
|
75
|
+
get maxBorrowableAssets(): bigint;
|
|
76
|
+
/**
|
|
77
|
+
* The maximum amount of collateral that can be seized in exchange for the outstanding debt.
|
|
78
|
+
*/
|
|
79
|
+
get seizableCollateral(): bigint;
|
|
80
|
+
/**
|
|
81
|
+
* The maximum amount of collateral that can be withdrawn.
|
|
82
|
+
*/
|
|
83
|
+
get withdrawableCollateral(): bigint;
|
|
84
|
+
/**
|
|
85
|
+
* Whether this position is healthy.
|
|
86
|
+
*/
|
|
87
|
+
get isHealthy(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* The price of the collateral quoted in loan assets that would allow this position to be liquidated.
|
|
90
|
+
*/
|
|
91
|
+
get liquidationPrice(): bigint | null;
|
|
92
|
+
/**
|
|
93
|
+
* The variation of the price of the collateral quoted in loan assets that would allow this position to be liquidated,
|
|
94
|
+
* relative to the current collateral price (scaled by WAD).
|
|
95
|
+
*/
|
|
96
|
+
get priceVariationToLiquidation(): bigint | null;
|
|
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(): bigint | null;
|
|
102
|
+
/**
|
|
103
|
+
* This position's health factor (collateral power over debt, scaled by WAD).
|
|
104
|
+
* If the debt is 0, health factor is `MaxUint256`.
|
|
105
|
+
*/
|
|
106
|
+
get healthFactor(): bigint | null;
|
|
107
|
+
/**
|
|
108
|
+
* The percentage of this position's borrow power currently used (scaled by WAD).
|
|
109
|
+
* If the collateral price is 0, usage is `MaxUint256`.
|
|
110
|
+
*/
|
|
111
|
+
get borrowCapacityUsage(): bigint | null;
|
|
112
|
+
get borrowCapacityLimit(): import("../market").CapacityLimit;
|
|
113
|
+
get withdrawCapacityLimit(): import("../market").CapacityLimit;
|
|
114
|
+
get withdrawCollateralCapacityLimit(): import("../market").CapacityLimit;
|
|
115
|
+
accrueInterest(timestamp: BigNumberish): AccrualPosition;
|
|
116
|
+
getRepayCapacityLimit(loanTokenBalance: bigint): import("../market").CapacityLimit;
|
|
117
|
+
getMaxCapacities(loanTokenBalance: bigint, collateralTokenBalance: bigint): import("../market").MaxPositionCapacities;
|
|
118
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccrualPosition = exports.Position = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const ethers_types_1 = require("ethers-types");
|
|
6
|
+
const addresses_1 = require("../addresses");
|
|
7
|
+
const chain_1 = require("../chain");
|
|
8
|
+
const market_1 = require("../market");
|
|
9
|
+
class Position {
|
|
10
|
+
static async fetch(user, marketId, runner, { chainId, overrides = {}, } = {}) {
|
|
11
|
+
chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
|
|
12
|
+
const { morpho } = (0, addresses_1.getChainAddresses)(chainId);
|
|
13
|
+
const { supplyShares, borrowShares, collateral } = await ethers_types_1.MorphoBlue__factory.connect(morpho, runner).position(marketId, user, overrides);
|
|
14
|
+
return new Position({
|
|
15
|
+
user,
|
|
16
|
+
marketId,
|
|
17
|
+
supplyShares,
|
|
18
|
+
borrowShares,
|
|
19
|
+
collateral,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
constructor({ user, marketId, supplyShares, borrowShares, collateral, }) {
|
|
23
|
+
this.user = user;
|
|
24
|
+
this.marketId = marketId;
|
|
25
|
+
this.supplyShares = (0, ethers_1.toBigInt)(supplyShares);
|
|
26
|
+
this.borrowShares = (0, ethers_1.toBigInt)(borrowShares);
|
|
27
|
+
this.collateral = (0, ethers_1.toBigInt)(collateral);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Position = Position;
|
|
31
|
+
class AccrualPosition extends Position {
|
|
32
|
+
static async fetchFromId(user, marketId, runner, options) {
|
|
33
|
+
const [position, market] = await Promise.all([
|
|
34
|
+
await Position.fetch(user, marketId, runner, options),
|
|
35
|
+
await market_1.Market.fetchFromId(marketId, runner, options),
|
|
36
|
+
]);
|
|
37
|
+
return new AccrualPosition(position, market);
|
|
38
|
+
}
|
|
39
|
+
static async fetchFromConfig(user, config, runner, options) {
|
|
40
|
+
const [position, market] = await Promise.all([
|
|
41
|
+
await Position.fetch(user, config.id, runner, options),
|
|
42
|
+
await market_1.Market.fetchFromConfig(config, runner, options),
|
|
43
|
+
]);
|
|
44
|
+
return new AccrualPosition(position, market);
|
|
45
|
+
}
|
|
46
|
+
constructor(position, market) {
|
|
47
|
+
super({ ...position, marketId: market.id });
|
|
48
|
+
this.market = market;
|
|
49
|
+
}
|
|
50
|
+
get supplyAssets() {
|
|
51
|
+
return this.market.toSupplyAssets(this.supplyShares);
|
|
52
|
+
}
|
|
53
|
+
get borrowAssets() {
|
|
54
|
+
return this.market.toBorrowAssets(this.borrowShares);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The value of this position's collateral quoted in loan assets.
|
|
58
|
+
*/
|
|
59
|
+
get collateralValue() {
|
|
60
|
+
return this.market.getCollateralValue(this.collateral);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The maximum amount of loan assets that can be borrowed against this position's collateral.
|
|
64
|
+
*/
|
|
65
|
+
get maxBorrowAssets() {
|
|
66
|
+
return this.market.getMaxBorrowAssets(this.collateral);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The maximum additional amount of assets that can be borrowed against this position's collateral.
|
|
70
|
+
*/
|
|
71
|
+
get maxBorrowableAssets() {
|
|
72
|
+
return this.market.getMaxBorrowableAssets(this);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The maximum amount of collateral that can be seized in exchange for the outstanding debt.
|
|
76
|
+
*/
|
|
77
|
+
get seizableCollateral() {
|
|
78
|
+
return this.market.getSeizableCollateral(this);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The maximum amount of collateral that can be withdrawn.
|
|
82
|
+
*/
|
|
83
|
+
get withdrawableCollateral() {
|
|
84
|
+
return this.market.getWithdrawableCollateral(this);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Whether this position is healthy.
|
|
88
|
+
*/
|
|
89
|
+
get isHealthy() {
|
|
90
|
+
return this.market.isHealthy(this);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The price of the collateral quoted in loan assets that would allow this position to be liquidated.
|
|
94
|
+
*/
|
|
95
|
+
get liquidationPrice() {
|
|
96
|
+
return this.market.getLiquidationPrice(this);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The variation of the price of the collateral quoted in loan assets that would allow this position to be liquidated,
|
|
100
|
+
* relative to the current collateral price (scaled by WAD).
|
|
101
|
+
*/
|
|
102
|
+
get priceVariationToLiquidation() {
|
|
103
|
+
return this.market.getPriceVariationToLiquidation(this);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* This position's Loan-To-Value (debt over collateral power, scaled by WAD).
|
|
107
|
+
* If the collateral price is 0, LTV is `MaxUint256`.
|
|
108
|
+
*/
|
|
109
|
+
get ltv() {
|
|
110
|
+
return this.market.getLtv(this);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* This position's health factor (collateral power over debt, scaled by WAD).
|
|
114
|
+
* If the debt is 0, health factor is `MaxUint256`.
|
|
115
|
+
*/
|
|
116
|
+
get healthFactor() {
|
|
117
|
+
return this.market.getHealthFactor(this);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The percentage of this position's borrow power currently used (scaled by WAD).
|
|
121
|
+
* If the collateral price is 0, usage is `MaxUint256`.
|
|
122
|
+
*/
|
|
123
|
+
get borrowCapacityUsage() {
|
|
124
|
+
return this.market.getBorrowCapacityUsage(this);
|
|
125
|
+
}
|
|
126
|
+
get borrowCapacityLimit() {
|
|
127
|
+
return this.market.getBorrowCapacityLimit(this.collateral);
|
|
128
|
+
}
|
|
129
|
+
get withdrawCapacityLimit() {
|
|
130
|
+
return this.market.getWithdrawCapacityLimit(this.supplyShares);
|
|
131
|
+
}
|
|
132
|
+
get withdrawCollateralCapacityLimit() {
|
|
133
|
+
return this.market.getWithdrawCollateralCapacityLimit(this);
|
|
134
|
+
}
|
|
135
|
+
accrueInterest(timestamp) {
|
|
136
|
+
return new AccrualPosition(this, this.market.accrueInterest(timestamp));
|
|
137
|
+
}
|
|
138
|
+
getRepayCapacityLimit(loanTokenBalance) {
|
|
139
|
+
return this.market.getRepayCapacityLimit(this.borrowShares, loanTokenBalance);
|
|
140
|
+
}
|
|
141
|
+
getMaxCapacities(loanTokenBalance, collateralTokenBalance) {
|
|
142
|
+
return this.market.getMaxCapacities(this, loanTokenBalance, collateralTokenBalance);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.AccrualPosition = AccrualPosition;
|