Integrate Web3Auth with the Metis Blockchain in React Native
While using the Web3Auth React Native SDK, you get a
EIP1193
provider, similar to the
Metamask Provider. This provider can be
used with libraries like web3.js
,
ethers.js
etc. to make
Metis Blockchain blockchain calls like getting the user's account
, fetching
balance
, signing transactions, sending transactions, and interacting with smart contracts. We have
highlighted a few key examples to get you started quickly on that.
Installation
To interact with an EVM blockchain in React Native, you can use any EIP1193
compatible package like
web3.js
, ethers.js
etc.
In this reference, we're using ethers.js
to demonstrate how to make blockchain calls using it with Web3Auth.
- Install the
ethers.js
package usingnpm
oryarn
:
npm install @ethersproject/shims ethers
- Import the packages and shims into your codebase:
// Import the required shims
import "@ethersproject/shims";
// Import the ethers library
import { ethers } from "ethers";
We have followed this guide to set up the ethers.js
package in React
Native.
Initializing Provider
Using eip155
as chainNamespace
while initializing web3auth
will provide an
EIP1193
compatible provider as web3auth.provider
after successful authentication.
Getting the chainConfig
- Mainnet
- Testnet
const chainConfig = {
chainNamespace: ChainNamespace.EIP155,
chainId: "0x440", // hex of 1088, Metis Andromeda Mainnet
rpcTarget: "https://andromeda.metis.io/?owner=1088",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Andromeda Mainnet",
blockExplorer: "https://andromeda-explorer.metis.io",
ticker: "METIS",
tickerName: "Metis",
};
const chainConfig = {
chainNamespace: ChainNamespace.EIP155,
chainId: "0xEA6E", // hex of 59902, Metis Sepolia Testnet
rpcTarget: "https://sepolia.metisdevops.link/",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Sepolia Testnet",
blockExplorer: "https://sepolia-explorer.metisdevops.link/",
ticker: "tMetis",
tickerName: "tMetis",
};
Initialize
- PnP React Native SDK
- SFA JS SDK
import * as WebBrowser from "@toruslabs/react-native-web-browser";
import EncryptedStorage from "react-native-encrypted-storage";
import Web3Auth, { LOGIN_PROVIDER, WEB3AUTH_NETWORK } from "@web3auth/react-native-sdk";
import "@ethersproject/shims";
import { ethers } from "ethers";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
const [provider, setProvider] = useState<IProvider | null>(null);
const ethereumPrivateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig,
},
});
const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, {
clientId,
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, // or other networks
});
await ethereumPrivateKeyProvider.setupProvider(web3auth.privKey);
setProvider(ethereumPrivateKeyProvider);
import React, {useEffect, useState} from 'react';
import '@ethersproject/shims';
import EncryptedStorage from 'react-native-encrypted-storage';
import {CHAIN_NAMESPACES, IProvider, WEB3AUTH_NETWORK} from '@web3auth/base';
// IMP START - Quick Start
import {Web3Auth, SDK_MODE, decodeToken} from '@web3auth/single-factor-auth';
import {EthereumPrivateKeyProvider} from '@web3auth/ethereum-provider';
// IMP END - Quick Start
import {ethers} from 'ethers';
// IMP START - Chain Config
const chainConfig = {
chainId: '0x1',
displayName: 'Ethereum Mainnet',
chainNamespace: CHAIN_NAMESPACES.EIP155,
tickerName: 'Ethereum',
ticker: 'ETH',
decimals: 18,
rpcTarget: 'https://rpc.ankr.com/eth',
blockExplorerUrl: 'https://etherscan.io',
logo: 'https://cryptologos.cc/logos/ethereum-eth-logo.png',
};
// IMP END - Chain Config
// IMP START - SDK Initialization
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {chainConfig},
});
const web3auth = new Web3Auth({
clientId, // Get your Client ID from Web3Auth Dashboard
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
privateKeyProvider,
storage: EncryptedStorage,
mode: SDK_MODE.REACT_NATIVE,
});
// IMP END - SDK Initialization
export default function App() {
const [provider, setProvider] = useState<IProvider | null>(null);
const [loggedIn, setLoggedIn] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const [userInfo, setUserInfo] = useState<string>('');
const [consoleUI, setConsoleUI] = useState<string>('');
useEffect(() => {
const init = async () => {
try {
// IMP START - SDK Initialization
await web3auth.init();
setProvider(web3auth.provider);
// IMP END - SDK Initialization
if (web3auth.connected) {
setLoggedIn(true);
}
} catch (error) {
uiConsole(error, 'mounted caught');
}
};
init();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Get User Info
You get the user information after a successful login is returned from the login
method. The userInfo
object contains the user information.
const info = web3auth.userInfo(); // User Information
Get Account
// For ethers v5
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
const ethersProvider = new ethers.BrowserProvider(provider!);
// For ethers v5
// const signer = ethersProvider.getSigner();
const signer = await ethersProvider.getSigner();
// Get user's Ethereum public address
const address = signer.getAddress();
uiConsole(address);
Get Balance
// For ethers v5
// const ethersProvider = new ethers.providers.Web3Provider(this.provider);
const ethersProvider = new ethers.BrowserProvider(provider!);
// For ethers v5
// const signer = ethersProvider.getSigner();
const signer = await ethersProvider.getSigner();
// Get user's Ethereum public address
const address = signer.getAddress();
// Get user's balance in ether
// For ethers v5
// const balance = ethers.utils.formatEther(
// await ethersProvider.getBalance(address) // Balance is in wei
// );
const balance = ethers.formatEther(
await ethersProvider.getBalance(address), // Balance is in wei
);