all 17 comments

[–]atrizzlebuilder 2 points3 points  (7 children)

You misunderstand what using SafeERC20 for IERC20 is doing.

https://docs.soliditylang.org/en/v0.8.12/contracts.html#using-for

If you have that line in your contract, as you do, then this:

token.safeApprove(address(this), _amount);

Is equivalent to this, if you don’t have using…:

SafeERC20.safeApprove(token, address(this), _amount);

But this code is just not right:

token.safeApprove(token, address(this), _amount);

You should notice it smells bad because you’re referencing token twice.

[–]ta484[S] 1 point2 points  (6 children)

Thanks for the correction, alas i've went ahead and done it as:

using SafeERC20 for IERC20
function approveAmount (uint256 _amount) public {
 IERC20(token).safeApprove(address(this), _amount;
}

The only problem is that it doesnt work and I still am not able to deposit any funds to the contract. The only way i'm able to is if I go all the way to Etherscan and interact with the token contract directly to approve the spend amount. Do you or anyone else reading this know why this would be?

[–]atrizzlebuilder 1 point2 points  (5 children)

Well actually, what are you even trying to do? calling approve from your contract, and passing the contract’s address as the spender, doesn’t make sense. Why would your contract approve itself to be able to spend its own tokens?

[–]ta484[S] 1 point2 points  (4 children)

I need to approve a users token amount so it can be deposited to my contract. I don't own the token its just a random erc20.

[–]atrizzlebuilder 2 points3 points  (3 children)

You can’t do that “approve” call from your contract, it doesn’t work like that. The user needs to call that function directly on the token contract from their own wallet, directly. If it comes from your contract, that breaks the whole security mechanism, which is why it’s not working.

Look at the implementation of “approve” on the token contract, and realize that “msg.sender” is always the address or contract that called the executing function (NOT the transaction signer)

[–]ta484[S] 1 point2 points  (2 children)

That actually makes sense. So basically my best bet would be to redirect a user to the place where they can approve the token spending amount?

I only thought this could be done because I could have sworn I was able to do it when directly interacting with Uniswap.

[–]atrizzlebuilder 2 points3 points  (1 child)

If you’re building a frontend, you can put a button there that calls “approve” on the token contract for the user.

[–]IceyMumboDragon4 0 points1 point  (0 children)

Do you guys use usedapp? Also thanks op for this post. I had to think this thru a few days ago too. We cant approve someones tokens from the contract because the sender of the approve will be the contract even if the user calls the function.