0

When we're asking a user to approve() the spending of a proprietary ERC20 Token for the first time (like say DAI, or SUSHI, etc.), the user has to enter the amount they're willing to approve - like say 100 tokens, or 100,000 tokens - or they can just tap on "MAX VALUE".

But how do we then pass that value along as an argument to the approve() method we're already calling?

It feels a bit like a chicken-egg type thing.

Here's what I mean:

// Here we call `approve()` to prompt the user to enter the AMOUNT of Tokens 
// they're willing to spend, except this call is also *already* requiring us to 
// pass-in some value for token-amount - *before* we know what the User entered:
CustomERC20Token.approve(contractAddress, 1000).then((approvalStatus) => {
         console.log(">Back from ERC20 'approve()'  --> 'approvalStatus' = ", approvalStatus);        
   }).catch(theError => {
         console.log("'approve()' - 'catch()' -->  Here's the error: ");
         console.log(theError);
});

As you can see, I hard-coded a value of 1000 Tokens - cause the function call is already looking for *some* value to be passed-in. But 1000 is not necessarily what the user entered...

So how do I get the value the user actually enters?

1 Answer 1

1

when you send the transaction you cannot override the argument again if you do that that will be another transaction. in your case you hardcoded the 1000 to be approved and in metamask user can enter any amount they want or press default value button which is your hardhcoded value(1000). for knowing the allowed amount you can call the allowance function on the ERC20 contract and watch for any changes happen, if you want require user to have sepcefic amount approved in order to execute some function you should check the allowance function in your contract to see it passes your requirment or not

1
  • I guess I should have been clearer: I'm already calling allowance() to see if the User has already previously approved the ERC20 Token in question. If the call to allowance() returns 0, then it means they haven't, and so that's when I call approve(). Either way, I'm not sure I understand your response - or what your advice is for how to handle this hard-coding situation. To be clear: I DON'T want to hard-code anything. I want to be able to READ and SEND the Value the User enters in Metamask. So my question is: how do I do that?
    – Mark55
    May 27 at 22:06

Your Answer

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

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