LoginSignup
4
1

More than 1 year has passed since last update.

Flutter + Web3dart で dAppの作成

Last updated at Posted at 2019-06-11

はじめに

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の下に配置しておく

スクリーンショット 2020-04-08 18.36.28.png

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();
  }
4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1