2

I need to send an ERC20 Token from a contract to another contract and calling a function of the other contract at the same time.

I know how to transfer an ERC20 Token from solidity to another contract, but how do I call a payable function of the recipient contract at the same time, to use the ERC20 Token I just sent?

2

In ERC20, you need to first approve the token for the contract providing the service.

Then you can use transferFrom function of ERC20 Token within your service contract to use the approved amount of tokens.

This explains it in more detail - https://medium.com/coinmonks/ethereum-payable-token-and-how-it-works-3bf3349a6a77

| improve this answer | |
  • That doesn't answer my question. My question was how to transfer an ERC20 Token to the function of another contract. The function is payable and expects an amount of an ERC20 Token. – kavofa Aug 27 at 5:45
  • Basically you want to pay some ERC20 tokens and get a service from the contract? – Achala Dissanayake Aug 27 at 5:49
  • Yes, that's basically it. Like for example, I want to send DAI (or any other ERC20 token) to a DEX's swap function. – kavofa Aug 27 at 5:54
  • The function doesn't need to be payable for that. With Only ERC20 the way I have mentioned is what you have to follow. First user have to approve and then the the function that accepts ERC20 can transfer the approved amount to the contract or any address. You can use. With only the ERC20 thats the way to do it. There are other ERC standard to directly pay. I'll add some links that explains it in more detail. Let me know if it solves your problem. – Achala Dissanayake Aug 27 at 5:59
  • Thanks, the problem that I am facing now is, that after I approve the receiver contract I call a payable function of the approved contract with contract.call{}(payload). But after that call was made, the token doesn't get transferred to the approved contract and the balance of my smart contract stays the same. – kavofa Aug 27 at 13:54

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.