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

Ape Staking Review #396

Closed
wants to merge 14 commits into from
Closed
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
36 changes: 19 additions & 17 deletions contracts/apestaking/AutoCompoundApe.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ contract AutoCompoundApe is
/// @inheritdoc IAutoCompoundApe
function deposit(address onBehalf, uint256 amount) external override {
require(amount > 0, "zero amount");
uint256 amountShare = getShareByPooledApe(amount);

uint256 rewardAmount = _getRewardApeBalance();
uint256 amountShare = _getShareByPooledApe(amount, rewardAmount);
if (amountShare == 0) {
amountShare = amount;
// permanently lock the first MINIMUM_LIQUIDITY tokens to prevent getPooledApeByShares return 0
Expand All @@ -64,7 +66,7 @@ contract AutoCompoundApe is
_mint(onBehalf, amountShare);

_transferTokenIn(msg.sender, amount);
_harvest();
_harvest(rewardAmount);
_compound();

emit Transfer(address(0), onBehalf, amount);
Expand All @@ -75,10 +77,11 @@ contract AutoCompoundApe is
function withdraw(uint256 amount) external override {
require(amount > 0, "zero amount");

uint256 amountShare = getShareByPooledApe(amount);
uint256 rewardAmount = _getRewardApeBalance();
uint256 amountShare = _getShareByPooledApe(amount, rewardAmount);
_burn(msg.sender, amountShare);

_harvest();
_harvest(rewardAmount);
uint256 _bufferBalance = bufferBalance;
if (amount > _bufferBalance) {
_withdrawFromApeCoinStaking(amount - _bufferBalance);
Expand All @@ -93,21 +96,25 @@ contract AutoCompoundApe is

/// @inheritdoc IAutoCompoundApe
function harvestAndCompound() external {
_harvest();
_harvest(_getRewardApeBalance());
_compound();
}

function _getTotalPooledApeBalance()
internal
view
override
returns (uint256)
{
function _getRewardApeBalance() internal view override returns (uint256) {
uint256 rewardAmount = apeStaking.pendingRewards(
APE_COIN_POOL_ID,
address(this),
0
);
return rewardAmount;
}

function _getTotalPooledApeBalance(uint256 rewardAmount)
internal
view
override
returns (uint256)
{
return stakingBalance + rewardAmount + bufferBalance;
}

Expand Down Expand Up @@ -139,12 +146,7 @@ contract AutoCompoundApe is
}
}

function _harvest() internal {
uint256 rewardAmount = apeStaking.pendingRewards(
APE_COIN_POOL_ID,
address(this),
0
);
function _harvest(uint256 rewardAmount) internal {
if (rewardAmount > 0) {
uint256 balanceBefore = apeCoin.balanceOf(address(this));
apeStaking.claimSelfApeCoin();
Expand Down
7 changes: 4 additions & 3 deletions contracts/apestaking/AutoYieldApe.sol
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,10 @@ contract AutoYieldApe is
address recipient,
uint256 amount
) internal override {
require(sender != recipient, Errors.SENDER_SAME_AS_RECEIVER);
_updateYieldIndex(sender, -(amount.toInt256()));
_updateYieldIndex(recipient, amount.toInt256());
if (sender != recipient) {
_updateYieldIndex(sender, -(amount.toInt256()));
_updateYieldIndex(recipient, amount.toInt256());
}
super._transfer(sender, recipient, amount);
}
}
Loading