Smart Contract Interaction – How to Use Any ERC20

contract-developmenterc-20erc-223solidity

How can I interact with various tokens with my deployed smart contract? I want to be able to receive tokens, count them, and perform logic based on the aforementioned events.

I tried using the ERC20 standard function, Transfer, but cannot figure out how to specify which token is being delivered. I assume, at some point, the address of the ERC20 will need to be included in the function.

In looking at this answer, I get some insight.

Would using ERC223 be the answer here?

Best Answer

Firstly, I assume you mean a smart contract you will deploy, as you can't change the functionality of one already deployed.

With ERC20, there is no function triggered on your contract when it receives tokens, the token contract itself is all that gets called. To detect the change you would need some external program to monitor the blockchain for relevant transactions and fire some method on your contract when it finds one, but this solution isn't ideal.

Alternatively if you have full control over the software that might be sending tokens to your contract, you can have this 'approve' your contract address on the token contract, and then call a function on your smart contract which will perform the 'transferFrom' function, so you are in control of the transfer and can handle it as desired.

In either case you will need to feed your smart contract the token address in order to check balances or perform the 'transferFrom'

If you opt only for compatibility with ERC223 tokens instead, the situation is much simpler - any ERC223 token transfer to your smart contract will call the 'tokenFallback' method on your contract, so you can simply add your desired functionality there.