Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Governance Refactor and Bug Fixes #11

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions abis/ICxToken.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Finalized",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
25 changes: 0 additions & 25 deletions abis/IPriceDiscovery.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "token",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "stablecoin",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "price",
"type": "uint256"
}
],
"name": "PriceUpdated",
"type": "event"
},
{
"inputs": [],
"name": "getName",
Expand Down
32 changes: 32 additions & 0 deletions abis/IVault.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@
"name": "GovernanceTransfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "bytes32",
"name": "key",
"type": "bytes32"
}
],
"name": "InterestAccrued",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -156,6 +169,18 @@
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "income",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "loss",
"type": "uint256"
}
],
"name": "StrategyReceipt",
Expand Down Expand Up @@ -217,6 +242,13 @@
"name": "Transfer",
"type": "event"
},
{
"inputs": [],
"name": "accrueInterest",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down
28 changes: 19 additions & 9 deletions contracts/core/Protocol.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "./ProtoBase.sol";

contract Protocol is IProtocol, ProtoBase {
using ProtoUtilV1 for bytes;
using RegistryLibV1 for IStore;
using ProtoUtilV1 for IStore;
using ValidationLibV1 for IStore;
using StoreKeyUtil for IStore;
Expand Down Expand Up @@ -38,27 +39,32 @@ contract Protocol is IProtocol, ProtoBase {
* @param values[10] resolutionCoolDownPeriod
*/
function initialize(address[] memory addresses, uint256[] memory values) external override nonReentrant whenNotPaused {
// @suppress-initialization Can only be initialized once by the deployer
// @suppress-acl Can only be called once by the deployer
// @suppress-initialization Can only be initialized by the deployer or an admin
// @suppress-acl Can only be called by the deployer or an admin
s.mustBeProtocolMember(msg.sender);

require(initialized == 0, "Already initialized");
initialized = 1;
if (initialized == 1) {
AccessControlLibV1.mustBeAdmin(s);
require(addresses[3] == address(0), "Can't change NPM");
} else {
require(addresses[3] != address(0), "Invalid NPM");

s.setAddressByKey(ProtoUtilV1.CNS_CORE, address(this));
s.setBoolByKeys(ProtoUtilV1.NS_CONTRACTS, address(this), true);

s.setAddressByKey(ProtoUtilV1.CNS_NPM, addresses[3]);
}

require(addresses[0] != address(0), "Invalid Burner");
require(addresses[1] != address(0), "Invalid Uniswap V2 Router");
require(addresses[2] != address(0), "Invalid Uniswap V2 Factory");
require(addresses[3] != address(0), "Invalid NPM");
require(addresses[4] != address(0), "Invalid Treasury");
require(addresses[5] != address(0), "Invalid Vault");
require(addresses[5] != address(0), "Invalid Reassurance Vault");

s.setAddressByKey(ProtoUtilV1.CNS_CORE, address(this));
s.setBoolByKeys(ProtoUtilV1.NS_CONTRACTS, address(this), true);
s.setAddressByKey(ProtoUtilV1.CNS_BURNER, addresses[0]);

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

Expand All @@ -74,6 +80,7 @@ contract Protocol is IProtocol, ProtoBase {
s.setUintByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_FLASH_LOAN_FEE_PROTOCOL, values[9]);
s.setUintByKey(ProtoUtilV1.NS_RESOLUTION_COOL_DOWN_PERIOD, values[10]);

initialized = 1;
emit Initialized(addresses, values);
}

Expand All @@ -95,6 +102,9 @@ contract Protocol is IProtocol, ProtoBase {
// @suppress-address-trust-issue Although the `contractAddress` can't be trusted, the upgrade admin has to check the contract code manually.
s.mustNotBePaused();
AccessControlLibV1.mustBeUpgradeAgent(s);
address current = s.getProtocolContract(namespace);

require(current == address(0), "Please upgrade contract");

s.addContractInternal(namespace, contractAddress);
emit ContractAdded(namespace, contractAddress);
Expand Down
3 changes: 3 additions & 0 deletions contracts/core/claims/Processor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ contract Processor is IClaimsProcessor, Recoverable {
function setClaimPeriod(bytes32 key, uint256 value) external override nonReentrant {
s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);

require(value > 0, "Please specify value");

uint256 previous;

if (key > 0) {
Expand Down
3 changes: 3 additions & 0 deletions contracts/core/cxToken/cxToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ contract cxToken is ICxToken, Recoverable, ERC20 {
) external override nonReentrant {
// @suppress-acl Can only be called by the latest policy contract
s.mustNotBePaused();

require(amount > 0, "Please specify amount");
require(key == coverKey, "Invalid cover");
s.callerMustBePolicyContract();

Expand All @@ -71,6 +73,7 @@ contract cxToken is ICxToken, Recoverable, ERC20 {
*/
function burn(uint256 amount) external override nonReentrant {
// @suppress-acl Marking this as publicly accessible
require(amount > 0, "Please specify amount");

s.mustNotBePaused();
super._burn(msg.sender, amount);
Expand Down
2 changes: 2 additions & 0 deletions contracts/core/cxToken/cxTokenFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ contract cxTokenFactory is ICxTokenFactory, Recoverable {
s.mustBeValidCoverKey(key);
s.callerMustBePolicyContract();

require(expiryDate > 0, "Please specify expiry date");

(bytes memory bytecode, bytes32 salt) = cxTokenFactoryLibV1.getByteCode(s, key, expiryDate, _getTokenName(key), _getTokenSymbol());

require(s.getAddress(salt) == address(0), "Already deployed");
Expand Down
4 changes: 4 additions & 0 deletions contracts/core/governance/Reporter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ abstract contract Reporter is IReporter, Witness {
function setFirstReportingStake(uint256 value) external override nonReentrant {
s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);
require(value > 0, "Please specify value");

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_MIN_FIRST_STAKE);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTING_MIN_FIRST_STAKE, value);
Expand All @@ -101,6 +102,8 @@ abstract contract Reporter is IReporter, Witness {
}

function setReportingBurnRate(uint256 value) external override nonReentrant {
require(value > 0, "Please specify value");

s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);

Expand All @@ -113,6 +116,7 @@ abstract contract Reporter is IReporter, Witness {
function setReporterCommission(uint256 value) external override nonReentrant {
s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);
require(value > 0, "Please specify value");

uint256 previous = s.getUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTER_COMMISSION);
s.setUintByKey(ProtoUtilV1.NS_GOVERNANCE_REPORTER_COMMISSION, value);
Expand Down
5 changes: 4 additions & 1 deletion contracts/core/governance/resolution/Finalization.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract contract Finalization is Recoverable, IFinalization {
function finalize(bytes32 key, uint256 incidentDate) external override nonReentrant {
s.mustNotBePaused();
AccessControlLibV1.mustBeGovernanceAgent(s);
require(incidentDate > 0, "Please specify incident date");

s.mustBeClaimingOrDisputed(key);
s.mustBeValidIncidentDate(key, incidentDate);
Expand All @@ -39,7 +40,9 @@ abstract contract Finalization is Recoverable, IFinalization {

function _finalize(bytes32 key, uint256 incidentDate) internal {
// Reset to normal
s.setStatus(key, CoverUtilV1.CoverStatus.Normal);
// @note: do not pass incident date as we need status by key and incident date for historical significance
s.setStatusInternal(key, 0, CoverUtilV1.CoverStatus.Normal);

s.deleteUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_INCIDENT_DATE, key);
s.deleteUintByKeys(ProtoUtilV1.NS_GOVERNANCE_RESOLUTION_TS, key);
s.deleteUintByKeys(ProtoUtilV1.NS_CLAIM_BEGIN_TS, key);
Expand Down
8 changes: 7 additions & 1 deletion contracts/core/governance/resolution/Resolvable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract contract Resolvable is Finalization, IResolvable {
using NTransferUtilV2 for IERC20;

function resolve(bytes32 key, uint256 incidentDate) external override nonReentrant {
require(incidentDate > 0, "Please specify incident date");

s.mustNotBePaused();
AccessControlLibV1.mustBeGovernanceAgent(s);
s.mustBeReportingOrDisputed(key);
Expand All @@ -40,6 +42,8 @@ abstract contract Resolvable is Finalization, IResolvable {
uint256 incidentDate,
bool decision
) external override nonReentrant {
require(incidentDate > 0, "Please specify incident date");

s.mustNotBePaused();
AccessControlLibV1.mustBeGovernanceAdmin(s);
s.mustBeValidIncidentDate(key, incidentDate);
Expand Down Expand Up @@ -94,7 +98,7 @@ abstract contract Resolvable is Finalization, IResolvable {
s.setUintByKeys(ProtoUtilV1.NS_CLAIM_EXPIRY_TS, key, claimExpiresAt);

// Status can change during `Emergency Resolution` attempt(s)
s.setStatus(key, status);
s.setStatusInternal(key, incidentDate, status);

if (deadline == 0) {
// Deadline can't be before claim begin date.
Expand All @@ -114,6 +118,8 @@ abstract contract Resolvable is Finalization, IResolvable {
AccessControlLibV1.mustBeGovernanceAdmin(s);
s.mustHaveNormalCoverStatus(key);

require(period > 0, "Please specify period");

if (key > 0) {
s.setUintByKeys(ProtoUtilV1.NS_RESOLUTION_COOL_DOWN_PERIOD, key, period);
} else {
Expand Down
12 changes: 10 additions & 2 deletions contracts/core/governance/resolution/Unstakable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ abstract contract Unstakable is Resolvable, IUnstakable {
* @param incidentDate Enter the incident date
*/
function unstake(bytes32 key, uint256 incidentDate) external override nonReentrant {
require(incidentDate > 0, "Please specify incident date");

// @suppress-acl Marking this as publicly accessible
// @suppress-pausable Already checked inside `validateUnstakeWithoutClaim`
s.validateUnstakeWithoutClaim(key, incidentDate);
Expand Down Expand Up @@ -71,8 +73,14 @@ abstract contract Unstakable is Resolvable, IUnstakable {
uint256 myStakeWithReward = myReward + myStakeInWinningCamp;

s.npmToken().ensureTransfer(msg.sender, myStakeWithReward);
s.npmToken().ensureTransfer(finalReporter, toReporter);
s.npmToken().ensureTransfer(burner, toBurn);

if (toReporter > 0) {
s.npmToken().ensureTransfer(finalReporter, toReporter);
}

if (toBurn > 0) {
s.npmToken().ensureTransfer(burner, toBurn);
}

s.updateStateAndLiquidity(key);

Expand Down
6 changes: 6 additions & 0 deletions contracts/core/lifecycle/CoverBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ abstract contract CoverBase is ICover, Recoverable {
}

function setCoverFees(uint256 value) external override nonReentrant {
require(value > 0, "Please specify value");

s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);

Expand All @@ -50,6 +52,8 @@ abstract contract CoverBase is ICover, Recoverable {
}

function setMinCoverCreationStake(uint256 value) external override nonReentrant {
require(value > 0, "Please specify value");

s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);

Expand All @@ -58,6 +62,8 @@ abstract contract CoverBase is ICover, Recoverable {
}

function setMinStakeToAddLiquidity(uint256 value) external override nonReentrant {
require(value > 0, "Please specify value");

s.mustNotBePaused();
AccessControlLibV1.mustBeCoverManager(s);

Expand Down
5 changes: 4 additions & 1 deletion contracts/core/lifecycle/CoverProvision.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ contract CoverProvision is ICoverProvision, Recoverable {
* @param amount Specify the amount of NPM tokens you would like to add
*/
function increaseProvision(bytes32 key, uint256 amount) external override nonReentrant {
require(amount > 0, "Please specify amount");

s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);

Expand All @@ -53,9 +55,10 @@ contract CoverProvision is ICoverProvision, Recoverable {
* @param amount Specify the amount of NPM tokens you would like to decrease
*/
function decreaseProvision(bytes32 key, uint256 amount) external override nonReentrant {
require(amount > 0, "Please specify amount");

s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);

s.mustHaveNormalCoverStatus(key);

uint256 provision = s.decreaseProvisionInternal(key, amount);
Expand Down
3 changes: 2 additions & 1 deletion contracts/core/lifecycle/CoverReassurance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ contract CoverReassurance is ICoverReassurance, Recoverable {
function setWeight(bytes32 key, uint256 weight) external override nonReentrant {
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);

s.mustBeValidCoverKey(key);

require(weight > 0, "Please specify weight");

s.setUintByKeys(ProtoUtilV1.NS_COVER_REASSURANCE_WEIGHT, key, weight);

s.updateStateAndLiquidity(key);
Expand Down
1 change: 1 addition & 0 deletions contracts/core/lifecycle/CoverStake.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ contract CoverStake is ICoverStake, Recoverable {
s.callerMustBeCoverContract();

uint256 drawingPower = _getDrawingPower(key, account);
require(amount > 0, "Please specify amount");
require(drawingPower >= amount, "Exceeds your drawing power");

// @suppress-subtraction
Expand Down
5 changes: 5 additions & 0 deletions contracts/core/liquidity/LiquidityEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ contract LiquidityEngine is ILiquidityEngine, Recoverable {
uint256 lendingPeriod,
uint256 withdrawalWindow
) external override nonReentrant {
require(lendingPeriod > 0, "Please specify lending period");
require(withdrawalWindow > 0, "Please specify withdrawal window");

s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);

Expand All @@ -48,6 +51,8 @@ contract LiquidityEngine is ILiquidityEngine, Recoverable {
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);

require(withdrawalWindow > 0, "Please specify withdrawal window");

s.setLendingPeriodsInternal(0, lendingPeriod, withdrawalWindow);
}

Expand Down
Loading