@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.
Files changed (151) hide show
  1. package/lib/addresses.d.ts +168 -0
  2. package/lib/addresses.js +169 -0
  3. package/lib/chain/chain.constants.d.ts +3 -0
  4. package/lib/chain/chain.constants.js +232 -0
  5. package/lib/chain/chain.types.d.ts +20 -0
  6. package/lib/chain/chain.types.js +30 -0
  7. package/lib/chain/chain.utils.d.ts +14 -0
  8. package/lib/chain/chain.utils.js +30 -0
  9. package/lib/chain/index.js +18 -0
  10. package/lib/constants.d.ts +8 -0
  11. package/lib/constants.js +13 -0
  12. package/lib/errors.d.ts +37 -0
  13. package/lib/errors.js +71 -0
  14. package/lib/ethers/index.js +18 -0
  15. package/lib/ethers/safeGetAddress.d.ts +1 -0
  16. package/lib/ethers/safeGetAddress.js +6 -0
  17. package/lib/ethers/safeParseUnits.d.ts +2 -0
  18. package/lib/ethers/safeParseUnits.js +25 -0
  19. package/lib/evm.d.ts +36 -0
  20. package/lib/evm.js +113 -0
  21. package/lib/helpers/format/format.d.ts +98 -0
  22. package/lib/helpers/format/format.js +301 -0
  23. package/lib/helpers/format/index.js +17 -0
  24. package/lib/helpers/getChecksumedAddress.d.ts +7 -0
  25. package/lib/helpers/getChecksumedAddress.js +17 -0
  26. package/lib/helpers/index.js +20 -0
  27. package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
  28. package/lib/helpers/isZeroAddressOrUnset.js +14 -0
  29. package/lib/helpers/locale.d.ts +36 -0
  30. package/lib/helpers/locale.js +86 -0
  31. package/lib/holding/Holding.d.ts +60 -0
  32. package/lib/holding/Holding.js +31 -0
  33. package/lib/holding/index.js +17 -0
  34. package/lib/index.d.ts +33 -0
  35. package/lib/index.js +62 -0
  36. package/lib/market/Market.d.ts +159 -0
  37. package/lib/market/Market.js +240 -0
  38. package/lib/market/MarketConfig.d.ts +44 -0
  39. package/lib/market/MarketConfig.js +56 -0
  40. package/lib/market/MarketUtils.d.ts +165 -0
  41. package/lib/market/MarketUtils.js +182 -0
  42. package/lib/market/index.js +19 -0
  43. package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
  44. package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
  45. package/lib/maths/MathLib.d.ts +94 -0
  46. package/lib/maths/MathLib.js +153 -0
  47. package/lib/maths/MathUtils.d.ts +15 -0
  48. package/lib/maths/MathUtils.js +33 -0
  49. package/lib/maths/SharesMath.d.ts +12 -0
  50. package/lib/maths/SharesMath.js +22 -0
  51. package/lib/maths/index.js +20 -0
  52. package/lib/notifications.d.ts +98 -0
  53. package/lib/notifications.js +52 -0
  54. package/lib/position/Position.d.ts +118 -0
  55. package/lib/position/Position.js +145 -0
  56. package/lib/position/index.js +17 -0
  57. package/lib/signatures/index.d.ts +12 -0
  58. package/lib/signatures/index.js +39 -0
  59. package/lib/signatures/manager.d.ts +10 -0
  60. package/lib/signatures/manager.js +37 -0
  61. package/lib/signatures/permit.d.ts +21 -0
  62. package/lib/signatures/permit.js +101 -0
  63. package/lib/signatures/permit2.d.ts +20 -0
  64. package/lib/signatures/permit2.js +91 -0
  65. package/lib/signatures/types.d.ts +13 -0
  66. package/lib/signatures/types.js +2 -0
  67. package/lib/signatures/utils.d.ts +6 -0
  68. package/lib/signatures/utils.js +44 -0
  69. package/lib/tests/mocks/markets.d.ts +17 -0
  70. package/lib/tests/mocks/markets.js +108 -0
  71. package/lib/token/ERC20Metadata.d.ts +249 -0
  72. package/lib/token/ERC20Metadata.js +81 -0
  73. package/lib/token/Token.d.ts +45 -0
  74. package/lib/token/Token.js +39 -0
  75. package/lib/token/TokenNamespace.d.ts +18 -0
  76. package/lib/token/TokenNamespace.js +55 -0
  77. package/lib/token/WrappedToken.d.ts +42 -0
  78. package/lib/token/WrappedToken.js +87 -0
  79. package/lib/token/index.js +18 -0
  80. package/lib/types.d.ts +29 -0
  81. package/lib/types.js +23 -0
  82. package/lib/user/User.d.ts +20 -0
  83. package/lib/user/User.js +11 -0
  84. package/lib/user/index.js +18 -0
  85. package/lib/user/user.types.d.ts +18 -0
  86. package/lib/user/user.types.js +2 -0
  87. package/lib/vault/Vault.d.ts +167 -0
  88. package/lib/vault/Vault.js +156 -0
  89. package/lib/vault/VaultAllocation.d.ts +38 -0
  90. package/lib/vault/VaultAllocation.js +18 -0
  91. package/lib/vault/VaultConfig.d.ts +23 -0
  92. package/lib/vault/VaultConfig.js +26 -0
  93. package/lib/vault/VaultUtils.d.ts +17 -0
  94. package/lib/vault/VaultUtils.js +17 -0
  95. package/lib/vault/index.js +20 -0
  96. package/package.json +5 -5
  97. package/src/addresses.ts +0 -261
  98. package/src/chain/chain.constants.ts +0 -235
  99. package/src/chain/chain.test.ts +0 -51
  100. package/src/chain/chain.types.ts +0 -42
  101. package/src/chain/chain.utils.ts +0 -44
  102. package/src/constants.ts +0 -18
  103. package/src/errors.ts +0 -75
  104. package/src/ethers/ethers.test.ts +0 -17
  105. package/src/ethers/safeGetAddress.ts +0 -4
  106. package/src/ethers/safeParseUnits.ts +0 -29
  107. package/src/evm.ts +0 -172
  108. package/src/helpers/format/format.test.ts +0 -340
  109. package/src/helpers/format/format.ts +0 -416
  110. package/src/helpers/getChecksumedAddress.ts +0 -15
  111. package/src/helpers/locale.ts +0 -108
  112. package/src/holding/Holding.ts +0 -109
  113. package/src/market/Market.ts +0 -479
  114. package/src/market/MarketConfig.ts +0 -108
  115. package/src/market/MarketUtils.test.ts +0 -25
  116. package/src/market/MarketUtils.ts +0 -467
  117. package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
  118. package/src/maths/MathLib.ts +0 -208
  119. package/src/maths/MathUtils.ts +0 -31
  120. package/src/maths/SharesMath.ts +0 -40
  121. package/src/notifications.ts +0 -167
  122. package/src/position/Position.ts +0 -251
  123. package/src/signatures/index.ts +0 -18
  124. package/src/signatures/manager.ts +0 -50
  125. package/src/signatures/permit.ts +0 -126
  126. package/src/signatures/permit2.ts +0 -120
  127. package/src/signatures/types.ts +0 -18
  128. package/src/signatures/utils.ts +0 -83
  129. package/src/tests/mocks/markets.ts +0 -110
  130. package/src/token/ERC20Metadata.ts +0 -124
  131. package/src/token/Token.ts +0 -83
  132. package/src/token/TokenNamespace.ts +0 -76
  133. package/src/token/WrappedToken.ts +0 -142
  134. package/src/types.ts +0 -37
  135. package/src/user/User.ts +0 -32
  136. package/src/user/user.types.ts +0 -23
  137. package/src/vault/Vault.ts +0 -370
  138. package/src/vault/VaultAllocation.ts +0 -58
  139. package/src/vault/VaultConfig.ts +0 -55
  140. package/src/vault/VaultUtils.ts +0 -47
  141. /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
  142. /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
  143. /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
  144. /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
  145. /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
  146. /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
  147. /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
  148. /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
  149. /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
  150. /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
  151. /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
@@ -0,0 +1,249 @@
1
+ import { ContractRunner } from "ethers";
2
+ import { ERC20, ERC20Interface } from "ethers-types/dist/token/ERC20/ERC20";
3
+ import { ChainId } from "../chain";
4
+ export declare const isBytes32ERC20Metadata: (address: string, chainId: ChainId) => boolean;
5
+ export declare const decodeString: (bytes32OrStr: string) => string;
6
+ export declare class Bytes32ERC20__factory {
7
+ static readonly abi: readonly [...({
8
+ readonly inputs: readonly [{
9
+ readonly internalType: "string";
10
+ readonly name: "name_";
11
+ readonly type: "string";
12
+ }, {
13
+ readonly internalType: "string";
14
+ readonly name: "symbol_";
15
+ readonly type: "string";
16
+ }];
17
+ readonly stateMutability: "nonpayable";
18
+ readonly type: "constructor";
19
+ } | {
20
+ readonly anonymous: false;
21
+ readonly inputs: readonly [{
22
+ readonly indexed: true;
23
+ readonly internalType: "address";
24
+ readonly name: "owner";
25
+ readonly type: "address";
26
+ }, {
27
+ readonly indexed: true;
28
+ readonly internalType: "address";
29
+ readonly name: "spender";
30
+ readonly type: "address";
31
+ }, {
32
+ readonly indexed: false;
33
+ readonly internalType: "uint256";
34
+ readonly name: "value";
35
+ readonly type: "uint256";
36
+ }];
37
+ readonly name: "Approval";
38
+ readonly type: "event";
39
+ } | {
40
+ readonly anonymous: false;
41
+ readonly inputs: readonly [{
42
+ readonly indexed: true;
43
+ readonly internalType: "address";
44
+ readonly name: "from";
45
+ readonly type: "address";
46
+ }, {
47
+ readonly indexed: true;
48
+ readonly internalType: "address";
49
+ readonly name: "to";
50
+ readonly type: "address";
51
+ }, {
52
+ readonly indexed: false;
53
+ readonly internalType: "uint256";
54
+ readonly name: "value";
55
+ readonly type: "uint256";
56
+ }];
57
+ readonly name: "Transfer";
58
+ readonly type: "event";
59
+ } | {
60
+ readonly inputs: readonly [{
61
+ readonly internalType: "address";
62
+ readonly name: "owner";
63
+ readonly type: "address";
64
+ }, {
65
+ readonly internalType: "address";
66
+ readonly name: "spender";
67
+ readonly type: "address";
68
+ }];
69
+ readonly name: "allowance";
70
+ readonly outputs: readonly [{
71
+ readonly internalType: "uint256";
72
+ readonly name: "";
73
+ readonly type: "uint256";
74
+ }];
75
+ readonly stateMutability: "view";
76
+ readonly type: "function";
77
+ } | {
78
+ readonly inputs: readonly [{
79
+ readonly internalType: "address";
80
+ readonly name: "spender";
81
+ readonly type: "address";
82
+ }, {
83
+ readonly internalType: "uint256";
84
+ readonly name: "amount";
85
+ readonly type: "uint256";
86
+ }];
87
+ readonly name: "approve";
88
+ readonly outputs: readonly [{
89
+ readonly internalType: "bool";
90
+ readonly name: "";
91
+ readonly type: "bool";
92
+ }];
93
+ readonly stateMutability: "nonpayable";
94
+ readonly type: "function";
95
+ } | {
96
+ readonly inputs: readonly [{
97
+ readonly internalType: "address";
98
+ readonly name: "account";
99
+ readonly type: "address";
100
+ }];
101
+ readonly name: "balanceOf";
102
+ readonly outputs: readonly [{
103
+ readonly internalType: "uint256";
104
+ readonly name: "";
105
+ readonly type: "uint256";
106
+ }];
107
+ readonly stateMutability: "view";
108
+ readonly type: "function";
109
+ } | {
110
+ readonly inputs: readonly [];
111
+ readonly name: "decimals";
112
+ readonly outputs: readonly [{
113
+ readonly internalType: "uint8";
114
+ readonly name: "";
115
+ readonly type: "uint8";
116
+ }];
117
+ readonly stateMutability: "view";
118
+ readonly type: "function";
119
+ } | {
120
+ readonly inputs: readonly [{
121
+ readonly internalType: "address";
122
+ readonly name: "spender";
123
+ readonly type: "address";
124
+ }, {
125
+ readonly internalType: "uint256";
126
+ readonly name: "subtractedValue";
127
+ readonly type: "uint256";
128
+ }];
129
+ readonly name: "decreaseAllowance";
130
+ readonly outputs: readonly [{
131
+ readonly internalType: "bool";
132
+ readonly name: "";
133
+ readonly type: "bool";
134
+ }];
135
+ readonly stateMutability: "nonpayable";
136
+ readonly type: "function";
137
+ } | {
138
+ readonly inputs: readonly [{
139
+ readonly internalType: "address";
140
+ readonly name: "spender";
141
+ readonly type: "address";
142
+ }, {
143
+ readonly internalType: "uint256";
144
+ readonly name: "addedValue";
145
+ readonly type: "uint256";
146
+ }];
147
+ readonly name: "increaseAllowance";
148
+ readonly outputs: readonly [{
149
+ readonly internalType: "bool";
150
+ readonly name: "";
151
+ readonly type: "bool";
152
+ }];
153
+ readonly stateMutability: "nonpayable";
154
+ readonly type: "function";
155
+ } | {
156
+ readonly inputs: readonly [];
157
+ readonly name: "name";
158
+ readonly outputs: readonly [{
159
+ readonly internalType: "string";
160
+ readonly name: "";
161
+ readonly type: "string";
162
+ }];
163
+ readonly stateMutability: "view";
164
+ readonly type: "function";
165
+ } | {
166
+ readonly inputs: readonly [];
167
+ readonly name: "symbol";
168
+ readonly outputs: readonly [{
169
+ readonly internalType: "string";
170
+ readonly name: "";
171
+ readonly type: "string";
172
+ }];
173
+ readonly stateMutability: "view";
174
+ readonly type: "function";
175
+ } | {
176
+ readonly inputs: readonly [];
177
+ readonly name: "totalSupply";
178
+ readonly outputs: readonly [{
179
+ readonly internalType: "uint256";
180
+ readonly name: "";
181
+ readonly type: "uint256";
182
+ }];
183
+ readonly stateMutability: "view";
184
+ readonly type: "function";
185
+ } | {
186
+ readonly inputs: readonly [{
187
+ readonly internalType: "address";
188
+ readonly name: "to";
189
+ readonly type: "address";
190
+ }, {
191
+ readonly internalType: "uint256";
192
+ readonly name: "amount";
193
+ readonly type: "uint256";
194
+ }];
195
+ readonly name: "transfer";
196
+ readonly outputs: readonly [{
197
+ readonly internalType: "bool";
198
+ readonly name: "";
199
+ readonly type: "bool";
200
+ }];
201
+ readonly stateMutability: "nonpayable";
202
+ readonly type: "function";
203
+ } | {
204
+ readonly inputs: readonly [{
205
+ readonly internalType: "address";
206
+ readonly name: "from";
207
+ readonly type: "address";
208
+ }, {
209
+ readonly internalType: "address";
210
+ readonly name: "to";
211
+ readonly type: "address";
212
+ }, {
213
+ readonly internalType: "uint256";
214
+ readonly name: "amount";
215
+ readonly type: "uint256";
216
+ }];
217
+ readonly name: "transferFrom";
218
+ readonly outputs: readonly [{
219
+ readonly internalType: "bool";
220
+ readonly name: "";
221
+ readonly type: "bool";
222
+ }];
223
+ readonly stateMutability: "nonpayable";
224
+ readonly type: "function";
225
+ })[], {
226
+ readonly inputs: readonly [];
227
+ readonly name: "symbol";
228
+ readonly outputs: readonly [{
229
+ readonly name: "symbol";
230
+ readonly type: "bytes32";
231
+ }];
232
+ readonly stateMutability: "view";
233
+ readonly type: "function";
234
+ }, {
235
+ readonly inputs: readonly [];
236
+ readonly name: "name";
237
+ readonly outputs: readonly [{
238
+ readonly name: "name";
239
+ readonly type: "bytes32";
240
+ }];
241
+ readonly stateMutability: "view";
242
+ readonly type: "function";
243
+ }];
244
+ static createInterface(): ERC20Interface;
245
+ static connect(address: string, runner?: ContractRunner | null): ERC20;
246
+ }
247
+ export declare class ERC20Metadata__factory {
248
+ static connect(address: string, chainId: ChainId, runner?: ContractRunner | null): ERC20;
249
+ }
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ERC20Metadata__factory = exports.Bytes32ERC20__factory = exports.decodeString = exports.isBytes32ERC20Metadata = void 0;
7
+ const ethers_1 = require("ethers");
8
+ const ethers_types_1 = require("ethers-types");
9
+ const addresses_1 = __importDefault(require("@morpho-org/blue-sdk/src/addresses"));
10
+ const chain_1 = require("../chain");
11
+ const isBytes32ERC20Metadata = (address, chainId) => {
12
+ switch (chainId) {
13
+ case chain_1.ChainId.EthMainnet:
14
+ return [addresses_1.default[chain_1.ChainId.EthMainnet].mkr].includes(address);
15
+ default:
16
+ return false;
17
+ }
18
+ };
19
+ exports.isBytes32ERC20Metadata = isBytes32ERC20Metadata;
20
+ const decodeString = (bytes32OrStr) => {
21
+ if ((0, ethers_1.isHexString)(bytes32OrStr, 32))
22
+ return (0, ethers_1.decodeBytes32String)(bytes32OrStr);
23
+ return bytes32OrStr;
24
+ };
25
+ exports.decodeString = decodeString;
26
+ const _bytes32ERC20Abi = [
27
+ ...ethers_types_1.ERC20__factory.abi.filter((fragment) => !("name" in fragment) ||
28
+ (fragment.name !== "name" && fragment.name !== "symbol")),
29
+ {
30
+ inputs: [],
31
+ name: "symbol",
32
+ outputs: [
33
+ {
34
+ name: "symbol",
35
+ type: "bytes32",
36
+ },
37
+ ],
38
+ stateMutability: "view",
39
+ type: "function",
40
+ },
41
+ {
42
+ inputs: [],
43
+ name: "name",
44
+ outputs: [
45
+ {
46
+ name: "name",
47
+ type: "bytes32",
48
+ },
49
+ ],
50
+ stateMutability: "view",
51
+ type: "function",
52
+ },
53
+ ];
54
+ class Bytes32ERC20__factory {
55
+ static createInterface() {
56
+ return new ethers_1.Interface(_bytes32ERC20Abi);
57
+ }
58
+ static connect(address, runner) {
59
+ const erc20 = new ethers_1.Contract(address, _bytes32ERC20Abi, runner);
60
+ const name = erc20.name.bind(erc20);
61
+ erc20.name = Object.assign((...args) => name(...args).then(exports.decodeString), name);
62
+ const symbol = erc20.symbol.bind(erc20);
63
+ erc20.symbol = Object.assign((...args) => symbol(...args).then(exports.decodeString), symbol);
64
+ return erc20;
65
+ }
66
+ }
67
+ exports.Bytes32ERC20__factory = Bytes32ERC20__factory;
68
+ Bytes32ERC20__factory.abi = _bytes32ERC20Abi;
69
+ class ERC20Metadata__factory {
70
+ static connect(address, chainId, runner) {
71
+ if ((0, exports.isBytes32ERC20Metadata)(address, chainId))
72
+ return Bytes32ERC20__factory.connect(address, runner);
73
+ const erc20 = ethers_types_1.ERC20__factory.connect(address, runner);
74
+ const name = erc20.name.bind(erc20);
75
+ erc20.name = Object.assign((...args) => name(...args).catch(() => Bytes32ERC20__factory.connect(address, runner).name(...args)), name);
76
+ const symbol = erc20.symbol.bind(erc20);
77
+ erc20.symbol = Object.assign((...args) => symbol(...args).catch(() => Bytes32ERC20__factory.connect(address, runner).symbol(...args)), symbol);
78
+ return erc20;
79
+ }
80
+ }
81
+ exports.ERC20Metadata__factory = ERC20Metadata__factory;
@@ -0,0 +1,45 @@
1
+ import { BigNumberish } from "ethers";
2
+ import { RoundingDirection } from "../maths";
3
+ import { Address } from "../types";
4
+ export interface InputToken {
5
+ address: Address;
6
+ decimals: number;
7
+ symbol: string;
8
+ name?: string;
9
+ }
10
+ export declare class Token implements InputToken {
11
+ /**
12
+ * The token's address.
13
+ */
14
+ readonly address: Address;
15
+ /**
16
+ * The token's number of decimals.
17
+ */
18
+ readonly decimals: number;
19
+ /**
20
+ * The token's symbol.
21
+ */
22
+ readonly symbol: string;
23
+ /**
24
+ * The name of the token (defaults to the symbol).
25
+ */
26
+ readonly name: string;
27
+ constructor({ address, decimals, symbol, name }: InputToken);
28
+ }
29
+ export declare class TokenWithPrice extends Token {
30
+ /**
31
+ * Price of the token in USD (scaled by WAD).
32
+ */
33
+ price?: bigint;
34
+ constructor(token: InputToken, price?: bigint);
35
+ /**
36
+ * Quotes an amount in USD (scaled by WAD) in this token.
37
+ * @param amount The amount of USD to quote.
38
+ */
39
+ fromUsd(amount: BigNumberish, rounding?: RoundingDirection): bigint | null;
40
+ /**
41
+ * Quotes an amount of tokens in USD (scaled by WAD).
42
+ * @param amount The amount of tokens to quote.
43
+ */
44
+ toUsd(amount: BigNumberish, rounding?: RoundingDirection): bigint | null;
45
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenWithPrice = exports.Token = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const maths_1 = require("../maths");
6
+ class Token {
7
+ constructor({ address, decimals, symbol, name }) {
8
+ this.address = address;
9
+ this.decimals = decimals;
10
+ this.symbol = symbol;
11
+ this.name = name ?? symbol;
12
+ }
13
+ }
14
+ exports.Token = Token;
15
+ class TokenWithPrice extends Token {
16
+ constructor(token, price) {
17
+ super(token);
18
+ this.price = price;
19
+ }
20
+ /**
21
+ * Quotes an amount in USD (scaled by WAD) in this token.
22
+ * @param amount The amount of USD to quote.
23
+ */
24
+ fromUsd(amount, rounding = "Down") {
25
+ if (this.price == null)
26
+ return null;
27
+ return maths_1.MathLib.mulDiv(amount, (0, ethers_1.parseUnits)("1", this.decimals), this.price, rounding);
28
+ }
29
+ /**
30
+ * Quotes an amount of tokens in USD (scaled by WAD).
31
+ * @param amount The amount of tokens to quote.
32
+ */
33
+ toUsd(amount, rounding = "Down") {
34
+ if (this.price == null)
35
+ return null;
36
+ return maths_1.MathLib.mulDiv(amount, this.price, (0, ethers_1.parseUnits)("1", this.decimals), rounding);
37
+ }
38
+ }
39
+ exports.TokenWithPrice = TokenWithPrice;
@@ -0,0 +1,18 @@
1
+ import { Provider } from "ethers";
2
+ import { ViewOverrides } from "ethers-types/dist/common";
3
+ import { ChainId } from "../chain";
4
+ import { Address } from "../types";
5
+ import { Token as TokenClass } from "./Token";
6
+ import { ConstantWrappedToken, ExchangeRateWrappedToken } from "./WrappedToken";
7
+ export declare class Token extends TokenClass {
8
+ }
9
+ export { TokenWithPrice } from "./Token";
10
+ export declare namespace Token {
11
+ function native(chainId: ChainId): Token;
12
+ function fetch(address: Address, runner: {
13
+ provider: Provider;
14
+ }, { chainId, overrides, }?: {
15
+ chainId?: ChainId;
16
+ overrides?: ViewOverrides;
17
+ }): Promise<Token | ConstantWrappedToken | ExchangeRateWrappedToken>;
18
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenWithPrice = exports.Token = void 0;
4
+ const ethers_types_1 = require("ethers-types");
5
+ const addresses_1 = require("../addresses");
6
+ const chain_1 = require("../chain");
7
+ const ERC20Metadata_1 = require("./ERC20Metadata");
8
+ const Token_1 = require("./Token");
9
+ const WrappedToken_1 = require("./WrappedToken");
10
+ class Token extends Token_1.Token {
11
+ }
12
+ exports.Token = Token;
13
+ var Token_2 = require("./Token");
14
+ Object.defineProperty(exports, "TokenWithPrice", { enumerable: true, get: function () { return Token_2.TokenWithPrice; } });
15
+ (function (Token) {
16
+ function native(chainId) {
17
+ const currency = chain_1.ChainUtils.chainMetadata[chainId].nativeCurrency;
18
+ return new Token({ ...currency, address: addresses_1.NATIVE_ADDRESS });
19
+ }
20
+ Token.native = native;
21
+ async function fetch(address, runner, { chainId, overrides = {}, } = {}) {
22
+ chainId ?? (chainId = chain_1.ChainUtils.parseSupportedChainId((await runner.provider.getNetwork()).chainId));
23
+ if (address === addresses_1.NATIVE_ADDRESS)
24
+ return Token.native(chainId);
25
+ const erc20 = ERC20Metadata_1.ERC20Metadata__factory.connect(address, chainId, runner);
26
+ const [decimals, symbol, name] = await Promise.all([
27
+ erc20.decimals(overrides),
28
+ erc20.symbol(overrides),
29
+ erc20.name(overrides),
30
+ ]);
31
+ const token = {
32
+ address,
33
+ decimals: parseInt(decimals.toString()),
34
+ symbol,
35
+ name,
36
+ };
37
+ const { wstEth, stEth } = (0, addresses_1.getChainAddresses)(chainId);
38
+ switch (address) {
39
+ case wstEth: {
40
+ if (stEth) {
41
+ const wstEthToken = ethers_types_1.WStEth__factory.connect(wstEth, runner);
42
+ const stEthPerWstEth = await wstEthToken.stEthPerToken(overrides);
43
+ return new WrappedToken_1.ExchangeRateWrappedToken(token, stEth, stEthPerWstEth);
44
+ }
45
+ break;
46
+ }
47
+ }
48
+ const unwrapToken = (0, addresses_1.getUnwrappedToken)(address, chainId);
49
+ if (unwrapToken) {
50
+ return new WrappedToken_1.ConstantWrappedToken(token, unwrapToken, token.decimals); //TODO fetch underlying token's decimals if it can be different form token's decimals
51
+ }
52
+ return new Token(token);
53
+ }
54
+ Token.fetch = fetch;
55
+ })(Token || (exports.Token = Token = {}));
@@ -0,0 +1,42 @@
1
+ import { RoundingDirection } from "../maths";
2
+ import { Address } from "../types";
3
+ import { Vault } from "../vault";
4
+ import { InputToken, Token } from "./Token";
5
+ export declare abstract class WrappedToken extends Token {
6
+ readonly underlying: Address;
7
+ protected abstract _wrap(amount: bigint, rounding: RoundingDirection): bigint;
8
+ protected abstract _unwrap(amount: bigint, rounding: RoundingDirection): bigint;
9
+ protected _noSlippage: boolean;
10
+ constructor(token: InputToken, underlying: Address);
11
+ /** The expected amount when wrapping `unwrappedAmount` */
12
+ toWrappedExactAmountIn(unwrappedAmount: bigint, slippage?: bigint, rounding?: RoundingDirection): bigint;
13
+ /** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
14
+ toWrappedExactAmountOut(wrappedAmount: bigint, slippage?: bigint, rounding?: RoundingDirection): bigint;
15
+ /** The expected amount when unwrapping `wrappedAmount` */
16
+ toUnwrappedExactAmountIn(wrappedAmount: bigint, slippage?: bigint, rounding?: RoundingDirection): bigint;
17
+ /** The amount of wrappedTokens that should be unwrapped to receive `unwrappedAmount` */
18
+ toUnwrappedExactAmountOut(unwrappedAmount: bigint, slippage?: bigint, rounding?: RoundingDirection): bigint;
19
+ }
20
+ export declare class ConstantWrappedToken extends WrappedToken {
21
+ readonly underlying: Address;
22
+ private readonly _underlyingDecimals;
23
+ protected _noSlippage: boolean;
24
+ constructor(token: InputToken, underlying: Address, _underlyingDecimals?: number);
25
+ protected _wrap(amount: bigint): bigint;
26
+ protected _unwrap(amount: bigint): bigint;
27
+ }
28
+ export declare class ExchangeRateWrappedToken extends WrappedToken {
29
+ readonly underlying: Address;
30
+ wrappedTokenExchangeRate: bigint;
31
+ protected _wrap(amount: bigint, rounding: RoundingDirection): bigint;
32
+ protected _unwrap(amount: bigint, rounding: RoundingDirection): bigint;
33
+ constructor(token: InputToken, underlying: Address, wrappedTokenExchangeRate: bigint);
34
+ }
35
+ export declare class VaultToken extends WrappedToken {
36
+ readonly underlying: Address;
37
+ protected _wrap(amount: bigint, rounding: RoundingDirection): bigint;
38
+ protected _unwrap(amount: bigint, rounding: RoundingDirection): bigint;
39
+ totalAssets: bigint;
40
+ totalSupply: bigint;
41
+ constructor(token: InputToken, underlying: Address, { totalAssets, totalSupply }: Pick<Vault, "totalAssets" | "totalSupply">);
42
+ }
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultToken = exports.ExchangeRateWrappedToken = exports.ConstantWrappedToken = exports.WrappedToken = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const ethers_2 = require("../ethers");
6
+ const maths_1 = require("../maths");
7
+ const Token_1 = require("./Token");
8
+ class WrappedToken extends Token_1.Token {
9
+ constructor(token, underlying) {
10
+ super(token);
11
+ this.underlying = underlying;
12
+ this._noSlippage = false;
13
+ }
14
+ /** The expected amount when wrapping `unwrappedAmount` */
15
+ toWrappedExactAmountIn(unwrappedAmount, slippage = 0n, rounding = "Down") {
16
+ const wrappedAmount = this._wrap(unwrappedAmount, rounding);
17
+ if (this._noSlippage)
18
+ return wrappedAmount;
19
+ return maths_1.MathLib.wMul(wrappedAmount, maths_1.MathLib.WAD - slippage, "Down");
20
+ }
21
+ /** The amount of unwrappedTokens that should be wrapped to receive `wrappedAmount` */
22
+ toWrappedExactAmountOut(wrappedAmount, slippage = 0n, rounding = "Up") {
23
+ const wAmountTarget = this._noSlippage
24
+ ? wrappedAmount
25
+ : maths_1.MathLib.wDiv(wrappedAmount, maths_1.MathLib.WAD - slippage, rounding);
26
+ return this._unwrap(wAmountTarget, rounding);
27
+ }
28
+ /** The expected amount when unwrapping `wrappedAmount` */
29
+ toUnwrappedExactAmountIn(wrappedAmount, slippage = 0n, rounding = "Down") {
30
+ const unwrappedAmount = this._unwrap(wrappedAmount, rounding);
31
+ if (this._noSlippage)
32
+ return unwrappedAmount;
33
+ return maths_1.MathLib.wMul(unwrappedAmount, maths_1.MathLib.WAD - slippage, "Up");
34
+ }
35
+ /** The amount of wrappedTokens that should be unwrapped to receive `unwrappedAmount` */
36
+ toUnwrappedExactAmountOut(unwrappedAmount, slippage = 0n, rounding = "Up") {
37
+ const unwrappedAmountToTarget = this._noSlippage
38
+ ? unwrappedAmount
39
+ : maths_1.MathLib.wDiv(unwrappedAmount, maths_1.MathLib.WAD - slippage, rounding);
40
+ return this._wrap(unwrappedAmountToTarget, rounding);
41
+ }
42
+ }
43
+ exports.WrappedToken = WrappedToken;
44
+ class ConstantWrappedToken extends WrappedToken {
45
+ constructor(token, underlying, _underlyingDecimals = 18) {
46
+ super(token, underlying);
47
+ this.underlying = underlying;
48
+ this._underlyingDecimals = _underlyingDecimals;
49
+ this._noSlippage = true;
50
+ }
51
+ _wrap(amount) {
52
+ return (0, ethers_2.safeParseUnits)((0, ethers_1.formatUnits)(amount, this._underlyingDecimals), this.decimals);
53
+ }
54
+ _unwrap(amount) {
55
+ return (0, ethers_2.safeParseUnits)((0, ethers_1.formatUnits)(amount, this.decimals), this._underlyingDecimals);
56
+ }
57
+ }
58
+ exports.ConstantWrappedToken = ConstantWrappedToken;
59
+ class ExchangeRateWrappedToken extends WrappedToken {
60
+ _wrap(amount, rounding) {
61
+ return maths_1.MathLib.wDiv(amount, this.wrappedTokenExchangeRate, rounding);
62
+ }
63
+ _unwrap(amount, rounding) {
64
+ return maths_1.MathLib.wMul(amount, this.wrappedTokenExchangeRate, rounding);
65
+ }
66
+ constructor(token, underlying, wrappedTokenExchangeRate) {
67
+ super(token, underlying);
68
+ this.underlying = underlying;
69
+ this.wrappedTokenExchangeRate = wrappedTokenExchangeRate;
70
+ }
71
+ }
72
+ exports.ExchangeRateWrappedToken = ExchangeRateWrappedToken;
73
+ class VaultToken extends WrappedToken {
74
+ _wrap(amount, rounding) {
75
+ return maths_1.SharesMath.toShares(amount, this.totalAssets, this.totalSupply, rounding);
76
+ }
77
+ _unwrap(amount, rounding) {
78
+ return maths_1.SharesMath.toAssets(amount, this.totalAssets, this.totalSupply, rounding);
79
+ }
80
+ constructor(token, underlying, { totalAssets, totalSupply }) {
81
+ super(token, underlying);
82
+ this.underlying = underlying;
83
+ this.totalAssets = totalAssets;
84
+ this.totalSupply = totalSupply;
85
+ }
86
+ }
87
+ exports.VaultToken = VaultToken;
@@ -0,0 +1,18 @@
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("./TokenNamespace"), exports);
18
+ __exportStar(require("./WrappedToken"), exports);
package/lib/types.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ export type Hex64 = string & {
2
+ __LENGTH__: 64;
3
+ };
4
+ /**
5
+ * The address of a Contract, or an EOA
6
+ */
7
+ export type Address = string;
8
+ /**
9
+ * The id of a market used on the Blue contract
10
+ */
11
+ export type MarketId = `0x${Hex64}` & {
12
+ __TYPE__: "marketId";
13
+ };
14
+ /**
15
+ * The possible transaction type on the Blue contract
16
+ */
17
+ export declare enum TransactionType {
18
+ Supply = "Supply",
19
+ SupplyCollateral = "Supply Collateral",
20
+ Withdraw = "Withdraw",
21
+ WithdrawCollateral = "Withdraw Collateral",
22
+ Borrow = "Borrow",
23
+ Repay = "Repay"
24
+ }
25
+ export type Loadable<T> = T | undefined;
26
+ export type Failable<T> = T | null;
27
+ export type Fetchable<T> = Failable<Loadable<T>>;
28
+ export declare function isFetched<T>(v: Fetchable<T>): v is T;
29
+ export declare const isMarketId: (value: any) => value is MarketId;