Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Flutter + Web3dart で dAppの作成

はじめに

SmartContractをアプリから制御するために、iOSはWeb3.swift、Androidなら、web3jが用意されており、これらを用いることで、Walletの作成やContractのメソッドをcallすることができる。
今回は、iOSとAndroid両対応が必要で、Flutterを利用したかったので、flutterに対応したweb3モジュール、Web3dartを用いた覚書。

web3dart

https://github.com/simolus3/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();
  }
oggata
iPhoneゲームの制作をしています。3Dで描かれた動物をモチーフにしたパズルゲーム、ネコダンなど。愛車HARLEY-DAVIDSON-XL1200R. http://oggata.tumblr.com
http://oggata.tumblr.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away