0

enter image description here

pragma solidity ^0.5.0; 

contract fisrtCoin {

    address deployer; 
    mapping(address=>uint) balances;  

    public function fisrtcoin() {
        deployer = msg.sender;  
    }



    public function giveCoin(uint amount, address receiver) {
      if(msg.sender == deployer){
          balances[receiver] += amount;
      }
      else {
          throw; 
      }
    }

    public function viewBalance() returns (uint) {
        return balances[msg.sender];
    }
}
1

First: the modifier come after the function, so it will be

function fisrtcoin() public  {
    deployer = msg.sender;  
}

Second user constructor key word, so it will be

constructor() external {
    deployer = msg.sender;  
}

Third throw; is deprecated and you dont need an "if else" you can use require(condition, "revert message") instead

require(msg.sender == deployer, "error sender is not deployer");
balances[receiver] += amount;

Forth when your function only ask for data and dont change it, then you need to add the view modifier

function viewBalance() public view returns (uint) {
    return balances[msg.sender];
}

see this https://solidity.readthedocs.io/en/v0.5.3/contracts.html#functions and try to finde up to date examples :) truffle and truffle boxes are a great place to start

0

You need to declare the public statement after the declaration of parameters, remember that since version 0.5.0 constructor is better suited in this case.

Also throw is deprecated in version 0.5.0, in that case, is better to use revert(), require or assert depending on your logic.

pragma solidity ^0.5.0;

    contract firstCoin{

address deployer; 
mapping(address=>uint) balances;  

constructor() public {
    deployer = msg.sender;  
}



function giveCoin(uint amount,address receiver) public
{
  if(msg.sender == deployer){

    balances[receiver] += amount;

  }
  else{
    revert(); 
  }
}

function viewBalance() public view returns (uint)
{
  return balances[msg.sender];
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.