Difference between revisions of "Solidity Contracts"

From rbachwiki
Jump to navigation Jump to search
 
Line 35: Line 35:
  }
  }


==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Solidity_ethereum_Language|Category]]==


==Bank account==
==Bank account==
Line 82: Line 81:
   
   
  }// end contract
  }// end contract
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Solidity_ethereum_Language|Category]]==

Latest revision as of 22:33, 13 March 2018

Solidity Contract

pragma solidity ^0.4.0;

contract HelloWorldContract{
    string word = "Hello";
    address public issuer;
    // constructor
    function HelloWorldContract(){
        issuer = msg.sender;
    }
    
    modifier ifIssuer(){
        if(issuer != msg.sender){
            throw;
        }
        else{
            _; // this means continue 
        }
    }
    
    // creating a getter 
    // the keyword constant is used to make it a getter without it the contract would 
    // use gas and would be a setter
    function getWord() constant returns(string){
        return word;
    }
    
    // setter 
    function setWord(string newWord) returns(string){
        word = newWord;
        return word;
    }
    
}


Bank account

pragma solidity ^0.4.0;

contract BankAccount{
address client;
bool _switch = false;
// event will fire when called only workd in setters
event UpdateStatus(string _msg, uint _amount);
event test(string _msg2);
// constructor
function BankAccount(){
    client = msg.sender;
}
// this will allow only the owner of the contrat to use
/*
so you create the modifier then add it to the functions that you want it to affect
*/
modifier ifClient(){
    if(msg.sender != client){
        throw;
    }
    _;
}

function depositFunds() payable{
    UpdateStatus("User has deposited", msg.value);
    test("funds deposited");
}

function witdrawFunds(uint amount) ifClient{
    // this returns a boolean
    if(client.send(amount)){
        UpdateStatus("User has deposited ", 0);
        _switch = true;
    }else{
        _switch = false;
    }
} 

function getFunds() ifClient constant returns(uint) {
    return this.balance;
} 
 

}// end contract

Back To Top- Home - Category