I have created an ERC20 token and want to make it payable with DAI.
I read some of the related questions but did not found any proper answer.
As dai is an ERC20 token so, how can I accept any token as payment instead of ether?
function () external payable {}
this accepts only ether, right?
sorry in advance for my English and little understanding of solidity
Best Answer
Here is an example of how you can achieve this:
Before calling your
buyXXX
function, your users will need to call theapprove
function on the DAI contract, passing to it your contract address as the custodian (i.e., the one being approved).Of course, since you use
address(this)
as the destination to transfer the user's DAI tokens to, those tokens will be transferred to your contract. And since you don't have a private key for it, you'll need to implement an internal mechanism (function) which will allow you to extract those tokens. Alternatively, instead ofaddress(this)
, you can add to your contract a state-variable ofaddress
type, which you will be able pre-configure somewhere in your contract (for example, in the constructor).