Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
- Added a check during bond creation to revert if the contract's balance is not sufficient to honor the bond claim in the future
- Fixed the bug of `GovernanceUtilV1.getReportingUnstakenAmount`
- Refactored `getCoverFeeInfoInternal` to throw if zero value is passed as cover duration
  • Loading branch information
heyaibi committed Mar 8, 2022
1 parent 82c437d commit c049a7b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
21 changes: 18 additions & 3 deletions contracts/libraries/BondPoolLibV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,16 @@ library BondPoolLibV1 {

require(minNpmDesired > 0, "Invalid value: `minNpmDesired`");
require(values[0] >= minNpmDesired, "Min bond `minNpmDesired` failed");
require(_getNpmBalance(s) >= values[0] + _getCommitment(s), "NPM balance insufficient to bond");

// @suppress-malicious-erc20 `bondLpToken` can't be manipulated via user input.
// Pull the tokens from the requester's account
IERC20(s.getAddressByKey(BondPoolLibV1.NS_BOND_LP_TOKEN)).ensureTransferFrom(msg.sender, s.getAddressByKey(BondPoolLibV1.NS_LQ_TREASURY), lpTokens);

// To claim later
// Commitment: Total NPM to reserve for bond claims
s.addUintByKey(BondPoolLibV1.NS_BOND_TO_CLAIM, values[0]);

// Your bond to claim later
bytes32 k = keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_TO_CLAIM, msg.sender));
s.addUintByKey(k, values[0]);

Expand All @@ -145,20 +149,31 @@ library BondPoolLibV1 {
s.setUintByKey(k, values[1]);
}

function _getNpmBalance(IStore s) private view returns (uint256) {
return IERC20(s.npmToken()).balanceOf(address(this));
}

function _getCommitment(IStore s) private view returns (uint256) {
return s.getUintByKey(BondPoolLibV1.NS_BOND_TO_CLAIM);
}

function claimBondInternal(IStore s) external returns (uint256[] memory values) {
s.mustNotBePaused();

values = new uint256[](1);

values[0] = _getYourBondClaimable(s, msg.sender); // npmToTransfer

// Commitment: Reduce NPM reserved for claims
s.subtractUintByKey(BondPoolLibV1.NS_BOND_TO_CLAIM, values[0]);

// Clear the claim amount
s.setUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_TO_CLAIM, msg.sender)), 0);
s.deleteUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_TO_CLAIM, msg.sender)));

uint256 unlocksOn = _getYourBondUnlockDate(s, msg.sender);

// Clear the unlock date
s.setUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_UNLOCK_DATE, msg.sender)), 0);
s.deleteUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_UNLOCK_DATE, msg.sender)));

require(block.timestamp >= unlocksOn, "Still vesting"); // solhint-disable-line
require(values[0] > 0, "Nothing to claim");
Expand Down
1 change: 1 addition & 0 deletions contracts/libraries/GovernanceUtilV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ library GovernanceUtilV1 {
uint256 incidentDate
) public view returns (uint256) {
bytes32 k = keccak256(abi.encodePacked(ProtoUtilV1.NS_GOVERNANCE_UNSTAKE_TS, key, incidentDate, account));
k = keccak256(abi.encodePacked(ProtoUtilV1.NS_GOVERNANCE_UNSTAKEN, key, incidentDate, account));
return s.getUintByKey(k);
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/libraries/PolicyHelperV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ library PolicyHelperV1 {
(floor, ceiling) = getPolicyRatesInternal(s, key);
uint256[] memory values = s.getCoverPoolSummaryInternal(key);

require(coverDuration <= 3, "Invalid duration");
require(coverDuration > 0 && coverDuration <= 3, "Invalid duration");
require(floor > 0 && ceiling > floor, "Policy rate config error");

// AMOUNT_IN_COVER_POOL - COVER_COMMITMENT > AMOUNT_TO_COVER
Expand Down
1 change: 0 additions & 1 deletion contracts/pool/Bond/BondPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ contract BondPool is BondPoolBase {

function createBond(uint256 lpTokens, uint256 minNpmDesired) external override nonReentrant {
// @suppress-acl Marking this as publicly accessible

s.mustNotBePaused();

uint256[] memory values = s.createBondInternal(lpTokens, minNpmDesired);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
]
},
"author": "",
"license": "ISC",
"license": "SEE LICENSE IN <LICENSE>",
"bugs": {
"url": "https://github.com/neptune-mutual/protocol/issues"
},
Expand Down
2 changes: 1 addition & 1 deletion util/composer/initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const initialize = async (suite, deploymentId) => {

await intermediate(cache, npm, 'approve', bondPoolContract.address, helper.ether(2_000))
let addresses = [npmUsdPair.address, sample.fake.TREASURY]
let values = [helper.percentage(0.75), helper.ether(10_000), bondPeriod, helper.ether(2_000)]
let values = [helper.percentage(0.75), helper.ether(10_000), bondPeriod, helper.ether(2_000_000)]

await intermediate(cache, bondPoolContract, 'setup', addresses, values)

Expand Down

0 comments on commit c049a7b

Please sign in to comment.