はじめに
SmartContractをアプリから制御するために、iOSはWeb3.swift、Androidなら、web3jが用意されており、これらを用いることで、Walletの作成やContractのメソッドをcallすることができる。
今回は、iOSとAndroid両対応が必要で、Flutterを利用したかったので、flutterに対応したweb3モジュール、Web3dartを用いた覚書。
web3dart
実装
準備
1.bwewインストール
https://brew.sh/index_ja
の手順に従う
$brew update
2.flutterインストール
https://flutter.dev/docs/get-started/install/macos
からダウンロード
3.cocoapodのインストール(必要な場合)
$brew install cocoapods
$pod setup
$sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
$sudo xcodebuild -runFirstLaunch
4.cloud fire storeを探す(必要な場合)
https://pub.dev/packages/cloud_firestore/versions
installingの手順に従って、
subspec.yaml--------
dependencies:
cloud_firestore: ^0.13.4+2
--------
$ flutter pub get
5.他に漏れがないかチェック
$flutter doctor
6.app作成
$ flutter create web3dart_sample
$ flutter devices
$ flutter run
vim pubspec.yamにweb3dartとethereumを追記(必要な場合)
> ethereum: ^3.1.0
> web3dart: ^0.4.1
$ flutter pub get
$ flutter run (flutter packages get)
firebaseを使う場合はRunner.xcworkspace をxcodeでh李相手、
GoogleService-info.plistをRunnerの下に配置しておく

serviceの作成とviewからcall
class DemoContractService {
Credentials credentials;
DeployedContract contract;
Web3Client client;
Future<DemoContractService> init() async {
var httpClient = new Client();
client = new Web3Client(INFURA_URL, httpClient);
client.printErrors = true;
credentials = Credentials.fromPrivateKeyHex(ETH_PRIVATE_KEY);
var contractABI = ContractABI.parseFromJSON(Abi, "");
contract = new DeployedContract(
contractABI,
new EthereumAddress(CAPSULE_ESCROW_CONTRACT_ADDRESS),
client,
credentials);
return this;
}
ContractFunction func(String fName) {
return contract.findFunctionsByName(fName).first;
}
getRandNum() async {
print("getRandNum");
var resp = await new Transaction(keys: credentials, maximumGas: MAX_GAS)
.prepareForCall(contract, func("random100"),[]).call(client);
var res = resp.toString();
print("rand num is: $res");
}
}
Future<List<int>> pushRandButton() async {
var c = await DemoContractService().init();
c.getRandNum();
}