1

how to add this functionality into a token contract? Function : whenever anyone sends any Ethereum to my contract address, any number more than 0 (even an amount like 0.000000001), I would like to send them back 1000 of my tokens, and send eth that can be contracted to a certain address?

Example contract like this : https://etherscan.io/address/0x55732acec59699e28bad93522684e0d241a64a35

thanks

2
  • I've added an example below, but it sounds like you are very new to smart contract development - I would encourage you to read up on some smart contract dev basics to have a better understanding of how they work.
    – Andrey
    Commented Feb 27, 2018 at 19:35
  • blog.zeppelin.solutions/… for example; also the one I posted in my answer
    – Andrey
    Commented Feb 27, 2018 at 19:36

2 Answers 2

4

One of the best prototypes of ERC20 token implementation is the OpenZeppelin's ERC20 implementation.

Follow it's guidelines and you'll archive to do it.

Related with your problem, the error that gives the other answer comes from the ERC20 implementation , if You've overridden the transfer() method and set only one parameter, you won't be able to do it.

Another problem is that msg.value is in wei, so as fas as a wei is indivisible, that may be causing errors too.

So doing it fast it'll be something like that:

contract MyToken {
    // ERC20 token implementation
}

contract SomeContract {
    address tokenAddress;
    MyToken token;
    token.approve(this( which is adress of SomeContract), X amount of tokens is able to donate has to be more than 1000 that is what you send)

    // Run this first, to tell the contract the address 
    // of your token smart contract
    function setTokenAddress(address _tokenAddress) public {
        tokenAddress = _tokenAddress;
        token = MyToken(_tokenAddress);
    }

    function GiveMeTokens() public payable {
        if (msg.value > 1) {
            token.transfer(msg.sender, 1000);
        }
    }
}

Use a standard ERC20 implementation and try again your method.

3

A few preconditions: your token contract is an ERC20 token; the smart contract that gives out tokens, has the tokens transferred to it.

contract MyToken {
    // ERC20 token implementation
}

contract SomeContract {
    address tokenAddress;
    MyToken token;    

    // Run this first, to tell the contract the address 
    // of your token smart contract
    function setTokenAddress(address _tokenAddress) public {
        tokenAddress = _tokenAddress;
        token = MyToken(_tokenAddress);
    }

    function GiveMeTokens() public payable {
        if (msg.value > 0.000000001) {
            // Transfer 1000 tokens from smart contract address 
            // to contract caller address
            token.transfer(msg.sender, 1000);
        }
    }
}

I haven't tested it, just typed it here, so it might not compile but should give you an idea of how to do that.

It will be helpful for your overall understandig to read about ERC20 token smart contracts and how they work. For example https://theethereum.wiki/w/index.php/ERC20_Token_Standard

3
  • this contract does not work on the part : function GiveMeTokens() public payable { if (msg.value > 0.00000001) { // Transfer 1000 tokens from smart contract address // to contract caller address token.transfer(msg.sender,1000); } }
    – gosdor xda
    Commented Feb 27, 2018 at 19:48
  • @gosdorxda - what exactly doesn't work? What error are you getting?
    – Andrey
    Commented Feb 28, 2018 at 1:22
  • this error : browser/ballot.sol:20:13: TypeError: Value transfer to a contract without a payable fallback function. token.transfer(msg.sender,100); ^------------^ browser/ballot.sol:20:13: TypeError: Wrong argument count for function call: 2 arguments given but expected 1. token.transfer(msg.sender,100); ^----------------------------^
    – gosdor xda
    Commented Feb 28, 2018 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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