@ensuro/core
Advanced tools
Comparing version 2.5.0 to 2.6.0-beta1
103
js/deploy.js
@@ -6,13 +6,6 @@ const upgrades_core = require("@openzeppelin/upgrades-core"); | ||
const ethers = require("ethers"); | ||
const { task, types } = require("hardhat/config"); | ||
const { WhitelistStatus } = require("../js/enums"); | ||
const { amountFunction, _W, grantRole, grantComponentRole, getDefaultSigner } = require("../js/utils"); | ||
const _BN = ethers.BigNumber.from; | ||
const WAD = _BN(1e10).mul(_BN(1e8)); // 1e10*1e8=1e18 | ||
const RAY = WAD.mul(_BN(1e9)); // 1e18*1e9=1e27 | ||
const WhitelistStatus = { | ||
notdefined: 0, | ||
whitelisted: 1, | ||
blacklisted: 2, | ||
}; | ||
const reservesOpts = { | ||
@@ -22,26 +15,2 @@ unsafeAllow: ["delegatecall"], | ||
/** | ||
* Creates a fixed-point conversion function for the desired number of decimals | ||
* @param decimals The number of decimals. Must be >= 6. | ||
* @returns The amount function created. The function can receive strings (recommended), | ||
* floats/doubles (not recommended) and integers. | ||
* | ||
* Floats will be rounded to 6 decimal before scaling. | ||
*/ | ||
function amountFunction(decimals) { | ||
return function (value) { | ||
if (value === undefined) return undefined; | ||
if (typeof value === "string" || value instanceof String) { | ||
return hre.ethers.utils.parseUnits(value, decimals); | ||
} | ||
if (!Number.isInteger(value)) { | ||
return _BN(Math.round(value * 1e6)).mul(_BN(Math.pow(10, decimals - 6))); | ||
} | ||
return _BN(value).mul(_BN(10).pow(decimals)); | ||
}; | ||
} | ||
function amountDecimals() { | ||
@@ -54,4 +23,2 @@ let decimals = Number.parseInt(process.env.DEPLOY_AMOUNT_DECIMALS); | ||
const GWei = amountFunction(9); | ||
const _W = amountFunction(18); | ||
const _R = amountFunction(27); | ||
const _A = amountFunction(amountDecimals()); | ||
@@ -191,44 +158,2 @@ | ||
function parseRole(role) { | ||
if (role.startsWith("0x")) return role; | ||
if (role === "DEFAULT_ADMIN_ROLE") return ethers.constants.HashZero; | ||
return ethers.utils.keccak256(ethers.utils.toUtf8Bytes(role)); | ||
} | ||
async function grantComponentRole(hre, contract, component, role, user) { | ||
let userAddress; | ||
if (user === undefined) { | ||
user = await _getDefaultSigner(hre); | ||
userAddress = user.address; | ||
} else { | ||
userAddress = user; | ||
} | ||
const roleHex = parseRole(role); | ||
const componentAddress = component.address || component; | ||
const componentRole = await contract.getComponentRole(componentAddress, roleHex); | ||
if (!(await contract.hasRole(componentRole, userAddress))) { | ||
await contract.grantComponentRole(componentAddress, roleHex, userAddress, txOverrides()); | ||
console.log(`Role ${role} (${roleHex}) Component ${componentAddress} granted to ${userAddress}`); | ||
} else { | ||
console.log(`Role ${role} (${roleHex}) Component ${componentAddress} already granted to ${userAddress}`); | ||
} | ||
} | ||
async function grantRole(hre, contract, role, user) { | ||
let userAddress; | ||
if (user === undefined) { | ||
user = await _getDefaultSigner(hre); | ||
userAddress = user.address; | ||
} else { | ||
userAddress = user; | ||
} | ||
const roleHex = parseRole(role); | ||
if (!(await contract.hasRole(roleHex, userAddress))) { | ||
await contract.grantRole(roleHex, userAddress, txOverrides()); | ||
console.log(`Role ${role} (${roleHex}) granted to ${userAddress}`); | ||
} else { | ||
console.log(`Role ${role} (${roleHex}) already granted to ${userAddress}`); | ||
} | ||
} | ||
async function grantRoleTask({ contractAddress, role, account, component, impersonate, impersonateBalance }, hre) { | ||
@@ -244,5 +169,5 @@ let contract = await hre.ethers.getContractAt("AccessManager", contractAddress); | ||
if (component === ethers.constants.AddressZero) { | ||
await grantRole(hre, contract, role, account); | ||
await grantRole(hre, contract, role, account, txOverrides(), console.log); | ||
} else { | ||
await grantComponentRole(hre, contract, component, role, account); | ||
await grantComponentRole(hre, contract, component, role, account, txOverrides(), console.log); | ||
} | ||
@@ -268,7 +193,2 @@ } | ||
async function _getDefaultSigner(hre) { | ||
const signers = await hre.ethers.getSigners(); | ||
return signers[0]; | ||
} | ||
async function deployPolicyPool({ accessAddress, currencyAddress, nftName, nftSymbol, treasuryAddress, ...opts }, hre) { | ||
@@ -456,3 +376,3 @@ return ( | ||
async function deployWhitelist( | ||
{ wlClass, poolAddress, extraConstructorArgs, extraArgs, eToken, eToken2, eToken3, defaultStatus, runAs, ...opts }, | ||
{ wlClass, poolAddress, extraConstructorArgs, extraArgs, eToken, eToken2, eToken3, defaultStatus, ...opts }, | ||
hre | ||
@@ -500,3 +420,3 @@ ) { | ||
customer = customer || (await _getDefaultSigner(hre)); | ||
customer = customer || (await getDefaultSigner(hre)); | ||
premium = _A(premium); | ||
@@ -545,3 +465,3 @@ | ||
await grantComponentRole(hre, access, rm, "PRICER_ROLE"); | ||
customer = customer || (await _getDefaultSigner(hre)); | ||
customer = customer || (await getDefaultSigner(hre)); | ||
premium = _A(premium); | ||
@@ -573,3 +493,3 @@ | ||
for (i = 0; i < etkCount; i++) { | ||
for (let i = 0; i < etkCount; i++) { | ||
const etk = await hre.ethers.getContractAt("EToken", await policyPool.getETokenAt(i)); | ||
@@ -612,3 +532,3 @@ const etkName = await etk.name(); | ||
taskArgs.saveAddr = "POOL"; | ||
let policyPoolAddress = await deployPolicyPool(taskArgs, hre); | ||
await deployPolicyPool(taskArgs, hre); | ||
}); | ||
@@ -800,4 +720,2 @@ | ||
deployProxyContract, | ||
grantRole, | ||
grantComponentRole, | ||
deployEToken, | ||
@@ -809,3 +727,2 @@ deployPremiumsAccount, | ||
deployWhitelist, | ||
WhitelistStatus, | ||
txOverrides, | ||
@@ -812,0 +729,0 @@ amountFunction, |
@@ -1,11 +0,5 @@ | ||
const { expect } = require("chai"); | ||
const hre = require("hardhat"); | ||
const { BigNumber } = require("ethers"); | ||
const { LogDescription } = require("ethers/lib/utils"); | ||
const { findAll } = require("solidity-ast/utils"); | ||
const helpers = require("@nomicfoundation/hardhat-network-helpers"); | ||
const { _W, grantRole, getTransactionEvent } = require("../js/utils"); | ||
const { RiskModuleParameter } = require("../js/enums"); | ||
WEEK = 3600 * 24 * 7; | ||
DAY = 3600 * 24; | ||
async function initCurrency(options, initial_targets, initial_balances) { | ||
@@ -28,32 +22,3 @@ const Currency = await hre.ethers.getContractFactory("TestCurrency"); | ||
async function approve_multiple(currency, spender, sources, amounts) { | ||
return Promise.all( | ||
sources.map(async function (source, index) { | ||
await currency.connect(source).approve(spender.address, amounts[index]); | ||
}) | ||
); | ||
} | ||
async function check_balances(currency, users, amounts) { | ||
return Promise.all( | ||
users.map(async function (user, index) { | ||
expect(await currency.balanceOf(user.address)).to.equal(amounts[index]); | ||
}) | ||
); | ||
} | ||
const RiskModuleParameter = { | ||
moc: 0, | ||
jrCollRatio: 1, | ||
collRatio: 2, | ||
ensuroPpFee: 3, | ||
ensuroCocFee: 4, | ||
jrRoc: 5, | ||
srRoc: 6, | ||
maxPayoutPerPolicy: 7, | ||
exposureLimit: 8, | ||
maxDuration: 9, | ||
}; | ||
const createRiskModule = async function ( | ||
async function createRiskModule( | ||
pool, | ||
@@ -103,3 +68,3 @@ premiumsAccount, | ||
return rm; | ||
}; | ||
} | ||
@@ -140,3 +105,3 @@ async function addRiskModule( | ||
const createEToken = async function ( | ||
async function createEToken( | ||
pool, | ||
@@ -166,5 +131,5 @@ { etkName, etkSymbol, maxUtilizationRate, poolLoanInterestRate, extraArgs, extraConstructorArgs } | ||
return etk; | ||
}; | ||
} | ||
addEToken = async function addEToken( | ||
async function addEToken( | ||
pool, | ||
@@ -183,38 +148,8 @@ { etkName, etkSymbol, maxUtilizationRate, poolLoanInterestRate, extraArgs, extraConstructorArgs } | ||
return etk; | ||
}; | ||
async function expected_change(protocol_attribute, initial, change) { | ||
change = BigNumber.from(change); | ||
let actual_value = await protocol_attribute(); | ||
expect(actual_value.sub(initial)).to.equal(change); | ||
return actual_value; | ||
} | ||
/** | ||
* Finds an event in the receipt | ||
* @param {Interface} interface The interface of the contract that contains the requested event | ||
* @param {TransactionReceipt} receipt Transaction receipt containing the events in the logs | ||
* @param {String} eventName The name of the event we are interested in | ||
* @returns {LogDescription} | ||
*/ | ||
const getTransactionEvent = function (interface, receipt, eventName) { | ||
// for each log in the transaction receipt | ||
for (const log of receipt.logs) { | ||
let parsedLog; | ||
try { | ||
parsedLog = interface.parseLog(log); | ||
} catch (error) { | ||
continue; | ||
} | ||
if (parsedLog.name == eventName) { | ||
return parsedLog; | ||
} | ||
} | ||
return null; // not found | ||
}; | ||
const randomAddress = "0x89cDb70Fee571251a66E34caa1673cE40f7549Dc"; | ||
/** | ||
* Deploys de PolicyPool contract and AccessManager | ||
* Deploys the PolicyPool contract and AccessManager | ||
* | ||
@@ -232,3 +167,3 @@ * By default deployes de PolicyPool and AccessManager and grants LEVEL 1, 2, 3 permissions | ||
*/ | ||
async function deployPool(hre, options) { | ||
async function deployPool(options) { | ||
const PolicyPool = await hre.ethers.getContractFactory("PolicyPool"); | ||
@@ -275,3 +210,3 @@ const AccessManager = await hre.ethers.getContractFactory("AccessManager"); | ||
async function deployPremiumsAccount(hre, pool, options, addToPool = true) { | ||
async function deployPremiumsAccount(pool, options, addToPool = true) { | ||
const PremiumsAccount = await hre.ethers.getContractFactory("PremiumsAccount"); | ||
@@ -295,108 +230,2 @@ const premiumsAccount = await hre.upgrades.deployProxy(PremiumsAccount, [], { | ||
async function _getDefaultSigner(hre) { | ||
const signers = await hre.ethers.getSigners(); | ||
return signers[0]; | ||
} | ||
async function grantRole(hre, contract, role, user) { | ||
let userAddress; | ||
if (user === undefined) { | ||
user = await _getDefaultSigner(hre); | ||
userAddress = user.address; | ||
} else { | ||
userAddress = user; | ||
} | ||
if (!(await contract.hasRole(getRole(role), userAddress))) { | ||
await contract.grantRole(getRole(role), userAddress); | ||
} | ||
} | ||
async function grantComponentRole(hre, accessManager, component, role, user) { | ||
let userAddress; | ||
if (user === undefined) { | ||
user = await _getDefaultSigner(hre); | ||
userAddress = user.address; | ||
} else { | ||
userAddress = user.address === undefined ? user : user.address; | ||
} | ||
const componentRole = getComponentRole(component.address, getRole(role)); | ||
if (!(await accessManager.hasRole(componentRole, userAddress))) { | ||
await accessManager.grantComponentRole(component.address, getRole(role), userAddress); | ||
} | ||
} | ||
const _E = hre.ethers.utils.parseEther; | ||
const _BN = hre.ethers.BigNumber.from; | ||
const WAD = _BN(10).pow(18); // 1e18 | ||
const RAY = _BN(10).pow(27); // 1e27 | ||
/** | ||
* Creates a fixed-point conversion function for the desired number of decimals | ||
* @param decimals The number of decimals. Must be >= 6. | ||
* @returns The amount function created. The function can receive strings (recommended), | ||
* floats/doubles (not recommended) and integers. | ||
* | ||
* Floats will be rounded to 6 decimal before scaling. | ||
*/ | ||
function amountFunction(decimals) { | ||
return function (value) { | ||
if (value === undefined) return undefined; | ||
if (typeof value === "string" || value instanceof String) { | ||
return hre.ethers.utils.parseUnits(value, decimals); | ||
} | ||
if (!Number.isInteger(value)) { | ||
return _BN(Math.round(value * 1e6)).mul(_BN(Math.pow(10, decimals - 6))); | ||
} | ||
return _BN(value).mul(_BN(10).pow(decimals)); | ||
}; | ||
} | ||
const _W = amountFunction(18); | ||
const _R = amountFunction(27); | ||
/** | ||
* Builds the component role identifier | ||
* | ||
* Mimics the behaviour of the PolicyPoolConfig.getComponentRole method | ||
* | ||
* Component roles are roles created doing XOR between the component | ||
* address and the original role. | ||
* | ||
* Example: | ||
* getComponentRole("0xc6e7DF5E7b4f2A278906862b61205850344D4e7d", "ORACLE_ADMIN_ROLE") | ||
* // "0x05e01b185238b49f750d03d945e38a7f6c3be8b54de0ee42d481eb7814f0d3a8" | ||
*/ | ||
function getComponentRole(componentAddress, role) { | ||
if (!role.startsWith("0x")) role = getRole(role); | ||
// 32 byte array | ||
const bytesRole = hre.ethers.utils.arrayify(role); | ||
// 20 byte array | ||
const bytesAddress = hre.ethers.utils.arrayify(componentAddress); | ||
// xor each byte, padding bytesAddress with zeros at the end | ||
return hre.ethers.utils.hexlify(bytesRole.map((elem, idx) => elem ^ (bytesAddress[idx] || 0))); | ||
} | ||
/* | ||
Builds AccessControl error message for comparison in tests | ||
*/ | ||
function accessControlMessage(address, component, role) { | ||
const roleHash = component !== null ? getComponentRole(component, role) : getRole(role); | ||
return `AccessControl: account ${address.toLowerCase()} is missing role ${roleHash}`; | ||
} | ||
function makePolicyId(rm, internalId) { | ||
return hre.ethers.BigNumber.from(rm.address).shl(96).add(internalId); | ||
} | ||
async function makePolicy(pool, rm, cust, payout, premium, lossProb, expiration, internalId, method = "newPolicy") { | ||
@@ -410,69 +239,13 @@ let tx = await rm.connect(cust)[method](payout, premium, lossProb, expiration, cust.address, internalId); | ||
function getRole(role) { | ||
return role === "DEFAULT_ADMIN_ROLE" | ||
? "0x0000000000000000000000000000000000000000000000000000000000000000" | ||
: hre.ethers.utils.keccak256(hre.ethers.utils.toUtf8Bytes(role)); | ||
} | ||
async function getStorageLayout(contractSrc, contractName) { | ||
const buildInfo = await hre.artifacts.getBuildInfo(`${contractSrc}:${contractName}`); | ||
if (buildInfo === undefined) throw new Error(`Contract ${contractSrc}:${contractName} not in artifacts`); | ||
const solcOutput = buildInfo.output; | ||
const storageLayouts = {}; | ||
for (const def of findAll("ContractDefinition", solcOutput.sources[contractSrc].ast)) { | ||
storageLayouts[def.name] = solcOutput.contracts[contractSrc][def.name].storageLayout; | ||
} | ||
return storageLayouts[contractName]; | ||
} | ||
function makeQuoteMessage({ rmAddress, payout, premium, lossProb, expiration, policyData, validUntil }) { | ||
return ethers.utils.solidityPack( | ||
["address", "uint256", "uint256", "uint256", "uint40", "bytes32", "uint40"], | ||
[rmAddress, payout, premium, lossProb, expiration, policyData, validUntil] | ||
); | ||
} | ||
async function makeSignedQuote(signer, policyParams) { | ||
const quoteMessage = makeQuoteMessage(policyParams); | ||
return ethers.utils.splitSignature(await signer.signMessage(ethers.utils.arrayify(quoteMessage))); | ||
} | ||
if (process.env.ENABLE_HH_WARNINGS !== "yes") hre.upgrades.silenceWarnings(); | ||
module.exports = { | ||
_BN, | ||
_E, | ||
_R, | ||
_W, | ||
accessControlMessage, | ||
addEToken, | ||
addRiskModule, | ||
amountFunction, | ||
approve_multiple, | ||
check_balances, | ||
createEToken, | ||
createRiskModule, | ||
DAY, | ||
deployPool, | ||
deployPremiumsAccount, | ||
expected_change, | ||
getComponentRole, | ||
getRole, | ||
getStorageLayout, | ||
getTransactionEvent, | ||
grantComponentRole, | ||
grantRole, | ||
initCurrency, | ||
makePolicy, | ||
makePolicyId, | ||
makeQuoteMessage, | ||
makeSignedQuote, | ||
RAY, | ||
RiskModuleParameter, | ||
WAD, | ||
WEEK, | ||
}; |
{ | ||
"name": "@ensuro/core", | ||
"description": "Ensuro - Decentralized insurance protocol", | ||
"version": "2.5.0", | ||
"version": "v2.6.0-beta1", | ||
"files": [ | ||
@@ -6,0 +6,0 @@ "**/*.sol", |
const hre = require("hardhat"); | ||
const { artifacts } = require("hardhat"); | ||
const { findAll } = require("solidity-ast/utils"); | ||
const { getStorageLayout } = require("../test/test-utils"); | ||
const { getStorageLayout } = require("../js/utils"); | ||
@@ -12,7 +10,5 @@ /** | ||
// | ||
async function main(contractSrc, contractName) { | ||
await hre.run("compile"); | ||
const layout = await getStorageLayout(contractSrc, contractName); | ||
const layout = await getStorageLayout(contractSrc, contractName, hre); | ||
@@ -33,3 +29,4 @@ const summary = layout.storage.map((item) => ({ | ||
main(contractSrc, contractName).then(() => { | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(0); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1636810
93
23202
1
1