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:

  1. 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 )
  2. 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 balanceOf method ( you will need this one to check if the contract has enough tokens balance to send to users ).
  3. 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

  • Please give I just want to use it to be learning ,and also want to modify it better pls Sep 20 '20 at 18:58
  • If possible a github repo Sep 20 '20 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.