// create a new mapping, which will check if the _receiver has already locked funds.
mapping(address => bool) public depositer;
function deposit(uint256 _amount, uint256 _duration, address _receiver) external override {
if (_amount == 0) {
revert ZeroAmountError();
}
// checks if the _receiver has already locked tokens
require (depositer[_receiver] == false, "Already locked funds");
// Don't allow locking > maxLockDuration
uint256 duration = _duration.min(maxLockDuration);
// Enforce min lockup duration to prevent flash loan or MEV transaction ordering
duration = duration.max(MIN_LOCK_DURATION);
depositToken.safeTransferFrom(_msgSender(), address(this), _amount);
uint256 mintAmount = _amount * getMultiplier(duration) / 1e18;
depositsOf[_receiver].push(Deposit({
amount: _amount,
shareAmount: mintAmount,
start: uint64(block.timestamp),
end: uint64(block.timestamp) + uint64(duration)
}));
_mint(_receiver, mintAmount);
// makes _receiver's bool as true, so he can't call the function deposit() again till he withdraws the tokens.
depositer[_receiver] == true;
emit Deposited(_amount, duration, _receiver, _msgSender());
}
function withdraw(uint256 _depositId, address _receiver) external {
if (_depositId >= depositsOf[_msgSender()].length) {
revert NonExistingDepositError();
}
Deposit memory userDeposit = depositsOf[_msgSender()][_depositId];
if (block.timestamp < userDeposit.end) {
revert TooSoonError();
}
// remove Deposit
depositsOf[_msgSender()][_depositId] = depositsOf[_msgSender()][depositsOf[_msgSender()].length - 1];
depositsOf[_msgSender()].pop();
// burn pool shares
_burn(_msgSender(), userDeposit.shareAmount);
// return tokens
depositToken.safeTransfer(_receiver, userDeposit.amount);
// makes _receiver's bool as false, so he can call the function deposit() and lock his funds again.
depositer[_receiver] == false
emit Withdrawn(_depositId, _receiver, _msgSender(), userDeposit.amount);
}
-
-
Save CodingNameKiki/e28ccf6a48df4fcf4f8231a6468e52e1 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment