Build a scalable dApp with the Liquidity Javascript SDK Guillaume - - PowerPoint PPT Presentation

build a scalable dapp with the liquidity javascript sdk
SMART_READER_LITE
LIVE PREVIEW

Build a scalable dApp with the Liquidity Javascript SDK Guillaume - - PowerPoint PPT Presentation

Build a scalable dApp with the Liquidity Javascript SDK Guillaume Felley - BDLT summer school 2019 NOCUST - Outline 1. Introduction 2. Presentation of the NOCUST SDK 3. Exercise NOCUST - Introduction What is it ? The benefits ?


slide-1
SLIDE 1

Build a scalable dApp with the Liquidity Javascript SDK

Guillaume Felley - BDLT summer school 2019

slide-2
SLIDE 2

NOCUST - Outline

1. Introduction 2. Presentation of the NOCUST SDK 3. Exercise

slide-3
SLIDE 3

NOCUST - Introduction

What is it ?

  • Layer 2 scalability solution
  • Off-chain transactions (and more)
  • Commit-chain, non-custodial side-chain

➡ Secure transactions, off-the-blockchain The benefits ?

  • Scalable
  • Free transactions ⛔⛽
  • Instant ⚡
  • Private (Upcoming)
  • Blockchain agnostique
  • And still trustless..

➡ Better user experience

slide-4
SLIDE 4

NOCUST - Introduction

The drawbacks ?

  • No smart contracts (transactions and atomic swaps only)
  • Online presence requirements
  • Architecturally centralised (But non-custodian!)
slide-5
SLIDE 5

NOCUST - Overview

slide-6
SLIDE 6

NOCUST - How does it work ?

slide-7
SLIDE 7

NOCUST - Instant finality

  • Insurance pool
  • Collateral re-usable each round
  • TX can be accepted instantly
slide-8
SLIDE 8

NOCUST - Account based data structure

slide-9
SLIDE 9

NOCUST - Registration

  • Ethereum addresses need to register with the commit-chain
  • Off-chain (= POST request)
  • ⚠ The recipient of a transaction need to have register to receive funds
slide-10
SLIDE 10

NOCUST - SDK, hands on !

Doc: Github: http://docs.liquidity.network https://github.com/liquidity-network

slide-11
SLIDE 11

NOCUST - Install and setup

> npm install nocust-client > npm install web3@1.0.0-beta.37 bignumber.js

Install the library from NPM: Install required dependencies:

slide-12
SLIDE 12

NOCUST - Choose your hub

Ethereum Mainnet NOCUST smart-contract address (contractAddress) 0x83aFD697144408C344ce2271Ce16F33A74b3d98b Hub API URL (hubApiUrl) https://public.liquidity.network/ LQD ERC-20 contract address 0xD29F0b5b3F50b07Fe9a9511F7d86F4f4bAc3f8c4 Rinkeby Testnet NOCUST smart-contract address (contractAddress) 0x66b26B6CeA8557D6d209B33A30D69C11B0993a3a Hub API URL (hubApiUrl) https://rinkeby.liquidity.network/ Test ERC-20 contract address 0xA9F86DD014C001Acd72d5b25831f94FaCfb48717 Limbo test commit-chain NOCUST smart-contract address (contractAddress) 0x9561C133DD8580860B6b7E504bC5Aa500f0f06a7 Hub API URL (hubApiUrl) https://limbo.liquidity.network/ Test ERC-20 contract address 0xe982E462b094850F12AF94d21D470e21bE9D0E9C Ethereum node RPC URL https://limbo.liquidity.network/ethrpc

slide-13
SLIDE 13

NOCUST - setup

const Web3 = require('web3') // Web3 1.0.0-beta.37 only for now const BigNumber = require('bignumber.js') const { NOCUSTManager } = require('nocust-client') // Setup web3 with Infura const web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/')); // Add bob’s private key to web3 instance for signing transactions const bobPrivateKey = '0xf6a0b0f4fa95051d0d58582d2a4182f02c602d1e54c6a9e4dbed07faa51e0385' const bob = web3.eth.accounts.privateKeyToAccount(bobPrivateKey).address web3.eth.accounts.wallet.add(bobPrivateKey) // Main nocust manager object const nocustManager = new NOCUSTManager({ rpcApi: web3,

  • peratorApiUrl: 'https://rinkeby.liquidity.network/',

contractAddress: '0x66b26B6CeA8557D6d209B33A30D69C11B0993a3a', });

slide-14
SLIDE 14

NOCUST - Registration

// Register Bob's public key await nocustManager.registerAddress(bob)

  • Adds Bob’s leaf into the Merkle tree/off-chain ledger
  • Fully off-chain operation (No blockchain transaction will be sent)
  • Registration signs a message with Bob’s private key
slide-15
SLIDE 15

NOCUST - Deposit

// Deposit ETH -> fETH await nocustManager.deposit( bob, // Depositor "100000000000000000", // Amount to deposit (in wei) "6000000000", // Gas price (in wei) 200000 // Gas limit )

  • It will make a blockchain transaction (on-chain!)
  • Deposit needs 20 blocks confirmation to be credited off-chain
slide-16
SLIDE 16

NOCUST - Get Bob’s ofg-chain balance

// Off-chain Ether balance const bobBalance = await nocustManager.getNOCUSTBalance(bob)

slide-17
SLIDE 17

NOCUST - Send an ofg-chain transaction

const txId = await nocustManager.sendTransaction({ to: alice, amount: '10', // in wei from: bob, });

  • ⚠ The recipient needs to have registered in the past !
  • Automatically signs the required messages under the hood
  • No transaction fees, instant 🔦 🚁
slide-18
SLIDE 18

NOCUST - Withdrawals (Request)

  • A withdrawal is a 2 step process (Request + confirmation)
  • It takes some time, between 36h and 72h
  • Freshly received off-chain funds are not immediately available

// First check how much is avaialbe to withdraw (!= balance) const withdrawalLimit = nocustManager.getWithdrawalLimit(bob) // Send the withdrawal request const transactionHash = await nocustManager.withdrawalRequest( bob, // Account from which to make the withdrawal withdrawalLimit, // Amount to withdraw '10000000000', // Gas price, 10 Gwei 300000 // Gas Limit );

slide-19
SLIDE 19

NOCUST - Withdrawals (Confimration)

if((await nocustManager.getBlocksToWithdrawalConfirmation(bob)) === 0) { const transactionHash = await nocustManager.withdrawalConfirmation( bob, // Account from which to make the withdrawal '1000000000', // Gas price, 10 Gwei 300000 // Gas Limit ); }

  • To call after clearing period (Between 36h and 72h)
  • Effectively transfer the funds on-chain to Bob’s address
slide-20
SLIDE 20

NOCUST - ERC-20 support

const daiTokenAddress = '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359' // Register Bob's public key for off-chain DAI await nocustManager.registerAddress(bob, daiTokenAddress) // DAI off-chain transfer const txId = await nocustManager.sendTransaction({ to: alice, amount: '10', // in wei from: faucet, token: daiTokenAddress, }); // Off-chain DAI balance const bobBalance = await nocustManager.getNOCUSTBalance(bob, daiTokenAddress)

  • ⚠ Token needs to have been whitelist by the operator for use.
slide-21
SLIDE 21

NOCUST - Exercise, in-browser wallet

1. Generate key pairs for Alice and Bob 2. Register the addresses with the commit-chain 3. Send funds from Bob to Alice

slide-22
SLIDE 22

NOCUST - Exercise, in-browser wallet

Clone the repo and follow the instructions at: https://github.com/liquidity-network/nocust-exercise