Solidity – How to Handle Ether and Token Payment in Same Contract?

contract-developmenterc-20soliditytokens

How do I have a single contract handle receiving payments in Ether in the constructor and receiving/transferring an ERC20 Standard Token in the same contract's methods?

constructor = receiving msg.value in Ether
contract method 1 = receiving msg.value as ERC20 Token
contract method 2 = transfer ERC20 Token to address

I understand and have implemented the receiving payment in Ether for the constructor, but I am kind of confused as to how to handle the ERC20 Standard Token in two other contract payable methods.

I understand I could keep track of the amounts of the ERC20 Token inside the contract, but I am wondering how to verify on receiving/transferring that it is that type of token?

Do I need to interface with the deployed ERC20 Token contract to hold all the values and process the transactions for this contract?

Best Answer

If I understand your problem correctly, You have an erc-20 standard token deployed, and now in another contract, you want to have functions for receiving/transferring that token.

contract method 1 = receiving msg.value as ERC20 Token

AFAIK, you can't have any such function. msg.value is always the amount of wei send along with the transaction. So you can't have tokens as msg.value.

However, you can have the amount of token as a function parameter.

I understand I could keep track of the amounts of the ERC20 Token inside the contract, but I am wondering how to verify on receiving/transferring that it is that type of token?

If you have to support only one specific token transfer, you won't need to verify the type of token. You can have a function like function tokenTransfer(unit tokenAmount) . If you want to support multiple erc-20 tokens, you can have another parameter named contractAddress as another function parameter.

Do I need to interface with the deployed ERC20 Token contract to hold all the values and process the transactions for this contract?

Yes, that is the clean approach to do so. I am not sure what your use case is, but the best way to transfer tokens is to call transfer function of that specific contract. But in case you want the same function in another contract you should call the tokenContract internally when someone calls function of your contract. This will be easier than keeping track of balances in both contracts.