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 ? ● Scalable Layer 2 scalability solution ● Free transactions ⛔⛽ ● ● Off-chain transactions (and more) ● Instant ⚡ Commit-chain, non-custodial side-chain ● Private (Upcoming) ● ➡ Secure transactions, off-the-blockchain ● Blockchain agnostique And still trustless.. ● ➡ Better user experience
NOCUST - Introduction The drawbacks ? No smart contracts (transactions and atomic swaps only) ● ● Online presence requirements Architecturally centralised (But non-custodian!) ●
NOCUST - Overview
NOCUST - How does it work ?
NOCUST - Instant finality Insurance pool ● ● Collateral re-usable each round TX can be accepted instantly ●
NOCUST - Account based data structure
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 ●
NOCUST - SDK, hands on ! Doc: http://docs.liquidity.network Github: https://github.com/liquidity-network
NOCUST - Install and setup Install the library from NPM: > npm install nocust-client Install required dependencies: > npm install web3@1.0.0-beta.37 bignumber.js
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
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, operatorApiUrl: 'https://rinkeby.liquidity.network/', contractAddress: '0x66b26B6CeA8557D6d209B33A30D69C11B0993a3a', });
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
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 ●
NOCUST - Get Bob’s ofg-chain balance // Off-chain Ether balance const bobBalance = await nocustManager.getNOCUSTBalance(bob)
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 🔦 🚁
NOCUST - Withdrawals (Request) // 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 ); ● 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
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 ●
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.
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
NOCUST - Exercise, in-browser wallet Clone the repo and follow the instructions at: https://github.com/liquidity-network/nocust-exercise
Recommend
More recommend