Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Aug 19, 2024
1 parent f68572a commit 1cde064
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/BaseStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ abstract contract BaseStaking is OwnableUpgradeable, ERC20VotesUpgradeable {
function convertToShares(
uint256 assets
) public view virtual returns (uint256) {
console.log("totoal supply", totalSupply());
console.log("total assets", _totalAssets());
console.log("assets", assets);
uint256 supply = totalSupply(); // Saves an extra SLOAD if totalSupply is non-zero.

return supply == 0 ? assets : assets.mulDivDown(supply, _totalAssets());
Expand Down Expand Up @@ -241,6 +238,10 @@ abstract contract BaseStaking is OwnableUpgradeable, ERC20VotesUpgradeable {
) internal returns (uint256 shares) {
shares = _previewWithdraw(amount);

console.log("balance ", balanceOf(user));
console.log("needs ", shares);
console.log("to withdraw", amount);

// Decrease the amount from the total locked
totalLocked[user] -= amount;

Expand Down
6 changes: 5 additions & 1 deletion src/DelegateStaking.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {console} from "@forge-std/console.sol";

import {EnumerableSetLib} from "@solady/utils/EnumerableSetLib.sol";
import {OwnableUpgradeable} from "@openzeppelin-upgradeable/contracts/access/OwnableUpgradeable.sol";
Expand Down Expand Up @@ -143,7 +144,7 @@ contract DelegateStaking is BaseStaking {
nextStakeId = 1;

// mint dead shares to avoid inflation attack
uint256 amount = 1000e18;
uint256 amount = 10_000e18;

// Calculate the amount of shares to mint
uint256 shares = convertToShares(amount);
Expand Down Expand Up @@ -285,9 +286,12 @@ contract DelegateStaking is BaseStaking {
require(shares > 0, UserHasNoShares());

uint256 assets = convertToAssets(shares);
console.log("user assets", assets);
uint256 locked = totalLocked[user];
console.log("locked", locked);

// need the first branch as convertToAssets rounds down
amount = locked >= assets ? 0 : assets - locked;
console.log("max withdrawable", amount);
}
}
3 changes: 2 additions & 1 deletion src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ contract Staking is BaseStaking {
nextStakeId = 1;

// mint dead shares to avoid inflation attack
uint256 amount = 1000e18;
uint256 amount = 10_000e18;

// Calculate the amount of shares to mint
uint256 shares = convertToShares(amount);

Expand Down
7 changes: 4 additions & 3 deletions test/DelegateStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,11 @@ contract Stake is DelegateStakingTest {
delegate.unstake(bobStakeId, bobAmount - 1e5);

// Alice earn less than bob
assertGt(
assertApproxEqRel(
govToken.balanceOf(bob),
aliceBalanceAfterAttack,
"Alice earn more than Bob after the attack"
bobAmount,
0.01e18,
"Bob must received the money back"
);
}

Expand Down
3 changes: 1 addition & 2 deletions test/Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1715,8 +1715,7 @@ contract ViewFunctions is StakingTest {

_jumpAhead(_jump);

// depositor 2 stakes and collect rewards from distirbutor
_mintGovToken(_depositoron2, _amount2);
_mintGovToken(_depositor2, _amount2);
_setKeyper(_depositor2, true);

_stake(_depositor2, _amount2);
Expand Down

0 comments on commit 1cde064

Please sign in to comment.