Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
- Refactored protocol initialization arguments
- Moved functions `setReportingBurnRate`, `setReportingCommission`, `setClaimPeriod`, `setCoverFees`, `setMinStake`, `setMinReportingStake`, `setMinLiquidityPeriod` to `Processor.sol`, `Reporter.sol`, and `CoverBase.sol`
- Renamed `liquidityToken` to `stablecoin`
- Extracted a dedicated namespace called `Contract Namespace` on ProtoUtilV1
- Refactored namespaces with simpler name and classification
  • Loading branch information
heyaibi committed Dec 28, 2021
1 parent 4b4f505 commit 65bd433
Show file tree
Hide file tree
Showing 54 changed files with 1,053 additions and 1,024 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@
"CXTOKEN",
"IERC",
"IERC20",
"IPFS",
"iszero",
"Keccak",
"mload",
"nbsp",
"Proto",
"Reentrancy",
"Unstakable",
"unstake",
"Unstaken"
"Unstaken",
"WXDAI",
"zerox"
]
}
164 changes: 37 additions & 127 deletions contracts/core/Protocol.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,140 +16,50 @@ contract Protocol is IProtocol, ProtoBase {

constructor(IStore store) ProtoBase(store) {} // solhint-disable-line

function initialize(
address uniswapV2RouterLike,
address npm,
address treasury,
address reassuranceVault,
uint256 coverFee,
uint256 minStake,
uint256 minReportingStake,
uint256 minLiquidityPeriod,
uint256 claimPeriod,
uint256 burnRate,
uint256 reporterCommission
) external nonReentrant whenNotPaused {
/**
* @dev Initializes the protocol
* @param addresses[0] uniswapV2RouterLike
* @param addresses[1] npm
* @param addresses[2] treasury
* @param addresses[3] reassuranceVault
* @param values[0] coverCreationFees
* @param values[1] minCoverCreationStake
* @param values[2] firstReportingStake
* @param values[3] minLiquidityPeriod
* @param values[4] claimPeriod
* @param values[5] burnRate
* @param values[6] reporterCommission
*/
function initialize(address[] memory addresses, uint256[] memory values) external override whenNotPaused {
// @suppress-acl Can only be called once by the deployer
s.mustBeProtocolMember(msg.sender);

require(initialized == 0, "Already initialized");
require(npm != address(0), "Invalid NPM");
require(uniswapV2RouterLike != address(0), "Invalid Router");
require(treasury != address(0), "Invalid Treasury");
require(reassuranceVault != address(0), "Invalid Vault");

s.setAddressByKey(ProtoUtilV1.NS_CORE, address(this));
s.setBoolByKeys(ProtoUtilV1.NS_CONTRACTS, address(this), true);
s.setAddressByKey(ProtoUtilV1.NS_BURNER, 0x0000000000000000000000000000000000000001);

s.setAddressByKey(ProtoUtilV1.NS_SETUP_NPM, npm);
s.setAddressByKey(ProtoUtilV1.NS_SETUP_UNISWAP_V2_ROUTER, uniswapV2RouterLike);
s.setAddressByKey(ProtoUtilV1.NS_TREASURY, treasury);
s.setAddressByKey(ProtoUtilV1.NS_REASSURANCE_VAULT, reassuranceVault);

_setCoverFees(coverFee);
_setMinStake(minStake);
_setMinReportingStake(minReportingStake);
_setMinLiquidityPeriod(minLiquidityPeriod);

_setReportingBurnRate(burnRate);
_setReporterCommission(reporterCommission);
_setClaimPeriod(claimPeriod);

initialized = 1;
}

function setReportingBurnRate(uint256 value) public nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);
_setReportingBurnRate(value);
}

function setReportingCommission(uint256 value) public nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);
_setReporterCommission(value);
}

function setClaimPeriod(uint256 value) public nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);
_setClaimPeriod(value);
}

function setCoverFees(uint256 value) public nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);
_setCoverFees(value);
}
require(addresses[0] != address(0), "Invalid Router");
require(addresses[1] != address(0), "Invalid NPM");
require(addresses[2] != address(0), "Invalid Treasury");
require(addresses[3] != address(0), "Invalid Vault");

function setMinStake(uint256 value) public nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);

_setMinStake(value);
}

function setMinReportingStake(uint256 value) public nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);
_setMinReportingStake(value);
}

function setMinLiquidityPeriod(uint256 value) public nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeLiquidityManager(s);

_setMinLiquidityPeriod(value);
}

function _setReportingBurnRate(uint256 value) private {
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_REPORTING_BURN_RATE);
s.setUintByKey(ProtoUtilV1.NS_REPORTING_BURN_RATE, value);

emit ReportingBurnRateSet(previous, value);
}

function _setReporterCommission(uint256 value) private {
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_REPORTER_COMMISSION);
s.setUintByKey(ProtoUtilV1.NS_REPORTER_COMMISSION, value);

emit ReporterCommissionSet(previous, value);
}

function _setClaimPeriod(uint256 value) private {
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_SETUP_CLAIM_PERIOD);
s.setUintByKey(ProtoUtilV1.NS_SETUP_CLAIM_PERIOD, value);

emit ClaimPeriodSet(previous, value);
}

function _setCoverFees(uint256 value) private {
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_SETUP_COVER_CREATION_FEE);
s.setUintByKey(ProtoUtilV1.NS_SETUP_COVER_CREATION_FEE, value);

emit CoverFeeSet(previous, value);
}

function _setMinStake(uint256 value) private {
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_SETUP_MIN_STAKE);
s.setUintByKey(ProtoUtilV1.NS_SETUP_MIN_STAKE, value);

emit MinStakeSet(previous, value);
}

function _setMinReportingStake(uint256 value) private {
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_SETUP_FIRST_REPORTING_STAKE);
s.setUintByKey(ProtoUtilV1.NS_SETUP_FIRST_REPORTING_STAKE, value);

emit MinReportingStakeSet(previous, value);
}

function _setMinLiquidityPeriod(uint256 value) private {
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_SETUP_MIN_LIQ_PERIOD);
s.setUintByKey(ProtoUtilV1.NS_SETUP_MIN_LIQ_PERIOD, value);

emit MinLiquidityPeriodSet(previous, value);
s.setAddressByKey(ProtoUtilV1.CNS_CORE, address(this));
s.setBoolByKeys(ProtoUtilV1.NS_CONTRACTS, address(this), true);
s.setAddressByKey(ProtoUtilV1.CNS_BURNER, 0x0000000000000000000000000000000000000001);

s.setAddressByKey(ProtoUtilV1.CNS_UNISWAP_V2_ROUTER, addresses[0]);
s.setAddressByKey(ProtoUtilV1.CNS_NPM, addresses[1]);
s.setAddressByKey(ProtoUtilV1.CNS_TREASURY, addresses[2]);
s.setAddressByKey(ProtoUtilV1.CNS_REASSURANCE_VAULT, addresses[3]);

s.setUintByKey(ProtoUtilV1.NS_COVER_CREATION_FEE, values[0]);
s.setUintByKey(ProtoUtilV1.NS_COVER_CREATION_MIN_STAKE, values[1]);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_MIN_FIRST_STAKE, values[2]);
s.setUintByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_MIN_PERIOD, values[3]);
s.setUintByKey(ProtoUtilV1.NS_CLAIM_PERIOD, values[4]);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_BURN_RATE, values[5]);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTER_COMMISSION, values[6]);

emit Initialized(addresses, values);
}

function upgradeContract(
Expand Down
10 changes: 10 additions & 0 deletions contracts/core/claims/Processor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ contract Processor is IClaimsProcessor, Recoverable {
return s.getUintByKeys(ProtoUtilV1.NS_CLAIM_EXPIRY_TS, key);
}

function setClaimPeriod(uint256 value) external override nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_CLAIM_PERIOD);
s.setUintByKey(ProtoUtilV1.NS_CLAIM_PERIOD, value);

emit ClaimPeriodSet(previous, value);
}

/**
* @dev Version number of this contract
*/
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/discovery/PriceDiscovery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract PriceDiscovery is IPriceDiscovery, Recoverable {
* @param multiplier Enter the token price multiplier
*/
function getTokenPriceInStableCoin(address token, uint256 multiplier) external view override returns (uint256) {
address stablecoin = s.getLiquidityToken();
address stablecoin = s.getStablecoin();
return this.getTokenPriceInLiquidityToken(token, stablecoin, multiplier);
}

Expand Down
38 changes: 34 additions & 4 deletions contracts/core/governance/Reporter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ abstract contract Reporter is IReporter, Witness {
uint256 incidentDate = block.timestamp; // solhint-disable-line
require(stake >= getMinStake(), "Stake insufficient");

s.setUintByKeys(ProtoUtilV1.NS_REPORTING_INCIDENT_DATE, key, incidentDate);
s.setUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_INCIDENT_DATE, key, incidentDate);

// Set the Resolution Timestamp
uint256 resolutionDate = block.timestamp + s.getReportingPeriod(key); // solhint-disable-line
s.setUintByKeys(ProtoUtilV1.NS_RESOLUTION_TS, key, resolutionDate);
s.setUintByKeys(ProtoUtilV1.NS_GOVERNANCE_RESOLUTION_TS, key, resolutionDate);

// Update the values
s.addAttestation(key, msg.sender, incidentDate, stake);
Expand Down Expand Up @@ -80,16 +80,46 @@ abstract contract Reporter is IReporter, Witness {
emit Refuted(key, msg.sender, incidentDate, stake);
}

function setFirstReportingStake(uint256 value) external override nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_MIN_FIRST_STAKE);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_MIN_FIRST_STAKE, value);

emit FirstReportingStakeSet(previous, value);
}

function setReportingBurnRate(uint256 value) external override nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_BURN_RATE);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_BURN_RATE, value);

emit ReportingBurnRateSet(previous, value);
}

function setReporterCommission(uint256 value) external override nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTER_COMMISSION);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTER_COMMISSION, value);

emit ReporterCommissionSet(previous, value);
}

function getActiveIncidentDate(bytes32 key) external view override returns (uint256) {
return s.getUintByKeys(ProtoUtilV1.NS_REPORTING_INCIDENT_DATE, key);
return s.getUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_INCIDENT_DATE, key);
}

function getReporter(bytes32 key, uint256 incidentDate) external view override returns (address) {
return s.getReporter(key, incidentDate);
}

function getResolutionDate(bytes32 key) external view override returns (uint256) {
return s.getUintByKeys(ProtoUtilV1.NS_RESOLUTION_TS, key);
return s.getUintByKeys(ProtoUtilV1.NS_GOVERNANCE_RESOLUTION_TS, key);
}

function getMinStake() public view override returns (uint256) {
Expand Down
8 changes: 4 additions & 4 deletions contracts/core/governance/resolution/Finalization.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ abstract contract Finalization is Recoverable, IFinalization {
function _finalize(bytes32 key, uint256 incidentDate) internal {
// Reset to normal
s.setStatus(key, CoverUtilV1.CoverStatus.Normal);
s.deleteUintByKeys(ProtoUtilV1.NS_REPORTING_INCIDENT_DATE, key);
s.deleteUintByKeys(ProtoUtilV1.NS_RESOLUTION_TS, key);
s.deleteUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_INCIDENT_DATE, key);
s.deleteUintByKeys(ProtoUtilV1.NS_GOVERNANCE_RESOLUTION_TS, key);
s.deleteUintByKeys(ProtoUtilV1.NS_CLAIM_EXPIRY_TS, key);

s.deleteAddressByKeys(ProtoUtilV1.NS_REPORTING_WITNESS_YES, key);
s.deleteUintByKeys(ProtoUtilV1.NS_REPORTING_WITNESS_YES, key);
s.deleteAddressByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_WITNESS_YES, key);
s.deleteUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_WITNESS_YES, key);

emit Finalized(key, msg.sender, incidentDate);
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/core/lifecycle/Cover.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ contract Cover is CoverBase {
s.getVault(key).addLiquidityInternal(key, msg.sender, initialLiquidity);

// Transfer liquidity only after minting the pods
IERC20(s.getLiquidityToken()).ensureTransferFrom(msg.sender, address(vault), initialLiquidity);
IERC20(s.getStablecoin()).ensureTransferFrom(msg.sender, address(vault), initialLiquidity);
}

emit CoverCreated(key, info, stakeWithFee, initialLiquidity);
Expand Down Expand Up @@ -147,7 +147,7 @@ contract Cover is CoverBase {

// Set cover info
s.setBytes32ByKeys(ProtoUtilV1.NS_COVER_INFO, key, info);
s.setUintByKeys(ProtoUtilV1.NS_REPORTING_PERIOD, key, reportingPeriod);
s.setUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_PERIOD, key, reportingPeriod);

// Set reassurance token
s.setAddressByKeys(ProtoUtilV1.NS_COVER_REASSURANCE_TOKEN, key, reassuranceToken);
Expand All @@ -159,7 +159,7 @@ contract Cover is CoverBase {
// Deploy cover liquidity contract
address deployed = s.getVaultFactoryContract().deploy(s, key);

s.setAddressByKeys(ProtoUtilV1.NS_CONTRACTS, ProtoUtilV1.NS_COVER_VAULT, key, deployed);
s.setAddressByKeys(ProtoUtilV1.NS_CONTRACTS, ProtoUtilV1.CNS_COVER_VAULT, key, deployed);
s.setBoolByKeys(ProtoUtilV1.NS_MEMBERS, deployed, true);
}

Expand Down
25 changes: 23 additions & 2 deletions contracts/core/lifecycle/CoverBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,31 @@ abstract contract CoverBase is ICover, Recoverable {
s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);

require(s.getAddressByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_TOKEN) == address(0), "Already initialized");
require(s.getAddressByKey(ProtoUtilV1.CNS_COVER_STABLECOIN) == address(0), "Already initialized");

s.setAddressByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_TOKEN, liquidityToken);
s.setAddressByKey(ProtoUtilV1.CNS_COVER_STABLECOIN, liquidityToken);
s.setBytes32ByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_NAME, liquidityName);

emit CoverInitialized(liquidityToken, liquidityName);
}

function setCoverFees(uint256 value) public override nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);
uint256 previous = s.getUintByKey(ProtoUtilV1.NS_COVER_CREATION_FEE);
s.setUintByKey(ProtoUtilV1.NS_COVER_CREATION_FEE, value);

emit CoverFeeSet(previous, value);
}

function setMinCoverCreationStake(uint256 value) public override nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeCoverManager(s);

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_COVER_CREATION_MIN_STAKE);
s.setUintByKey(ProtoUtilV1.NS_COVER_CREATION_MIN_STAKE, value);

emit MinCoverCreationStakeSet(previous, value);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions contracts/core/liquidity/VaultBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract contract VaultBase is IVault, Recoverable, ERC20 {
using ProtoUtilV1 for bytes;
using ProtoUtilV1 for IStore;
using ValidationLibV1 for IStore;
using StoreKeyUtil for IStore;
using CoverUtilV1 for IStore;
using NTransferUtilV2 for IERC20;

Expand Down Expand Up @@ -124,6 +125,16 @@ abstract contract VaultBase is IVault, Recoverable, ERC20 {
emit PodsIssued(account, podsToMint, amount);
}

function setMinLiquidityPeriod(uint256 value) external override nonReentrant {
ValidationLibV1.mustNotBePaused(s);
AccessControlLibV1.mustBeLiquidityManager(s);

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_MIN_PERIOD);
s.setUintByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_MIN_PERIOD, value);

emit MinLiquidityPeriodSet(previous, value);
}

/**
* @dev Version number of this contract
*/
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/liquidity/VaultFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract VaultFactory is IVaultFactory, Recoverable {
s.mustBeValidCover(key);
s.callerMustBeCoverContract();

(bytes memory bytecode, bytes32 salt) = VaultFactoryLibV1.getByteCode(s, key, s.getLiquidityToken());
(bytes memory bytecode, bytes32 salt) = VaultFactoryLibV1.getByteCode(s, key, s.getStablecoin());

// solhint-disable-next-line
assembly {
Expand Down
Loading

0 comments on commit 65bd433

Please sign in to comment.