KEMBAR78
Working with Smart contracts in Javascript | PDF
Working with
smart contracts in JS
Mikhail Kuznetcov
Amsterdam 2018
About
Agenda
Introduction
Smart contracts and DApps
Developer tools and environment setup
Demo
Web3
Ethereum
Vitalik Buterin,
co-founder of Ethereum
● Open source
● Decentralised
● Features smart contract
functionality
Ethereum network
2
1
EVM
2 distinct types of accounts:
● User
addres, funds
● Contract
address, funds, code attached to it
What are Smart Contracts?
[..] A smart contract is a set of promises, specified
in digital form, including protocols within which
the parties perform on these promises.
Nick Szabo, 1996
Real world examples:
● Pension or trust fund
● Network democracy, voting
● Assets exchange
● Investments, ICOs
SC example
pragma solidity ^0.4.16;
import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
// declaration
contract TodoList {
TodoItem[] public todoItems;
// data objects
struct TodoItem {
bytes32 value;
bool active;
}
// methods
function addTodoItem(bytes32 _value) returns (bool success) {
TodoItem memory todoItem;
todoItem.value = _value;
todoItem.active = false;
todoItems.push(todoItem);
return true;
}
function toggleTodoItem(uint index) returns (bool success) {
if (index >= todoItems.length) return;
bool newValue = !todoItems[index].active;
todoItems[index].active = newValue;
return newValue;
}
Solidity PL
SC lifecycle
Solidity
code
Compile
ABI
Bytecode
Deploy
Address `0x56f…`
Interact
Functions
Events
Blockchain network
DApps stack
Blockchain
Ethereum platform
Smart contracts
DApps
DApp JS workflow with Web3
// init, using provide of your choice
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
//set defaults
web3.eth.defaultAccount = '0x8888f1f195afa192cfee860698000c030f4c9db1'
//do stuff
if(web3.isConnected() && web3.isAddress(SOME_ADDRES)) {
const balance = web3.eth.getBalance(SOME_ADDRES)
web3.eth.sendTransaction(TRANSACTION, onSuccess)
}
Web3
Connecting to the network
JS setup: Ganache CLI (former ethereumjs-testrpc)
Local test network as simple as:
$: npm install -g ganache-cli
$: ganache-cli <args>
… or use GUI runner
Truffle workflow
$: npm install -g truffle
$: mkdir myproject && cd myproject
$: truffle init
Blockchain
sol
sol
sol
Tuffle
Web3
$: truffle compile
$: truffle migrate [--reset]
$: truffle console
On contract change
Setup
DApp
ĐApp interaction with contract
Transaction
SC validation
Mining
Costs
New block
Verification and execution on
other nodes
Blockchain network
DApp UI interaction New status in UI
Demo
● Setup and deploy ToDo list app, based on blockchain and smart
contracts. https://github.com/mbeaudru/ethereum-todolist
● Extend functionality of a contract and app UI
Further steps
Oraclize.it
● IDE, Remix
● Collections of tested and reusable contract blocks
● External events triggers
● Various libs bindings
Questions!
QR code here

Working with Smart contracts in Javascript

  • 1.
    Working with smart contractsin JS Mikhail Kuznetcov Amsterdam 2018
  • 2.
  • 3.
    Agenda Introduction Smart contracts andDApps Developer tools and environment setup Demo Web3
  • 4.
    Ethereum Vitalik Buterin, co-founder ofEthereum ● Open source ● Decentralised ● Features smart contract functionality
  • 5.
    Ethereum network 2 1 EVM 2 distincttypes of accounts: ● User addres, funds ● Contract address, funds, code attached to it
  • 6.
    What are SmartContracts? [..] A smart contract is a set of promises, specified in digital form, including protocols within which the parties perform on these promises. Nick Szabo, 1996 Real world examples: ● Pension or trust fund ● Network democracy, voting ● Assets exchange ● Investments, ICOs
  • 7.
    SC example pragma solidity^0.4.16; import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol'; // declaration contract TodoList { TodoItem[] public todoItems; // data objects struct TodoItem { bytes32 value; bool active; } // methods function addTodoItem(bytes32 _value) returns (bool success) { TodoItem memory todoItem; todoItem.value = _value; todoItem.active = false; todoItems.push(todoItem); return true; } function toggleTodoItem(uint index) returns (bool success) { if (index >= todoItems.length) return; bool newValue = !todoItems[index].active; todoItems[index].active = newValue; return newValue; } Solidity PL
  • 8.
  • 9.
  • 10.
    DApp JS workflowwith Web3 // init, using provide of your choice web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")) //set defaults web3.eth.defaultAccount = '0x8888f1f195afa192cfee860698000c030f4c9db1' //do stuff if(web3.isConnected() && web3.isAddress(SOME_ADDRES)) { const balance = web3.eth.getBalance(SOME_ADDRES) web3.eth.sendTransaction(TRANSACTION, onSuccess) } Web3
  • 11.
    Connecting to thenetwork JS setup: Ganache CLI (former ethereumjs-testrpc) Local test network as simple as: $: npm install -g ganache-cli $: ganache-cli <args> … or use GUI runner
  • 12.
    Truffle workflow $: npminstall -g truffle $: mkdir myproject && cd myproject $: truffle init Blockchain sol sol sol Tuffle Web3 $: truffle compile $: truffle migrate [--reset] $: truffle console On contract change Setup DApp
  • 13.
    ĐApp interaction withcontract Transaction SC validation Mining Costs New block Verification and execution on other nodes Blockchain network DApp UI interaction New status in UI
  • 14.
    Demo ● Setup anddeploy ToDo list app, based on blockchain and smart contracts. https://github.com/mbeaudru/ethereum-todolist ● Extend functionality of a contract and app UI
  • 15.
    Further steps Oraclize.it ● IDE,Remix ● Collections of tested and reusable contract blocks ● External events triggers ● Various libs bindings
  • 16.