Solidity - Basics+ganache
Solidity - Basics+ganache
1
Prerequisite
• Blockchain
• Ethereum
• Smart contracts
Blockchain
Fully Distributed Database like BTC
Advantages:
• Highly Secure
• Transparent
• Immutable
Disadvantages:
• Scaling
• Performance
Summarizing
Blockchain
• It is a decentralized distributed ledger (data structure) where data is being
stored inside blocks in form of transactions.
• Remove the dependency on the trusted third party for recording the data in
Blocks.
• Since each block is built on top of previous Block immutability has been
achieved.
• Very difficult to add a fake block & very easy to detect the fake Block.
• Every participants of the Blockchain contain the almost same copy of the
Blockchain.
Blockchain Main
Themes
• Longitudinal immutability
• Reliable timestamps
• Cryptographic security
• Fully distributed replication
• Publicly verifiable code governing all transactions
• Value in Smart Contract Assets
Accounts and Wallets in
Accounts:
Ethereum
• Two Kinds:
External Owned Accounts - (EOA, most common account)
Contract Accounts
• Consist of a public/private keypair
• Allow for interaction with the blockchain
Wallets:
• A set of one or more external accounts
• Used to store/transfer ether
What is a Smart
Contract
• Smart contract is a term used to describe computer program code that
is capable of facilitating, executing, and enforcing the negotiation or
performance of an agreement (i.e. contract) using blockchain
technology.
• The entire process is automated can act as a complement, or substitute,
for legal contracts, where the terms of the smart contract are recorded
in a computer language as a set of instructions.
Smart Contracts
• Executable code
• Turing Complete
• Function like an external account
Hold funds
Can interact with other accounts and smart contracts
Contain code
• Can be called through transactions
Code
Execution
• Every node contains a virtual machine (similar to Java)
Called the Ethereum Virtual Machine (EVM)
Compiles code from high-level language to bytecode
Executes smart contract code and broadcasts state
• Every full-node on the blockchain processes every
transaction and stores the entire state
Gas Cost
• Gas Price: current market price of a unit of Gas (in Wei)
Check gas price here: https://ethgasstation.info/
Is always set before a transaction by user
• Gas Limit: maximum amount of Gas user is willing to spend
• Helps to regulate load on network
• Gas Cost (used when sending transactions) is calculated by
gasLimit*gasPrice.
All blocks have a Gas Limit (maximum Gas each block can use)
Atom Ethereum interface - Plugin for the Atom editor that features
syntax highlighting, compilation and a runtime environment (requires
backend node).
Atom Solidity Linter - Plugin for the Atom editor that provides Solidity
linting.
Vim Solidity - Plugin for the Vim editor providing syntax highlighting.
Vim Syntastic - Plugin for the Vim editor providing compile checking.
B. Smart Contract Programming
• Solidity (javascript based), most popular
Not yet as functional as other, more mature, programming
languages
• Serpent (python based)
• LLL (lisp based)
Serpent
Serpent is a language similar to Python which can be used to develop contracts and compile to EVM
bytecode. It is intended to be maximally clean and simple, combining many of the efficiency benefits of
a low-level language with ease-of-use in programming style, and at the same time adding special
domain-specific features for contract programming. Serpent is compiled using LLL.
• Serpent on the ethereum wiki
• Serpent EVM compiler
Atom Ethereum interface - Plugin for the Atom editor that features
syntax highlighting, compilation and a runtime environment (requires
backend node).
Atom Solidity Linter - Plugin for the Atom editor that provides Solidity
linting.
Vim Solidity - Plugin for the Vim editor providing syntax highlighting.
Vim Syntastic - Plugin for the Vim editor providing compile checking.
uint value;
logIncrement.watch(function(err, result) {
// do something with result
})
Account
address
Account
balance
Testnet (Rinkeby) faucet
• ETH on testnet has no real value; therefore we get testnet ETH from
faucet
• Example, Rinkeby faucet, Ropsten faucet
Rinkeby faucet
About solidity
• High level statically typed programming language
• With solidity you can create contracts for uses such as voting, blind
auction, crowdfunding, and multi-signature wallets
• Case sensitive
• Latest updates are available on solidity documentation
Variables in solidity
• A variable is basically a placeholder for data which can be manipulated at
runtime.
• Synatax:
• Example:
Solidity- View vs Pure functions
• View function can be declared view in which case they
promise not to modify the state. they can view the state
variable but can't modify it
Variable
Getter function
Setter function
Compile the
Contract
• Compile tab: Start to compile button
Set Deployment
Parameters (1/2)
• Run tab: Environment = JavaScript VM
Set Deployment
Parameters (2/2)
• JavaScript VM: All the transactions will be executed in a sandbox blockchain in the
browser. Nothing will be persisted and a page reload will restart a new blockchain
from scratch, the old one will not be saved.
• Injected Provider: Remix will connect to an injected web3 provider. Mist and
Metamask are example of providers that inject web3, thus they can be used with
this option.
• Web3 Provider: Remix will connect to a remote node. You will need to provide
the URL address to the selected provider: geth, parity or any Ethereum client.
• Gas Limit: The maximum amount of gas that can be set for all the instructions of a
contract.
• Value: Input some ether with the next created transaction (wei = 10-18 of ether).
Deploy the Contract on the Private
• Run tab: Deploy button
Blockchain of Remix
Interact with the
Contract
• Setter = Red Button: Creates transaction
• Getter= Blue Button: Just gives information
We want to save
the address of the
contract creator
A sample program
Solidity- View vs Pure functions
Example
Solidity- View vs Pure functions
Example
Receive ether (Payable
function)
• Transfer money to the contract
Payable keyword
allows receiving
ether
Hidden Code:
Address(this).balance += msg.value; We can get the
balance of the
contract 19
Receive ether
Input the value as wei
1
(10-18 of ether)
2.Gas Limit
will
automaticall
y be
calculated
3.Paste your
private key
from Ganache
31
MyEtherWal
let on Sign Transaction button to deploy
Click
your contract
32
Ganache
You can see now you have one transaction for your
address and your balance has been changed because
of the amount of gas you paid for creating the
contract.
Interacting with the smart
contract
Extract the contract address Extract the ABI (Application Binary
from Ganache Interface) of the code from Remix
Read
Generate the transaction
35
Rem
ix
Copy the ABI
(ABI is the interface that tells MyEtherWallet how to
interact with the contract)
MyEtherWallet
Contracts tab:
Interact with Contract = Paste the contract address
from Ganache and the ABI from Remix
MyEtherWallet
You now can interact with the contract by selecting a
function and invoking it
MyEtherWal
let
If you select the getValue function you will
receive the value without paying any gas
(There is no operation cost for getting
information)
MyEtherWallet
If you choose a function that updates the state of
the contract, you will need to pay gas for it in a
transaction.
C. Development Workflow
Testing Cycle
• From friends
• Faucet
• Exchanges (for public blockchain)
TestRPC/TestChain
• Local development or Test Blockchain
• https://github.com/ethereumjs/testrpc