Independent Tech & Cryptocurrency News
Bitcoin
$6,350.38
-13.77
(-0.22%)
Ethereum
$207.39
-2.12
(-1.01%)
Ripple
$0.51
-0.01
(-1.54%)
Litecoin
$49.66
-0.98
(-1.94%)
EOS
$5.31
-0.12
(-2.21%)
Cardano
$0.07
-0
(-3.24%)
Stellar
$0.25
-0.02
(-6.37%)
NEO
$15.51
-0.21
(-1.34%)
NEM
$0.11
-0.01
(-6.66%)
DigitalCash
$163.12
-1.63
(-0.99%)
Tether
$0.97
-0.01
(-0.59%)
Binance Coin
$9.20
QTUM
$3.67
-0.06
(-1.61%)
Verge
$0.01
-0
(-2.74%)
Ontology
$1.55
0
(0%)
ZCash
$129.49
-5.62
(-4.16%)
Steem
$0.72

Blockchain dApps for mobile πŸ“± – Hacker Noon

0


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.

TLDR; 🤓

  1. Clone this repo git clone https://github.com/kunsachdeva/React-Native-Blockchain-Skeleton
  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 rinkeby.infura.io.

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 rinkeby.infura.io with password, the mnemonic.

const TESTRPC_ADDRESS = 'https://rinkeby.infura.io/'
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 });
console.log(transaction)

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.



coindesk.com

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

Leave A Reply

Your email address will not be published.