2017-12-25 26 views
1

誰かが私のトークンアドレスにetherを送信した後、自動的に同等のトークン量(私はトークン価格を手動で設定しています)を返送する必要があります。問題は私がトークンアドレスにエーテルを送ることができないことです。私はethereum.orgからコードを学んでいます。そこからコードをコピーして、少し変更を加えました。私のトークン契約でエーテルを受け取って自動的に支払人に転送する方法

は、ここで私が次に行うこと

pragma solidity ^0.4.16; 

contract owned { 
    address public owner; 

    function owned() public { 
     owner = msg.sender; 
    } 

    modifier onlyOwner { 
     require(msg.sender == owner); 
     _; 
    } 

    function transferOwnership(address newOwner) onlyOwner public { 
     owner = newOwner; 
    } 
} 
/** 
* @title SafeMath 
* @dev Math operations with safety checks that throw on error 
*/ 
library SafeMath { 
    function mul(uint256 a, uint256 b) internal pure returns (uint256) { 
    if (a == 0) { 
     return 0; 
    } 
    uint256 c = a * b; 
    assert(c/a == b); 
    return c; 
    } 

    function div(uint256 a, uint256 b) internal pure returns (uint256) { 
    // assert(b > 0); // Solidity automatically throws when dividing by 0 
    uint256 c = a/b; 
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold 
    return c; 
    } 

    function sub(uint256 a, uint256 b) internal pure returns (uint256) { 
    assert(b <= a); 
    return a - b; 
    } 

    function add(uint256 a, uint256 b) internal pure returns (uint256) { 
    uint256 c = a + b; 
    assert(c >= a); 
    return c; 
    } 
} 
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; } 

contract TokenERC20 { 
    // Public variables of the token 
    string public name; 
    string public symbol; 
    uint8 public decimals; 
    // 18 decimals is the strongly suggested default, avoid changing it 
    uint256 public totalSupply; 

    // This creates an array with all balances 
    mapping (address => uint256) public balanceOf; 
    mapping (address => mapping (address => uint256)) public allowance; 

    // This generates a public event on the blockchain that will notify clients 
    event Transfer(address indexed from, address indexed to, uint256 value); 

    // This notifies clients about the amount burnt 
    event Burn(address indexed from, uint256 value); 

    /** 
    * Constructor function 
    * 
    * Initializes contract with initial supply tokens to the creator of the contract 
    */ 
    function TokenERC20(
     uint256 initialSupply, 
     string tokenName, 
     string tokenSymbol, 
     uint8 dividetoken 
    ) public { 

     balanceOf[msg.sender] = totalSupply;    // Give the creator all initial tokens 
     name = tokenName;         // Set the name for display purposes 
     symbol = tokenSymbol;        // Set the symbol for display purposes 
     decimals = dividetoken; 
     totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount 
    } 

    /** 
    * Internal transfer, only can be called by this contract 
    */ 
    function _transfer(address _from, address _to, uint _value) internal { 
     // Prevent transfer to 0x0 address. Use burn() instead 
     require(_to != 0x0); 
     // Check if the sender has enough 
     require(balanceOf[_from] >= _value); 
     // Check for overflows 
     require(balanceOf[_to] + _value > balanceOf[_to]); 
     // Save this for an assertion in the future 
     uint previousBalances = balanceOf[_from] + balanceOf[_to]; 
     // Subtract from the sender 
     balanceOf[_from] -= _value; 
     // Add the same to the recipient 
     balanceOf[_to] += _value; 
     Transfer(_from, _to, _value); 
     // Asserts are used to use static analysis to find bugs in your code. They should never fail 
     assert(balanceOf[_from] + balanceOf[_to] == previousBalances); 
    } 

    /** 
    * Transfer tokens 
    * 
    * Send `_value` tokens to `_to` from your account 
    * 
    * @param _to The address of the recipient 
    * @param _value the amount to send 
    */ 
    function transfer(address _to, uint256 _value) public { 
     _transfer(msg.sender, _to, _value); 
    } 

    /** 
    * Transfer tokens from other address 
    * 
    * Send `_value` tokens to `_to` in behalf of `_from` 
    * 
    * @param _from The address of the sender 
    * @param _to The address of the recipient 
    * @param _value the amount to send 
    */ 
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { 
     require(_value <= allowance[_from][msg.sender]);  // Check allowance 
     allowance[_from][msg.sender] -= _value; 
     _transfer(_from, _to, _value); 
     return true; 
    } 

    /** 
    * Set allowance for other address 
    * 
    * Allows `_spender` to spend no more than `_value` tokens in your behalf 
    * 
    * @param _spender The address authorized to spend 
    * @param _value the max amount they can spend 
    */ 
    function approve(address _spender, uint256 _value) public 
     returns (bool success) { 
     allowance[msg.sender][_spender] = _value; 
     return true; 
    } 

    /** 
    * Set allowance for other address and notify 
    * 
    * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it 
    * 
    * @param _spender The address authorized to spend 
    * @param _value the max amount they can spend 
    * @param _extraData some extra information to send to the approved contract 
    */ 
    function approveAndCall(address _spender, uint256 _value, bytes _extraData) 
     public 
     returns (bool success) { 
     tokenRecipient spender = tokenRecipient(_spender); 
     if (approve(_spender, _value)) { 
      spender.receiveApproval(msg.sender, _value, this, _extraData); 
      return true; 
     } 
    } 

    /** 
    * Destroy tokens 
    * 
    * Remove `_value` tokens from the system irreversibly 
    * 
    * @param _value the amount of money to burn 
    */ 
    function burn(uint256 _value) public returns (bool success) { 
     require(balanceOf[msg.sender] >= _value); // Check if the sender has enough 
     balanceOf[msg.sender] -= _value;   // Subtract from the sender 
     totalSupply -= _value;      // Updates totalSupply 
     Burn(msg.sender, _value); 
     return true; 
    } 

    /** 
    * Destroy tokens from other account 
    * 
    * Remove `_value` tokens from the system irreversibly on behalf of `_from`. 
    * 
    * @param _from the address of the sender 
    * @param _value the amount of money to burn 
    */ 
    function burnFrom(address _from, uint256 _value) public returns (bool success) { 
     require(balanceOf[_from] >= _value);    // Check if the targeted balance is enough 
     require(_value <= allowance[_from][msg.sender]); // Check allowance 
     balanceOf[_from] -= _value;       // Subtract from the targeted balance 
     allowance[_from][msg.sender] -= _value;    // Subtract from the sender's allowance 
     totalSupply -= _value;        // Update totalSupply 
     Burn(_from, _value); 
     return true; 
    } 
} 

/******************************************/ 
/*  ADVANCED TOKEN STARTS HERE  */ 
/******************************************/ 

contract mintableToken is owned, TokenERC20 { 
using SafeMath for uint256; 
    uint256 public sellPrice; 
    uint256 public buyPrice; 
    uint256 public cap; //Hard Cap Amount 
    string public version ;  //Version standard. Just an arbitrary versioning scheme. 
    mapping (address => bool) public frozenAccount; 
    /* This generates a public event on the blockchain that will notify clients */ 
    event FrozenFunds(address target, bool frozen); 

    /* Initializes contract with initial supply tokens to the creator of the contract */ 
    function mintableToken(
     uint256 initialSupply, 
     string tokenName, 
     string tokenSymbol, 
     uint8 decimals, 
     uint256 _cap, 
     string _version 
    ) TokenERC20(initialSupply, tokenName, tokenSymbol,decimals) public { 
    require(_cap > 0); 
    cap = _cap; 
    version=_version; 
    } 

    /* Internal transfer, only can be called by this contract */ 
    function _transfer(address _from, address _to, uint _value) internal { 
     require (_to != 0x0);        // Prevent transfer to 0x0 address. Use burn() instead 
     require (balanceOf[_from] >= _value);    // Check if the sender has enough 
     require (balanceOf[_to] + _value > balanceOf[_to]); // Check for overflows 
     require(!frozenAccount[_from]);      // Check if sender is frozen 
     require(!frozenAccount[_to]);      // Check if recipient is frozen 
     balanceOf[_from] -= _value;       // Subtract from the sender 
     balanceOf[_to] += _value;       // Add the same to the recipient 
     Transfer(_from, _to, _value); 
    } 

    /// @notice Create `mintedAmount` tokens and send it to `target` 
    /// @param target Address to receive the tokens 
    /// @param mintedAmount the amount of tokens it will receive 
    function mintToken(address target, uint256 mintedAmount) onlyOwner public { 
     require(totalSupply.add(mintedAmount) <= cap); 
     balanceOf[target] += mintedAmount; 
     totalSupply += mintedAmount; 
     Transfer(0, this, mintedAmount); 
     Transfer(this, target, mintedAmount); 
    } 

    /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens 
    /// @param target Address to be frozen 
    /// @param freeze either to freeze it or not 
    function freezeAccount(address target, bool freeze) onlyOwner public { 
     frozenAccount[target] = freeze; 
     FrozenFunds(target, freeze); 
    } 

    /// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth 
    /// @param newSellPrice Price the users can sell to the contract 
    /// @param newBuyPrice Price users can buy from the contract 
    function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public { 
     sellPrice = newSellPrice; 
     buyPrice = newBuyPrice; 
    } 

    /// @notice Buy tokens from contract by sending ether 
    function buy() payable public { 
     uint amount = msg.value/buyPrice;    // calculates the amount 
     _transfer(this, msg.sender, amount);    // makes the transfers 
    } 

    /// @notice Sell `amount` tokens to contract 
    /// @param amount amount of tokens to be sold 
    function sell(uint256 amount) public { 
     require(this.balance >= amount * sellPrice);  // checks if the contract has enough ether to buy 
     _transfer(msg.sender, this, amount);    // makes the transfers 
     msg.sender.transfer(amount * sellPrice);   // sends ether to the seller. It's important to do this last to avoid recursion attacks 
    } 
} 

// TestCoin 
contract TestCoin is mintableToken(0,"TestCoin","TEC",4,100000000,"Ver-2.0"){ 

    function() payable public{ 
     mintableToken.buy(); 
    } 
} 

を試みたものだ、またはいくつかの問題は、コードに存在する場合。私は完全に3日間くつろいだ。誰かが貴重な時間をかけてコードを見ることができれば、大きな助けになるでしょう。

事前に感謝

編集

私は、次のエラーが(error_22)ガスを見積もることができませんでした

を示している対処トークンにエーテルを送信しようとしていますとき。 アカウントに十分な資金がないか、受信契約のアドレスがエラーになります。 を手動でガスを手動でセットしてから、自由に感じてください。

EDIT -2

今トークン契約にエーテルを送信することができる午前すなわち問題上記解決されます。以下は完全に変更されたコードです(私はより多くの条件文を実装するためにそれをアップグレードします、私は唯一のトークンの同等の量はエーテル消費者に報酬を与える必要があります)、今度はそれはミンタブルトークンではない、

pragma solidity ^0.4.4; 

contract Token { 

    /// @return total amount of tokens 
    function totalSupply() constant returns (uint256 supply) {} 

    /// @param _owner The address from which the balance will be retrieved 
    /// @return The balance 
    function balanceOf(address _owner) constant returns (uint256 balance) {} 

    /// @notice send `_value` token to `_to` from `msg.sender` 
    /// @param _to The address of the recipient 
    /// @param _value The amount of token to be transferred 
    /// @return Whether the transfer was successful or not 
    function transfer(address _to, uint256 _value) returns (bool success) {} 

    /// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from` 
    /// @param _from The address of the sender 
    /// @param _to The address of the recipient 
    /// @param _value The amount of token to be transferred 
    /// @return Whether the transfer was successful or not 
    function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {} 

    /// @notice `msg.sender` approves `_addr` to spend `_value` tokens 
    /// @param _spender The address of the account able to transfer the tokens 
    /// @param _value The amount of wei to be approved for transfer 
    /// @return Whether the approval was successful or not 
    function approve(address _spender, uint256 _value) returns (bool success) {} 

    /// @param _owner The address of the account owning tokens 
    /// @param _spender The address of the account able to transfer the tokens 
    /// @return Amount of remaining tokens allowed to spent 
    function allowance(address _owner, address _spender) constant returns (uint256 remaining) {} 

    event Transfer(address indexed _from, address indexed _to, uint256 _value); 
    event Approval(address indexed _owner, address indexed _spender, uint256 _value); 

} 



contract StandardToken is Token { 

    function transfer(address _to, uint256 _value) returns (bool success) { 
     //Default assumes totalSupply can't be over max (2^256 - 1). 
     //If your token leaves out totalSupply and can issue more tokens as time goes on, you need to check if it doesn't wrap. 
     //Replace the if with this one instead. 
     //if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) { 
     if (balances[msg.sender] >= _value && _value > 0) { 
      balances[msg.sender] -= _value; 
      balances[_to] += _value; 
      Transfer(msg.sender, _to, _value); 
      return true; 
     } else { return false; } 
    } 

    function transferFrom(address _from, address _to, uint256 _value) returns (bool success) { 
     //same as above. Replace this line with the following if you want to protect against wrapping uints. 
     //if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) { 
     if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) { 
      balances[_to] += _value; 
      balances[_from] -= _value; 
      allowed[_from][msg.sender] -= _value; 
      Transfer(_from, _to, _value); 
      return true; 
     } else { return false; } 
    } 

    function balanceOf(address _owner) constant returns (uint256 balance) { 
     return balances[_owner]; 
    } 

    function approve(address _spender, uint256 _value) returns (bool success) { 
     allowed[msg.sender][_spender] = _value; 
     Approval(msg.sender, _spender, _value); 
     return true; 
    } 

    function allowance(address _owner, address _spender) constant returns (uint256 remaining) { 
     return allowed[_owner][_spender]; 
    } 

    mapping (address => uint256) balances; 
    mapping (address => mapping (address => uint256)) allowed; 
    uint256 public totalSupply; 
} 


//name this contract whatever you'd like 
contract TestCoin is StandardToken { 

    function() payable public { 

    } 

    /* Public variables of the token */ 

    /* 
    NOTE: 
    The following variables are OPTIONAL vanities. One does not have to include them. 
    They allow one to customise the token contract & in no way influences the core functionality. 
    Some wallets/interfaces might not even bother to look at this information. 
    */ 
    string public name;     //fancy name: eg Simon Bucks 
    uint8 public decimals;    //How many decimals to show. ie. There could 1000 base units with 3 decimals. Meaning 0.980 SBX = 980 base units. It's like comparing 1 wei to 1 ether. 
    string public symbol;     //An identifier: eg SBX 
    string public version = 'H1.0';  //human 0.1 standard. Just an arbitrary versioning scheme. 

// 
// CHANGE THESE VALUES FOR YOUR TOKEN 
// 

//make sure this function name matches the contract name above. So if you're token is called TutorialToken, make sure the //contract name above is also TutorialToken instead of ERC20Token 

    function TestCoin(
     ) { 
     balances[msg.sender] = 1000000000000;    // Give the creator all initial tokens (100000 for example) 
     totalSupply = 1000000000000;      // Update total supply (100000 for example) 
     name = "TestCoin";         // Set the name for display purposes 
     decimals = 4;       // Amount of decimals for display purposes 
     symbol = "BPC";        // Set the symbol for display purposes 
    } 

    /* Approves and then calls the receiving contract */ 
    function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) { 
     allowed[msg.sender][_spender] = _value; 
     Approval(msg.sender, _spender, _value); 

     //call the receiveApproval function on the contract you want to be notified. This crafts the function signature manually so one doesn't have to include a contract in here just for this. 
     //receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData) 
     //it is assumed that when does this that the call *should* succeed, otherwise one would use vanilla approve instead. 
     if(!_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData)) { throw; } 
     return true; 
    } 
} 
+0

'TestCoin'契約を展開し、あなたの単体テストと思われるエーテルを送信していますか?そうであれば、0の供給でトークンを初期化しています。購入価格は0です。TokenERC20()コンストラクタでは、初期残高を設定するときに 'totalSupply'は常に0になります。また、契約を展開しているときには、所有者は 'TestCoin'を配備している人のアドレスになりますが、' _transfer() 'を呼び出すときはfromアドレスとして' this'を渡します。配備された契約それは混乱のようなものです。 Remixでデバッガを使用します。 –

答えて

2

トークンが一般的にどのように機能し、実際にどのようなトークンであるかを理解する必要があります。トークンは残高に関する情報を保持するスマート契約です(マッピングaddress => uint)。したがって、指定されたアドレスに保持されているトークンの量を保持するだけです。これ以上何もない。あなたが知る必要があるもう一つのことは、フォールバック機能(名前のないもの)です。あなたの場合は空です。あなたがする必要がある何

function() payable public { 

} 

は以下のように変更され:

function() payable public { 
    balances[msg.sender] += msg.value; 
} 

あなたはシングル責任原則を破るとは何か、あなたのトークンの契約に直接トークン販売機能を追加しようとしているようにそれはまた見えます一般的に良い考えではありません。私はrepositoryをチェックし、物事を分かち合うためにどのように編成できるかを見てみることをお勧めします。理解しやすくするために、私はいくつかのテストをテストフォルダに追加しました。テストを読んで、すべての動作を理解し、契約の期待される動作を理解してください。

関連する問題