Blockchain Programming
Blockchain Programming
Ali Dorri
Traditional Programming
se
R e sp o n
DApps: Distributed Applications
DApps: Distributed Applications
Open Source: All nodes have to verify the contract to verify transaction and
state in blockchain.
Decentralized
Algorithm/Protocol
Immutable
Different Blockchains, Different Platforms
• To store its transaction, each node has to verify two other transactions
• Self-scaling
• Open source
Solidity
• EVM: The computer that all full nodes agree to run, i.e., runtime
environment for smart contract
• Gas Limit
• Gas Price
https://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html
You can define Structs in solidity
struct Person {
uint age;
string name;
}
Function visibility
• Public
o Your contract can be called by anyone or any other contract
o Default
o Security issue
• Private
o Only functions within the contract can call the function
function setAge(string _name, uint _age) private {
• Internal
o Similar to private, but accessible to contracts that inherit from this contract
• External
o Similar to public, but can “only” be called outside the contract
• Return value
string greeting = "What's up";
function sayHello() public returns (string) {
return greeting;
}
• Function modifiers
• View: the function only views data, but not modifying any value, i.e., only
read action is performed.
• Pure: the function does not access data in blockchain, i.e., the return value
only depends on the input values.
function _multiply(uint a, uint b) private pure returns
(uint) {
return a * b;
}
• Mapping (keyàvalue)
mapping (address => uint) public accountBalance;
• How to find out the address of the person who called the transaction?
msg.sender
contract Doge {
function catchphrase() public returns (string) {
return "So Wow CryptoDoge";
}}
import "./someothercontract.sol";
Ownable
https://github.com/OpenZeppelin/openzeppelin-solidity
Payable
transferThing(msg.sender);
} }
• Withdraw
uint itemFee = 0.001 ether;
msg.sender.transfer(msg.value - itemFee);
What to do after writing the smart code?
JSON files
Infura
• Maintains a set of Ethereum nodes
https://infura.io/
Metamask
• Install Node.js,
o brew install node
• Install compiler
o npm install –g solc
• Install Ethereum
o brew tap ethereum/ethereum brew
o install ethereum
Create a
genesis.JSON file
https://medium.com/cybermiles/running-a-quick-ethereum-private-network-for-experimentation-and-testing-6b1c23605bce
Initialize the first node
geth --datadir ~/gethDataDir/ init genesis.json
Create account
personal.newAccount("<YOUR_PASSPHRASE>")
Check account
Eth.accounts
Checking account balance Eth.getbalance(eth.coinbase/eth.accounts[0])