0

I want to send an erc20 token to the constructor of a contract. I can't manually use the approve(spender, amount) function before deploying the contract as the spender address needs to be the address of the contract. If I use token.approve or token.transfer methods in the contract it returns transfer amount exceeds balance, though I have sufficient balance in my account. I assume that it's trying to approve or transfer tokens from the contract.

Note: This question is different from this. Using token.approve inside the contract approves from the contract and not from msg.sender.

0

As I see it, you have two options:

  1. Figure out the contract address in advance. Then add allowance for the contract to withdraw tokens from you, and in the constructor add transferFrom. Here are some details on how to figure out the address before deployment: How is the address of an Ethereum contract computed?

  2. Deploy and send tokens in a separate transaction

1
  • Hi! The first method can't be used in my application as the sender is not fixed. The sender can be any user who uses the application. I am using the second method. Thank you! – Arvinth Saravanan Apr 25 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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