Having trouble understanding how to correctly implement Approve and transferFrom. Some guidance would be much appreciated, thank you for your time. Below is my Deposit function:
/ Users can deposit DAI to the contract
function deposit(uint deposit_amount) public {
bool withdrawalLock = false;
// Access the contract's state before making the external call
require(withdrawalAllowed, "Withdrawals are not currently allowed");
require(deposit_amount >= MIN_DEPOSIT && deposit_amount <= MAX_DEPOSIT, "Invalid
deposit amount");
// Use a reentrancy lock to prevent untrusted callees from re-entering the contract
require(!withdrawalLock, "Reentrancy detected");
withdrawalLock = true;
// Approve DAI token
IERC20(daitoken).approve(address(this), deposit_amount);
// Transfer the DAI to the contract
IERC20(daitoken).transferFrom(msg.sender, address(this), deposit_amount);
// Update the user's balance in the mapping
userBalances[msg.sender] = userBalances[msg.sender].add(deposit_amount);
emit Deposit(msg.sender, deposit_amount);
// Unlock the contract to allow future withdrawals
withdrawalLock = false;
}
This selfdestruct function does work if the some DAI is sent to the contract address so i am pretty sure the IERC20 interface is working correctly. However in this case the user should use the Deposit function so they can then call Refund on their own.
// Contract is owned by onlyOwner
contract mortal is owned {
// Only owner can shutdown this contract.
function destroy() public onlyOwner {
daitoken.transfer(owner, daitoken.balanceOf(address(this)));
selfdestruct(payable(msg.sender));
}
}