Independent Tech & Cryptocurrency News
Binance Coin

Blockchain dApps for mobile πŸ“± – Hacker Noon


You must have come across web apps that use truffle and some wallet integration to call smart contracts. But there are very few apps (specially cross-platform) in the market. I think this is because of lack of tutorials or rather excess thereof.

This is my attempt help some developers make a mobile app that connects to blockchain 🌈

This tutorial shows how to wrap and un-warp Ether token using the canonical-weth smart contract.


  1. Clone this repo git clone
  2. npm install or yarn
  3. Update constants.js file
  4. Voila! 🤷

Here’s the longer version;

Start by creating an empty vanilla react native app the old-fashioned way:

react-native init MyDecentralisedApp

Let’s go ahead 🔥 and make it happen!

npm i canonical-weth -S

This will give you the artifact to the weth smart contract. It contains the abi array and some descriptions of the compiled solidity code. You can learn about that stuff here (not necessary for this tutorial).

To make a Contract object out of it, we need the truffle library.
Install it using:

npm i truffle-contract -S

With this we also need web3. We are using web3 version 0.20.5 for simplicity, but you may also use web3 1.x.x. It just needs you to add a preset for es2015.

npm i web3@0.20.5 -S 

At this point building your app should give you error for some node libraries missing such as crypto or events. This happens because react-native does not have the nodejs modules by default. This has been mentioned in react-native issues. Even though it uses node as its package manager, it doesn’t contain its inbuilt modules.

READ  More Stable Than NASDAQ: 3 Factors That Could Wake Up Bitcoin Price

In order to solve it, we add node modules from the react-native-node-modules library

npm i abec/react-native-node-modules -S

This should solve your node issues 🚀. Moving on, lets make the truffle object.

var Contract = require('truffle-contract')
var wethArtifact = require('canonical-weth')
var contract = Contract(wethArtifact)
var contractAtAddress = contract.atAdd('0xc778417e063141139fce010982780140aa0cd5ab')

I got the above address from this article on weth. It is for rinkeby testnet chain. It is publicly located and can be seen on etherscan. To use any contract on the blockchain, you must find its address. Usually, you can just search on etherscan.

Now that we have our contract, we need to call this contract as a wallet on the same blockchain. You can think of this wallet as the user, and you are using this user to call the smart contract function. This is necessary on the blockchain every-time, every call has to be associated with an address.

In our scenario, we have a number of options:
 — Ethers Wallet

— Truffle HD Wallet

— Web3 Wallet Provider

You may chose any. In this example we are going to use Truffle HD Wallet.
Sample usage here. 
Lets install this as well:

npm i trufflehdwallet -S

Now we can create a new wallet on the chain or use an existing one. We are going to use an existing one. For this we need the RPC network address for the blockchain where the smart contract has been deployed.

Since we are using the address from Rinkeby testnet on Infura, so our RPC url would be

And we will use this url to create a provider object. This sets the “environment” for using the wallet. You can think of this as logging in. You log into your wallet with with password, the mnemonic.

const web3Provider = new HDWalletProvider(MNEMONIC,TESTRPC_ADDRESS);
web3 = new Web3(web3Provider);

Replace MNEMONIC with your own, in String format.

We are also set to call the smart contract now. Based on the definition of the weth smart contract that we are using, we know it has deposit and withdraw functions. These can be called by an async call to the contract or by using it as a Promise.

var transaction = await wethDeployed.deposit({ from: MY_ADDRESS, value: 0.001 * 1e18, gas: 2000000 });

The transaction variable will give you a lot of information about the place of your transaction in the blockchain. You can extract the address from it and go back to etherscan to find it.

🗿We made it! Thats all you need to make your own cross-platform dApp.

Get real time updates directly on you device, subscribe now.

Leave A Reply

Your email address will not be published.