I am rewriting the solidty-by-example.org crowdfunding exercise.
IERC20 public immutable token;
function pledge(uint _id, uint _amount) external {
Campaign storage campaign = campaigns[_id];
require(block.timestamp >= campaign.startAt, "not started");
require(block.timestamp <= campaign.endAt, "ended");
campaign.pledged += _amount;
pledgedAmount[_id][msg.sender] += _amount;
token.transferFrom(msg.sender, address(this), _amount);
emit Pledge(_id, msg.sender, _amount);
}
is it necessary to execute ERC20 approve before (with the contract address as spender) calling transferFrom in the pledge function of the contract?
Is there any way to perform the approve within the pledge function itself?