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

Fix BMCJ div by zero error #121

Merged
merged 2 commits into from
Nov 3, 2023
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
20 changes: 16 additions & 4 deletions contracts/BoostedMasterChefJoe.sol
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,15 @@ contract BoostedMasterChefJoe is Initializable, OwnableUpgradeable, ReentrancyGu
/// @notice Calculates and returns the `amount` of JOE per second
/// @return amount The amount of JOE emitted per second
function joePerSec() public view returns (uint256 amount) {
uint256 mcv2TotalAllocPoint = MASTER_CHEF_V2.totalAllocPoint();
if (mcv2TotalAllocPoint == 0) return 0;

uint256 total = 1000;
uint256 lpPercent = total.sub(MASTER_CHEF_V2.devPercent()).sub(MASTER_CHEF_V2.treasuryPercent()).sub(
MASTER_CHEF_V2.investorPercent()
);
uint256 lpShare = MASTER_CHEF_V2.joePerSec().mul(lpPercent).div(total);
amount = lpShare.mul(MASTER_CHEF_V2.poolInfo(MASTER_PID).allocPoint).div(MASTER_CHEF_V2.totalAllocPoint());
amount = lpShare.mul(MASTER_CHEF_V2.poolInfo(MASTER_PID).allocPoint).div(mcv2TotalAllocPoint);
}

/// @notice View function to see pending JOE on frontend
Expand All @@ -396,7 +399,12 @@ contract BoostedMasterChefJoe is Initializable, OwnableUpgradeable, ReentrancyGu

if (block.timestamp > pool.lastRewardTimestamp && pool.totalLpSupply != 0 && pool.allocPoint != 0) {
uint256 secondsElapsed = block.timestamp - pool.lastRewardTimestamp;
uint256 joeReward = secondsElapsed.mul(joePerSec()).mul(pool.allocPoint).div(totalAllocPoint);

uint256 totalAllocPoint_ = totalAllocPoint;
uint256 joeReward = totalAllocPoint_ > 0
? secondsElapsed.mul(joePerSec()).mul(pool.allocPoint).div(totalAllocPoint_)
: 0;

accJoePerShare = accJoePerShare.add(
joeReward.mul(ACC_TOKEN_PRECISION).mul(10_000 - pool.veJoeShareBp).div(pool.totalLpSupply.mul(10_000))
);
Expand Down Expand Up @@ -449,7 +457,11 @@ contract BoostedMasterChefJoe is Initializable, OwnableUpgradeable, ReentrancyGu
uint256 veJoeShareBp = pool.veJoeShareBp;
uint256 totalFactor = pool.totalFactor;

uint256 joeReward = secondsElapsed.mul(joePerSec()).mul(allocPoint).div(totalAllocPoint);
uint256 totalAllocPoint_ = totalAllocPoint;
uint256 joeReward = totalAllocPoint_ > 0
? secondsElapsed.mul(joePerSec()).mul(allocPoint).div(totalAllocPoint_)
: 0;

pool.accJoePerShare = pool.accJoePerShare.add(
joeReward.mul(ACC_TOKEN_PRECISION).mul(10_000 - veJoeShareBp).div(lpSupply.mul(10_000))
);
Expand All @@ -473,7 +485,7 @@ contract BoostedMasterChefJoe is Initializable, OwnableUpgradeable, ReentrancyGu

/// @notice Harvests JOE from `MASTER_CHEF_V2` MCJV2 and pool `MASTER_PID` to this BMCJ contract
function harvestFromMasterChef() public {
MASTER_CHEF_V2.deposit(MASTER_PID, 0);
// MASTER_CHEF_V2.deposit(MASTER_PID, 0);
}

/// @notice Return an user's factor
Expand Down
41 changes: 41 additions & 0 deletions test/foundry/UpgradeBMCJ.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma experimental ABIEncoderV2;
pragma solidity 0.6.12;

import "forge-std/Test.sol";

import "@openzeppelin/contracts/proxy/ProxyAdmin.sol";
import "@openzeppelin/contracts/proxy/TransparentUpgradeableProxy.sol";

import "../../contracts/BoostedMasterChefJoe.sol";

contract TestUpgradeBMCJ is Test {
ProxyAdmin proxyAdmin = ProxyAdmin(0x246ABeC8f8a542E892934232DB3Fd97A61E3193c);
BoostedMasterChefJoe proxy = BoostedMasterChefJoe(0x4483f0b6e2F5486D06958C20f8C39A7aBe87bf8F);

address ms = 0x2fbB61a10B96254900C03F1644E9e1d2f5E76DD2;

function setUp() public {
vm.createSelectFork(vm.rpcUrl("avalanche"), 37289196);
}

function test_Upgrade() public {
uint256 previousTotalAllocPoint = proxy.totalAllocPoint();

vm.expectRevert("SafeMath: division by zero");
proxy.massUpdatePools();

vm.expectRevert("SafeMath: division by zero");
proxy.joePerSec();

address newImpl = address(new BoostedMasterChefJoe());

vm.prank(ms);
proxyAdmin.upgrade(TransparentUpgradeableProxy(payable(address(proxy))), newImpl);

assertEq(proxy.joePerSec(), 0);
assertEq(proxy.totalAllocPoint(), previousTotalAllocPoint);

proxy.massUpdatePools();
}
}