flutter_web3 1.0.1 copy "flutter_web3: ^1.0.1" to clipboard
flutter_web3: ^1.0.1 copied to clipboard

outdated

Web3 Ethereum and Etherjs wrapper for Flutter Web.

flutter_web3 #

This is a fork of flutter_web3_provider. Be sure to check out the original package

flutter_web3 is Dart class and method wrapper for Ethereum object and Ethers js package. This package also offers others Blockchain data query utils and a lot of helper function for developing dapps.

By utilizing dart2js functionality and dart extension, we manage to get Typing and Asynchonous into dart!

NOTE: This is for web only!

Ethereum Object #

You can access the Ethereum object by accessing the ethereum getter.

if (ethereum != null) {
    final accs = await ethereum!.getAccounts(); // get all accounts in node disposal
    accs // [foo,bar]
}

Prompt user to connect to provider, aka eth_requestAccounts,

final accs = await ethereum!.requestAccount(); // prompt the connection, make sure to handle the error when user cancle.
accs // [foo,bar]

Subscribe to accountsChanged event,

ethereum!.onAccountChanged((accs) {
 print(accs); // [foo,bar]
});

Handle other dynamic event,

ethereum!.onEvent('message', (message) {
 final json = convertToDart(message); // Convert js to Dart object.
 json['foo'] // Foo
 json['bar']['baz'] // Barbaz
});

Or call other json rpc request method that have generic return type T,

final result = await ethereum!.dartRequest<BigNumber>('eth_gasPrice');
result.toBigInt; // 100,000,000,000

Ethers.js #

Initialize #

To initialize, add ethers.js script to web/index.html.

<script src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js" type="application/javascript"></script>

Usage #

Provider

Then create an ethers provider:

// For a read-only provider:
final provider = JsonRpcProvider("https://rpc.foo.io");
// For a read-write provider (ie: metamask, trust wallet, binance chain, etc.)
final provider = Web3Provider(ethereum!);

Or use the default Web3Provider getter

final web3provider = provider;

Then we can query various Blockchain data,

final balance = await provider!.getBalance('0xBar');
balance // 100,000,000 (in Wei)

final block = await provider!.getBlock(3949294);
block.miner // 0xbar
block.transaction // [0xfoo,0xbar,0xbarbaz]
block.nounce // 1293014

Or directly calling Ethers js with specific result type,

final result  = await provider!.call<BigNumber>('getGasPrice');
result.toBigInt;// 100,000,000,000,000

Signer

Use signer to get specific data about address in possession,

final balance = await provider!.getBalance();
balance; // 100,000,000,000,000

Or use signer to send transaction,

final tx = await provider!.getSigner().send(TxParams(to: '0xbar',value: '100,000,000'));
tx['hash'] // 0xbaz

Contract

Initializing Contract object, Supported Abi types refer to Ether.js docs

final abi = [
    // 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)"
];

final contract = Contract('0xfoo', abi, provider!);

Calling view-only constant method,

final name = await contract.call<String>('name');
name // FooBarBaz

final symbol = await contract.call<String>('symbol');
symbol // FBB

Sending write method,

final tx = await contract.send('transfer',['0xbarbaz','100,000,000']);
tx['hash'] // 0xfoo

And wait until transaction is successfully mined

final receipt = await provider!.waitForTransaction(tx['hash']);
receipt.isSuccessful // true if successful
receipt.logs.firstWhere((e) => e.topics.first == '0xbar').data // 0xfoobar

Subscribe to any emitted event,

contract.onEvent('Transfer', (from,to,amount,data) {
 convertToDart(data) // {'foo':'bar','baz':'foobar',...}
 from // 0xbar
 to // 0xbaz
 amount // 100,000,000,000
});
147
likes
0
pub points
90%
popularity

Publisher

unverified uploader

Web3 Ethereum and Etherjs wrapper for Flutter Web.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, js, meta

More

Packages that depend on flutter_web3