how can I create an automatic smart contract to send tokens ,if a user send ETH to the smart contract and also to bee able to recover ETH from the smart contract and tokens too thank you. To explain brief, if Bob sends 1ETH he receives automatic 100tokens in ERC20 and the contract owner can withdraw ETH from the smart contract and tokens (incase tokens are mistakenly sent).
I will not give you the complete code, because this will be way too easy for you, but I will give you the following hints:
- Check the Ownable contract and how to implement it in order to have functions which are unlocked only for the contract owner. ( you will need this for the logic where only the contract owner should be allowed to withdraw eth and tokens )
- Look how to inherit the erc20 token contract and check how to call external functions like
transfer( you will need this method in order to send tokens to users ) and
balanceOfmethod ( you will need this one to check if the contract has enough tokens balance to send to users ).
- You also need to decide if the conversion eth <=> token will be hardcoded in the contract or dynamic. If you want the conversion to be dynamic you can check how the Oraclize library works or even better choice would be to take a look at the decentralized Chainlink oracle services. You can use the oracles in order to read the eth/ token price outside of the blockchain, from external URL.
If you struggle implementing some of the features feel free to ask me as comments below this post, I will try to be helpful as much as I can. Cheers