ethereum
smart contract
林修平
online compiler
•
online compiler :
/>
MANUAL COMPILE(using geth)
•
connect to console
•
set up a chain
•
connect to main chain:go-ethereum/build/bin/geth console
•
connect to testnet:go-ethereum/build/bin/geth --testnet console
•
build a private net:go-ethereum/build/bin/geth --datadir “your_directory"
--rpc --rpcport port --rpccorsdomain "*" --port "30303" --nodiscover -ipcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --rpcapi
"db,eth,net,web3" --autodag --networkid number --nat "any" console
MANUAL COMPILE(using geth)
•
in console
•
var contractABI = web3.eth.contract([{contractABI}]);
•
var contract123 = contractABI.new(
parameters,
{from: address,
data: bytecode,
gas: gas
}, callback_function)
Quick Intro
Quick Intro
•
price unit:ether
•
address:you can withdraw from or save ether to it.
•
it can represent an user account
•
•
•
no code
or represent a contract
•
tied with a code
•
NOTE: same code on different address is different contract
transaction:
•
send ether
•
or to execute function in a contract
•
or both
Quick Intro
•
contract: comprised of state and function
•
state: used by user to keep information regarding the contract
•
function: change state of a contract
•
NOTE: Ethereum discourage users from using state to preserve information, so it cost a lot of gas to either
create or change the state.
•
•
WHY? Since every node(miner) needs to keep a full copy of the blockchain, they have to be compensated
for storage cost of every contract.
gas
•
every function comprised of many operations and there’s a price to every type of operation
•
there’s a fixed amount of gas cost to function you design and you have to pay for it when you want to execute a
function
•
you can decide how much ether you want to pay per gas and that becomes transaction fee needed for this
execution
Quick Intro
•
If you are on a public chain, every storage cost matters.
•
But what if you are on a private chain?
•
you can have ether as many as you want so you don’t need to
worry about transaction fee
•
but does it mean that you can use as many storage as you want?
•
YES! but still, the costs of storing these states are to be
taken by all nodes in your private chain
How it work
How it work
• state
• function:operate
• design
on states
logic:use functions
to make your contract work
How it work
Transaction(Deploy)
contract Count123{
uint counts(0);
function incre(){
counts = count + 1;
}
}
How it work
contract Count123{
uint counts(0);
function incre(){
counts = count + 1;
}
}
How it work
Transaction(Invoke)
Count123.incre()
contract Count123{
uint counts(0);
function incre(){
counts = count + 1;
}
}
How it work
let’s take slock.it for example and write a simple bike renting contract!
slock.it
state declaration
state declaration
contract bikeRenting {
address public owner;
address public currentRenter;
uint public expireTime;
…
}
state declaration
•
type , visibility , variable_name
contract bikeRenting {
address public owner;
address public currentRenter;
uint public expireTime;
…
}
state declaration
•
type , visibility , variable_name
•
type : bool, int, uint, address, mapping, bytes, string, struct
•
visibility : public or private(default)
•
•
public: accessible externally
(declare in your contract) uint public totalCount = 3;
(access in a console) mycontract.totalCount(); //3
(access from other contract) thatcontract.totalCount() //3
variable_name
state declaration
•
Array in solidity :
•
address[] owners;
•
push item into array:
•
delete item:
•
address[3] threeOwners;
owners.push(address)
delete owners[2]
pitfall: only changes the value of specified item to zero
instead of erase the item
state declaration
•
mapping :
•
mapping(typeA => typeB) variable_name;
•
example
•
declare a mapping:
•
•
map address 0x123456789abcdef to integer 10:
•
•
mapping(address => uint) deposits;
deposits[0x123456789abcdef] = 10;
uninitialized or undeclared values are zero instead of NULL
•
NOTE: there’s no NULL in Solidity
state declaration
•
Units and Globally available variables
•
Ether unit:ether、finney、wei
•
time unit:seconds、weeks、years、now
•
now: present time
•
more specifically, the time when this block is mined, i.e,
the time_stamp parameter in the block header
state declaration
•
Special variables and functions
•
•
msg: information regarding this transaction
•
msg.sender: address who send the transaction
•
msg.value: value sent with the transaction
address related functions
•
address.balance
•
address.send(amount):send ether to address
state declaration
•
Special variables and functions
•
throw
•
reverts all changes made so far by the underlying transaction
•
confiscate all gas provided by underlying transaction
initialization
initialization
contract bikeRenting {
address public owner;
address public currentRenter;
uint public expireTime;
uint public unitPrice;
function bikeRenting(uint _unitPrice){
owner = msg.sender;
currentRenter = 0x0;
expireTime = now;
unitPrice = _unitPrice;
}
}