0

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?

1 Answer 1

2

No, it is not possible to implement this inside the function. As you might already know that when we call functions of contract "A" via contract "B" then contract B becomes the msg.sender. So it is necessary for the token holder to approve directly by signing the transaction.

Now as I see you have used the tag of delegatecall as well, but delegatecall will also not work here, because it executes the function within the context of the caller contract, so the state variables of the caller contract will update and this will break the logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.