Smart contract is computer protocol that facilitate, verify, or enforce the negotiation or performance of a contract, or that make a contractual clause unnecessary.
State variables are stored permanently on EVM. Data types ● Boolean bool ● Integer int / uint (unsigned) ● Ethereum address address ● Bytes and String bytes / string
mapping (address => uint) likes; function like(address _friend) public { likes[_friend]++; } Functions and Modifiers modifier onlyOwner { if (msg.sender != owner) throw; _; } function makeAdmin(_candidate) public onlyOwner { admin = _candidate; } The rest of the function goes here Looks pretty much like a JS function.
Function Visibilities 1. public a. Visible externally and interally b. Automatically creates accessor. 2. private a. Only visible in current contract 3. internal a. Similar to private, but callable by child contracts. 4. external a. Similar to public, different low level handling. Remember, on blockchain, all data is publicly viewable!
Roles 1. Issuer a. Able to issue SGDT. b. SGDT <==> SGD exchanger & guarantor. 2. User a. Able to freely transact SGDT. 3. Authority a. Grant license to issuers.
pragma solidity ^0.4.2; contract SGDT { /* Public variables of the token */ string public name = 'Singapore Dollar Token'; string public symbol = 'SGDT'; uint8 public decimals = 2; uint public totalSupply = 0; address public authority; /* This creates an array with all balances */ mapping (address => uint) public balanceOf; mapping (address => uint) public issuedBy; mapping (address => bool) public isIssuer; } Define state variables
Issuer functions function issue(uint _value) public onlyIssuer { issuedBy[msg.sender] += _value; balanceOf[msg.sender] += _value; TokenIssued(msg.sender, _value); } Can you work on the redeem() function?
User functions /* Send coins */ function transfer(address _to, uint _value) public { if (balanceOf[msg.sender] < _value) { throw; // Check if the sender has enough } 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 }