Skip to content

Commit

Permalink
fix FarmLensV2 (#122)
Browse files Browse the repository at this point in the history
* fix FarmLensV2

* rename function
  • Loading branch information
0x0Louis authored Nov 3, 2023
1 parent 405de79 commit bdcdb6e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
26 changes: 18 additions & 8 deletions contracts/traderjoe/FarmLensV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ contract FarmLensV2 {
allFarmData.joePriceUsd = joePrice;

allFarmData.totalAllocChefV2 = chefv2.totalAllocPoint();
allFarmData.joePerSecChefV2 = chefv2.joePerSec();
allFarmData.joePerSecChefV2 = _joePerSec(address(chefv2));

allFarmData.totalAllocChefV3 = chefv3.totalAllocPoint();
allFarmData.joePerSecChefV3 = chefv3.joePerSec();
allFarmData.joePerSecChefV3 = _joePerSec(address(chefv3));

allFarmData.totalAllocBMCJ = bmcj.totalAllocPoint();
allFarmData.joePerSecBMCJ = bmcj.joePerSec();
allFarmData.joePerSecBMCJ = _joePerSec(address(bmcj));

allFarmData.farmInfosV2 = _getMCFarmInfos(chefv2, avaxPrice, whitelistedPidsV2);
allFarmData.farmInfosV3 = _getMCFarmInfos(chefv3, avaxPrice, whitelistedPidsV3);
Expand All @@ -262,6 +262,16 @@ contract FarmLensV2 {
return allFarmData;
}

/// @notice Returns the joePerSec of a given MasterChef
/// @param mc The address of the MasterChef
function _joePerSec(address mc) internal view returns (uint256) {
try IMasterChef(mc).joePerSec() returns (uint256 joePerSec) {
return joePerSec;
} catch {
return 0;
}
}

/// @notice Returns the price of avax in Usd internally
/// @return uint256 the avax price, scaled to 18 decimals
function _getAvaxPrice() private view returns (uint256) {
Expand All @@ -281,9 +291,9 @@ contract FarmLensV2 {
uint256 decimals1 = IERC20(pair.token1()).safeDecimals();

if (derivedtoken0) {
return _scaleTo(reserve0, decimals1.add(18).sub(decimals0)).div(reserve1);
return reserve1 == 0 ? 0 : _scaleTo(reserve0, decimals1.add(18).sub(decimals0)).div(reserve1);
} else {
return _scaleTo(reserve1, decimals0.add(18).sub(decimals1)).div(reserve0);
return reserve0 == 0 ? 0 : _scaleTo(reserve1, decimals0.add(18).sub(decimals1)).div(reserve0);
}
}

Expand Down Expand Up @@ -359,7 +369,7 @@ contract FarmLensV2 {
FarmInfo[] memory farmInfos = new FarmInfo[](whitelistLength);

uint256 chefTotalAlloc = chef.totalAllocPoint();
uint256 chefJoePerSec = chef.joePerSec();
uint256 chefJoePerSec = _joePerSec(address(chef));

for (uint256 i; i < whitelistLength; i++) {
uint256 pid = whitelistedPids[i];
Expand Down Expand Up @@ -432,7 +442,7 @@ contract FarmLensV2 {
address user,
uint256[] calldata whitelistedPids
) private view returns (FarmInfoBMCJ[] memory) {
GlobalInfo memory globalInfo = GlobalInfo(address(bmcj), bmcj.totalAllocPoint(), bmcj.joePerSec());
GlobalInfo memory globalInfo = GlobalInfo(address(bmcj), bmcj.totalAllocPoint(), _joePerSec(address(bmcj)));

uint256 whitelistLength = whitelistedPids.length;
FarmInfoBMCJ[] memory farmInfos = new FarmInfoBMCJ[](whitelistLength);
Expand Down Expand Up @@ -518,7 +528,7 @@ contract FarmLensV2 {
poolReserveUsd /
BP_PRECISION;

if (user.amount != 0 && user.factor != 0) {
if (user.amount != 0 && user.factor != 0 && pool.totalFactor != 0) {
uint256 userLpUsd = user.amount.mul(farmInfo.reserveUsd) / pool.totalLpSupply;

farmInfo.userBoostedApr =
Expand Down
34 changes: 34 additions & 0 deletions test/foundry/FarmLensV2.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma experimental ABIEncoderV2;
pragma solidity 0.6.12;

import "forge-std/Test.sol";

import "../../contracts/traderjoe/FarmLensV2.sol";

contract testFarmLensV2 is Test {
FarmLensV2 constant lens = FarmLensV2(0xF16d25Eba0D8E51cEAF480141bAf577aE55bfdd2);

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

function test_GetAllFarmData() public {
vm.expectRevert("SafeMath: division by zero");
lens.getAllFarmData(new uint256[](1), new uint256[](1), new uint256[](1), address(this));

FarmLensV2 newLens = new FarmLensV2(
lens.joe(),
lens.wavax(),
lens.wavaxUsdte(),
lens.wavaxUsdce(),
lens.wavaxUsdc(),
lens.joeFactory(),
lens.chefv2(),
lens.chefv3(),
lens.bmcj()
);

newLens.getAllFarmData(new uint256[](1), new uint256[](1), new uint256[](1), address(this));
}
}

0 comments on commit bdcdb6e

Please sign in to comment.