flutter_light_core_node 0.0.1 flutter_light_core_node: ^0.0.1 copied to clipboard
A flutter libraary to be able to run a light node of Core blockchain
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_light_core_node/flutter_light_core_node.dart';
import 'package:web3dart/web3dart.dart';
import 'package:http/http.dart';
import 'package:wifi_info_flutter/wifi_info_flutter.dart';
import 'utils.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _nodePort;
Web3Client web3Client;
@override
void initState() {
super.initState();
getNodePort();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> getNodePort() async {
int nodePort;
var wifiIP = await WifiInfo().getWifiIP();
String _rpcUrl = wifiIP;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
nodePort = await FlutterLightCoreNode.getNodePort;
} on PlatformException {
nodePort = 0;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
_nodePort = nodePort;
_rpcUrl = "http://" + _rpcUrl + ":" + _nodePort.toString();
web3Client = Web3Client(_rpcUrl, Client());
startCommunicationWithNode();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_nodePort\n'),
),
),
);
}
Future<void> startCommunicationWithNode() async {
final credentials = await web3Client.credentialsFromPrivateKey(PRIVATE_KEY);
final address = await credentials.extractAddress();
print(address);
print(address.hexEip55);
print(await web3Client.getBalance(address));
String responseHash = await web3Client.sendTransaction(
credentials,
Transaction(
to: EthereumAddress.fromHex(
'0xC914Bb2ba888e3367bcecEb5C2d99DF7C7423706'),
gasPrice: EtherAmount.inWei(BigInt.one),
maxGas: 100000,
value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 1),
),
);
print(responseHash);
}
}