Transcript
Smart Contract & Solidity Solidity
winterj.me@gmail.com
JungWinter
Smart Contract
Smart Contract
•
• 1994 Nick Szabo
•
•
Transaction
Smart Contract
•
•
•
•
• Contacting external services, Enforcing on-chain payments
• (= )
Ethereum Contract
Bitcoin Contract
• Script
• OPCODE
• Output Input Public Key HASH CHECKSIG Private Key TRUE
Ethereum Contract
• Turing Complete
• APPLY(S)=S’ (APPLY: , S: , S’: )
• Smart Contract
• Loop , Contract Account, Block Transaction , Gas
Loop Gas
• Operation Gas Cost
• Transaction fee = Gas_used * Gas_price[Gwei]1 Gwei = 0.000000001 ETH
• Gas = Gas
Smart Contract Code• Smart Contract
Smart Contract Code
• EVM(Ethereum Virtual Machine) EVM
• • Mutan: C Deprecated• LLL: Low level OPCODE• Serpent: Python • Solidity: Javascript
Solidity
Solidity Hello Worldpragma solidity ^0.4.0;
contract SimpleStorage { uint storedData;
function set(uint x) { storedData = x; }
function get() constant returns (uint retVal) { return storedData; }}
pragma
constant
returns
uint
http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html
Solidity Hello Worldpragma solidity ^0.4.0;
contract SimpleStorage { uint storedData;
function set(uint x) { storedData = x; }
function get() constant returns (uint retVal) { return storedData; }}
pragma
constant
returns
uint
^0.4.0 Solidity 0.5.0 npm
^1.2.3 → >=1.2.3 <2.0.0 ^0.2.3 → >=0.2.3 <0.3.0 ^0.0.3 → >=0.0.3 <0.0.4
Solidity Hello Worldpragma solidity ^0.4.0;
contract SimpleStorage { uint storedData;
function set(uint x) { storedData = x; }
function get() constant returns (uint retVal) { return storedData; }}
pragma
constant
returns
uint
constant
State constant Gas
Modifier
. 0.4.16 constant view pure constant view alias pure pure
Solidity Hello Worldpragma solidity ^0.4.0;
contract SimpleStorage { uint storedData;
function set(uint x) { storedData = x; }
function get() constant returns (uint retVal) { return storedData; }}
pragma
constant
returns
uint
return
1
Solidity Hello Worldpragma solidity ^0.4.0;
contract SimpleStorage { uint storedData;
function set(uint x) { storedData = x; }
function get() constant returns (uint retVal) { return storedData; }}
pragma
constant
returns
uint
uint unsigned int uint8 uint256 uint uint256
Solidity Code → Byte Code
SMART CONTRACT (http://goodjoon.tistory.com/261)
SMART CONTRACT (http://goodjoon.tistory.com/261)
dApp
Contract Meta data
pragma solidity ^0.4.0;
contract Coin{ address public minter; mapping (address => uint ) public balances;
event Sent (address from, address to, uint amount);
function Coin() { minter = msg.sender; } function mint(address receiver, uint amount) { if (msg.sender != minter) return; balances[receiver] += amount; }
function send(address receiver, uint amount) { if (balances[msg.sender] < amount) return; balances[msg.sender] -= amount; balances[receiver] += amount; Sent(msg.sender, receiver, amount); }
}
http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html
address
public
mapping
event
msg
address
public
mapping
event
msg
http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html
20byte( ) balance( ), transfer( )
Python dict . mapping(address => uint) address key uint value dict
public address public minter; function minter() returns (address) { return minter; }
), msg.sender , msg.value Wei(Ether)
.data, .gas, .sig
. listener
(3)— (https://medium.com/@soonhyungjung/ - - -3- - - -44a9d58d687a)
pragma solidity^0.4.0;
contract Bank { uint totalDeposit; mapping(address=> uint) balanceOf; function deposit() payable { balanceOf[msg.sender] += msg.value; totalDeposit += msg.value; } function withdraw(uint _amount) payable { balanceOf[msg.sender] -= _amount; totalDeposit -= _amount; msg.sender.call.value(_amount)(); // msg.sender.transfer(_amount); } function getTotalBalance() constant returns(uint) { return totalDeposit; } function getBalance(address _account) constant returns(uint) { return balanceOf[_account]; }}
payable
payable & modifierpragma solidity ^0.4.11;
contract Purchase { address public seller;
modifier onlySeller() { // Modifier require(msg.sender == seller); _; }
function abort() onlySeller { // Modifier usage // ... }}
Solidity modifier payable (https://medium.com/@ggogun/solidity -modifier- -payable-d892a833920c)
Python Decorator, Ruby on Rails before_filter _; modifier .
TODO
• Solidity Voting
• Campaign & Crowdfunding
• Custom Token
• Why Many Smart Contract Use Cases Are Simply Impossible (https://
www.coindesk.com/three-smart-contract-misconceptions/)
• Calculating Costs in Ethereum Contracts (https://hackernoon.com/ether-purchase-power-df40a38c5a2f)
• Gas Cost from Yellow Paper (http://bit.ly/2xfBHWN)
• SMART CONTRACT - (http://goodjoon.tistory.com/253)
• SMART CONTRACT (http://goodjoon.tistory.com/261)
• dApp (http://www.chaintalk.io/archive/lecture/86)
• Solidity (https://www.gitbook.com/book/ggs134/solidityguide)
• Solidity (https://solidity.readthedocs.io/en/develop/)
top related