- Expoのアプリを作成
expo init
- node-libs-browser をインストール
npm install --save node-libs-browser
- rn-cli.config.js を作成し、以下のように記述する
const nodeLibs = require('node-libs-browser');
module.exports = {
resolver: {
extraNodeModules: nodeLibs
}
};
- global.js を作成し、以下のように記述する
// Inject node globals into React Native global scope.
global.Buffer = require('buffer').Buffer;
global.process = require('process');
if (typeof btoa === 'undefined') {
global.btoa = function (str) {
return new Buffer(str, 'binary').toString('base64');
};
}
if (typeof atob === 'undefined') {
global.atob = function (b64Encoded) {
return new Buffer(b64Encoded, 'base64').toString('binary');
};
}
- App.js ファイル内で、global.js をインポートする
import './global';
npm で install web3.js ライブラリをインストール
npm install --save web3
App.js 内で Web3 ライブラリーを使用可能に
const Web3 = require('web3');
- 使用例: 最新のブロックを取得
componentWillMount() {
const web3 = new Web3(
new Web3.providers.HttpProvider('https://mainnet.infura.io/')
);
web3.eth.getBlock('latest').then(console.log)
}
最新のブロック高を取得
web3.eth.getBlockNumber().then(console.log)
npx create-expo-app my-app
cd my-app
npx expo start # アプリ開発用サーバーの起動
- Expo (React Native) and Web3.js
https://github.com/abcoathup/expo-web3/blob/master/README.md
- Web3.js のドキュメント(web3.ethの使い方)
https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html
- Expo: Initializing the project https://docs.expo.dev/get-started/create-a-new-app/