Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

throw depreciated doc update #625

Closed
julesmoretti opened this issue Jul 24, 2017 · 5 comments
Closed

throw depreciated doc update #625

julesmoretti opened this issue Jul 24, 2017 · 5 comments

Comments

@julesmoretti
Copy link

Hello there:

Was trying to get my hands dirty and follow one of your tutorials but copying and pasting your code did not work due to the new throw depreciation in favor to other things.

screen shot 2017-07-24 at 9 33 01 am

So please update ethereum-org/views/content/token.md

And let me know how you would rewrite the following:

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
    }
@clone1612
Copy link
Contributor

The pull request I made updating the tutorial to the new syntax was merged a while back but it seems it's not yet active on the live website (#614).

If you want to look at how to rewrite that you can look at the token file in the repo, that already contains the updated code: https://github.com/ethereum/ethereum-org/blob/master/views/content/token.md

So for your example it would become:

 /* Send coins */
    function transfer(address _to, uint256 _value) {
        require(_to != 0x0);                                // Prevent transfer to 0x0 address. Use burn() instead
        require(balanceOf[msg.sender] >= _value);           // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        balanceOf[msg.sender] -= _value;                    // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
        Transfer(msg.sender, _to, _value);                  // Notify anyone listening that this transfer took place
    }

Basically it means replacing if ... throw with the new require() syntax.

@kuojenski
Copy link

/* This unnamed function is called whenever someone tries to send ether to it */
function () {
throw; // Prevents accidental sending of ether
}
}

contract MyAdvancedToken is owned, token {

uint256 public sellPrice;
uint256 public buyPrice;

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 MyAdvancedToken(
    uint256 initialSupply,
    string tokenName,
    uint8 decimalUnits,
    string tokenSymbol
) token (initialSupply, tokenName, decimalUnits, tokenSymbol) {}

/* Send coins */
function transfer(address _to, uint256 _value) {
    require(balanceOf[msg.sender] >= _value);           // Check if the sender has enough
    require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
    require(!frozenAccount[msg.sender]);                // Check if frozen
    balanceOf[msg.sender] -= _value;                    // Subtract from the sender
    balanceOf[_to] += _value;                           // Add the same to the recipient
    Transfer(msg.sender, _to, _value);                  // Notify anyone listening that this transfer took place
}


/* A contract attempts to get the coins */
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
    require(!frozenAccount[_from]);                     // Check if frozen            
    require(balanceOf[_from] >= _value);                // Check if the sender has enough
    require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
    require(_value <= allowance[_from][msg.sender]);    // Check allowance
    balanceOf[_from] -= _value;                         // Subtract from the sender
    balanceOf[_to] += _value;                           // Add the same to the recipient
    allowance[_from][msg.sender] -= _value;
    Transfer(_from, _to, _value);
    return true;
}

function mintToken(address target, uint256 mintedAmount) onlyOwner {
    balanceOf[target] += mintedAmount;
    totalSupply += mintedAmount;
    Transfer(0, this, mintedAmount);
    Transfer(this, target, mintedAmount);
}

function freezeAccount(address target, bool freeze) onlyOwner {
    frozenAccount[target] = freeze;
    FrozenFunds(target, freeze);
}

function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner {
    sellPrice = newSellPrice;
    buyPrice = newBuyPrice;
}

function buy() payable {
    uint amount = msg.value / buyPrice;               // calculates the amount
    require(balanceOf[this] >= amount);               // checks if it has enough to sell
    balanceOf[msg.sender] += amount;                  // adds the amount to buyer's balance
    balanceOf[this] -= amount;                        // subtracts amount from seller's balance
    Transfer(this, msg.sender, amount);               // execute an event reflecting the change
}

function sell(uint256 amount) {
    require(balanceOf[msg.sender] >= amount);        // checks if the sender has enough to sell
    balanceOf[this] += amount;                        // adds the amount to owner's balance
    balanceOf[msg.sender] -= amount;                  // subtracts the amount from seller's balance
    if (!msg.sender.send(amount * sellPrice)) {       // sends ether to the seller. It's important
        throw;                                        // to do this last to avoid recursion attacks
    } else {
        Transfer(msg.sender, this, amount);           // executes an event reflecting on the change
    }               
}

}

there still "throw" in the code, I think there still three of it, so how can I fix it?? help

@julesmoretti
Copy link
Author

@clone1612 thank you that is very helpful :)

@renanserrano
Copy link

@alexvandesande i realized that the code is not updated on the ethereum website, but at github is right =D

@alexvandesande
Copy link

It will be updated shortly! ;-)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants