The following functions don't follow CEI patterns. It's better to follow CEI (Checks, Effects, Interactions).
See the code and adjustment suggestions below
function deposit(uint256 amount) public {
if (loveToken.balanceOf(address(stakingVault)) == 0)
revert Staking__NoMoreRewards();
// No require needed because of overflow protection
userStakes[msg.sender] += amount;
+ emit Deposited(msg.sender, amount);
loveToken.transferFrom(msg.sender, address(this), amount);
- emit Deposited(msg.sender, amount);
}
function withdraw(uint256 amount) public {
// No require needed because of overflow protection
userStakes[msg.sender] -= amount;
+ emit Withdrew(msg.sender, amount);
+ loveToken.transfer(msg.sender, amount);
- emit Withdrew(msg.sender, amount);
}
function claimRewards() public {
uint256 soulmateId = soulmateContract.ownerToId(msg.sender);
// first claim
if (lastClaim[msg.sender] == 0) {
lastClaim[msg.sender] = soulmateContract.idToCreationTimestamp(
soulmateId
);
}
// How many weeks passed since the last claim.
// Thanks to round-down division, it will be the lower amount possible until a week has completly pass.
uint256 timeInWeeksSinceLastClaim = ((block.timestamp -
lastClaim[msg.sender]) / 1 weeks);
if (timeInWeeksSinceLastClaim < 1)
revert Staking__StakingPeriodTooShort();
lastClaim[msg.sender] = block.timestamp;
// Send the same amount of LoveToken as the week waited times the number of token staked
uint256 amountToClaim = userStakes[msg.sender] *
timeInWeeksSinceLastClaim;
+ emit RewardsClaimed(msg.sender, amountToClaim);
loveToken.transferFrom(
address(stakingVault),
msg.sender,
amountToClaim
);
- emit RewardsClaimed(msg.sender, amountToClaim);
}
function initVault(ILoveToken loveToken, address managerContract) public {
if (vaultInitialize) revert Vault__AlreadyInitialized();
+ vaultInitialize = true;
loveToken.initVault(managerContract);
- vaultInitialize = true;
}