[Episode 2] Mastering Ethereum : A Short Summarized story
In this episode we will learn about types of Wallets, EOAs and Contracts.
Here we are covering chapter 2 : Ethereum Basics of "Mastering Ethereum" book. So get ready to write your first smart contract.
So lets get started.
Ethereum Currency Units
Ethereum’s currency unit is called ether(“ETH”). Ether is subdivided into smaller units, wei being the smallest.
Exponent | Common name | SI name |
1 | wei | Wei |
10^3 | Babbage | Kilowei or Femtoether |
10^6 | Lovelace | Megawei or Picoether |
10^9 | Shannon | Gigawei or Nanoether |
10^12 | Szabo | Microether |
10^15 | Finney | Milliether |
10^18 | Ether | Ether |
10^21 | - | Grand Kiloether |
10^24 | - | Megaether |
Wallets
- Wallet is a software application that STORES your keys and can create and broadcast transactions on your behalf i.e gateway to the Ethereum system.
- Their servers are always connected to internet and thats why we say they are prone to attacks.
- So download from sources you trust.
- Hardware wallet is a portable physical pen-drive type device that store your private keys and therefore no access to internet so no phising attack.
Wallets | Description |
MetaMask | Browser extension wallet able to connect to a variety of Ethereum nodes and test blockchains. |
Jaxx | Multi-Platform & Multicurrency wallet that runs on Android, iOS, Windows, macOS, and Linux and ease of use. |
MyEtherWallet | Web-based wallet having multiple sophisticated features |
Emerald | Designed to Work with the Ethereum Classic, but compatible with other Ethereum-based blockchains. It's open source desktop app and can run a full node or connect to a public remote node, working in a “light” mode. |
Metamask
- Download : Download real one offered by metamask.io.
- Mnemonic : After download it will ask you to either create a mnemonic or import if you have already. mnemonic is father of all wallets. Public and private key pairs are derived from this 12 words phrase mnemonic.
Losing private key is not a problem. Because you can derive it from mnemonic. But losing mnemonic will lose all your accounts and fund and no one in this world can recover it. So keep you mnemonic secure and safe.
- Password : This Wallet is protected by password everytime you log into it. So enter your password for easy sigin and logout from your wallet.
Dashboard : Dashboard pops up after login which shows your account page with account name (“Account 1” by default), and ETH address( 0x9E713...). You can switch to different accounts
Networks : By default, MetaMask will connect to the main network. Other choices are public testnets(kovan, rinkeby, ropsten), any Ethereum node, private blockchains on your own computer (localhost 8545), or custom RPC. You can switch to different networks. Accounts will remain same on all networks. Available balance will be updated according to network selected.
Faucet : faucet.metamask.io is a fake ether faucet that will allow you to get test ethers that you can use on test networks.
- Integration : MetaMask integrates Ethereum-enabled web pages with your MetaMask wallet to send transactions and also populates your account address if web page requests it.
- Transaction History : You can view transaction history of any account you selected below the balance.
- Send Ether : Click green send button, then enter the recipient address. You can select max gas and hit confirm. Boom transaction signed and sent to selected ethererum blockchain network.
Difference Between EOAs vs contracts
Externally owned accounts are those that have a private key. Other type of account is a contract account that does not have a private key.
EOAs | Contract Account |
EOAs have a private key i.e having control over access to funds or contracts. | They do not have a private key. |
EOA cant have contract code | These accounts have smart contract code |
Owned and controlled by user using private key | Owned and controlled by logic of its SCs code and executed by EVM. |
Can initite a transaction | Cannot initate a transaction due to no private key but can call other contracts building complex execution paths. |
Addresses are created off-chain | Address is created on-chain by EVMs |
- Just like EOAs, Contracts can also send and receive ether.
- When a transaction destination is a contract address, it causes that contract to run in the EVM.
- Transactions can call functions within contracts with parameters to pass to that function.
- A EOAs initates a contract creation transaction which returns address of the contract account.
This is a special transaction known as "registering a contract" whose destination address is 0x0000000..(zero address) as before contract creation we do not have contract address.
- From then we can call a function of that contract by passing destination address as contract address and function name and parameters in transaction's data field.
A Simple Contract: A Test Ether Faucet
Ethereum has many different high-level languages to write a contract but by far the dominant choice is Solidity. Solidity was created by Dr. Gavin Wood, co-founder of Ethererum.
Lets see a simple Solidity contract and try to understand it. We will create a faucet contract that gives out ether to any address that asks, and can be refilled periodically.
// declares a contract object, similar to a class declaration
contract Faucet {
// This function take unsigned integer withdraw_amount
// as argument and its task is to Give out requested ether
function withdraw(uint withdraw_amount) public {
// This line test a precondition(withdraw_amount should be less than 0.1 ether)
// if fails resulting in contract execution to stop and fail with an exception.
require(withdraw_amount <= 100000000000000000);
// msg is a global object attached directly by EVMs.
// msg.sender is the sender address who called this function.
// transfer is a built-in function that transfers ether from the current contract
//to the address of the sender.
msg.sender.transfer(withdraw_amount);
}
// fallback or un-named function usually used to accept ether into the conrtract
// and is called if the transaction didn’t contain any data.
// Defined as public(anybody can call) and payable(it accepts ether) function
function () public payable {}
}
Using remix IDE you can quickly compile this contract and deploy it on any chain using metamask wallet or remix's internal private network.
EVM Bytecodes & ABI
Compilation process generates EVM bytecode and ABI of a contract.
Bytecodes : Kind of Machine code for EVMs to execute
Example : PUSH1 0x60 PUSH1 0x40 MSTORE CALLVALUE ISZERO PUSH2 0xF JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH1 0xE5 DUP1 PUSH2 0x1D PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN STOP PUSH1 0x60 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH1 0x3F JUMPI PUSH1 0x0 CALLDATALOAD PUSH29
ABI : ABI stands for Application Binary Interface. Its an Array of Objects that contains all function's information(rules and metadata) defined in contract .
If you want to call a contract's function from your dApp, you need a way to call the correct bytecode in the contract. So Web3 Libraries uses ABI to encode and decode the human readable functions into bytecode.
Internal Transactions
Transactions originating from the contract code are called internal transactions.
For example Bob initiated a transaction which called a function of a contract and inside that function it then sends ether to Alice. Then the ether transfer to Alice is a internal transaction. Because original one was which the Bob triggered.
Conclusion
So thats it for this episode. We created our first contract with Solidity. Hurray !!
Go and play around with Metamask and Remix IDE. Create your cool contracts and showcase your hard work and do tag me @ jakharnish on twitter.
Stay Tuned for other episodes. :)