Skip to content

Commit

Permalink
Merge pull request #10 from gosuto-inzasheru/issue/9
Browse files Browse the repository at this point in the history
feat: prevent denominators being zero
  • Loading branch information
gosuto-inzasheru authored Jun 26, 2024
2 parents 54cb2fc + 2e7e627 commit 13b1a94
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ __pycache__
build/
reports/
venv/
.vscode/
node_modules/
artifacts/
cache/
Expand Down
16 changes: 16 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"plugins": ["prettier-plugin-solidity"],
"overrides": [
{
"files": "*.sol",
"options": {
"parser": "solidity-parse",
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false
}
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"solidity.formatter": "prettier"
}
29 changes: 22 additions & 7 deletions contracts/BadgerVotingShare.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ contract BadgerVotingShare {
function _sushiswapBalanceOf(
address _voter
) internal view returns (uint256) {
uint256 total = badger_wBTC_SLP.totalSupply();
if (total == 0) {
return 0;
}
uint256 bSLPPricePerShare = sett_badger_wBTC_SLP.getPricePerFullShare();
(, uint112 reserve1, ) = badger_wBTC_SLP.getReserves();
uint256 totalSLPBalance = badger_wBTC_SLP.balanceOf(_voter) +
Expand All @@ -171,7 +175,7 @@ contract BadgerVotingShare {
(geyser_badger_wBTC_SLP.totalStakedFor(_voter) *
bSLPPricePerShare) /
1e18;
return (totalSLPBalance * reserve1) / badger_wBTC_SLP.totalSupply();
return (totalSLPBalance * reserve1) / total;
}

/*
Expand All @@ -182,15 +186,17 @@ contract BadgerVotingShare {
After adding the 2 balances we calculate how much BADGER it corresponds to using the pool's reserves.
*/
function _curveBalanceOf(address _voter) internal view returns (uint256) {
uint256 total = badger_wBTC_crv_token.totalSupply();
if (total == 0) {
return 0;
}
// coin 0 is BADGER
uint256 bCrvPricePerShare = sett_badger_wBTC_crv.getPricePerFullShare();
uint256 poolBadgerBalance = badger_wBTC_crv_pool.balances(0);
uint256 voterLpBalance = badger_wBTC_crv_token.balanceOf(_voter) +
(sett_badger_wBTC_crv.balanceOf(_voter) * bCrvPricePerShare) /
1e18;
return
(voterLpBalance * poolBadgerBalance) /
badger_wBTC_crv_token.totalSupply();
return (voterLpBalance * poolBadgerBalance) / total;
}

/*
Expand Down Expand Up @@ -233,11 +239,14 @@ contract BadgerVotingShare {
The voter may have deposited BADGER into the across pool:
*/
function _acrossBalanceOf(address _voter) internal view returns (uint256) {
uint256 total = aBADGER.totalSupply();
if (total == 0) {
return 0;
}
int256 numerator = int256(aBADGER.liquidReserves()) +
int256(aBADGER.utilizedReserves()) -
int256(aBADGER.undistributedLpFees());
uint256 exchangeRateCurrent = (uint256(numerator) * 1e18) /
aBADGER.totalSupply();
uint256 exchangeRateCurrent = (uint256(numerator) * 1e18) / total;

return (exchangeRateCurrent * aBADGER.balanceOf(_voter)) / 1e18;
}
Expand All @@ -257,14 +266,17 @@ contract BadgerVotingShare {
(IERC20[] memory tokens, uint256[] memory balances, ) = balancer_vault
.getPoolTokens(poolId);
uint256 poolBadgerAmount;
for (uint i = 0; i < tokens.length; i++) {
for (uint256 i = 0; i < tokens.length; i++) {
if (tokens[i] == badger) {
poolBadgerAmount = balances[i];
break;
}
}

uint256 bptTotalSupply = badger_wBTC_balancer.totalSupply();
if (bptTotalSupply == 0) {
return 0;
}
uint256 voterBalance = badger_wBTC_balancer.balanceOf(_voter);
uint256 voterVaultBalance = sett_badger_wBTC_balancer.balanceOf(_voter);
uint256 voterStakedBalance = bptStakedBadgerWbtc.balanceOf(_voter);
Expand Down Expand Up @@ -306,6 +318,9 @@ contract BadgerVotingShare {
}

uint256 bptTotalSupply = badgerRethBalancer.totalSupply();
if (bptTotalSupply == 0) {
return 0;
}
uint256 voterBalance = badgerRethBalancer.balanceOf(_voter);

uint256 bptVotes = (voterBalance * poolBadgerAmount) / bptTotalSupply;
Expand Down

0 comments on commit 13b1a94

Please sign in to comment.