@morpho-org/blue-sdk 1.0.3 → 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 (158) 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/market/Market.d.ts +159 -0
  42. package/lib/market/Market.js +240 -0
  43. package/lib/market/MarketConfig.d.ts +44 -0
  44. package/lib/market/MarketConfig.js +56 -0
  45. package/lib/market/MarketUtils.d.ts +165 -0
  46. package/lib/market/MarketUtils.js +182 -0
  47. package/lib/market/MarketUtils.test.d.ts +1 -0
  48. package/lib/market/MarketUtils.test.js +19 -0
  49. package/lib/market/index.js +19 -0
  50. package/lib/maths/AdaptiveCurveIrmLib.d.ts +37 -0
  51. package/lib/maths/AdaptiveCurveIrmLib.js +116 -0
  52. package/lib/maths/MathLib.d.ts +94 -0
  53. package/lib/maths/MathLib.js +153 -0
  54. package/lib/maths/MathUtils.d.ts +15 -0
  55. package/lib/maths/MathUtils.js +33 -0
  56. package/lib/maths/SharesMath.d.ts +12 -0
  57. package/lib/maths/SharesMath.js +22 -0
  58. package/lib/maths/index.js +20 -0
  59. package/lib/notifications.d.ts +98 -0
  60. package/lib/notifications.js +52 -0
  61. package/lib/position/Position.d.ts +118 -0
  62. package/lib/position/Position.js +145 -0
  63. package/lib/position/index.js +17 -0
  64. package/lib/signatures/index.d.ts +12 -0
  65. package/lib/signatures/index.js +39 -0
  66. package/lib/signatures/manager.d.ts +10 -0
  67. package/lib/signatures/manager.js +37 -0
  68. package/lib/signatures/permit.d.ts +21 -0
  69. package/lib/signatures/permit.js +101 -0
  70. package/lib/signatures/permit2.d.ts +20 -0
  71. package/lib/signatures/permit2.js +91 -0
  72. package/lib/signatures/types.d.ts +13 -0
  73. package/lib/signatures/types.js +2 -0
  74. package/lib/signatures/utils.d.ts +6 -0
  75. package/lib/signatures/utils.js +44 -0
  76. package/lib/tests/mocks/markets.d.ts +17 -0
  77. package/lib/tests/mocks/markets.js +108 -0
  78. package/lib/token/ERC20Metadata.d.ts +249 -0
  79. package/lib/token/ERC20Metadata.js +81 -0
  80. package/lib/token/Token.d.ts +45 -0
  81. package/lib/token/Token.js +39 -0
  82. package/lib/token/TokenNamespace.d.ts +18 -0
  83. package/lib/token/TokenNamespace.js +55 -0
  84. package/lib/token/WrappedToken.d.ts +42 -0
  85. package/lib/token/WrappedToken.js +87 -0
  86. package/lib/token/index.js +18 -0
  87. package/lib/types.d.ts +29 -0
  88. package/lib/types.js +23 -0
  89. package/lib/user/User.d.ts +20 -0
  90. package/lib/user/User.js +11 -0
  91. package/lib/user/index.js +18 -0
  92. package/lib/user/user.types.d.ts +18 -0
  93. package/lib/user/user.types.js +2 -0
  94. package/lib/vault/Vault.d.ts +167 -0
  95. package/lib/vault/Vault.js +156 -0
  96. package/lib/vault/VaultAllocation.d.ts +38 -0
  97. package/lib/vault/VaultAllocation.js +18 -0
  98. package/lib/vault/VaultConfig.d.ts +23 -0
  99. package/lib/vault/VaultConfig.js +26 -0
  100. package/lib/vault/VaultUtils.d.ts +17 -0
  101. package/lib/vault/VaultUtils.js +17 -0
  102. package/lib/vault/index.js +20 -0
  103. package/package.json +6 -9
  104. package/src/addresses.ts +0 -247
  105. package/src/chain/chain.constants.ts +0 -235
  106. package/src/chain/chain.test.ts +0 -51
  107. package/src/chain/chain.types.ts +0 -42
  108. package/src/chain/chain.utils.ts +0 -44
  109. package/src/constants.ts +0 -18
  110. package/src/errors.ts +0 -75
  111. package/src/ethers/ethers.test.ts +0 -17
  112. package/src/ethers/safeGetAddress.ts +0 -4
  113. package/src/ethers/safeParseUnits.ts +0 -29
  114. package/src/evm.ts +0 -172
  115. package/src/helpers/format/format.test.ts +0 -340
  116. package/src/helpers/format/format.ts +0 -416
  117. package/src/helpers/getChecksumedAddress.ts +0 -15
  118. package/src/helpers/locale.ts +0 -108
  119. package/src/holding/Holding.ts +0 -109
  120. package/src/market/Market.ts +0 -479
  121. package/src/market/MarketConfig.ts +0 -108
  122. package/src/market/MarketUtils.test.ts +0 -25
  123. package/src/market/MarketUtils.ts +0 -467
  124. package/src/maths/AdaptiveCurveIrmLib.ts +0 -143
  125. package/src/maths/MathLib.ts +0 -208
  126. package/src/maths/MathUtils.ts +0 -31
  127. package/src/maths/SharesMath.ts +0 -40
  128. package/src/notifications.ts +0 -167
  129. package/src/position/Position.ts +0 -251
  130. package/src/signatures/index.ts +0 -18
  131. package/src/signatures/manager.ts +0 -50
  132. package/src/signatures/permit.ts +0 -126
  133. package/src/signatures/permit2.ts +0 -120
  134. package/src/signatures/types.ts +0 -18
  135. package/src/signatures/utils.ts +0 -83
  136. package/src/tests/mocks/markets.ts +0 -110
  137. package/src/token/ERC20Metadata.ts +0 -124
  138. package/src/token/Token.ts +0 -83
  139. package/src/token/TokenNamespace.ts +0 -76
  140. package/src/token/WrappedToken.ts +0 -142
  141. package/src/types.ts +0 -37
  142. package/src/user/User.ts +0 -32
  143. package/src/user/user.types.ts +0 -25
  144. package/src/vault/Vault.ts +0 -370
  145. package/src/vault/VaultAllocation.ts +0 -58
  146. package/src/vault/VaultConfig.ts +0 -55
  147. package/src/vault/VaultUtils.ts +0 -47
  148. /package/{src/chain/index.ts → lib/chain/index.d.ts} +0 -0
  149. /package/{src/ethers/index.ts → lib/ethers/index.d.ts} +0 -0
  150. /package/{src/helpers/format/index.ts → lib/helpers/format/index.d.ts} +0 -0
  151. /package/{src/helpers/index.ts → lib/helpers/index.d.ts} +0 -0
  152. /package/{src/holding/index.ts → lib/holding/index.d.ts} +0 -0
  153. /package/{src/market/index.ts → lib/market/index.d.ts} +0 -0
  154. /package/{src/maths/index.ts → lib/maths/index.d.ts} +0 -0
  155. /package/{src/position/index.ts → lib/position/index.d.ts} +0 -0
  156. /package/{src/token/index.ts → lib/token/index.d.ts} +0 -0
  157. /package/{src/user/index.ts → lib/user/index.d.ts} +0 -0
  158. /package/{src/vault/index.ts → lib/vault/index.d.ts} +0 -0
@@ -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;
@@ -0,0 +1,17 @@
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("./Position"), exports);
@@ -0,0 +1,12 @@
1
+ import * as _Manager from "./manager";
2
+ import * as _Permit from "./permit";
3
+ import * as _Permit2 from "./permit2";
4
+ import { safeSignTypedData as _safeSignTypedData, verifySignature as _verifySignature, getMessage as _getMessage } from "./utils";
5
+ export declare namespace SignatureUtils {
6
+ export import Permit = _Permit;
7
+ export import Permit2 = _Permit2;
8
+ export import Manager = _Manager;
9
+ const safeSignTypedData: typeof _safeSignTypedData;
10
+ const verifySignature: typeof _verifySignature;
11
+ const getMessage: typeof _getMessage;
12
+ }
@@ -0,0 +1,39 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.SignatureUtils = void 0;
27
+ const _Manager = __importStar(require("./manager"));
28
+ const _Permit = __importStar(require("./permit"));
29
+ const _Permit2 = __importStar(require("./permit2"));
30
+ const utils_1 = require("./utils");
31
+ var SignatureUtils;
32
+ (function (SignatureUtils) {
33
+ SignatureUtils.Permit = _Permit;
34
+ SignatureUtils.Permit2 = _Permit2;
35
+ SignatureUtils.Manager = _Manager;
36
+ SignatureUtils.safeSignTypedData = utils_1.safeSignTypedData;
37
+ SignatureUtils.verifySignature = utils_1.verifySignature;
38
+ SignatureUtils.getMessage = utils_1.getMessage;
39
+ })(SignatureUtils || (exports.SignatureUtils = SignatureUtils = {}));
@@ -0,0 +1,10 @@
1
+ import { ChainId } from "../chain";
2
+ import { SignatureMessage } from "./types";
3
+ export interface ManagerApprovalSignatureArgs {
4
+ authorizer: string;
5
+ authorized: string;
6
+ isAuthorized: boolean;
7
+ nonce: bigint;
8
+ deadline: bigint;
9
+ }
10
+ export declare const getManagerApprovalMessage: (args: ManagerApprovalSignatureArgs, chainId: ChainId) => SignatureMessage;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getManagerApprovalMessage = void 0;
4
+ const addresses_1 = require("../addresses");
5
+ const utils_1 = require("./utils");
6
+ const getManagerApprovalMessage = (args, chainId) => {
7
+ const domain = {
8
+ chainId: chainId.toString(),
9
+ verifyingContract: (0, addresses_1.getChainAddresses)(chainId).morpho,
10
+ };
11
+ const types = {
12
+ Authorization: [
13
+ {
14
+ name: "authorizer",
15
+ type: "address",
16
+ },
17
+ {
18
+ name: "authorized",
19
+ type: "address",
20
+ },
21
+ {
22
+ name: "isAuthorized",
23
+ type: "bool",
24
+ },
25
+ {
26
+ name: "nonce",
27
+ type: "uint256",
28
+ },
29
+ {
30
+ name: "deadline",
31
+ type: "uint256",
32
+ },
33
+ ],
34
+ };
35
+ return (0, utils_1.getMessage)(domain, types, args);
36
+ };
37
+ exports.getManagerApprovalMessage = getManagerApprovalMessage;
@@ -0,0 +1,21 @@
1
+ import { ChainId } from "../chain";
2
+ import { Token } from "../token";
3
+ import { Address } from "../types";
4
+ import { SignatureMessage } from "./types";
5
+ export interface PermitArgs {
6
+ erc20: Token;
7
+ owner: Address;
8
+ spender: Address;
9
+ allowance: bigint;
10
+ nonce: bigint;
11
+ deadline: bigint;
12
+ }
13
+ /**
14
+ * Permit signature for ERC20 tokens, following EIP-2612.
15
+ * Docs: https://eips.ethereum.org/EIPS/eip-2612
16
+ */
17
+ export declare const getPermitMessage: ({ deadline, owner, nonce, spender, erc20, allowance }: PermitArgs, chainId: ChainId) => SignatureMessage;
18
+ export declare const WITH_SIMPLE_PERMIT: {
19
+ [id in ChainId]: (Address | undefined)[];
20
+ };
21
+ export declare const hasSimplePermit: (chainId: ChainId, address: Address) => boolean;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasSimplePermit = exports.WITH_SIMPLE_PERMIT = exports.getPermitMessage = void 0;
4
+ const addresses_1 = require("../addresses");
5
+ const chain_1 = require("../chain");
6
+ const utils_1 = require("./utils");
7
+ /**
8
+ * Permit signature for ERC20 tokens, following EIP-2612.
9
+ * Docs: https://eips.ethereum.org/EIPS/eip-2612
10
+ */
11
+ const getPermitMessage = ({ deadline, owner, nonce, spender, erc20, allowance }, chainId) => {
12
+ const { usdc, dai } = (0, addresses_1.getChainAddresses)(chainId);
13
+ const domain = {
14
+ name: erc20.name,
15
+ version: erc20.address === usdc ? "2" : "1",
16
+ chainId: chainId.toString(),
17
+ verifyingContract: erc20.address,
18
+ };
19
+ if (erc20.address === dai)
20
+ return (0, utils_1.getMessage)(domain, {
21
+ Permit: [
22
+ {
23
+ name: "holder",
24
+ type: "address",
25
+ },
26
+ {
27
+ name: "spender",
28
+ type: "address",
29
+ },
30
+ {
31
+ name: "nonce",
32
+ type: "uint256",
33
+ },
34
+ {
35
+ name: "expiry",
36
+ type: "uint256",
37
+ },
38
+ {
39
+ name: "allowed",
40
+ type: "bool",
41
+ },
42
+ ],
43
+ }, {
44
+ holder: owner,
45
+ spender,
46
+ allowed: allowance > 0n,
47
+ nonce,
48
+ expiry: deadline,
49
+ });
50
+ const types = {
51
+ Permit: [
52
+ {
53
+ name: "owner",
54
+ type: "address",
55
+ },
56
+ {
57
+ name: "spender",
58
+ type: "address",
59
+ },
60
+ {
61
+ name: "value",
62
+ type: "uint256",
63
+ },
64
+ {
65
+ name: "nonce",
66
+ type: "uint256",
67
+ },
68
+ {
69
+ name: "deadline",
70
+ type: "uint256",
71
+ },
72
+ ],
73
+ };
74
+ const value = {
75
+ owner,
76
+ spender,
77
+ value: allowance,
78
+ nonce,
79
+ deadline,
80
+ };
81
+ return (0, utils_1.getMessage)(domain, types, value);
82
+ };
83
+ exports.getPermitMessage = getPermitMessage;
84
+ exports.WITH_SIMPLE_PERMIT = {
85
+ [chain_1.ChainId.EthMainnet]: [
86
+ addresses_1.addresses[chain_1.ChainId.EthMainnet].wbIB01,
87
+ addresses_1.addresses[chain_1.ChainId.EthMainnet].wbC3M,
88
+ addresses_1.addresses[chain_1.ChainId.EthMainnet].wstEth,
89
+ addresses_1.addresses[chain_1.ChainId.EthMainnet].sDai,
90
+ addresses_1.addresses[chain_1.ChainId.EthMainnet].osEth,
91
+ addresses_1.addresses[chain_1.ChainId.EthMainnet].usdc,
92
+ addresses_1.addresses[chain_1.ChainId.EthMainnet].dai,
93
+ ],
94
+ [chain_1.ChainId.EthGoerliTestnet]: [
95
+ "0x0aCd15Fb54034492c392596B56ED415bD07e70d7", // Fake DAI
96
+ "0xD8134205b0328F5676aaeFb3B2a0DC15f4029d8C", // Real sDAI
97
+ ],
98
+ [chain_1.ChainId.BaseMainnet]: [addresses_1.addresses[chain_1.ChainId.BaseMainnet].usdc],
99
+ };
100
+ const hasSimplePermit = (chainId, address) => exports.WITH_SIMPLE_PERMIT[chainId]?.includes(address);
101
+ exports.hasSimplePermit = hasSimplePermit;
@@ -0,0 +1,20 @@
1
+ import { ChainId } from "../chain";
2
+ import { Address } from "../types";
3
+ import { SignatureMessage } from "./types";
4
+ export interface Permit2Args {
5
+ erc20: Address;
6
+ allowance: bigint;
7
+ nonce: bigint;
8
+ deadline: bigint;
9
+ spender: Address;
10
+ expiration?: bigint;
11
+ }
12
+ export interface Permit2TransferFromArgs {
13
+ erc20: Address;
14
+ allowance: bigint;
15
+ spender: Address;
16
+ nonce: bigint;
17
+ deadline: bigint;
18
+ }
19
+ export declare const getPermit2TransferFromMessage: (args: Permit2TransferFromArgs, chainId: ChainId) => SignatureMessage;
20
+ export declare const getPermit2Message: (args: Permit2Args, chainId: ChainId) => SignatureMessage;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPermit2Message = exports.getPermit2TransferFromMessage = void 0;
4
+ const addresses_1 = require("../addresses");
5
+ const maths_1 = require("../maths");
6
+ const utils_1 = require("./utils");
7
+ const getPermit2TransferFromMessage = (args, chainId) => {
8
+ const domain = {
9
+ name: "Permit2",
10
+ chainId: chainId.toString(),
11
+ verifyingContract: (0, addresses_1.getChainAddresses)(chainId).permit2,
12
+ };
13
+ const types = {
14
+ PermitTransferFrom: [
15
+ { name: "permitted", type: "TokenPermissions" },
16
+ { name: "spender", type: "address" },
17
+ { name: "nonce", type: "uint256" },
18
+ { name: "deadline", type: "uint256" },
19
+ ],
20
+ TokenPermissions: [
21
+ { name: "token", type: "address" },
22
+ { name: "amount", type: "uint256" },
23
+ ],
24
+ };
25
+ const value = {
26
+ permitted: {
27
+ token: args.erc20,
28
+ amount: maths_1.MathLib.min(args.allowance, maths_1.MathLib.MAX_UINT_160),
29
+ },
30
+ spender: args.spender,
31
+ nonce: args.nonce,
32
+ deadline: args.deadline,
33
+ };
34
+ return (0, utils_1.getMessage)(domain, types, value);
35
+ };
36
+ exports.getPermit2TransferFromMessage = getPermit2TransferFromMessage;
37
+ const getPermit2Message = (args, chainId) => {
38
+ const domain = {
39
+ name: "Permit2",
40
+ chainId: chainId.toString(),
41
+ verifyingContract: (0, addresses_1.getChainAddresses)(chainId).permit2,
42
+ };
43
+ const types = {
44
+ PermitSingle: [
45
+ {
46
+ name: "details",
47
+ type: "PermitDetails",
48
+ },
49
+ {
50
+ name: "spender",
51
+ type: "address",
52
+ },
53
+ {
54
+ name: "sigDeadline",
55
+ type: "uint256",
56
+ },
57
+ ],
58
+ PermitDetails: [
59
+ {
60
+ name: "token",
61
+ type: "address",
62
+ },
63
+ {
64
+ name: "amount",
65
+ type: "uint160",
66
+ },
67
+ {
68
+ name: "expiration",
69
+ type: "uint48",
70
+ },
71
+ {
72
+ name: "nonce",
73
+ type: "uint48",
74
+ },
75
+ ],
76
+ };
77
+ const value = {
78
+ details: {
79
+ token: args.erc20,
80
+ amount: maths_1.MathLib.min(args.allowance, maths_1.MathLib.MAX_UINT_160),
81
+ // Use an unlimited expiration because it most
82
+ // closely mimics how a standard approval works.
83
+ expiration: maths_1.MathLib.min(args.expiration ?? maths_1.MathLib.MAX_UINT_48, maths_1.MathLib.MAX_UINT_48),
84
+ nonce: args.nonce,
85
+ },
86
+ spender: args.spender,
87
+ sigDeadline: args.deadline,
88
+ };
89
+ return (0, utils_1.getMessage)(domain, types, value);
90
+ };
91
+ exports.getPermit2Message = getPermit2Message;
@@ -0,0 +1,13 @@
1
+ import { TypedDataDomain } from "ethers";
2
+ export interface SignatureData {
3
+ domain: TypedDataDomain;
4
+ types: Record<string, {
5
+ name: string;
6
+ type: string;
7
+ }[]>;
8
+ value: object;
9
+ }
10
+ export interface SignatureMessage {
11
+ hash: string;
12
+ data: SignatureData;
13
+ }
@@ -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
+ };