Blockchain Development
Agenda
- Blockchain
- Ethereum
- Smart Contract
- Solidity
- Truffle & Ganache
- ERC20 Standard
- Create your own token
Blockchain
This is a blockchain
Git is also a blockchain
- Each commit has a unique hash, based on its content
- Each commit has a parent commit
- Each repo has its own copy of all the commits
Ethereum
You can have regular addresses, used by people
These addresses have a balance in ether that can transfer to any other address
Addresses
There's also contract addresses, created when you deploy a new contract
These addresses also have a balance, but they cannot initiate a tx by their own
They also contain code and memory to persist data
Transactions
Each Ethereum block contains transactions
A transactions is made basically by:
- from
- to
- value
- data
Transfer
- from the sender
- to the receiver
- value ether
- data empty
Contract Creation
- from the sender
- to empty
- value ether (if any)
- data contract code
Contract Invocation
- from the sender
- to the contract
- value ether (if any)
- data method with params
Gas
Each operation costs gas
Each transaction defines how much ether is willing to pay per gas consumed
Higher the price, the tx will be mined faster
Lower the price, the tx will be mined slower, or not mined at all
Smart Contract
- Can be written in any language, as long as it compiles to Ethereum bytecode
- Most popular languages are Solidity and Serpent (python)
- Contracts are the analog to classes in OOP
- Have their own space in memory to store their instance properties
- Have no access to the outside word, no network requests, no file system. Only data in blockchain can be consumed
- Contracts cannot initiate actions by their own
- The code cannot be modified
- Cannot be destroyed from the outside
- Have limited execution stack
Solidity
import { Company } from './company.sol';
contract Employee {
enum Roles { frontend, backend, scrum }
struct Skill {
string name;
int8 level;
}
uint public salary;
uint8 private age;
string name;
boolean active;
Company company;
address account;
Employee[] bosses;
Skills[] skills;
Roles role;
function Employee(address companyAddress, address employeeAddress) public {
salary = 100;
age = 26;
name = 'Diego';
active = true;
company = Company(companyAddress);
account = employeeAddress;
role = Roles.frontend;
}
}
contract Employee {
string public name;
string lastName;
uint8 age;
boolean updatedOnce = false;
function getFullName() constant public returns (string _name, string _lastName) {
_name = name;
_lastName = lastName;
}
function multiply(uint a, uint b) pure public returns (uint) {
return a * b;
}
modifier updateOnce() {
if (!updatedOnce) {
_;
}
updatedOnce = true;
}
modifier adult(uint8 _age) {
if (_age > 18) {
_;
}
}
function setAge(uint8 _age) adult(_age) updateOnce public {
age = _age;
}
}
Truffle & Ganache
npm install truffle ganache-cli
npx truffle unbox metacoin
# or
./node_modules/.bin/truffle unbox metacoin
# Start the dev node
npx ganache
# Build the contracts
npx truffle build
# Run the unit tests
npx truffle test
# Run the console of the dev node
npx truffle console
Thanks!
Blockchain
By Diego Barahona
Blockchain
- 335