简体   繁体   中英

How to send ethers to a contract?

I have written the FarihaToken which is ERC20 standard complaint, the code is below:

 contract FarihaToken is ERC20Interface{
    using SafeMath for uint;

    string public symbol;
    string public name;
    uint8 public decimals;
    uint public _totalSupply;
    mapping(address => uint) balances;
    mapping(address => mapping(address => uint)) allowed;

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------
    function FarihaToken() public{
        symbol = "FTC";
        name = "Fariha Token";
        decimals = 18;
        _totalSupply = totalSupply();
        balances[msg.sender] = _totalSupply;
        Transfer(address(0),msg.sender,_totalSupply);
    }

    function totalSupply() public constant returns (uint){
       return 1000000 * 10**uint(decimals);
    }

    // ------------------------------------------------------------------------
    // Get the token balance for account `tokenOwner`
    // ------------------------------------------------------------------------
    function balanceOf(address tokenOwner) public constant returns (uint balance) {
        return balances[tokenOwner];
    }

    // ------------------------------------------------------------------------
    // Transfer the balance from token owner's account to `to` account
    // - Owner's account must have sufficient balance to transfer
    // - 0 value transfers are allowed
    // ------------------------------------------------------------------------
    function transfer(address to, uint tokens) public returns (bool success){
        balances[msg.sender] = balances[msg.sender].sub(tokens);
        balances[to] = balances[to].add(tokens);
        Transfer(msg.sender,to,tokens);
        return true;
    }

     // ------------------------------------------------------------------------
    // Token owner can approve for `spender` to transferFrom(...) `tokens`
    // from the token owner's account
    //
    // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
    // recommends that there are no checks for the approval double-spend attack
    // as this should be implemented in user interfaces 
    // ------------------------------------------------------------------------
    function approve(address spender, uint tokens) public returns (bool success){
        allowed[msg.sender][spender] = tokens;
        Approval(msg.sender,spender,tokens);
        return true;
    }

    // ------------------------------------------------------------------------
    // Transfer `tokens` from the `from` account to the `to` account
    // 
    // The calling account must already have sufficient tokens approve(...)-d
    // for spending from the `from` account and
    // - From account must have sufficient balance to transfer
    // - Spender must have sufficient allowance to transfer
    // - 0 value transfers are allowed
    // ------------------------------------------------------------------------
    function transferFrom(address from, address to, uint tokens) public returns (bool success){
        balances[from] = balances[from].sub(tokens);
        allowed[from][to] = allowed[from][to].sub(tokens);
        balances[to] = balances[to].add(tokens);
        Transfer(msg.sender,to,tokens);
        return true;
    }

    // ------------------------------------------------------------------------
    // Returns the amount of tokens approved by the owner that can be
    // transferred to the spender's account
    // ------------------------------------------------------------------------
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
        return allowed[tokenOwner][spender];
    }
}

contract PurchaseToken is FarihaToken{
    // Accept ETH and return FTC
    function() public payable {
        var amount = msg.value/2 ;
        require(balanceOf(this) >= amount);
        transfer(msg.sender,amount);
    }
} 

Use case is that when some one sends 2 ethers it'll send back 1 token. I'm using truffle as dev framework, I'm newbie to this so I have some questions to ask if someone can help me with them:

  1. I want to send ethers. How can I do that? which account to use? a truffle command will be useful.

  2. I want to make sure that the account that send ethers have received tokens as well. Command would be useful.

  3. Is there a way I can see my ethers in account using truffle ?

    PS: I tried running this on truffle but it reverts the action.

    PurchaseToken.deployed().then(function(D){ff=D;}); ff.send(2)

Transfer of tokens means you will deduct the tokens from contract account and add these token to msg.sender. Please go through the following contract which consists of the functionality- https://github.com/ganeshdipdumbare/coin-with-whitelisting/blob/master/GCoinAdvance.sol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM