Professional Documents
Culture Documents
LAB 2:
INTRODUCTION
TO SOLIDITY
BLOCKCHAIN ELEMENTS LAB 2
LAB OUTLINE
2 SOLIDITY: OVERVIEW
3 QUICK DOCS
ETHEREUM
1 VIRTUAL
MACHINE
BLOCKCHAIN ELEMENTS LAB 2
2 SOLIDITY
OVERVIEW
BLOCKCHAIN ELEMENTS LAB 2
https://learnxinyminutes.com/docs/solidity/
● deposit()
○ Takes no parameters, but we can send
Ether using the payable modifier.
○ public makes function externally
readable (not writeable) by users or
contracts
○ Returns user’s balance as an unsigned
integer (uint)
● Withdraw()
○ withdrawAmount parameter
○ Returns user’s balance
● Note the way we deduct the balance
right away, before sending
○ We do this because of the risk of a recursive call that may allow the caller to
● balance():
○ constant prevents the function
from editing state variables, that
are stored on the blockchain.
● A contract can have exactly one unnamed function, which cannot have arguments and
cannot return anything.
● It is executed on a call to the contract if none of the other functions match the given
function identifier (or if no data was supplied at all).
● Furthermore, this function is executed whenever the contract receives plain Ether
(without data). To receive Ether and add it to the total balance of the contract, the
fallback function must be marked payable. If no such function exists, the contract cannot
receive Ether through regular transactions and throws an exception.
3 QUICK DOCS
BLOCKCHAIN ELEMENTS LAB 2
DATA TYPES
BYTES
DATA TYPES
STRING
DATA TYPES
VAR
DATA TYPES
// Variables can be used to assign function to variable
function a(uint x) public returns (uint) {
return x * 2;
}
var f = a;
f(22); // call
DATA
3.2 STRUCTURES
BLOCKCHAIN ELEMENTS LAB 2
DATA STRUCTURES
ARRAYS
// Arrays
bytes32[5] nicknames; // static array
bytes32[] names; // dynamic array
uint newLength = names.push("John"); // push returns the new length of the array (Here 1)
// multidimensional array
uint x[][5]; // arr with 5 dynamic array elements (opposite order of most languages)
DATA STRUCTURES
MAPPINGS
// Dictionaries (any type to any other type)
mapping (string => uint) public balances;
balances["charles"] = 1;
console.log(balances["ada"]);
// gives 0, all non-set key values return zero
DATA STRUCTURES
MAPPINGS
// Nested mappings
mapping (address => mapping (address => uint)) public custodians;
// To delete
delete balances["John"]; // sets corresponding element to 0
delete balances; // do not set any element to 0
// Unlike other languages, we CANNOT iterate through all elements in a
// mapping, without knowing source keys. However, we can build a data structure
// on top to do this
DATA STRUCTURES
MAPPINGS
Delete has no effect on mappings as the keys of mappings may be arbitrary and are
generally unknown.
However, individual keys and what they map to can be deleted.
Eg: If ‘a’ is a mapping, then delete a[x] will delete the value stored at x.
DATA STRUCTURES
STRUCTS
}
Bank b = Bank({
owner: msg.sender,
balance: 5
});
DATA STRUCTURES
STRUCTS
// or
struct Bank {
address owner;
uint balance;
}
Bank c = Bank(msg.sender, 5);
c.balance = 9; // set to new value
delete b;
DATA STRUCTURES
ENUMS
// Enums
enum State { Created, Locked, Inactive } // often used for state machine
State public state; // Declare variable from enum
state = State.Created;
● Data locations: memory vs. storage vs. stack - all complex types (array, struct, mapping) have a data location.
○ memory does not persist, storage does.
● Default is storage for local and state variables; memory for function arguments
○ For most types, the data location to use can be explicitly set
4 EXERCISE
BLOCKCHAIN ELEMENTS LAB 2
REMIX IDE
THE WORLD ON A SINGLE WEB APP
REMIX IDE
THE WORLD ON A SINGLE WEB APP
REMIX IDE
THE WORLD ON A SINGLE WEB APP
REMIX IDE
THE WORLD ON A SINGLE WEB APP
REMIX IDE
THE WORLD ON A SINGLE WEB APP
REMIX IDE
THE WORLD ON A SINGLE WEB APP
THINGS TO DO
UH-OH, TIME TO WORK
We want you to flex your creative problem-solving skills in this new environment.
With a mind for clean code, use the Remix IDE to implement the following:
● A ‘greeter’ contract with a 'greet' method that returns the string “Hello World!”
○ TRY: The user should be able to change the greeting without redeploying the contract
● The Fibonacci function
○ TRY: Iteratively
THINGS TO DO
UH-OH, TIME TO WORK
● An XOR function
○ Input ‘1’ or ‘0’
○ This does not require any bitwise operations!
○ TRY: Input a string of 1's and 0's, e.g. "10001110101101"
● A method to concatenate two strings
○ Importing a module is fine
○ TRY: Do not use a module