4. CONTRACTS
Contracts are accounts that contain code
Store the state
• Example:
keeps account
balance
Source of notifications
(events)
• Example: messaging
service that forwards
only messages when
certain conditions are
met
Mapping - relationship
between users
• Example: mapping
real-world financial
contract to Etherium
contract
Act like a software
library
• Example:
return calculated
value based on
message arguments
5. CALLS AND MESSAGES
Contract A Contract B
Message Call
• Source
• Target
• Data Payload
• Ether
• Gas
• Return Data
Externally
owned
account
Message CallSend Transaction
Call
6. WHAT CONTRACT CAN DO?
Read internal state
• contract
ReadStateDemo {
• uint public state;
• function read() {
• console.log(“State:
"+state);
• }
• }
Modify internal State
• contract
ModStateDemo {
• uint public state;
• function update() {
• state=state+1;
• }
• }
Consume message
• contract
ReadStateDemo {
• uint public state;
• function hi() {
• console.log(“Hello
from: “ "+msg.sender
+ “ to “ + msg.
receiver);
• }
• }
Trigger execution of
another contract
• contract CallDemo {
• function send(address
receiver, uint amount)
public {
• Sent(msg.sender,
receiver, amount);
• }
• }
9. ELEMENTARY DATA TYPES
Data Type Example
uint<M> , M=1,2,4, …, 256
int<M>, uint, int
Unsigned/signed integer uint256 counter = 1;
address 160-bit value that does not
allow any arithmetic
operations. It is suitable for
storing addresses of
contracts or keypairs
belonging to external
persons
address owner =
msg.sender;
bool Same as uint8 but values are
0 or 1
Bool b = 0;
fixed<M>x<N>
ufixed<M>x<N>
Signed fixed-point decimal
number of M bits
fixed128x19 f = 1.1;
bytes<M>, M=1..32 Binary of M bytes bytes32 n=123;
function Same as bytes24 Function f;
10. ARRAYS
Data Type Example
<type>[M] fixed-length array of fixed-
length type
byte[100]
<type>[] variable-length array of
fixed-length type
byte[]
bytes dynamic sized byte sequence bytes ab;
string dynamic sized Unicode
string
String s = “String”;
fixed<M>x<N>
ufixed<M>x<N>
Signed fixed-point decimal
number of M bits
fixed128x19 f = 1.1;
11. FUNCTIONS
Functions
Constant
Transactional
Function Visibility
External Can be called via
transactions
Public Can be called via
messages or locally
Internal Only locally called or
from derived contracts
Private Locally called
pragma solidity^0.4.12;
contract Test {
function test(uint[20] a) public returns (uint){
return a[10]*2;
}
function test2(uint[20] a) external returns (uint){
return a[10]*2; }
}
}
28. MORE COMPLEX CODE EXAMPLE,
ERC20
contract Erc20Token
constructor
totalSupply()
balanceOf()
transfer()
transferFrom()
approve()
allowance()
29. VERY SIMPLE ERC20 CONTRACT
pragma solidity >=0.4.22 <0.6.0;
contract MyToken {
mapping (address => uint256) public balanceOf;
constructor() public {
balanceOf[msg.sender] = 10000; // Give the creator all initial tokens
}
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
return true;
}
}
30. EVEN MORE COMPLEX CODE
EXAMPLE, VOTING
contract Ballot
Voter
Voter
Voter
Voter
Proposal
Proposal
Proposal
Proposal
Chairperson
constructor
giveRightToVote()
delegate()
vote()
winningProposal()
winnerName()
32. STAY IN TOUCH
Gene Leybzon https://www.linkedin.com/in/leybzon/
https://www.meetup.com/members/90744
20/
https://www.leybzon.com
Notas do Editor
Every account has a persistent key-value store mapping 256-bit words to 256-bit words called storage
Accounts can be externally owned or contracts
Message: This is contract-to-contract. These are not delayed by mining because they are part of the transaction execution.
Transaction: Like a message, but originates with an externally owned account. It's always a transaction that gets things started, but multiple messages may be fired off to complete the action.
Ethereum also has two modes of execution. These modes are indifferent to the syntax or vernacular in the client that summoned the contract. So, the important things are to know which one is appropriate, and how to select it in your client of choice.
sendTransaction: Transaction is sent to the network and verified by miners. Sender gets a transaction hash initially since no results are available until after the transaction is mined. These are potentially state-changing.
call: Transaction is executed locally on the user's local machine which alone evaluates the result. These are read-only and fast. They can't change the blockchain in any way because they are never sent to the network. Most languages/platforms don't have this idea of running the code in either "read-only/dry run/practice" mode and "read-write/real world/sticky" mode.
(https://ethereum.stackexchange.com/questions/12065/what-is-the-difference-between-a-call-message-call-and-a-message)