Difference between revisions of "Solidity ethereum Language"

From rbachwiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Solidity Contract ==
==[[Solidity Contracts]]==
 
==[[Setting Up Local Mining Environment]]==
pragma solidity ^0.4.0;
==[[Setting up Coding Environment]]==
==[[Setting up Truffle]]==
contract HelloWorldContract{
==[[Setting up Truffle to interact with Ganache]]==
    string word = "Hello";
==[[Building a Dapp Store]]==
    address public issuer;
==[[Solidity Terms]]==
    // 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

Latest revision as of 15:03, 21 March 2018