Ivan Voras
Ivan Voras is a freelancer and entrepreneur, proud of the breadth of the projects under his belt, and those span from Bitcoin and blockchain technologies, to hardware solutions for Internet of Things devices.
Toptal academy Blockchain lectures #6
2018-03-12
ivan.voras@toptal.com
Intended use:
They can also use other features / interfaces such as:
Basically, "let's re-invent everything, on the Blockchain."
This will create a new development blockchain just for you, disconnected from the network, with a huge number of Ether in a "default" address, and also create you a new address for testing.
The password for the big account is the empty string.
Note: in dev mode, blocks are mined only if there are txs ready to be included in a new block, to conserve CPU time and disk space for empty blocks. This is what "block sealing failed" means.
This will connect Mist with the geth instance running the private development blockchain.
On Windows, I've had bad luck doing it with IPC, this is why I'm using JSON-RPC in the example above.
You should test this setup by sending some Ether back and forth. In dev mode, blocks are mined only if there are txs ready.
The Remix IDE is an "DAPP", a front-end-only, JS-based application using the Ethereum blockchain for everything.
It can optionally (i.e. if started in a browser without a connection to a running node) emulate a blockchain,
for testing & debugging purposes only.
Note what's going on in geth, and the increase in block count in mist.
pragma solidity ^0.4.21;
contract HelloWorld {
uint256 count = 0; // stored in the blockchain!
// inc() and dec() change the state of the blockchain, i.e. cost gas
function inc() public {
count++;
}
function dec() public {
count--;
}
// a read-only function, doesn't change the blockchain, free
function get() public view returns (uint256) {
return count;
}
}
pragma solidity ^0.4.21;
contract HelloWorld {
// declaration of an "event" (or log message) outputted by the contract
event PowerOf10(uint256 which);
uint256 count = 0;
function _check() internal {
if (count % 10 == 0) {
emit PowerOf10(count); // emits the event / log
}
if (count % 13 == 0) { revert(); } // fail the tx
}
function inc() public {
count++;
_check();
}
function dec() public {
count--;
_check();
}
function get() public view returns (uint256) {
return count;
}
// "default" or "fallback" handler, receives payments to the contract address
function () public payable {
count += msg.value;
_check();
}
}
pragma solidity ^0.4.21;
import "github.com/OpenZeppelin/zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
contract HelloToken is StandardToken {
string public name = "HelloToken";
string public symbol = "HT";
uint8 public decimals = 4;
uint public INITIAL_SUPPLY = 1000 * (10 ** uint(decimals));
// constructor
function HelloToken() public {
totalSupply_ = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}
}
This is a complete ERC20 token contract.
You can use its address in Mist to monitor the number of tokens you have, use it in "smarter" wallets to transfer tokens, etc.
As the owner, you can send the tokens anywhere with .transfer()
"database" of who has how many tokens
Homework
Create your own token, using Remix with Mist. Name the token by including your own name, deploy it on the private network, transfer some token values between accounts.
Like Solidity, but inspired by Python... so if you like Python's syntax more, it may be a good solution. It is somewhat inspired by the older "Serpent" whose future is uncertain.
OTOH, Vyper is currently considered "experimental".
@public
def transferFrom(_from: address, _to: address, _value: int128(uint256)) -> bool:
if _value <= self.allowed[_from][msg.sender] and \
_value <= self.balances[_from]:
self.balances[_from] -= _value # decrease balance of from address.
self.allowed[_from][msg.sender] -= _value # decrease allowance.
self.balances[_to] += _value # incease balance of to address.
log.Transfer(_from, _to, convert(_value, 'uint256')) # log transfer event.
return True
else:
return False
NodeJS / npm based environment.
npm install truffle
To start a new project:
mkdir ProjectName
cd ProjectName
truffle init
npm install zeppelin-solidity
In truffle.js, add the dev network:
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*", // Match any network id
gas: 5000000 // give enough gas for contracts
}
}
};
We'll use the same example HelloToken, placed in the
"contracts" folder. Note the different path for the import, and the different compiler version!
pragma solidity ^0.4.19;
import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
contract HelloToken is StandardToken {
string public name = "HelloToken";
string public symbol = "HT";
uint8 public decimals = 4;
uint public INITIAL_SUPPLY = 1000 * (10 ** uint(decimals));
// constructor
function HelloToken() public {
totalSupply_ = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}
}
In the project directory, run:
truffle compile
(if you're running Windows, it might try to load truffle.js from the current directory; if so, go to
C:\Users\USERNAME\AppData\Roaming\npm
and make a copy of truffle.bat to truf.bat or something similar, then use "truf" instead of "truffle).
In the project directory, run:
truffle deploy
The result should be something like:
E:\TruffleExample>truf migrate
Using network 'development'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0xeaa8b652d6245a37c4cfcab05aef12aa96c06f0756872ff604ee9a724f1aa2bc
Migrations: 0x8fc64ca3bf5d7530c1a54493fe0d69444cb70e96
Saving successful migration to network...
... 0x1580562d92f3c536ea143537579e21b584a938039bd7b61196fae85e231e39c7
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying HelloToken...
... 0x08db67ee2a56ac6d1eff528cd7102d3fabe68c51990803b82535c097e1629fbf
HelloToken: 0xb6f65e6702539c2ecfe3350bfb362fc565818427
Saving successful migration to network...
... 0x9fb54fbe4c870a8fe7c35e1d32561aa4971f84d9fdf4fa6a527fb428f76c9cc3
Saving artifacts...
Contract address
"Zeppelin Solutions" is one of the earliest companies doing smart contracts professionally. Its library "OpenZeppelin" is one of the best and most commonly used for creating smart contracts.
https://github.com/OpenZeppelin
In the same sense of "don't invent your own cryptography", it is best to use established, tested solutions. There are many pitfalls and unexpected interactions which this library solves.
ivan.voras@toptal.com
Blockchain lecture #6: Smart Contracts
March 2018
By Ivan Voras
Introduction into smart contract and their relationship to blockchain. High level overview of the major smart contract platforms and where to get started with them. Bit more details about Ethereum virtual machine (EVM) and Solidity.
Ivan Voras is a freelancer and entrepreneur, proud of the breadth of the projects under his belt, and those span from Bitcoin and blockchain technologies, to hardware solutions for Internet of Things devices.