パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v2.0.2

flutter_web3_provider

Flutter web web3 provider

30日間ダウンロード
80
いいね数
20
Pub ポイント
25

パッケージをインストール

flutter pub add flutter_web3_provider

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Flutterパッケージで、ウェブアプリケーション向けにWeb3技術との統合を可能にし、ユーザーがFlutterウェブアプリから直接イーサリアムベースのブロックチェーンとやり取りできるようにします。ブロックチェーン接続、アカウント状態、トランザクション処理を管理するためのプロバイダーパターンの実装を提供しています。ウェブプラットフォーム専用に設計されており、MetaMaskなどのウォレットへのアクセスを簡素化し、トランザクションの署名やスマートコントラクトデータの読み取りといった標準的なWeb3メソッドをサポートしています。このパッケージは軽量であり、Flutterの状態管理エコシステムとシームレスに連携するように構築されています。

用途

  • ウェブアプリでのイーサリアムウォレットへの接続
  • スマートコントラクトからデータを読み取る
  • オンチェーンでの取引署名
  • ユーザー財布アカウントの管理
  • 分散型アプリ(dApp)機能の有効化

主な機能

  • Web専用のweb3統合
  • Provider を使った状態管理
  • MetaMask および類似ウォレットのサポート
  • 一般的な web3 操作用のシンプルな API
  • 組み込みの接続および切断処理

適している対象

  • Flutter開発者がdAppsを構築しています
  • ウェブベースのブロックチェーンアプリケーション
  • ウォレット接続を必要とするプロジェクト
  • イーサリアム互換チェーンをターゲットにするユーザー
  • FlutterのリアクティブUIフレームワークを使用する開発者

注意事項

  • ウェブプラットフォームのみ対応
  • ブラウザベースのウォレットのサポートが必要です
  • 外部ウォレット拡張機能に依存しています
  • モバイルまたはデスクトップアプリには適していません
  • ネットワーク関連の問題に対応するため、追加のエラーハンドリングが必要になる可能性があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

flutter_web3_provider

Flutter wrapper for using web3 providers, ie: accessing window.ethereum.

NOTE: This is for web only!

Getting Started

For full example, see: https://github.com/gochain/flutter_web3_provider/blob/main/example/lib/main.dart

Add import import 'package:flutter_web3_provider/ethereum.dart';

Then you can access it just be using the ethereum variable.

if(ethereum != null){
    // then an ethereum provider was injected
    print(ethereum.selectedAddress);
}

Ask user to connect their wallet:

RaisedButton(
    child: Text("Connect Wallet"),
    onPressed: () async {
        var accounts = await promiseToFuture(
            ethereum.request(RequestParams(method: 'eth_requestAccounts')));
        print(accounts);
        String se = ethereum.selectedAddress;
        print("selectedAddress: $se");
        setState(() {
            selectedAddress = se;
        });
    },
)
Using ethers.js

Add ethers.js to web/index.html.

  <script type="module">
    import { ethers } from "https://cdnjs.cloudflare.com/ajax/libs/ethers/6.7.0/ethers.min.js";
    window.ethers = ethers
    // Your code here...
  </script>

Import package:

import 'package:flutter_web3_provider/ethers.dart';

Then create an ethers provider:

// For a read-only provider:
var provider = JsonRpcProvider("https://rpc.gochain.io");
// For a read-write provider (ie: metamask, trust wallet, etc)
var web3 = BrowserProvider(ethereum);

Then you can do things like check balance and submit transactions, etc:

var abalanceF = promiseToFuture(web3.getBalance(ethereum.selectedAddress));

Future tx = promiseToFuture(web3.Signer().sendTransaction(TxParams(
      to: to,
      value: "0x" +
          BigInt.parse(toBase(amount, 18).toString()).toRadixString(16))));

Or use a contract:

const erc20Abi = [
    // Some details about the token
    "function name() view returns (string)",
    "function symbol() view returns (string)",

    // Get the account balance
    "function balanceOf(address) view returns (uint)",

    // Send some of your tokens to someone else
    "function transfer(address to, uint amount)",

    // An event triggered whenever anyone transfers to someone else
    "event Transfer(address indexed from, address indexed to, uint amount)"
];
var contract = Contract(contractAddress, erc20Abi, web3);
// call balanceOf function
var usdcBalanceF = promiseToFuture(
          callMethod(contract, "balanceOf", [ethereum.selectedAddress]));
          
// to make a write transaction, first get the signer (this will use metamask/wallet)
contract = contract.connect(await promiseToFuture(web3.getSigner())); // uses the connected wallet as signer
// then call the function:
var res =
    await promiseToFuture(callMethod(contract, "transfer", [
    '0x39C5190c09ec04cF09C782bA4311C469473Ffe83',
    "0x" + amount.toString()).toRadixString(16)
    ]));

If you need to additional overrides like set gas price or passing the value param, you can add one extra param in the array with those fields, eg:

var res = await promiseToFuture(callMethod(
    contract, "transfer", ['0x39C5190c09ec04cF09C782bA4311C469473Ffe83', "0x" + amount.toString()).toRadixString(16), 
    TxParams(value: "1000000000000000000")]));

NOTES:

  • If you're using the human readable ABI's (ethers.js feature) like above, use uint, not uint256 even if the real abi is a uint256.
  • There are some common functions on the Contract, but you can also call any method using callMethod like above.

Developers

To publish:

make publish

カテゴリー

依存関係

依存関係一覧 1 件

  • js^0.7.1

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。