There is a payable fallback function available now on my smart contract, but I want to function it like if someone send 0.01 / 0.05 / 0.10 / 0.50 / 1 ETH and the contract will automatically send the required amount of Token to the sender.

For Example:

Token Name: TEST TOKEN
Symbol: TEST
Total Supply: 21,000,000 TEST
Decimals: 18

My main code is given below. Now if I want to send 1 TEST to the sender who sent on my contract 0.10 ETH then what changes do I need to make on the following code.

function TESTTOKEN() public {
    symbol = "TEST";
    name = "TEST TOKEN";
    decimals = 18;
    _totalSupply = 21000000000000000000000000;
    balances[0x57669331B5c97532Bbc5Fc2bA0CE27D54E91CfdC] = _totalSupply;
    Transfer(address(0), 0x57669331B5c97532Bbc5Fc2bA0CE27D54E91CfdC, _totalSupply);

function () public payable {

Problem Flow Chart


Ideally, define a fixed price for 1 TEST (that you can maybe change later) and divide the msg.value by the price.

function() payable public {
    uint256 tokens_to_credit = msg.value/FIXED_PRICE; //use SafeMath's div eventually
    //Credit the user and emit a transfer event
| improve this answer | |
  • Thanks @eli-drion and do I need to change on my SafeMath which is like this one: link – SoftCode Apr 23 '19 at 16:37
  • And do I need to add any function for setting up Gas or Gwei on Code or anywhere. Thanks again @eli-drion – SoftCode Apr 23 '19 at 16:43

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