@ensuro/core 2.6.2 → 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 (79) 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 +39 -91
  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 +73 -29
  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/constants.js +8 -2
  31. package/js/deploy.js +65 -62
  32. package/js/enums.js +26 -8
  33. package/js/test-utils.js +105 -23
  34. package/js/utils.js +78 -28
  35. package/package.json +1 -1
  36. package/scripts/utils.sh +23 -12
  37. package/build/contracts/AccessManager.sol/AccessManager.json +0 -618
  38. package/build/contracts/ERC4626AssetManager.sol/ERC4626AssetManager.json +0 -238
  39. package/build/contracts/EToken.sol/EToken.json +0 -1277
  40. package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +0 -647
  41. package/build/contracts/LiquidityThresholdAssetManager.sol/LiquidityThresholdAssetManager.json +0 -222
  42. package/build/contracts/Policy.sol/Policy.json +0 -10
  43. package/build/contracts/PolicyPool.sol/PolicyPool.json +0 -1416
  44. package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +0 -332
  45. package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +0 -970
  46. package/build/contracts/Reserve.sol/Reserve.json +0 -403
  47. package/build/contracts/RiskModule.sol/RiskModule.json +0 -559
  48. package/build/contracts/SignedBucketRiskModule.sol/SignedBucketRiskModule.json +0 -1262
  49. package/build/contracts/SignedQuoteRiskModule.sol/SignedQuoteRiskModule.json +0 -1110
  50. package/build/contracts/TieredSignedQuoteRiskModule.sol/TieredSignedQuoteRiskModule.json +0 -1316
  51. package/build/contracts/TimeScaled.sol/TimeScaled.json +0 -10
  52. package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +0 -984
  53. package/build/contracts/dependencies/ERC1967Proxy.sol/ERC1967Proxy.json +0 -64
  54. package/build/contracts/dependencies/IERC20Metadata.sol/IERC20Metadata.json +0 -233
  55. package/build/contracts/dependencies/WadRayMath.sol/WadRayMath.json +0 -10
  56. package/build/contracts/interfaces/IAccessManager.sol/IAccessManager.json +0 -343
  57. package/build/contracts/interfaces/IAssetManager.sol/IAssetManager.json +0 -128
  58. package/build/contracts/interfaces/IEToken.sol/IEToken.json +0 -460
  59. package/build/contracts/interfaces/ILPWhitelist.sol/ILPWhitelist.json +0 -103
  60. package/build/contracts/interfaces/IPolicyHolder.sol/IPolicyHolder.json +0 -108
  61. package/build/contracts/interfaces/IPolicyPool.sol/IPolicyPool.json +0 -501
  62. package/build/contracts/interfaces/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +0 -43
  63. package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +0 -315
  64. package/build/contracts/interfaces/IRiskModule.sol/IRiskModule.json +0 -165
  65. package/build/contracts/migration/LPManualWhitelistV20.sol/LPManualWhitelistV20.json +0 -481
  66. package/build/contracts/migration/LPManualWhitelistV20Upgraded.sol/LPManualWhitelistV20Upgraded.json +0 -492
  67. package/build/contracts/mocks/FixedRateVault.sol/FixedRateVault.json +0 -736
  68. package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +0 -43
  69. package/build/contracts/mocks/IMintableERC20.sol/IMintableERC20.json +0 -47
  70. package/build/contracts/mocks/InterfaceIdCalculator.sol/InterfaceIdCalculator.json +0 -180
  71. package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +0 -228
  72. package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +0 -54
  73. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +0 -721
  74. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +0 -92
  75. package/build/contracts/mocks/RiskModuleMock.sol/RiskModuleMock.json +0 -783
  76. package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +0 -343
  77. package/build/contracts/mocks/TestNFT.sol/TestNFT.json +0 -388
  78. package/contracts/dependencies/ERC1967Proxy.sol +0 -19
  79. package/contracts/dependencies/IERC20Metadata.sol +0 -19
package/js/utils.js CHANGED
@@ -1,11 +1,15 @@
1
1
  const { findAll } = require("solidity-ast/utils");
2
2
  const ethers = require("ethers");
3
3
  const helpers = require("@nomicfoundation/hardhat-network-helpers");
4
+ const { DAY, IMPLEMENTATION_SLOT } = require("./constants");
4
5
 
5
- const _E = ethers.utils.parseEther;
6
- const _BN = ethers.BigNumber.from;
7
- const WAD = _BN(10).pow(18); // 1e18
8
- 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
+ }
9
13
 
10
14
  async function getStorageLayout(hre, contractSrc, contractName) {
11
15
  const buildInfo = await hre.artifacts.getBuildInfo(`${contractSrc}:${contractName}`);
@@ -35,14 +39,14 @@ function amountFunction(decimals) {
35
39
  if (value === undefined) return undefined;
36
40
 
37
41
  if (typeof value === "string" || value instanceof String) {
38
- return ethers.utils.parseUnits(value, decimals);
42
+ return ethers.parseUnits(value, decimals);
39
43
  }
40
44
 
41
45
  if (!Number.isInteger(value)) {
42
- 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());
43
47
  }
44
48
 
45
- return _BN(value).mul(_BN(10).pow(decimals));
49
+ return BigInt(value.toString()) * BigInt("10") ** BigInt(decimals.toString());
46
50
  };
47
51
  }
48
52
 
@@ -57,9 +61,7 @@ const _R = amountFunction(27);
57
61
  */
58
62
  function getRole(role) {
59
63
  if (role.startsWith("0x")) return role;
60
- return role === "DEFAULT_ADMIN_ROLE"
61
- ? ethers.constants.HashZero
62
- : ethers.utils.keccak256(ethers.utils.toUtf8Bytes(role));
64
+ return role === "DEFAULT_ADMIN_ROLE" ? ethers.ZeroHash : ethers.keccak256(ethers.toUtf8Bytes(role));
63
65
  }
64
66
 
65
67
  /**
@@ -78,14 +80,14 @@ function getComponentRole(componentAddress, role) {
78
80
  if (!role.startsWith("0x")) role = getRole(role);
79
81
 
80
82
  // 32 byte array
81
- const bytesRole = ethers.utils.arrayify(role);
83
+ const bytesRole = ethers.getBytes(role);
82
84
 
83
85
  // 20 byte array
84
- const bytesAddress = ethers.utils.arrayify(componentAddress);
86
+ const bytesAddress = ethers.getBytes(componentAddress);
85
87
 
86
88
  // xor each byte, padding bytesAddress with zeros at the end
87
89
  // eslint-disable-next-line no-bitwise
88
- return ethers.utils.hexlify(bytesRole.map((elem, idx) => elem ^ (bytesAddress[idx] || 0)));
90
+ return ethers.hexlify(bytesRole.map((elem, idx) => elem ^ (bytesAddress[idx] || 0)));
89
91
  }
90
92
 
91
93
  async function getDefaultSigner(hre) {
@@ -153,7 +155,7 @@ function getTransactionEvent(interface, receipt, eventName) {
153
155
  } catch (error) {
154
156
  continue;
155
157
  }
156
- if (parsedLog.name == eventName) {
158
+ if (parsedLog?.name == eventName) {
157
159
  return parsedLog;
158
160
  }
159
161
  }
@@ -163,10 +165,12 @@ function getTransactionEvent(interface, receipt, eventName) {
163
165
  /**
164
166
  * Builds AccessControl error message for comparison in tests
165
167
  */
166
- function accessControlMessage(address, component, role) {
167
- 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);
168
172
 
169
- return `AccessControl: account ${address.toLowerCase()} is missing role ${roleHash}`;
173
+ return `AccessControl: account ${userAddr.toLowerCase()} is missing role ${roleHash}`;
170
174
  }
171
175
 
172
176
  /**
@@ -174,8 +178,12 @@ function accessControlMessage(address, component, role) {
174
178
  *
175
179
  * Mimics the PolicyPool.newPolicy method of building the policy id.
176
180
  */
177
- function makePolicyId(rmAddress, internalId) {
178
- 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;
179
187
  }
180
188
 
181
189
  /**
@@ -184,7 +192,7 @@ function makePolicyId(rmAddress, internalId) {
184
192
  * Mimics the behaviour of the SignedQuoteRiskModule._newSignedPolicy method.
185
193
  */
186
194
  function makeQuoteMessage({ rmAddress, payout, premium, lossProb, expiration, policyData, validUntil }) {
187
- return ethers.utils.solidityPack(
195
+ return ethers.solidityPacked(
188
196
  ["address", "uint256", "uint256", "uint256", "uint40", "bytes32", "uint40"],
189
197
  [rmAddress, payout, premium, lossProb, expiration, policyData, validUntil]
190
198
  );
@@ -205,7 +213,7 @@ function makeBucketQuoteMessage({
205
213
  bucketId,
206
214
  validUntil,
207
215
  }) {
208
- return ethers.utils.solidityPack(
216
+ return ethers.solidityPacked(
209
217
  ["address", "uint256", "uint256", "uint256", "uint40", "bytes32", "uint256", "uint40"],
210
218
  [rmAddress, payout, premium, lossProb, expiration, policyData, bucketId, validUntil]
211
219
  );
@@ -218,36 +226,75 @@ function makeBucketQuoteMessage({
218
226
  */
219
227
  async function makeSignedQuote(signer, policyParams, makeQuoteMessageFn = makeQuoteMessage) {
220
228
  const quoteMessage = makeQuoteMessageFn(policyParams);
221
- 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);
222
239
  }
223
240
 
224
241
  /**
225
242
  * Build a default policy parameters object.
226
243
  */
227
244
  async function defaultPolicyParams(
228
- { rmAddress, payout, premium, lossProb, expiration, policyData, validUntil },
245
+ { rm, payout, premium, lossProb, expiration, policyData, validUntil },
229
246
  _A = amountFunction(6)
230
247
  ) {
231
248
  const now = await helpers.time.latest();
249
+ const rmAddress = rm ? await ethers.resolveAddress(rm) : rm;
232
250
  return {
233
251
  rmAddress,
234
252
  payout: payout || _A(1000),
235
- premium: premium || ethers.constants.MaxUint256,
253
+ premium: premium || ethers.MaxUint256,
236
254
  lossProb: lossProb || _W(0.1),
237
- expiration: expiration || now + 3600 * 24 * 30,
238
- policyData: policyData || ethers.utils.hexlify(ethers.utils.randomBytes(32)),
239
- validUntil: validUntil || now + 3600 * 24 * 30,
255
+ expiration: expiration || now + DAY * 30,
256
+ policyData: policyData || ethers.hexlify(ethers.randomBytes(32)),
257
+ validUntil: validUntil || now + DAY * 30,
258
+ };
259
+ }
260
+
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
+ },
240
273
  };
241
274
  }
242
275
 
276
+ async function readImplementationAddress(hre, contractAddress) {
277
+ const implStorage = await hre.ethers.provider.getStorageAt(contractAddress, IMPLEMENTATION_SLOT);
278
+ return ethers.utils.getAddress(ethers.utils.hexDataSlice(implStorage, 12));
279
+ }
280
+
281
+ /**
282
+ * Converts a string value to uint256(keccak(value))
283
+ * @param {string} value
284
+ * @returns {ethers.BigNumber}
285
+ */
286
+ function uintKeccak(value) {
287
+ return BigInt(ethers.keccak256(ethers.toUtf8Bytes(value)));
288
+ }
289
+
243
290
  module.exports = {
244
- _BN,
245
291
  _E,
246
292
  _R,
247
293
  _W,
248
294
  accessControlMessage,
249
295
  amountFunction,
250
296
  defaultPolicyParams,
297
+ defaultBucketParams,
251
298
  getComponentRole,
252
299
  getDefaultSigner,
253
300
  getRole,
@@ -260,5 +307,8 @@ module.exports = {
260
307
  makeQuoteMessage,
261
308
  makeSignedQuote,
262
309
  RAY,
310
+ readImplementationAddress,
311
+ recoverAddress,
312
+ uintKeccak,
263
313
  WAD,
264
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.6.2",
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
  }