@ensuro/core 2.7.0 → 2.8.0-beta1

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 (77) hide show
  1. package/README.md +11 -5
  2. package/contracts/AccessManager.sol +3 -13
  3. package/contracts/EToken.sol +29 -95
  4. package/contracts/LPManualWhitelist.sol +11 -26
  5. package/contracts/LiquidityThresholdAssetManager.sol +10 -21
  6. package/contracts/Policy.sol +6 -17
  7. package/contracts/PolicyPool.sol +23 -75
  8. package/contracts/PolicyPoolComponent.sol +9 -32
  9. package/contracts/PremiumsAccount.sol +16 -47
  10. package/contracts/Reserve.sol +10 -16
  11. package/contracts/RiskModule.sol +22 -80
  12. package/contracts/SignedBucketRiskModule.sol +18 -80
  13. package/contracts/SignedQuoteRiskModule.sol +13 -44
  14. package/contracts/TieredSignedQuoteRiskModule.sol +7 -13
  15. package/contracts/TimeScaled.sol +8 -39
  16. package/contracts/TrustfulRiskModule.sol +13 -48
  17. package/contracts/interfaces/IAccessManager.sol +2 -11
  18. package/contracts/interfaces/IEToken.sol +1 -5
  19. package/contracts/interfaces/ILPWhitelist.sol +2 -10
  20. package/contracts/interfaces/IPolicyHolder.sol +2 -11
  21. package/contracts/interfaces/IPremiumsAccount.sol +1 -5
  22. package/contracts/migration/LPManualWhitelistV20.sol +3 -15
  23. package/contracts/migration/LPManualWhitelistV20Upgraded.sol +1 -5
  24. package/contracts/mocks/FixedRateVault.sol +1 -6
  25. package/contracts/mocks/PolicyHolderMock.sol +3 -13
  26. package/contracts/mocks/PolicyPoolComponentMock.sol +1 -3
  27. package/contracts/mocks/PolicyPoolMock.sol +8 -28
  28. package/contracts/mocks/RiskModuleMock.sol +3 -16
  29. package/contracts/mocks/TestNFT.sol +1 -1
  30. package/js/deploy.js +44 -39
  31. package/js/test-utils.js +30 -19
  32. package/js/utils.js +62 -29
  33. package/package.json +1 -1
  34. package/scripts/utils.sh +23 -12
  35. package/build/contracts/AccessManager.sol/AccessManager.json +0 -618
  36. package/build/contracts/ERC4626AssetManager.sol/ERC4626AssetManager.json +0 -238
  37. package/build/contracts/EToken.sol/EToken.json +0 -1277
  38. package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +0 -647
  39. package/build/contracts/LiquidityThresholdAssetManager.sol/LiquidityThresholdAssetManager.json +0 -222
  40. package/build/contracts/Policy.sol/Policy.json +0 -10
  41. package/build/contracts/PolicyPool.sol/PolicyPool.json +0 -1416
  42. package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +0 -332
  43. package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +0 -970
  44. package/build/contracts/Reserve.sol/Reserve.json +0 -403
  45. package/build/contracts/RiskModule.sol/RiskModule.json +0 -559
  46. package/build/contracts/SignedBucketRiskModule.sol/SignedBucketRiskModule.json +0 -1262
  47. package/build/contracts/SignedQuoteRiskModule.sol/SignedQuoteRiskModule.json +0 -1110
  48. package/build/contracts/TieredSignedQuoteRiskModule.sol/TieredSignedQuoteRiskModule.json +0 -1316
  49. package/build/contracts/TimeScaled.sol/TimeScaled.json +0 -10
  50. package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +0 -984
  51. package/build/contracts/dependencies/ERC1967Proxy.sol/ERC1967Proxy.json +0 -64
  52. package/build/contracts/dependencies/IERC20Metadata.sol/IERC20Metadata.json +0 -233
  53. package/build/contracts/dependencies/WadRayMath.sol/WadRayMath.json +0 -10
  54. package/build/contracts/interfaces/IAccessManager.sol/IAccessManager.json +0 -343
  55. package/build/contracts/interfaces/IAssetManager.sol/IAssetManager.json +0 -128
  56. package/build/contracts/interfaces/IEToken.sol/IEToken.json +0 -460
  57. package/build/contracts/interfaces/ILPWhitelist.sol/ILPWhitelist.json +0 -103
  58. package/build/contracts/interfaces/IPolicyHolder.sol/IPolicyHolder.json +0 -108
  59. package/build/contracts/interfaces/IPolicyPool.sol/IPolicyPool.json +0 -501
  60. package/build/contracts/interfaces/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +0 -43
  61. package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +0 -315
  62. package/build/contracts/interfaces/IRiskModule.sol/IRiskModule.json +0 -165
  63. package/build/contracts/migration/LPManualWhitelistV20.sol/LPManualWhitelistV20.json +0 -481
  64. package/build/contracts/migration/LPManualWhitelistV20Upgraded.sol/LPManualWhitelistV20Upgraded.json +0 -492
  65. package/build/contracts/mocks/FixedRateVault.sol/FixedRateVault.json +0 -736
  66. package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +0 -43
  67. package/build/contracts/mocks/IMintableERC20.sol/IMintableERC20.json +0 -47
  68. package/build/contracts/mocks/InterfaceIdCalculator.sol/InterfaceIdCalculator.json +0 -180
  69. package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +0 -319
  70. package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +0 -54
  71. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +0 -721
  72. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +0 -92
  73. package/build/contracts/mocks/RiskModuleMock.sol/RiskModuleMock.json +0 -783
  74. package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +0 -343
  75. package/build/contracts/mocks/TestNFT.sol/TestNFT.json +0 -388
  76. package/contracts/dependencies/ERC1967Proxy.sol +0 -19
  77. package/contracts/dependencies/IERC20Metadata.sol +0 -19
package/js/utils.js CHANGED
@@ -3,10 +3,13 @@ const ethers = require("ethers");
3
3
  const helpers = require("@nomicfoundation/hardhat-network-helpers");
4
4
  const { DAY, IMPLEMENTATION_SLOT } = require("./constants");
5
5
 
6
- const _E = ethers.utils.parseEther;
7
- const _BN = ethers.BigNumber.from;
8
- const WAD = _BN(10).pow(18); // 1e18
9
- const RAY = _BN(10).pow(27); // 1e27
6
+ const _E = ethers.parseEther;
7
+ const WAD = 10n ** 18n; // 1e18
8
+ const RAY = 10n ** 27n; // 1e27
9
+
10
+ function getAddress(addressable) {
11
+ return addressable.address || addressable.target || addressable;
12
+ }
10
13
 
11
14
  async function getStorageLayout(hre, contractSrc, contractName) {
12
15
  const buildInfo = await hre.artifacts.getBuildInfo(`${contractSrc}:${contractName}`);
@@ -36,14 +39,14 @@ function amountFunction(decimals) {
36
39
  if (value === undefined) return undefined;
37
40
 
38
41
  if (typeof value === "string" || value instanceof String) {
39
- return ethers.utils.parseUnits(value, decimals);
42
+ return ethers.parseUnits(value, decimals);
40
43
  }
41
44
 
42
45
  if (!Number.isInteger(value)) {
43
- return _BN(Math.round(value * 1e6)).mul(_BN(Math.pow(10, decimals - 6)));
46
+ return BigInt(Math.round(value * 1e6).toString()) * BigInt(Math.pow(10, decimals - 6).toString());
44
47
  }
45
48
 
46
- return _BN(value).mul(_BN(10).pow(decimals));
49
+ return BigInt(value.toString()) * BigInt("10") ** BigInt(decimals.toString());
47
50
  };
48
51
  }
49
52
 
@@ -58,9 +61,7 @@ const _R = amountFunction(27);
58
61
  */
59
62
  function getRole(role) {
60
63
  if (role.startsWith("0x")) return role;
61
- return role === "DEFAULT_ADMIN_ROLE"
62
- ? ethers.constants.HashZero
63
- : ethers.utils.keccak256(ethers.utils.toUtf8Bytes(role));
64
+ return role === "DEFAULT_ADMIN_ROLE" ? ethers.ZeroHash : ethers.keccak256(ethers.toUtf8Bytes(role));
64
65
  }
65
66
 
66
67
  /**
@@ -79,14 +80,14 @@ function getComponentRole(componentAddress, role) {
79
80
  if (!role.startsWith("0x")) role = getRole(role);
80
81
 
81
82
  // 32 byte array
82
- const bytesRole = ethers.utils.arrayify(role);
83
+ const bytesRole = ethers.getBytes(role);
83
84
 
84
85
  // 20 byte array
85
- const bytesAddress = ethers.utils.arrayify(componentAddress);
86
+ const bytesAddress = ethers.getBytes(componentAddress);
86
87
 
87
88
  // xor each byte, padding bytesAddress with zeros at the end
88
89
  // eslint-disable-next-line no-bitwise
89
- return ethers.utils.hexlify(bytesRole.map((elem, idx) => elem ^ (bytesAddress[idx] || 0)));
90
+ return ethers.hexlify(bytesRole.map((elem, idx) => elem ^ (bytesAddress[idx] || 0)));
90
91
  }
91
92
 
92
93
  async function getDefaultSigner(hre) {
@@ -154,7 +155,7 @@ function getTransactionEvent(interface, receipt, eventName) {
154
155
  } catch (error) {
155
156
  continue;
156
157
  }
157
- if (parsedLog.name == eventName) {
158
+ if (parsedLog?.name == eventName) {
158
159
  return parsedLog;
159
160
  }
160
161
  }
@@ -164,10 +165,12 @@ function getTransactionEvent(interface, receipt, eventName) {
164
165
  /**
165
166
  * Builds AccessControl error message for comparison in tests
166
167
  */
167
- function accessControlMessage(address, component, role) {
168
- const roleHash = component !== null ? getComponentRole(component, role) : getRole(role);
168
+ function accessControlMessage(user, component, role) {
169
+ const userAddr = getAddress(user);
170
+ const compAddr = component !== null ? getAddress(component) : component;
171
+ const roleHash = component !== null ? getComponentRole(compAddr, role) : getRole(role);
169
172
 
170
- return `AccessControl: account ${address.toLowerCase()} is missing role ${roleHash}`;
173
+ return `AccessControl: account ${userAddr.toLowerCase()} is missing role ${roleHash}`;
171
174
  }
172
175
 
173
176
  /**
@@ -175,8 +178,12 @@ function accessControlMessage(address, component, role) {
175
178
  *
176
179
  * Mimics the PolicyPool.newPolicy method of building the policy id.
177
180
  */
178
- function makePolicyId(rmAddress, internalId) {
179
- return _BN(rmAddress).shl(96).add(internalId);
181
+ function makePolicyId(rm, internalId) {
182
+ const rmAddress = getAddress(rm);
183
+ const bigRmAddress = BigInt(rmAddress);
184
+ // eslint-disable-next-line no-bitwise
185
+ const shiftedValue = (bigRmAddress << BigInt(96)) + BigInt(internalId);
186
+ return shiftedValue;
180
187
  }
181
188
 
182
189
  /**
@@ -185,7 +192,7 @@ function makePolicyId(rmAddress, internalId) {
185
192
  * Mimics the behaviour of the SignedQuoteRiskModule._newSignedPolicy method.
186
193
  */
187
194
  function makeQuoteMessage({ rmAddress, payout, premium, lossProb, expiration, policyData, validUntil }) {
188
- return ethers.utils.solidityPack(
195
+ return ethers.solidityPacked(
189
196
  ["address", "uint256", "uint256", "uint256", "uint40", "bytes32", "uint40"],
190
197
  [rmAddress, payout, premium, lossProb, expiration, policyData, validUntil]
191
198
  );
@@ -206,7 +213,7 @@ function makeBucketQuoteMessage({
206
213
  bucketId,
207
214
  validUntil,
208
215
  }) {
209
- return ethers.utils.solidityPack(
216
+ return ethers.solidityPacked(
210
217
  ["address", "uint256", "uint256", "uint256", "uint40", "bytes32", "uint256", "uint40"],
211
218
  [rmAddress, payout, premium, lossProb, expiration, policyData, bucketId, validUntil]
212
219
  );
@@ -219,30 +226,55 @@ function makeBucketQuoteMessage({
219
226
  */
220
227
  async function makeSignedQuote(signer, policyParams, makeQuoteMessageFn = makeQuoteMessage) {
221
228
  const quoteMessage = makeQuoteMessageFn(policyParams);
222
- return ethers.utils.splitSignature(await signer.signMessage(ethers.utils.arrayify(quoteMessage)));
229
+ return ethers.Signature.from(await signer.signMessage(ethers.getBytes(quoteMessage)));
230
+ }
231
+
232
+ /**
233
+ * Recover address from signed quote
234
+ */
235
+ function recoverAddress(policyParams, signature, makeQuoteMessageFn = makeQuoteMessage) {
236
+ const quoteMessage = makeQuoteMessageFn(policyParams);
237
+ const msg = ethers.getBytes(quoteMessage);
238
+ return ethers.verifyMessage(msg, signature);
223
239
  }
224
240
 
225
241
  /**
226
242
  * Build a default policy parameters object.
227
243
  */
228
244
  async function defaultPolicyParams(
229
- { rmAddress, payout, premium, lossProb, expiration, policyData, validUntil },
245
+ { rm, payout, premium, lossProb, expiration, policyData, validUntil },
230
246
  _A = amountFunction(6)
231
247
  ) {
232
248
  const now = await helpers.time.latest();
249
+ const rmAddress = rm ? await ethers.resolveAddress(rm) : rm;
233
250
  return {
234
251
  rmAddress,
235
252
  payout: payout || _A(1000),
236
- premium: premium || ethers.constants.MaxUint256,
253
+ premium: premium || ethers.MaxUint256,
237
254
  lossProb: lossProb || _W(0.1),
238
255
  expiration: expiration || now + DAY * 30,
239
- policyData: policyData || ethers.utils.hexlify(ethers.utils.randomBytes(32)),
256
+ policyData: policyData || ethers.hexlify(ethers.randomBytes(32)),
240
257
  validUntil: validUntil || now + DAY * 30,
241
258
  };
242
259
  }
243
260
 
244
- async function readImplementationAddress(contractAddress) {
245
- const implStorage = await ethers.provider.getStorageAt(contractAddress, IMPLEMENTATION_SLOT);
261
+ function defaultBucketParams({ moc, jrCollRatio, collRatio, ensuroPpFee, ensuroCocFee, jrRoc, srRoc }) {
262
+ return {
263
+ moc: moc !== undefined ? moc : _W("1.1"),
264
+ jrCollRatio: jrCollRatio !== undefined ? jrCollRatio : _W("0.1"),
265
+ collRatio: collRatio !== undefined ? collRatio : _W("0.2"),
266
+ ensuroPpFee: ensuroPpFee !== undefined ? ensuroPpFee : _W("0.05"),
267
+ ensuroCocFee: ensuroCocFee !== undefined ? ensuroCocFee : _W("0.2"),
268
+ jrRoc: jrRoc !== undefined ? jrRoc : _W("0.1"),
269
+ srRoc: srRoc !== undefined ? srRoc : _W("0.2"),
270
+ asParams: function () {
271
+ return [this.moc, this.jrCollRatio, this.collRatio, this.ensuroPpFee, this.ensuroCocFee, this.jrRoc, this.srRoc];
272
+ },
273
+ };
274
+ }
275
+
276
+ async function readImplementationAddress(hre, contractAddress) {
277
+ const implStorage = await hre.ethers.provider.getStorageAt(contractAddress, IMPLEMENTATION_SLOT);
246
278
  return ethers.utils.getAddress(ethers.utils.hexDataSlice(implStorage, 12));
247
279
  }
248
280
 
@@ -252,17 +284,17 @@ async function readImplementationAddress(contractAddress) {
252
284
  * @returns {ethers.BigNumber}
253
285
  */
254
286
  function uintKeccak(value) {
255
- return ethers.BigNumber.from(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(value)));
287
+ return BigInt(ethers.keccak256(ethers.toUtf8Bytes(value)));
256
288
  }
257
289
 
258
290
  module.exports = {
259
- _BN,
260
291
  _E,
261
292
  _R,
262
293
  _W,
263
294
  accessControlMessage,
264
295
  amountFunction,
265
296
  defaultPolicyParams,
297
+ defaultBucketParams,
266
298
  getComponentRole,
267
299
  getDefaultSigner,
268
300
  getRole,
@@ -276,6 +308,7 @@ module.exports = {
276
308
  makeSignedQuote,
277
309
  RAY,
278
310
  readImplementationAddress,
311
+ recoverAddress,
279
312
  uintKeccak,
280
313
  WAD,
281
314
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ensuro/core",
3
3
  "description": "Ensuro - Decentralized insurance protocol",
4
- "version": "2.7.0",
4
+ "version": "2.8.0-beta1",
5
5
  "files": [
6
6
  "**/*.sol",
7
7
  "/build/contracts/*.json",
package/scripts/utils.sh CHANGED
@@ -13,23 +13,34 @@ export DEPLOY_AMOUNT_DECIMALS=6
13
13
 
14
14
  NETWORK=${NETWORK:-localhost}
15
15
 
16
+ getHHNodePID() {
17
+ ps ax | grep "/.bin/hardhat node" | grep -v grep | awk '{print $1}'
18
+ }
19
+
20
+ waitHHNode() {
21
+ if [ -z "$1" ]; then
22
+ echo "Usage: $0 <node_url>"
23
+ exit 1
24
+ fi
25
+ while ! curl $1 --fail -X POST -H "Content-Type: application/json" -d '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}' > /dev/null 2>&1; do
26
+ echo "Waiting for node start"
27
+ sleep 1
28
+ done
29
+ }
30
+
16
31
  startHHNode() {
17
32
  if [ $NETWORK == "localhost" ]; then
18
- HHNODE_RUNNING=`ps aux | grep "/.bin/hardhat node" | grep -v grep | cut -c10-15`
19
- if [ -z $HHNODE_RUNNING ]; then
20
- START_HHNODE=1
33
+ HHNODE_RUNNING=$(getHHNodePID)
34
+ if [ -n "$HHNODE_RUNNING" ]; then
35
+ echo "Terminating existing hardhat node at PID $HHNODE_RUNNING"
36
+ kill -- $HHNODE_RUNNING
21
37
  fi
22
38
  fi
23
39
 
24
- if [ ! -z $START_HHNODE ]; then
25
- echo "Starting hardhat node"
26
- # npx hardhat node $* >/dev/null &
27
- node_modules/.bin/hardhat node $* >/dev/null &
40
+ echo "Starting hardhat node"
41
+ node_modules/.bin/hardhat node "$@" > /dev/null &
28
42
 
29
- HHNODE_PID=$!
30
- sleep 5
31
- kill -0 $HHNODE_PID || die "Error launching hardhat node localhost"
32
- fi
43
+ waitHHNode http://localhost:8545
33
44
  }
34
45
 
35
46
  readAddress() {
@@ -59,7 +70,7 @@ readPK() {
59
70
  }
60
71
 
61
72
  killPID() {
62
- if [ ! -z $1 ]; then
73
+ if [ -n "$1" ]; then
63
74
  kill $1
64
75
  fi
65
76
  }