Skip to content

Instantly share code, notes, and snippets.

@giovapanasiti
Last active January 11, 2018 14:55
Show Gist options
  • Save giovapanasiti/f4a0437a1722dcc3dc4d0774b13338a1 to your computer and use it in GitHub Desktop.
Save giovapanasiti/f4a0437a1722dcc3dc4d0774b13338a1 to your computer and use it in GitHub Desktop.
First Smart Contract Example with comments
pragma solidity ^0.4.17;
// specify the version of Solidity that our code is written with
contract Inbox {
// define a new contract (almost identical to a class) that will
// have some number of methods and variables
string public message;
// - storage variables:
// declares all of the instance variables (and their types)
// that will exist in this contract
// - public defines who have access to that variable
// - message is the name of the variable --> the value of the variable will be
// stored in the ethereum blockchain for ethernity
// IN CONTRAST WITH STORAGE VARIABLES WE HAVE LOCAL VARIABLES WHICH ARE NEVER STORED IN THE BLOCKCHAIN
function Inbox(string initialMessage) public {
// this function is called constructor function since it has the same
// name as the contract.
// A constructor function is automatically called one time when the
// contract is created. Invoked automatically when the contract is deployed
message = initialMessage;
}
function setMessage(string newMessage) public {
// this functions with a different name from the contract's one can be called
// once the contract has been deployed on the blockchain
message = newMessage;
// we are modifying the message variable here so we can't use either constant and view in the function type
}
// this function is unuseful since the message variable is public so solidity will
// create a function for use to return the message variable's value
function getMessage() public view returns (string) {
// public and view are the function type declaration
// most common function types --> [public, private, view, constant, pure, payable]
return message;
// we are just accessing data not modifying. This is why we use the view function type
// so the compiler knows we only access data.
// - returns (with return's type) can only be specified with functions that have
// view and constant aas function type.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment