패키지, 저장소 및 앱 카테고리 검색

패키지 상세v2.0.2

flutter_web3_provider

Flutter web web3 provider

30일 다운로드
80
좋아요
20
Pub 점수
25

패키지 설치

flutter pub add flutter_web3_provider

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

웹 애플리케이션용 웹3 기술과의 통합을 가능하게 하는 플러터 패키지로, 사용자가 플러터 웹 앱에서 직접 이더리움 기반 블록체인과 상호작용할 수 있도록 합니다. 블록체인 연결, 계정 상태 및 거래 처리를 관리하기 위한 프로바이더 패턴 구현을 제공합니다. 웹 플랫폼 전용으로 설계되어 메타마스크와 같은 지갑에 쉽게 접근할 수 있으며, 거래 서명 및 스마트 계약 데이터 읽기와 같은 표준 웹3 메서드를 지원합니다. 이 패키지는 가볍고 플러터의 상태 관리 생태계와 원활하게 작동하도록 제작되었습니다.

사용 사례

  • 웹 앱에서 이더리움 지갑 연결하기
  • 스마트 계약에서 데이터 읽기
  • 체인 내 거래 서명
  • 사용자 지갑 계정 관리
  • 탈중앙화 앱(dApp) 기능 활성화

주요 기능

  • 웹 전용 웹3 통합
  • 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

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.