I have a contract that holds all its tokens. Now I need to transfer tokens to an user that sends some BNB to its "receive" fallback.
So... in OpenZeppelin derived contracts we have no access to the balance array and the "transfer" function always use the sender (the actual user) as the one that will give tokens.
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
In the receive method I can't make the contract give tokens to the sender.
receive() external payable {
require( msg.value > 0 );
transfer( <MUST_BE_THE_USER>, tokens );
}
When "transfer()" calls "_transfer()" it will make the user giving tokens to itself. I need to call "_transfer(<THE_CONTRACT_ADDRESS>, <THE_USER_ADDRESS>, amount);" but this function cannot be accessed by my contract that extends O.Z.'s ERC20.
As alternative we have "transferFrom(address sender, address recipient, uint256 amount) " but it have all that "approve" stuff ....
How can I make transfers using OZ base contracts when the spender is not the msg.sender ?