Loom Network - Develop and deploy a smart contract
Loom Network is a Layer 2 scaling solution for Ethereum focusing on social and gaming dApps that require a very high throughput. Loom SDK enables to generate a sidechain called dAppChain using a dPoS consensus optimised for high-scalability. Loom is contributing on Plasma which is a mechanism to securely transfer a digital asset (ERC20 or ERC721) to a mainchain.
You can run your own DAppChain using the Loom software or connect to a public one (plasma-chain, social-chain or gaming-chain) running under a dPoS Ethereum blockchain to enable scalable dApps.
For this tutorial, we will create and fund a Loom account, deploy a contract on the External Dev Plasma network called extdev and interact with it from a frontend application.
Step 1 - Install Loom on your machine and fund an account
In the first, we will install Loom software on your machine to generate a keypair and fund the account with the Loom faucet.
- Download the executable
1 | $ wget https://private.delegatecall.com/loom/osx/stable/loom |
- Generate a key pair
1 | $ ./loom genkey -k priv_key -a pub_key |
The keypair is created under the same folder.
Go to the Loom Faucet
Enter your address generated above
Click on Request and wait until you see 100 faucet-karma
Step 2: Deploy a contract on the Loom extdev network
The second step consists in writing a very simple Smart Contract in Solidity and deploy it on the Loom extdev network using Truffle framework.
- Install Truffle on your machine
1 | $ npm install -g truffle |
- Initialise your Truffle project
1 | $ truffle init |
Let’s now write a simple Smart Contract that increments and store a counter
Create a file in
contracts/Counter.sol
and paste the following Solidity code
1 | pragma solidity ^0.4.20; |
You can verify that your code compiles correctly with the following command $ truffle compile
Now, we need to create a deployment script
Create a file in
migrations/2_deploy_contracts.js
and paste the following code
1 | var Counter = artifacts.require("./Counter.sol"); |
Finally we need to configure the connection to the Loom extdev network as well as our wallet info
Install the following JavaScript dependencies:
- truffle-hdwallet-provider enables to sign transactions for addresses derived from a 12-word mnemonic
- loom-truffle-provider is an adapter that allows Truffle Suite to communicate with Loom DappChain
- dotenv is a module to configure environment variables
1 | $ npm install truffle-hdwallet-provider loom-truffle-provider dotenv --save-dev |
Copy your private key from the file previously generated (priv_key)
Create a
.env
in the Truffle project and copy the private key like this
1 | PRIVATE_KEY="0kwCi...iWNNw==" |
- Open the file
truffle.js
and add the following configuration
1 | require('dotenv').config(); |
- Run the deployment to the Loom extdev network
1 | $ truffle migrate --network extdev |
Step 3: Interact with the contract from the web app
In the next step, we will develop a simplistic dApp using React, Web3 and Loom-js to interact with the Smart Contract previously deployed on the Loom extdev network
- Initialise a React project
1 | $ npx create-react-app frontend |
- Install the necessary dependencies
- web3 (1.0.0-beta.34) This is the Ethereum compatible JavaScript API which implements the Generic JSON RPC spec
- loom-js is a JavaScript library for building browser apps & NodeJS services that interact with Loom
1 | $ npm install [email protected] loom-js --save |
- Edit
frontend/package.json
and add the following line under the scripts section to access the Truffle contract artifacts from the webapp
1 | "link-contracts": "run-script-os", |
The full code of this file is available here: package.json
- Open an edit
frontend/src/App.js
- Imports
1 | import Web3 from 'web3' |
- Connect to the Loom network with web3 and loom-js
1 | // Read the user private key (from browser storage or input) |
- Use web3 to load the smart contract Truffle artifacts
1 |
|
- Interaction with the smart contract
1 | // Get the counter value |
The full code of this file is available here: App.js
- Create a link to the Truffle JSON artefacts
1 | $ npm run link-contracts:linux:darwin |
- Start the webserver
1 | $ npm start |
- Result
After copy-pasting our private key in the input field, the application is able to connect to the Loom extdev node to interact with the contract (read and increment the counter).
Notes
Key Management: There is currently no wallet (such as Metamask) with Loom network, the enduser has to handle the private key manually (browser storage) and the dAppChain provider must most likely provide a way to recover a private key.
You can generate and manipulate a private key using loom-js like this:
1 | // Generate a private key |
karma : is a ERC20 token, sybil resistant, that can be used to prevent spam attack via a reputation-based transaction limiting system.
Links and resources
extdev
- Network ID: extdev-plasma-us1
- Faucet: https://faucet.dappchains.com/
- Documentation: https://loomx.io/developers/docs/en/phaser-sdk-demo-websocket.html
Code: https://github.com/gjeanmart/kauri-content/tree/master/loom_tutorial_extdev_part1