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

Implement and test methods to alter an existing deposit #18

Merged
merged 3 commits into from
Jan 24, 2024
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
56 changes: 55 additions & 1 deletion src/UniStaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract UniStaker is ReentrancyGuard {
error UniStaker__Unauthorized(bytes32 reason, address caller);
error UniStaker__InvalidRewardRate();
error UniStaker__InsufficientRewardBalance();
error UniStaker__InvalidAddress();

struct Deposit {
uint256 balance;
Expand Down Expand Up @@ -86,9 +87,51 @@ contract UniStaker is ReentrancyGuard {
_depositId = _stake(_amount, _delegatee, _beneficiary);
}

function stakeMore(DepositIdentifier _depositId, uint256 _amount) external nonReentrant {
apbendi marked this conversation as resolved.
Show resolved Hide resolved
Deposit storage deposit = deposits[_depositId];
_revertIfNotDepositOwner(deposit);

_updateReward(deposit.beneficiary);
apbendi marked this conversation as resolved.
Show resolved Hide resolved

DelegationSurrogate _surrogate = surrogates[deposit.delegatee];
_stakeTokenSafeTransferFrom(msg.sender, address(_surrogate), _amount);

totalSupply += _amount;
totalDeposits[msg.sender] += _amount;
earningPower[deposit.beneficiary] += _amount;
deposit.balance += _amount;
}

function alterDelegatee(DepositIdentifier _depositId, address _newDelegatee) public nonReentrant {
_revertIfAddressZero(_newDelegatee);
Deposit storage deposit = deposits[_depositId];
_revertIfNotDepositOwner(deposit);

DelegationSurrogate _oldSurrogate = surrogates[deposit.delegatee];
deposit.delegatee = _newDelegatee;
DelegationSurrogate _newSurrogate = _fetchOrDeploySurrogate(_newDelegatee);
_stakeTokenSafeTransferFrom(address(_oldSurrogate), address(_newSurrogate), deposit.balance);
}

function alterBeneficiary(DepositIdentifier _depositId, address _newBeneficiary)
apbendi marked this conversation as resolved.
Show resolved Hide resolved
public
nonReentrant
{
_revertIfAddressZero(_newBeneficiary);
Deposit storage deposit = deposits[_depositId];
_revertIfNotDepositOwner(deposit);

_updateReward(deposit.beneficiary);
earningPower[deposit.beneficiary] -= deposit.balance;

_updateReward(_newBeneficiary);
deposit.beneficiary = _newBeneficiary;
earningPower[_newBeneficiary] += deposit.balance;
}

function withdraw(DepositIdentifier _depositId, uint256 _amount) external nonReentrant {
Deposit storage deposit = deposits[_depositId];
if (msg.sender != deposit.owner) revert UniStaker__Unauthorized("not owner", msg.sender);
_revertIfNotDepositOwner(deposit);

_updateReward(deposit.beneficiary);

Expand Down Expand Up @@ -148,6 +191,9 @@ contract UniStaker is ReentrancyGuard {
internal
returns (DepositIdentifier _depositId)
{
_revertIfAddressZero(_delegatee);
_revertIfAddressZero(_beneficiary);

_updateReward(_beneficiary);

DelegationSurrogate _surrogate = _fetchOrDeploySurrogate(_delegatee);
Expand Down Expand Up @@ -176,4 +222,12 @@ contract UniStaker is ReentrancyGuard {
rewards[_beneficiary] = earned(_beneficiary);
userRewardPerTokenPaid[_beneficiary] = rewardPerTokenStored;
}

function _revertIfNotDepositOwner(Deposit storage deposit) internal view {
if (msg.sender != deposit.owner) revert UniStaker__Unauthorized("not owner", msg.sender);
}

function _revertIfAddressZero(address _account) internal pure {
if (_account == address(0)) revert UniStaker__InvalidAddress();
}
}
Loading
Loading