2017-12-25 18 views
1

누군가가 토큰 주소로 ether를 보낸 후 자동으로 토큰 금액 (수동으로 토큰 가격을 설정 함)을 다시 보내야 할 때 달성하고 싶은 것. 문제는 토큰 주소에 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가) 가스를 추정 할 수 없습니다 보여주고 해결합니다. 계정에 충분한 금액이 없거나 수신 계약 주소로 인해 오류가 발생합니다. 자유롭게 가스를 수동으로 설정하고 계속 진행하십시오.

편집 -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을 확인하고 항목을 분리 된 상태로 유지하는 방법을 살펴 보시기 바랍니다. 더 쉽게 이해할 수 있도록 테스트 폴더에 몇 가지 테스트를 추가 했으므로 테스트를 읽으면 모든 것이 작동하는 방식과 계약의 예상되는 동작을 이해할 수 있습니다.