Ethereum Providers

Using Ethers.js or Web3.js with the Wepin Provider allows you to interact with EVM-compatible blockchain networks.

Supported Networks

Is the blockchain you need not listed? Please contact the Wepin team for blockchain support.

Initialize Web3

First, initialize Wepin, then obtain the provider.

const appId = 'app_id_eg12sf3491azgs520' // Test App ID
const appKey = 'ak_test_ghq1D5s1sfG234sbnhdsw24mnovk313' // Test App Key
const attributes = {
	type: 'show'
}
// Initialize Wepin
await Wepin.init(appId, appKey, attributes)
// Obtain the provider from the initialized Wepin.
const provider = Wepin.getProvider({ network: 'ethereum' });

Initialize Web3 using web3.js

import Web3 from 'web3'
const provider = Wepin.getProvider({ network: 'ethereum' })
const web3 = new Web3(provider)

Initialize Web3 using ethers.js

Please refer to: ethers.js for React native

import "react-native-get-random-values"
// Import the the ethers shims (**BEFORE** ethers)
import "@ethersproject/shims"
// Import the ethers library
import { ethers } from "ethers"
const provider = wepin.getProvider({ network: 'ethereum' })
const web3 = new ethers.providers.Web3Provider(provider)

Methods

Get Accounts

Through the initialized web3, you can retrieve account information.

const accounts = await web3.eth.getAccounts()

Get Balance

Using the account information, you can query the balance.

const balance = await web3.eth.getBalance(accounts[0])

You can refer to the link below to query not only the balance but also fee information, block number, etc.

Send Transaction

You can send transactions.

const accounts = await web3.eth.getAccounts()
const tx = {
    from: accounts[0],
    gasPrice: "2000000000",
    gas: "21000",
    to: '0x11f4d0A3c1......13F7E19D048276DAe',
    value: "10000000000000000",
}
const response = await web3.eth.sendTransaction(tx)

Contract Call

You can perform contract calls.

const callObject = {
	to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe', //contract address
	data: '0xc6888fa10000000000000000000000000000000000000000000000000000000000000003'
}
const response = await web3.eth.call(callObject)

For more details on the Ethereum-compatible network provider, please refer to the link below.

Last updated