[Ethereum] Correct way of sending Token via contract

contract-developmenterc-20

[Sender] -> [My Contract] -> [ERC20 Contract]

The sender invokes my Contract, and my contract in turns will invoke ERC20 contract to Transfer token on behalf of the senders (using his msg.sender).

However, the ERC20 contracts Transfer() function msg.sender will become the My Contract's address instead of the Sender's address.

May I know what is the correct way for My Contract to Transfer a Token on behalf the Sender?

Best Answer

You cannot do this using the ERC20 transfer(..) function.

You will need to call the transferFrom(..) function on the ERC20 contract.

To be able to use transferFrom(..), your [Sender] will first have to call approve(..) on the ERC20 contract to allow your contract to use transferFrom(..).