[Ethereum] Can a smart contract send the ERC20 token it owns to another address on trigger

contract-developmentcontract-invocationerc-20soliditytokens

I know that a smart contract address can accepts a ERC20 token sent to it.

Let’s say there are two smart contracts A and B. A is a deployed smart contract implementing a ERC20 token and B is the contract I want to write.

I would like to know can I write a method in the contract B so that, on trigger by sending a transaction to call its method, it sends a certain amount of the A token owned by the contract B address to another address I specified?

A follow up question: In a manual transaction sender has to sign the raw ERC20 token transaction and send it to network. For smart contract B, is it as simple as calling the target ERC20 smart contract A’s method and it can use the tokens owned by B’s address? Is there any steps needed to "sign" the transfer?

Thank you.

Best Answer

1) Depends on what you mean with a "trigger". Smart contracts can never do anything without someone/something issuing them a transaction. So contracts can't "monitor" things by themselves or run in the background in that sense. Otherwise, yes, a smart contract can transfer tokens to another contract/address.

2) Because all action is initiated through a transaction, the original transaction sender is the "signer". The one who issues the original transaction also has to pay (with gas) for all the transactions that occur in the possible chain of contracts.

EDIT After the asker added more information about the problem:

Sending tokens to another address is done by calling the transfer method in the ERC20 token contract. That method should actually just modify the contract A's inner state, so transferring tokens from contract B only requires B to call A's transfer function. B does not need to "send" any tokens itself - it only needs to call the transfer function to change the ownership of said tokens.

Related Topic