Introduction to Solidity
@tomusdrw
Tomasz Drwięga
Parity Technologies
Any code presented on those slides is here only for educational purposes and is NOT production ready.
The code was not audited or tested properly and most probably doesn't work.
Never use it with real ETH.
"A smart contract is a
computerized transaction protocol that
executes the terms of a contract.
The general objectives are to satisfy common contractual conditions (such as
payment terms,
liens,
confidentiality, and even
enforcement), minimize exceptions both malicious and accidental, and
minimize the need for trusted intermediaries.
Related economic goals include lowering fraud loss, arbitrations and enforcement costs, and other transaction costs."
A decentralized program that controls money
and acts according to programmed rules without a trusted 3rd party.
pragma solidity ^0.4.11;
contract Burn {
uint256 public value;
address public owner;
function Burn() payable {
value = msg.value;
owner = msg.sender;
}
}
A high-level "JavaScript-like" contract-oriented language compiled to EVM bytecode.
pragma solidity ^0.4.11;
contract Parity {
uint256 public value;
address public owner;
function export() payable {
value += msg.value;
owner = msg.sender;
}
}
0xc0de15de4d... at 0x123..456
binary at /usr/bin/parity
$ parity export blocks
from: 0x456..def
to: 0x123..456
value: 5 * 10^18 wei (5 ETH)
gas: 100,000
gasPrice: 4 * 10^9 wei (4 shannon)
nonce: 0
data: 0x444...
("call function export")
0x123456... (Transaction RLP)
pragma solidity ^0.4.11;
contract Burn {
uint256 public value;
address public owner;
function Burn() payable {
value = msg.value;
owner = msg.sender;
}
}
contract Lock {
uint256 public value;
address public owner;
function Lock() payable {
value = msg.value;
owner = msg.sender;
}
function withdraw() {
if (msg.sender != owner) {
throw;
}
msg.sender.transfer(value);
}
}
pragma solidity ^0.4.11;
contract Lock {
uint256 public value;
address public owner;
modifier onlyOwner() {
if (msg.sender != owner) throw;
_;
}
function Lock() payable {
value = msg.value;
owner = msg.sender;
}
function withdraw() onlyOwner {
msg.sender.transfer(value);
}
}
contract Lock {
uint256 public value;
address public owner;
uint256 public lockedUntil;
modifier onlyOwner() {
if (msg.sender != owner) throw;
_;
}
function Lock() payable {
value = msg.value;
owner = msg.sender;
lockedUntil = now + 5 days;
}
function withdraw() onlyOwner {
if (block.timestamp < lockedUntil) {
throw;
}
msg.sender.transfer(value);
}
}
Browser Solidity / Remix
https://ethereum.github.io/browser-solidity/
Truffle
https://github.com/trufflesuite/truffle
contract Lock {
struct LockedFunds {
uint256 value;
uint256 until;
}
mapping(address => LockedFunds) public locked;
function lock() payable {
locked[msg.sender].value = msg.value;
locked[msg.sender].until = block.number + 5;
}
function unlock() {
var data = locked[msg.sender];
if (data.value == 0) throw;
if (block.number < data.until) throw;
delete locked[msg.sender];
msg.sender.transfer(data.value);
}
}
contract Lock {
struct LockedFunds { uint256 value; uint256 until; }
address public owner;
mapping(address => LockedFunds) public locked;
function Lock() { owner = msg.sender; }
function lock() payable {
locked[msg.sender].value = msg.value;
locked[msg.sender].until = block.number + 5;
}
function unlock() {
var data = locked[msg.sender];
if (data.value == 0) throw;
if (block.number < data.until) throw;
delete locked[msg.sender];
msg.sender.transfer(data.value);
}
function withdraw() {
if (msg.sender != owner) throw;
owner.transfer(this.balance);
}
}
contract Lock {
struct LockedFunds { uint256 value; uint256 until; }
address public owner;
mapping(address => LockedFunds) public locked;
function Lock() { owner = msg.sender; }
function lock() payable {
// make sure to increment value here otherwise the funds are lost!
locked[msg.sender].value += msg.value;
locked[msg.sender].until = block.number + 5;
}
function unlock() {
// copy the structure to memory
LockedFunds memory data = locked[msg.sender];
if (data.value == 0) throw;
if (data.until < block.number) throw;
delete locked[msg.sender];
msg.sender.transfer(data.value);
}
function withdraw() {
if (msg.sender != owner) throw;
owner.transfer(this.balance);
}
}
pragma solidity ^0.4.21;
contract Lock {
struct LockedFunds { uint256 value; uint256 until; }
address public owner;
mapping(address => LockedFunds) public locked;
// Declare an event
event FundsLocked(address indexed sender, uint256 value);
function Lock() public { owner = msg.sender; }
function lock() public payable {
locked[msg.sender].value += msg.value;
locked[msg.sender].until = block.number + 5;
// emit an event
emit FundsLocked(msg.sender, locked[msg.sender].value);
}
function unlock() public {
LockedFunds memory data = locked[msg.sender];
require(data.value != 0);
require(data.until >= block.number);
delete locked[msg.sender];
msg.sender.transfer(data.value);
}
function withdraw() public {
require(msg.sender == owner);
owner.transfer(this.balance);
}
}