如何将醚发送给合同?

时间:2018-03-22 10:54:54

标签: blockchain ethereum solidity smartcontracts

我写的是FarihaToken,这是ERC20标准投诉,代码如下:

 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);
    }
} 

用例是当有人发送2个ethers时,它会发回1个令牌。我正在使用松露作为开发框架,我是新手,所以我有一些问题要问是否有人可以帮助我:

  1. 我想发送醚类。我怎样才能做到这一点?哪个帐号可以使用?一个 truffle命令会很有用。

  2. 我想确保发送ethers的帐户已收到 令牌也是。命令会很有用。

  3. 有没有办法可以用松露看到我的醚?

    P.S:我尝试在松露上运行它,但它恢复了动作。

    PurchaseToken.deployed(),然后(函数(d){FF = d;})。 ff.send(2)

1 个答案:

答案 0 :(得分:0)

转移代币意味着您将从合约帐户中扣除代币并将这些代币添加到msg.sender。 请仔细阅读以下包含功能的合同 - https://github.com/ganeshdipdumbare/coin-with-whitelisting/blob/master/GCoinAdvance.sol