@ensuro/core 2.0.0-beta1 → 2.0.0-beta10
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.
- package/README.md +23 -14
- package/build/contracts/AccessManager.sol/AccessManager.json +559 -0
- package/build/contracts/ERC4626AssetManager.sol/ERC4626AssetManager.json +219 -0
- package/build/contracts/EToken.sol/EToken.json +134 -52
- package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +4 -4
- package/build/contracts/LiquidityThresholdAssetManager.sol/LiquidityThresholdAssetManager.json +203 -0
- package/build/contracts/Policy.sol/Policy.json +2 -2
- package/build/contracts/PolicyPool.sol/PolicyPool.json +437 -61
- package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +2 -2
- package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +123 -16
- package/build/contracts/Reserve.sol/Reserve.json +66 -8
- package/build/contracts/RiskModule.sol/RiskModule.json +2 -2
- package/build/contracts/SignedQuoteRiskModule.sol/SignedQuoteRiskModule.json +960 -0
- package/build/contracts/TimeScaled.sol/TimeScaled.json +3 -17
- package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +5 -5
- package/build/contracts/{WadRayMath.sol → dependencies/WadRayMath.sol}/WadRayMath.json +3 -3
- package/build/contracts/interfaces/{IPolicyPoolConfig.sol/IPolicyPoolConfig.json → IAccessManager.sol/IAccessManager.json} +77 -59
- package/build/contracts/interfaces/IAssetManager.sol/IAssetManager.json +109 -0
- package/build/contracts/interfaces/IEToken.sol/IEToken.json +13 -0
- package/build/contracts/interfaces/IPolicyPool.sol/IPolicyPool.json +40 -16
- package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +13 -0
- package/build/contracts/mocks/FixedRateVault.sol/FixedRateVault.json +736 -0
- package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +2 -2
- package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +28 -2
- package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +2 -2
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +118 -19
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +6 -6
- package/build/contracts/mocks/RiskModuleMock.sol/RiskModuleMock.json +764 -0
- package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +2 -2
- package/build/contracts/mocks/TestNFT.sol/TestNFT.json +2 -2
- package/contracts/AccessManager.sol +103 -0
- package/contracts/ERC4626AssetManager.sol +52 -0
- package/contracts/EToken.sol +123 -87
- package/contracts/LPManualWhitelist.sol +1 -1
- package/contracts/LiquidityThresholdAssetManager.sol +160 -0
- package/contracts/Policy.sol +1 -1
- package/contracts/PolicyPool.sol +333 -115
- package/contracts/PolicyPoolComponent.sol +36 -41
- package/contracts/PremiumsAccount.sol +285 -79
- package/contracts/Reserve.sol +183 -4
- package/contracts/RiskModule.sol +26 -19
- package/contracts/SignedQuoteRiskModule.sol +241 -0
- package/contracts/TimeScaled.sol +19 -15
- package/contracts/TrustfulRiskModule.sol +62 -2
- package/contracts/dependencies/WadRayMath.sol +126 -0
- package/contracts/interfaces/IAccessManager.sol +148 -0
- package/contracts/interfaces/IAssetManager.sol +84 -0
- package/contracts/interfaces/IEToken.sol +25 -2
- package/contracts/interfaces/ILPWhitelist.sol +17 -0
- package/contracts/interfaces/IPolicyPool.sol +20 -13
- package/contracts/interfaces/IPolicyPoolComponent.sol +3 -0
- package/contracts/interfaces/IPremiumsAccount.sol +5 -0
- package/contracts/interfaces/IRiskModule.sol +67 -4
- package/contracts/mocks/FixedRateVault.sol +85 -0
- package/contracts/mocks/PolicyHolderMock.sol +21 -0
- package/contracts/mocks/PolicyPoolMock.sol +41 -15
- package/contracts/mocks/RiskModuleMock.sol +75 -0
- package/js/test-utils.js +227 -149
- package/package.json +1 -1
- package/build/contracts/FlightDelayRiskModule.sol/FlightDelayRiskModule.json +0 -890
- package/build/contracts/PolicyNFT.sol/PolicyNFT.json +0 -583
- package/build/contracts/PolicyPoolConfig.sol/PolicyPoolConfig.json +0 -691
- package/build/contracts/dependencies/ChainlinkClientUpgradeable.sol/ChainlinkClientUpgradeable.json +0 -63
- package/build/contracts/interfaces/IPolicyNFT.sol/IPolicyNFT.json +0 -321
- package/build/contracts/mocks/LinkTokenMock.sol/LinkTokenMock.json +0 -370
- package/build/contracts/mocks/TestCurrencyUUPS.sol/TestCurrencyUUPS.json +0 -353
- package/build/contracts/upgraded/IPolicyNFTv1.sol/IPolicyNFTv1.json +0 -322
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1.json +0 -584
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1_Upgrade.json +0 -615
- package/contracts/FlightDelayRiskModule.sol +0 -217
- package/contracts/PolicyNFT.sol +0 -96
- package/contracts/PolicyPoolConfig.sol +0 -204
- package/contracts/WadRayMath.sol +0 -135
- package/contracts/dependencies/ChainlinkClientUpgradeable.sol +0 -371
- package/contracts/interfaces/IPolicyNFT.sol +0 -15
- package/contracts/interfaces/IPolicyPoolConfig.sol +0 -96
- package/contracts/mocks/LinkTokenMock.sol +0 -30
- package/contracts/upgraded/IPolicyNFTv1.sol +0 -15
- package/contracts/upgraded/PolicyNFTv1.sol +0 -126
package/js/test-utils.js
CHANGED
|
@@ -1,61 +1,85 @@
|
|
|
1
1
|
const { expect } = require("chai");
|
|
2
|
+
const hre = require("hardhat");
|
|
2
3
|
const { BigNumber } = require("ethers");
|
|
3
|
-
const {LogDescription} = require("ethers/lib/utils");
|
|
4
|
+
const { LogDescription } = require("ethers/lib/utils");
|
|
4
5
|
exports.WEEK = 3600 * 24 * 7;
|
|
5
6
|
exports.DAY = 3600 * 24;
|
|
6
7
|
|
|
7
|
-
exports.initCurrency = async function(options, initial_targets, initial_balances) {
|
|
8
|
-
const Currency = await ethers.getContractFactory("TestCurrency");
|
|
8
|
+
exports.initCurrency = async function (options, initial_targets, initial_balances) {
|
|
9
|
+
const Currency = await hre.ethers.getContractFactory("TestCurrency");
|
|
9
10
|
let currency = await Currency.deploy(
|
|
10
11
|
options.name || "Test Currency",
|
|
11
12
|
options.symbol || "TEST",
|
|
12
13
|
options.initial_supply,
|
|
13
|
-
options.decimals || 18
|
|
14
|
+
options.decimals || 18
|
|
14
15
|
);
|
|
15
16
|
initial_targets = initial_targets || [];
|
|
16
|
-
await Promise.all(
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
await Promise.all(
|
|
18
|
+
initial_targets.map(async function (user, index) {
|
|
19
|
+
await currency.transfer(user.address, initial_balances[index]);
|
|
20
|
+
})
|
|
21
|
+
);
|
|
19
22
|
return currency;
|
|
20
|
-
}
|
|
23
|
+
};
|
|
21
24
|
|
|
22
|
-
exports.approve_multiple = async function(currency, spender, sources, amounts) {
|
|
23
|
-
return Promise.all(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
25
|
+
exports.approve_multiple = async function (currency, spender, sources, amounts) {
|
|
26
|
+
return Promise.all(
|
|
27
|
+
sources.map(async function (source, index) {
|
|
28
|
+
await currency.connect(source).approve(spender.address, amounts[index]);
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
};
|
|
27
32
|
|
|
28
|
-
exports.check_balances = async function(currency, users, amounts) {
|
|
29
|
-
return Promise.all(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
+
exports.check_balances = async function (currency, users, amounts) {
|
|
34
|
+
return Promise.all(
|
|
35
|
+
users.map(async function (user, index) {
|
|
36
|
+
expect(await currency.balanceOf(user.address)).to.equal(amounts[index]);
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
};
|
|
33
40
|
|
|
34
|
-
exports.now = function() {
|
|
41
|
+
exports.now = function () {
|
|
35
42
|
return Math.floor(new Date().getTime() / 1000);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
exports.addRiskModule = async function(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
exports.addRiskModule = async function (
|
|
46
|
+
pool,
|
|
47
|
+
premiumsAccount,
|
|
48
|
+
contractFactory,
|
|
49
|
+
{
|
|
50
|
+
rmName,
|
|
51
|
+
scrPercentage,
|
|
52
|
+
scrInterestRate,
|
|
53
|
+
ensuroFee,
|
|
54
|
+
maxScrPerPolicy,
|
|
55
|
+
scrLimit,
|
|
56
|
+
moc,
|
|
57
|
+
wallet,
|
|
58
|
+
extraArgs,
|
|
59
|
+
extraConstructorArgs,
|
|
60
|
+
}
|
|
61
|
+
) {
|
|
42
62
|
extraArgs = extraArgs || [];
|
|
43
63
|
extraConstructorArgs = extraConstructorArgs || [];
|
|
44
64
|
const _A = pool._A || _W;
|
|
45
|
-
const rm = await hre.upgrades.deployProxy(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
const rm = await hre.upgrades.deployProxy(
|
|
66
|
+
contractFactory,
|
|
67
|
+
[
|
|
68
|
+
rmName || "RiskModule",
|
|
69
|
+
_W(scrPercentage) || _W(1),
|
|
70
|
+
_W(ensuroFee) || _W(0),
|
|
71
|
+
_W(scrInterestRate) || _W(0.1),
|
|
72
|
+
_A(maxScrPerPolicy) || _A(1000),
|
|
73
|
+
_A(scrLimit) || _A(1000000),
|
|
74
|
+
wallet || "0xdD2FD4581271e230360230F9337D5c0430Bf44C0", // Random address
|
|
75
|
+
...extraArgs,
|
|
76
|
+
],
|
|
77
|
+
{
|
|
78
|
+
kind: "uups",
|
|
79
|
+
unsafeAllow: ["delegatecall"],
|
|
80
|
+
constructorArgs: [pool.address, premiumsAccount.address, ...extraConstructorArgs],
|
|
81
|
+
}
|
|
82
|
+
);
|
|
59
83
|
|
|
60
84
|
await rm.deployed();
|
|
61
85
|
|
|
@@ -63,66 +87,63 @@ exports.addRiskModule = async function(pool, premiumsAccount, contractFactory, {
|
|
|
63
87
|
moc = _W(moc);
|
|
64
88
|
await rm.setParam(0, moc);
|
|
65
89
|
}
|
|
66
|
-
|
|
67
|
-
await policyPoolConfig.addRiskModule(rm.address);
|
|
90
|
+
await pool.addComponent(rm.address, 2);
|
|
68
91
|
return rm;
|
|
69
|
-
}
|
|
92
|
+
};
|
|
70
93
|
|
|
71
|
-
exports.addEToken = async function(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const EToken = await ethers.getContractFactory("EToken");
|
|
94
|
+
exports.addEToken = async function (
|
|
95
|
+
pool,
|
|
96
|
+
{ etkName, etkSymbol, maxUtilizationRate, poolLoanInterestRate, extraArgs, extraConstructorArgs }
|
|
97
|
+
) {
|
|
98
|
+
const EToken = await hre.ethers.getContractFactory("EToken");
|
|
76
99
|
extraArgs = extraArgs || [];
|
|
77
100
|
extraConstructorArgs = extraConstructorArgs || [];
|
|
78
|
-
const etk = await hre.upgrades.deployProxy(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
const etk = await hre.upgrades.deployProxy(
|
|
102
|
+
EToken,
|
|
103
|
+
[
|
|
104
|
+
etkName || "EToken",
|
|
105
|
+
etkSymbol || "eUSD1YEAR",
|
|
106
|
+
_W(maxUtilizationRate) || _W(1),
|
|
107
|
+
_W(poolLoanInterestRate) || _W("0.05"),
|
|
108
|
+
...extraArgs,
|
|
109
|
+
],
|
|
110
|
+
{
|
|
111
|
+
kind: "uups",
|
|
112
|
+
unsafeAllow: ["delegatecall"],
|
|
113
|
+
constructorArgs: [pool.address, ...extraConstructorArgs],
|
|
114
|
+
}
|
|
115
|
+
);
|
|
89
116
|
|
|
90
117
|
await etk.deployed();
|
|
91
|
-
await pool.
|
|
118
|
+
await pool.addComponent(etk.address, 1);
|
|
92
119
|
return etk;
|
|
93
|
-
}
|
|
120
|
+
};
|
|
94
121
|
|
|
95
|
-
exports.expected_change = async function(protocol_attribute, initial, change) {
|
|
122
|
+
exports.expected_change = async function (protocol_attribute, initial, change) {
|
|
96
123
|
change = BigNumber.from(change);
|
|
97
124
|
let actual_value = await protocol_attribute();
|
|
98
125
|
expect(actual_value.sub(initial)).to.equal(change);
|
|
99
126
|
return actual_value;
|
|
100
|
-
}
|
|
127
|
+
};
|
|
101
128
|
|
|
102
|
-
exports.impersonate = async function(address, setBalanceTo) {
|
|
103
|
-
const ok = await hre.network.provider.request(
|
|
104
|
-
|
|
105
|
-
);
|
|
106
|
-
if (!ok)
|
|
107
|
-
throw "Error impersonatting " + address;
|
|
129
|
+
exports.impersonate = async function (address, setBalanceTo) {
|
|
130
|
+
const ok = await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: [address] });
|
|
131
|
+
if (!ok) throw "Error impersonatting " + address;
|
|
108
132
|
|
|
109
133
|
if (setBalanceTo !== undefined)
|
|
110
|
-
await hre.network.provider.request(
|
|
111
|
-
{method: "hardhat_setBalance", params: [address, setBalanceTo.toHexString()]}
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
return await ethers.getSigner(address);
|
|
115
|
-
}
|
|
134
|
+
await hre.network.provider.request({ method: "hardhat_setBalance", params: [address, setBalanceTo.toHexString()] });
|
|
116
135
|
|
|
136
|
+
return await hre.ethers.getSigner(address);
|
|
137
|
+
};
|
|
117
138
|
|
|
118
139
|
/**
|
|
119
|
-
* Finds an event in the receipt
|
|
120
|
-
* @param {Interface} interface The interface of the contract that contains the requested event
|
|
121
|
-
* @param {TransactionReceipt} receipt Transaction receipt containing the events in the logs
|
|
122
|
-
* @param {String} eventName The name of the event we are interested in
|
|
123
|
-
* @returns {LogDescription}
|
|
124
|
-
*/
|
|
125
|
-
|
|
140
|
+
* Finds an event in the receipt
|
|
141
|
+
* @param {Interface} interface The interface of the contract that contains the requested event
|
|
142
|
+
* @param {TransactionReceipt} receipt Transaction receipt containing the events in the logs
|
|
143
|
+
* @param {String} eventName The name of the event we are interested in
|
|
144
|
+
* @returns {LogDescription}
|
|
145
|
+
*/
|
|
146
|
+
const getTransactionEvent = function (interface, receipt, eventName) {
|
|
126
147
|
// for each log in the transaction receipt
|
|
127
148
|
for (const log of receipt.events) {
|
|
128
149
|
let parsedLog;
|
|
@@ -135,68 +156,64 @@ exports.getTransactionEvent = function(interface, receipt, eventName) {
|
|
|
135
156
|
return parsedLog;
|
|
136
157
|
}
|
|
137
158
|
}
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
159
|
+
return null; // not found
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
exports.getTransactionEvent = getTransactionEvent;
|
|
140
163
|
|
|
141
|
-
exports.deployPool = async function(hre, options) {
|
|
142
|
-
const PolicyPool = await ethers.getContractFactory("PolicyPool");
|
|
143
|
-
const
|
|
144
|
-
const PolicyNFT = await ethers.getContractFactory("PolicyNFT");
|
|
164
|
+
exports.deployPool = async function (hre, options) {
|
|
165
|
+
const PolicyPool = await hre.ethers.getContractFactory("PolicyPool");
|
|
166
|
+
const AccessManager = await hre.ethers.getContractFactory("AccessManager");
|
|
145
167
|
|
|
146
|
-
// Deploy
|
|
147
|
-
const
|
|
148
|
-
|
|
168
|
+
// Deploy AccessManager
|
|
169
|
+
const accessManager = await hre.upgrades.deployProxy(AccessManager, [], { kind: "uups" });
|
|
170
|
+
|
|
171
|
+
await accessManager.deployed();
|
|
172
|
+
|
|
173
|
+
const policyPool = await hre.upgrades.deployProxy(
|
|
174
|
+
PolicyPool,
|
|
149
175
|
[
|
|
150
176
|
options.nftName || "Policy NFT",
|
|
151
177
|
options.nftSymbol || "EPOL",
|
|
152
|
-
options.
|
|
178
|
+
options.treasuryAddress || hre.ethers.constants.AddressZero,
|
|
153
179
|
],
|
|
154
|
-
{
|
|
180
|
+
{
|
|
181
|
+
constructorArgs: [accessManager.address, options.currency],
|
|
182
|
+
kind: "uups",
|
|
183
|
+
unsafeAllow: ["delegatecall"],
|
|
184
|
+
}
|
|
155
185
|
);
|
|
156
|
-
await policyNFT.deployed();
|
|
157
|
-
|
|
158
|
-
// Deploy PolicyPoolConfig
|
|
159
|
-
const policyPoolConfig = await hre.upgrades.deployProxy(PolicyPoolConfig, [
|
|
160
|
-
options.policyPoolDetAddress || ethers.constants.AddressZero,
|
|
161
|
-
options.treasuryAddress || ethers.constants.AddressZero
|
|
162
|
-
], {kind: 'uups'});
|
|
163
|
-
|
|
164
|
-
await policyPoolConfig.deployed();
|
|
165
|
-
|
|
166
|
-
const policyPool = await hre.upgrades.deployProxy(PolicyPool, [], {
|
|
167
|
-
constructorArgs: [policyPoolConfig.address, policyNFT.address, options.currency],
|
|
168
|
-
kind: 'uups',
|
|
169
|
-
unsafeAllow: ["delegatecall"],
|
|
170
|
-
});
|
|
171
186
|
|
|
172
187
|
await policyPool.deployed();
|
|
173
188
|
|
|
174
|
-
for (const role of
|
|
175
|
-
await grantRole(hre,
|
|
189
|
+
for (const role of options.grantRoles || []) {
|
|
190
|
+
await grantRole(hre, accessManager, role);
|
|
176
191
|
}
|
|
177
192
|
|
|
178
|
-
await grantRole(hre,
|
|
179
|
-
await grantRole(hre,
|
|
180
|
-
await grantRole(hre,
|
|
193
|
+
await grantRole(hre, accessManager, "LEVEL1_ROLE");
|
|
194
|
+
await grantRole(hre, accessManager, "LEVEL2_ROLE");
|
|
195
|
+
await grantRole(hre, accessManager, "LEVEL3_ROLE");
|
|
181
196
|
return policyPool;
|
|
182
|
-
}
|
|
197
|
+
};
|
|
183
198
|
|
|
184
|
-
exports.deployPremiumsAccount = async function (hre, pool, options) {
|
|
185
|
-
const PremiumsAccount = await ethers.getContractFactory("PremiumsAccount");
|
|
199
|
+
exports.deployPremiumsAccount = async function (hre, pool, options, addToPool = true) {
|
|
200
|
+
const PremiumsAccount = await hre.ethers.getContractFactory("PremiumsAccount");
|
|
186
201
|
const premiumsAccount = await hre.upgrades.deployProxy(PremiumsAccount, [], {
|
|
187
202
|
constructorArgs: [
|
|
188
203
|
pool.address,
|
|
189
|
-
options.jrEtkAddr || ethers.constants.AddressZero,
|
|
190
|
-
options.srEtkAddr || ethers.constants.AddressZero,
|
|
204
|
+
options.jrEtkAddr || hre.ethers.constants.AddressZero,
|
|
205
|
+
options.srEtkAddr || hre.ethers.constants.AddressZero,
|
|
191
206
|
],
|
|
192
|
-
kind:
|
|
207
|
+
kind: "uups",
|
|
193
208
|
unsafeAllow: ["delegatecall"],
|
|
194
209
|
});
|
|
195
210
|
|
|
196
211
|
await premiumsAccount.deployed();
|
|
197
|
-
|
|
212
|
+
|
|
213
|
+
if (addToPool) await pool.addComponent(premiumsAccount.address, 3);
|
|
214
|
+
|
|
198
215
|
return premiumsAccount;
|
|
199
|
-
}
|
|
216
|
+
};
|
|
200
217
|
|
|
201
218
|
async function _getDefaultSigner(hre) {
|
|
202
219
|
const signers = await hre.ethers.getSigners();
|
|
@@ -211,68 +228,129 @@ async function grantRole(hre, contract, role, user) {
|
|
|
211
228
|
} else {
|
|
212
229
|
userAddress = user;
|
|
213
230
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
await contract.grantRole(roleHex, userAddress);
|
|
231
|
+
if (!(await contract.hasRole(getRole(role), userAddress))) {
|
|
232
|
+
await contract.grantRole(getRole(role), userAddress);
|
|
217
233
|
}
|
|
218
234
|
}
|
|
219
235
|
|
|
220
236
|
exports.grantRole = grantRole;
|
|
221
237
|
|
|
222
|
-
async function grantComponentRole(hre,
|
|
238
|
+
async function grantComponentRole(hre, accessManager, component, role, user) {
|
|
223
239
|
let userAddress;
|
|
224
240
|
if (user === undefined) {
|
|
225
241
|
user = await _getDefaultSigner(hre);
|
|
226
242
|
userAddress = user.address;
|
|
227
243
|
} else {
|
|
228
|
-
userAddress = user;
|
|
244
|
+
userAddress = user.address === undefined ? user : user.address;
|
|
229
245
|
}
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
await contract.grantComponentRole(component.address, roleHex, userAddress);
|
|
246
|
+
const componentRole = getComponentRole(component.address, getRole(role));
|
|
247
|
+
if (!(await accessManager.hasRole(componentRole, userAddress))) {
|
|
248
|
+
await accessManager.grantComponentRole(component.address, getRole(role), userAddress);
|
|
234
249
|
}
|
|
235
250
|
}
|
|
236
251
|
|
|
237
252
|
exports.grantComponentRole = grantComponentRole;
|
|
238
253
|
|
|
239
|
-
exports._E = ethers.utils.parseEther;
|
|
254
|
+
exports._E = hre.ethers.utils.parseEther;
|
|
240
255
|
|
|
241
|
-
const _BN = ethers.BigNumber.from;
|
|
242
|
-
const WAD = _BN(1e10).mul(_BN(1e8));
|
|
243
|
-
const RAY = WAD.mul(_BN(1e9));
|
|
256
|
+
const _BN = hre.ethers.BigNumber.from;
|
|
257
|
+
const WAD = _BN(1e10).mul(_BN(1e8)); // 1e10*1e8=1e18
|
|
258
|
+
const RAY = WAD.mul(_BN(1e9)); // 1e18*1e9=1e27
|
|
244
259
|
|
|
245
260
|
exports._BN = _BN;
|
|
246
261
|
|
|
247
|
-
const _W = function(value) {
|
|
248
|
-
if (value === undefined)
|
|
249
|
-
|
|
250
|
-
if (!Number.isInteger(value))
|
|
251
|
-
return _BN(Math.round(value * 1e9)).mul(_BN(1e9));
|
|
262
|
+
const _W = function (value) {
|
|
263
|
+
if (value === undefined) return undefined;
|
|
264
|
+
if (!Number.isInteger(value)) return _BN(Math.round(value * 1e9)).mul(_BN(1e9));
|
|
252
265
|
return _BN(value).mul(WAD);
|
|
253
|
-
}
|
|
266
|
+
};
|
|
254
267
|
|
|
255
268
|
exports._W = _W;
|
|
256
269
|
|
|
257
|
-
const _R = function(value) {
|
|
258
|
-
if (value === undefined)
|
|
259
|
-
|
|
260
|
-
if (!Number.isInteger(value))
|
|
261
|
-
return _BN(Math.round(value * 1e9)).mul(WAD);
|
|
270
|
+
const _R = function (value) {
|
|
271
|
+
if (value === undefined) return undefined;
|
|
272
|
+
if (!Number.isInteger(value)) return _BN(Math.round(value * 1e9)).mul(WAD);
|
|
262
273
|
return _BN(value).mul(RAY);
|
|
263
|
-
}
|
|
274
|
+
};
|
|
264
275
|
|
|
265
276
|
exports._R = _R;
|
|
266
277
|
|
|
267
278
|
exports.amountFunction = function (decimals) {
|
|
268
279
|
// Decimals must be at least 6
|
|
269
280
|
return function (value) {
|
|
270
|
-
if (value === undefined)
|
|
271
|
-
|
|
272
|
-
if (typeof value === 'string' || value instanceof String) {
|
|
281
|
+
if (value === undefined) return undefined;
|
|
282
|
+
if (typeof value === "string" || value instanceof String) {
|
|
273
283
|
return _BN(value).mul(_BN(Math.pow(10, decimals)));
|
|
274
284
|
} else {
|
|
275
285
|
return _BN(Math.round(value * 1e6)).mul(_BN(Math.pow(10, decimals - 6)));
|
|
276
286
|
}
|
|
277
|
-
}
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Builds the component role identifier
|
|
292
|
+
*
|
|
293
|
+
* Mimics the behaviour of the PolicyPoolConfig.getComponentRole method
|
|
294
|
+
*
|
|
295
|
+
* Component roles are roles created doing XOR between the component
|
|
296
|
+
* address and the original role.
|
|
297
|
+
*
|
|
298
|
+
* Example:
|
|
299
|
+
* getComponentRole("0xc6e7DF5E7b4f2A278906862b61205850344D4e7d", "ORACLE_ADMIN_ROLE")
|
|
300
|
+
* // "0x05e01b185238b49f750d03d945e38a7f6c3be8b54de0ee42d481eb7814f0d3a8"
|
|
301
|
+
*/
|
|
302
|
+
function getComponentRole(componentAddress, roleName) {
|
|
303
|
+
// 32 byte array
|
|
304
|
+
const bytesRole = hre.ethers.utils.arrayify(getRole(roleName));
|
|
305
|
+
|
|
306
|
+
// 20 byte array
|
|
307
|
+
const bytesAddress = hre.ethers.utils.arrayify(componentAddress);
|
|
308
|
+
|
|
309
|
+
// xor each byte, padding bytesAddress with zeros at the end
|
|
310
|
+
return hre.ethers.utils.hexlify(bytesRole.map((elem, idx) => elem ^ (bytesAddress[idx] || 0)));
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
exports.getComponentRole = getComponentRole;
|
|
314
|
+
|
|
315
|
+
/*
|
|
316
|
+
Builds AccessControl error message for comparison in tests
|
|
317
|
+
*/
|
|
318
|
+
function accessControlMessage(address, component, role) {
|
|
319
|
+
const roleHash = component !== null ? getComponentRole(component, role) : getRole(role);
|
|
320
|
+
|
|
321
|
+
return `AccessControl: account ${address.toLowerCase()} is missing role ${roleHash}`;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
exports.accessControlMessage = accessControlMessage;
|
|
325
|
+
|
|
326
|
+
function makePolicyId(rm, internalId) {
|
|
327
|
+
return hre.ethers.BigNumber.from(rm.address).shl(96).add(internalId);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
exports.makePolicyId = makePolicyId;
|
|
331
|
+
|
|
332
|
+
async function makePolicy(pool, rm, cust, payout, premium, lossProb, expiration, internalId) {
|
|
333
|
+
let tx = await rm.connect(cust).newPolicy(payout, premium, lossProb, expiration, cust.address, internalId);
|
|
334
|
+
let receipt = await tx.wait();
|
|
335
|
+
const newPolicyEvt = getTransactionEvent(pool.interface, receipt, "NewPolicy");
|
|
336
|
+
|
|
337
|
+
return newPolicyEvt;
|
|
278
338
|
}
|
|
339
|
+
|
|
340
|
+
exports.makePolicy = makePolicy;
|
|
341
|
+
|
|
342
|
+
async function blockchainNow(owner) {
|
|
343
|
+
return (await owner.provider.getBlock("latest")).timestamp;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
exports.blockchainNow = blockchainNow;
|
|
347
|
+
|
|
348
|
+
function getRole(role) {
|
|
349
|
+
return role === "DEFAULT_ADMIN_ROLE"
|
|
350
|
+
? "0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
351
|
+
: hre.ethers.utils.keccak256(hre.ethers.utils.toUtf8Bytes(role));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
exports.getRole = getRole;
|
|
355
|
+
|
|
356
|
+
if (process.env.ENABLE_HH_WARNINGS !== "yes") hre.upgrades.silenceWarnings();
|