Skip to content

Commit

Permalink
feat: spend from stake
Browse files Browse the repository at this point in the history
  • Loading branch information
the-emerald committed Jan 4, 2025
1 parent 22af480 commit a03a8cb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/WanderStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract WanderStaking is Initializable, PausableUpgradeable, OwnableUpgradeable

event Stake(address indexed user, uint256 amount);
event Unstake(address indexed user, uint256 amount);
event SpendFromStake(address indexed user, address indexed to, uint256 amount);

error ZeroAmount();
error InsufficientBalance();
Expand Down Expand Up @@ -74,6 +75,24 @@ contract WanderStaking is Initializable, PausableUpgradeable, OwnableUpgradeable
token.safeTransfer(msg.sender, amount);
}

function spendFromStake(address to, uint256 amount) external whenNotPaused {
if (amount == 0) {
revert ZeroAmount();
}

if (userStake[msg.sender] < amount) {
revert InsufficientBalance();
}

userStake[msg.sender] -= amount;
totalStaked -= amount;

emit Unstake(msg.sender, amount);
emit SpendFromStake(msg.sender, to, amount);

token.safeTransfer(to, amount);
}

function getTotalStaked() external view returns (uint256) {
return totalStaked;
}
Expand Down
37 changes: 37 additions & 0 deletions test/WanderStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,41 @@ contract WanderStakingTest is Test {
vm.expectRevert();
staking.unstake(unstakeAmount);
}

function test_spend(uint64 _amount, uint64 _spendAmount) public {
// hello nick
address to = 0x6F4E4664E9B519DEAB043676D9Aafe6c9621C088;

vm.assume(_amount > 0);
vm.assume(_spendAmount > 0);
vm.assume(_amount >= _spendAmount);
uint256 amount = uint256(_amount) * (10 ** 18);
uint256 spendAmount = uint256(_spendAmount) * (10 ** 18);

token.mint(address(this), amount);
staking.stake(amount);

staking.spendFromStake(to, spendAmount);

assert(token.balanceOf(to) == spendAmount);
assert(staking.getAmountStaked(address(this)) == amount - spendAmount);
assert(token.balanceOf(address(staking)) == amount - spendAmount);
}

function test_spend_over(uint64 _amount, uint64 _spendAmount) public {
// hello nick
address to = 0x6F4E4664E9B519DEAB043676D9Aafe6c9621C088;

vm.assume(_amount > 0);
vm.assume(_spendAmount > 0);
vm.assume(_amount < _spendAmount);
uint256 amount = uint256(_amount) * (10 ** 18);
uint256 spendAmount = uint256(_spendAmount) * (10 ** 18);

token.mint(address(this), amount);
staking.stake(amount);

vm.expectRevert(WanderStaking.InsufficientBalance.selector);
staking.spendFromStake(to, spendAmount);
}
}

0 comments on commit a03a8cb

Please sign in to comment.