@morpho-org/blue-sdk 1.0.2 → 1.0.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.
Files changed (159) hide show
  1. package/lib/addresses.d.ts +156 -0
  2. package/lib/addresses.js +160 -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.test.d.ts +1 -0
  6. package/lib/chain/chain.test.js +37 -0
  7. package/lib/chain/chain.types.d.ts +20 -0
  8. package/lib/chain/chain.types.js +30 -0
  9. package/lib/chain/chain.utils.d.ts +14 -0
  10. package/lib/chain/chain.utils.js +30 -0
  11. package/lib/chain/index.js +18 -0
  12. package/lib/constants.d.ts +8 -0
  13. package/lib/constants.js +13 -0
  14. package/lib/errors.d.ts +37 -0
  15. package/lib/errors.js +71 -0
  16. package/lib/ethers/ethers.test.d.ts +1 -0
  17. package/lib/ethers/ethers.test.js +11 -0
  18. package/lib/ethers/index.js +18 -0
  19. package/lib/ethers/safeGetAddress.d.ts +1 -0
  20. package/lib/ethers/safeGetAddress.js +6 -0
  21. package/lib/ethers/safeParseUnits.d.ts +2 -0
  22. package/lib/ethers/safeParseUnits.js +25 -0
  23. package/lib/evm.d.ts +36 -0
  24. package/lib/evm.js +113 -0
  25. package/lib/helpers/format/format.d.ts +98 -0
  26. package/lib/helpers/format/format.js +301 -0
  27. package/lib/helpers/format/format.test.d.ts +1 -0
  28. package/lib/helpers/format/format.test.js +257 -0
  29. package/lib/helpers/format/index.js +17 -0
  30. package/lib/helpers/getChecksumedAddress.d.ts +7 -0
  31. package/lib/helpers/getChecksumedAddress.js +17 -0
  32. package/lib/helpers/index.js +20 -0
  33. package/{src/helpers/isZeroAddressOrUnset.ts → lib/helpers/isZeroAddressOrUnset.d.ts} +1 -7
  34. package/lib/helpers/isZeroAddressOrUnset.js +14 -0
  35. package/lib/helpers/locale.d.ts +36 -0
  36. package/lib/helpers/locale.js +86 -0
  37. package/lib/holding/Holding.d.ts +60 -0
  38. package/lib/holding/Holding.js +31 -0
  39. package/lib/holding/index.js +17 -0
  40. package/{src/index.ts → lib/index.d.ts} +0 -1
  41. package/lib/index.js +62 -0
  42. package/lib/market/Market.d.ts +159 -0
  43. package/lib/market/Market.js +240 -0
  44. package/lib/market/MarketConfig.d.ts +44 -0
  45. package/lib/market/MarketConfig.js +56 -0
  46. package/lib/market/MarketUtils.d.ts +165 -0
  47. package/lib/market/MarketUtils.js +182 -0
  48. package/lib/market/MarketUtils.test.d.ts +1 -0
  49. package/lib/market/MarketUtils.test.js +19 -0
  50. package/lib/market/index.js +19 -0
  51. package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
  52. package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
  53. package/lib/maths/MathLib.d.ts +94 -0
  54. package/lib/maths/MathLib.js +153 -0
  55. package/lib/maths/MathUtils.d.ts +15 -0
  56. package/lib/maths/MathUtils.js +33 -0
  57. package/lib/maths/SharesMath.d.ts +12 -0
  58. package/lib/maths/SharesMath.js +22 -0
  59. package/lib/maths/index.js +20 -0
  60. package/lib/notifications.d.ts +98 -0
  61. package/lib/notifications.js +52 -0
  62. package/lib/position/Position.d.ts +118 -0
  63. package/lib/position/Position.js +145 -0
  64. package/lib/position/index.js +17 -0
  65. package/lib/signatures/index.d.ts +12 -0
  66. package/lib/signatures/index.js +39 -0
  67. package/lib/signatures/manager.d.ts +10 -0
  68. package/lib/signatures/manager.js +37 -0
  69. package/lib/signatures/permit.d.ts +21 -0
  70. package/lib/signatures/permit.js +101 -0
  71. package/lib/signatures/permit2.d.ts +20 -0
  72. package/lib/signatures/permit2.js +91 -0
  73. package/lib/signatures/types.d.ts +13 -0
  74. package/lib/signatures/types.js +2 -0
  75. package/lib/signatures/utils.d.ts +6 -0
  76. package/lib/signatures/utils.js +44 -0
  77. package/lib/tests/mocks/markets.d.ts +17 -0
  78. package/lib/tests/mocks/markets.js +108 -0
  79. package/lib/token/ERC20Metadata.d.ts +249 -0
  80. package/lib/token/ERC20Metadata.js +81 -0
  81. package/lib/token/Token.d.ts +45 -0
  82. package/lib/token/Token.js +39 -0
  83. package/lib/token/TokenNamespace.d.ts +18 -0
  84. package/lib/token/TokenNamespace.js +55 -0
  85. package/lib/token/WrappedToken.d.ts +42 -0
  86. package/lib/token/WrappedToken.js +87 -0
  87. package/lib/token/index.js +18 -0
  88. package/lib/types.d.ts +29 -0
  89. package/lib/types.js +23 -0
  90. package/lib/user/User.d.ts +20 -0
  91. package/lib/user/User.js +11 -0
  92. package/lib/user/index.js +18 -0
  93. package/lib/user/user.types.d.ts +18 -0
  94. package/lib/user/user.types.js +2 -0
  95. package/lib/vault/Vault.d.ts +167 -0
  96. package/lib/vault/Vault.js +156 -0
  97. package/lib/vault/VaultAllocation.d.ts +38 -0
  98. package/lib/vault/VaultAllocation.js +18 -0
  99. package/lib/vault/VaultConfig.d.ts +23 -0
  100. package/lib/vault/VaultConfig.js +26 -0
  101. package/lib/vault/VaultUtils.d.ts +17 -0
  102. package/lib/vault/VaultUtils.js +17 -0
  103. package/lib/vault/index.js +20 -0
  104. package/package.json +7 -11
  105. package/src/addresses.ts +0 -261
  106. package/src/chain/chain.constants.ts +0 -235
  107. package/src/chain/chain.test.ts +0 -51
  108. package/src/chain/chain.types.ts +0 -42
  109. package/src/chain/chain.utils.ts +0 -44
  110. package/src/constants.ts +0 -18
  111. package/src/errors.ts +0 -75
  112. package/src/ethers/ethers.test.ts +0 -17
  113. package/src/ethers/safeGetAddress.ts +0 -4
  114. package/src/ethers/safeParseUnits.ts +0 -29
  115. package/src/evm.ts +0 -172
  116. package/src/helpers/format/format.test.ts +0 -340
  117. package/src/helpers/format/format.ts +0 -416
  118. package/src/helpers/getChecksumedAddress.ts +0 -15
  119. package/src/helpers/locale.ts +0 -108
  120. package/src/holding/Holding.ts +0 -109
  121. package/src/market/Market.ts +0 -479
  122. package/src/market/MarketConfig.ts +0 -108
  123. package/src/market/MarketUtils.test.ts +0 -25
  124. package/src/market/MarketUtils.ts +0 -467
  125. package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
  126. package/src/maths/MathLib.ts +0 -208
  127. package/src/maths/MathUtils.ts +0 -31
  128. package/src/maths/SharesMath.ts +0 -40
  129. package/src/notifications.ts +0 -167
  130. package/src/position/Position.ts +0 -251
  131. package/src/signatures/index.ts +0 -18
  132. package/src/signatures/manager.ts +0 -50
  133. package/src/signatures/permit.ts +0 -126
  134. package/src/signatures/permit2.ts +0 -120
  135. package/src/signatures/types.ts +0 -18
  136. package/src/signatures/utils.ts +0 -83
  137. package/src/tests/mocks/markets.ts +0 -110
  138. package/src/token/ERC20Metadata.ts +0 -124
  139. package/src/token/Token.ts +0 -83
  140. package/src/token/TokenNamespace.ts +0 -76
  141. package/src/token/WrappedToken.ts +0 -142
  142. package/src/types.ts +0 -37
  143. package/src/user/User.ts +0 -32
  144. package/src/user/user.types.ts +0 -23
  145. package/src/vault/Vault.ts +0 -370
  146. package/src/vault/VaultAllocation.ts +0 -58
  147. package/src/vault/VaultConfig.ts +0 -55
  148. package/src/vault/VaultUtils.ts +0 -47
  149. /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
  150. /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
  151. /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
  152. /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
  153. /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
  154. /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
  155. /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
  156. /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
  157. /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
  158. /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
  159. /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ import { Signature, Signer, TypedDataDomain, TypedDataField } from "ethers";
2
+ import { Address } from "../types";
3
+ import { SignatureMessage } from "./types";
4
+ export declare function safeSignTypedData(signer: Signer, domain: TypedDataDomain, types: Record<string, TypedDataField[]>, value: Record<string, any>): Promise<Signature>;
5
+ export declare function verifySignature(signature: Signature, hash: string, signerAddress: Address): void;
6
+ export declare function getMessage(domain: TypedDataDomain, types: Record<string, TypedDataField[]>, value: Record<string, any>): SignatureMessage;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMessage = exports.verifySignature = exports.safeSignTypedData = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const errors_1 = require("../errors");
6
+ async function safeSignTypedData(signer, domain, types, value) {
7
+ const populated = await ethers_1.TypedDataEncoder.resolveNames(domain, types, value, (name) => {
8
+ return signer.resolveName(name);
9
+ });
10
+ // Fix the chainId parsing issue
11
+ // Tracking of https://github.com/ethers-io/ethers.js/issues/4649
12
+ const initialPayload = ethers_1.TypedDataEncoder.getPayload(populated.domain, types, populated.value);
13
+ const provider = signer.provider;
14
+ const unsafeChainId = Number(initialPayload.domain.chainId);
15
+ if (provider && "send" in provider && Number.isSafeInteger(unsafeChainId)) {
16
+ const correctedPayload = {
17
+ ...initialPayload,
18
+ domain: {
19
+ ...initialPayload.domain,
20
+ chainId: unsafeChainId, // that is safe now
21
+ },
22
+ };
23
+ return ethers_1.Signature.from(
24
+ // @ts-ignore if send is defined in the provider, it accepts raw RPC call args
25
+ await signer.provider.send("eth_signTypedData_v4", [
26
+ // Doing the same thing that inside of the signTypedData function.
27
+ await signer.getAddress().then((r) => r.toLowerCase()),
28
+ JSON.stringify(correctedPayload),
29
+ ]));
30
+ }
31
+ return ethers_1.Signature.from(await signer.signTypedData(populated.domain, types, populated.value));
32
+ }
33
+ exports.safeSignTypedData = safeSignTypedData;
34
+ function verifySignature(signature, hash, signerAddress) {
35
+ const recoveredAddress = (0, ethers_1.recoverAddress)(hash, signature);
36
+ if (recoveredAddress === ethers_1.ZeroAddress || recoveredAddress !== signerAddress)
37
+ throw new errors_1.InvalidSignatureError(hash, signerAddress, recoveredAddress);
38
+ }
39
+ exports.verifySignature = verifySignature;
40
+ function getMessage(domain, types, value) {
41
+ const hash = ethers_1.TypedDataEncoder.hash(domain, types, value);
42
+ return { data: { domain, types, value }, hash };
43
+ }
44
+ exports.getMessage = getMessage;
@@ -0,0 +1,17 @@
1
+ import { MarketConfig } from "../../market";
2
+ export declare const MAINNET_MARKETS: {
3
+ eth_wstEth: MarketConfig;
4
+ eth_rEth: MarketConfig;
5
+ usdt_wbtc: MarketConfig;
6
+ usdt_wstEth: MarketConfig;
7
+ usdc_wbtc: MarketConfig;
8
+ usdc_wstEth: MarketConfig;
9
+ usdc_sDai: MarketConfig;
10
+ idle_usdc: MarketConfig;
11
+ crvUsd_stkcvxcrvUSDTWBTCWETH: MarketConfig;
12
+ crvUsd_stkcvxcrvUSDCWBTCWETH: MarketConfig;
13
+ crvUsd_stkcvxcrvCRVUSDTBTCWSTETH: MarketConfig;
14
+ crvUsd_stkcvxTryLSD: MarketConfig;
15
+ crvUsd_stkcvxcrvUSDETHCRV: MarketConfig;
16
+ usda_re7Eth: MarketConfig;
17
+ };
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MAINNET_MARKETS = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const market_1 = require("../../market");
6
+ exports.MAINNET_MARKETS = {
7
+ eth_wstEth: new market_1.MarketConfig({
8
+ loanToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
9
+ collateralToken: "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0",
10
+ oracle: "0x2a01EB9496094dA03c4E364Def50f5aD1280AD72",
11
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
12
+ lltv: (0, ethers_1.parseUnits)("0.945"),
13
+ }),
14
+ eth_rEth: new market_1.MarketConfig({
15
+ loanToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
16
+ collateralToken: "0xae78736Cd615f374D3085123A210448E74Fc6393",
17
+ oracle: "0x1b4A3F92e5Fffd1d35A98751c9FE4472483579bB",
18
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
19
+ lltv: (0, ethers_1.parseUnits)("0.945"),
20
+ }),
21
+ usdt_wbtc: new market_1.MarketConfig({
22
+ loanToken: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
23
+ collateralToken: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
24
+ oracle: "0x008bF4B1cDA0cc9f0e882E0697f036667652E1ef",
25
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
26
+ lltv: (0, ethers_1.parseUnits)("0.86"),
27
+ }),
28
+ usdt_wstEth: new market_1.MarketConfig({
29
+ loanToken: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
30
+ collateralToken: "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0",
31
+ oracle: "0x95DB30fAb9A3754e42423000DF27732CB2396992",
32
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
33
+ lltv: (0, ethers_1.parseUnits)("0.86"),
34
+ }),
35
+ usdc_wbtc: new market_1.MarketConfig({
36
+ // USDC(wBTC, 86%, Chainlink, AdaptiveCurve)
37
+ loanToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
38
+ collateralToken: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
39
+ oracle: "0xDddd770BADd886dF3864029e4B377B5F6a2B6b83",
40
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
41
+ lltv: (0, ethers_1.parseUnits)("0.86"),
42
+ }),
43
+ usdc_wstEth: new market_1.MarketConfig({
44
+ // USDC(wstETH, 86%, Chainlink, AdaptiveCurve)
45
+ loanToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
46
+ collateralToken: "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0",
47
+ oracle: "0x48F7E36EB6B826B2dF4B2E630B62Cd25e89E40e2",
48
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
49
+ lltv: (0, ethers_1.parseUnits)("0.86"),
50
+ }),
51
+ usdc_sDai: new market_1.MarketConfig({
52
+ // USDC(wstETH, 86%, Chainlink, AdaptiveCurve)
53
+ loanToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
54
+ collateralToken: "0x83F20F44975D03b1b09e64809B757c47f942BEeA",
55
+ oracle: "0x6CAFE228eC0B0bC2D076577d56D35Fe704318f6d",
56
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
57
+ lltv: (0, ethers_1.parseUnits)("0.965"),
58
+ }),
59
+ idle_usdc: new market_1.MarketConfig({
60
+ loanToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
61
+ collateralToken: ethers_1.ZeroAddress,
62
+ oracle: ethers_1.ZeroAddress,
63
+ irm: ethers_1.ZeroAddress,
64
+ lltv: 0n,
65
+ }),
66
+ crvUsd_stkcvxcrvUSDTWBTCWETH: new market_1.MarketConfig({
67
+ collateralToken: "0xb0Ce26C88e4e7DCa51968b6047f44646f5064278",
68
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
69
+ lltv: (0, ethers_1.parseUnits)("0.86"),
70
+ loanToken: "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E",
71
+ oracle: "0x077Af6c2D4A75D4145d141F9e9421864C3940CB3",
72
+ }),
73
+ crvUsd_stkcvxcrvUSDCWBTCWETH: new market_1.MarketConfig({
74
+ collateralToken: "0x0ea1a65A2c255f24Ee8D81eA6AaC54Decd9d269e",
75
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
76
+ lltv: (0, ethers_1.parseUnits)("0.86"),
77
+ loanToken: "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E",
78
+ oracle: "0xd2F7C3B2fC97cC7b6AfDd76D163394680EFc35b9",
79
+ }),
80
+ crvUsd_stkcvxcrvCRVUSDTBTCWSTETH: new market_1.MarketConfig({
81
+ collateralToken: "0x3ce8Ec9f3d89aD0A2DdbCC3FDB8991BD241Fc82E",
82
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
83
+ lltv: (0, ethers_1.parseUnits)("0.86"),
84
+ loanToken: "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E",
85
+ oracle: "0xa9f7900476F43C45Ebf56cEa669B9c960C176112",
86
+ }),
87
+ crvUsd_stkcvxTryLSD: new market_1.MarketConfig({
88
+ collateralToken: "0x6BA072F0d22806F2C52e9792AF47f2D59103BEBE",
89
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
90
+ lltv: (0, ethers_1.parseUnits)("0.86"),
91
+ loanToken: "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E",
92
+ oracle: "0x18B0d7311a97c5377445C80c768ab5201Bb27B5a",
93
+ }),
94
+ crvUsd_stkcvxcrvUSDETHCRV: new market_1.MarketConfig({
95
+ collateralToken: "0xAc904BAfBb5FB04Deb2b6198FdCEedE75a78Ce5a",
96
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
97
+ lltv: (0, ethers_1.parseUnits)("0.86"),
98
+ loanToken: "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E",
99
+ oracle: "0xad7e157815df05029125B568E39d5402550d60bb",
100
+ }),
101
+ usda_re7Eth: new market_1.MarketConfig({
102
+ collateralToken: "0x78Fc2c2eD1A4cDb5402365934aE5648aDAd094d0",
103
+ irm: "0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC",
104
+ lltv: (0, ethers_1.parseUnits)("0.86"),
105
+ loanToken: "0x0000206329b97DB379d5E1Bf586BbDB969C63274",
106
+ oracle: "0x76052A2A28fDCB8124f4686C63C68355b142de3B",
107
+ }),
108
+ };
@@ -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 = {}));