Skip to content
This repository was archived by the owner on Nov 18, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion hello_world/.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: 77d935af4db863f6abd0b9c31c7e6df2a13de57b
revision: 5464c5bac742001448fe4fc0597be939379f88ea
channel: stable

project_type: app
23 changes: 12 additions & 11 deletions hello_world/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<img src="screenshot/Flutter_and_Blockchain.jpg" width="70%">
# hello_world

# Project 1
## Hello World (Flutter + Blockchain)
A basic blockchain and flutter based project.
A new Flutter project.

| | |
|----------------------------------------------------------|:----------------------------------------------------:|
| <img src="screenshot/Screenshot_1.png" width="50%" > | <img src="screenshot/Screenshot_2.png" width="50%" > |

<img src="screenshot/Screenshot_3.png" width="50%" >
## Getting Started

---
This project is a starting point for a Flutter application.

For tutorial of Hello World Dapp check [https://aniketambore.netlify.app/Developement/hello_world_dapp/](https://aniketambore.netlify.app/Developement/hello_world_dapp/)
A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
20 changes: 2 additions & 18 deletions hello_world/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,7 @@

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

include: package:very_good_analysis/analysis_options.2.4.0.yaml
linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
public_member_api_docs: false
2 changes: 1 addition & 1 deletion hello_world/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
Expand Down
16 changes: 8 additions & 8 deletions hello_world/contracts/HelloWorld.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.16 <0.9.0 ;

pragma solidity ^0.8.7;

contract HelloWorld {
string public yourName ;
string public yourName;

constructor(){
yourName = "Unknown" ;
constructor() {
yourName = "Unknown";
}

function setName(string memory name) public {
yourName = name;
function setName(string memory nm) public{
yourName = nm;
}

}
}
2 changes: 1 addition & 1 deletion hello_world/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
<false/>
</dict>
</plist>
101 changes: 53 additions & 48 deletions hello_world/lib/contract_linking.dart
Original file line number Diff line number Diff line change
@@ -1,82 +1,89 @@
// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart';
import 'package:dart_web3/dart_web3.dart';
import 'package:http/http.dart' as http;
import 'package:web3dart/web3dart.dart';
import 'package:web_socket_channel/io.dart';

class ContractLinking extends ChangeNotifier {
final String _rpcURl = "http://10.0.2.2:7545";
final String _wsURl = "ws://10.0.2.2:7545/";
ContractLinking() {
initialSetup();
}

final String _rpcUrl = 'http://127.0.0.1:7545';
final String _wsUrl = 'ws://10.0.2.2:7545/';
final String _privateKey =
"a3a3229cdc4e6cc94e79c7f5d34f2e7d761fc4116707e82b0e6bbe296b51c153";
'f7c4e01b7af6c36ef6f50d21926f2e90c5f0305350e6a74b6bdc8d0c27693270';

bool isLoading = true;
late Web3Client _client;
late String _abiCode;

late EthereumAddress _contractAddress;
late Credentials _credentials;

late DeployedContract _contract;
late ContractFunction _yourName;
late ContractFunction _setName;
late String deployedName;

bool isLoading = true;
String? deployedName;

ContractLinking() {
initialSetup();
}

initialSetup() async {
Future<void> initialSetup() async {
// establish a connection to the ethereum rpc node. The socketConnector
// property allows more efficient event streams over websocket instead of
// http-polls. However, the socketConnector property is experimental.
_client = Web3Client(_rpcURl, Client(), socketConnector: () {
return IOWebSocketChannel.connect(_wsURl).cast<String>();
});
_client = Web3Client(
_rpcUrl,
http.Client(),
socketConnector: () {
return IOWebSocketChannel.connect(_wsUrl).cast<String>();
},
);

await getAbi();
await getCredentials();
getCredentials();
await getDeployedContract();
}

Future<void> getAbi() async {
// Reading the contract abi
String abiStringFile =
await rootBundle.loadString("src/artifacts/HelloWorld.json");
var jsonAbi = jsonDecode(abiStringFile);
_abiCode = jsonEncode(jsonAbi["abi"]);
// print(_abiCode);

_contractAddress =
EthereumAddress.fromHex(jsonAbi["networks"]["5777"]["address"]);
// print(_contractAddress);
final abiStringFile =
await rootBundle.loadString('src/artifacts/HelloWorld.json');
final jsonAbi = jsonDecode(abiStringFile) as Map<String, dynamic>;
_abiCode = jsonEncode(jsonAbi['abi']);

_contractAddress = EthereumAddress.fromHex(
// ignore: avoid_dynamic_calls
jsonAbi['networks']['5777']['address'] as String,
);
}

Future<void> getCredentials() async {
void getCredentials() {
_credentials = EthPrivateKey.fromHex(_privateKey);
// print(_credentials);
}

Future<void> getDeployedContract() async {
// Telling Web3dart where our contract is declared.
_contract = DeployedContract(
ContractAbi.fromJson(_abiCode, "HelloWorld"), _contractAddress);
ContractAbi.fromJson(_abiCode, 'HelloWorld'),
_contractAddress,
);

// Extracting the functions, declared in contract.
_yourName = _contract.function("yourName");
_setName = _contract.function("setName");
getName();
_yourName = _contract.function('yourName');
_setName = _contract.function('setName');
await getName();
}

Future<void> getName() async {
// Getting the current name declared in the smart contract.
var currentName = await _client
.call(contract: _contract, function: _yourName, params: []);
// print(currentName);
deployedName = currentName[0];
final currentName = await _client.call(
contract: _contract,
function: _yourName,
params: <dynamic>[],
);
deployedName = currentName[0] as String;
isLoading = false;
notifyListeners();
}
Expand All @@ -86,15 +93,13 @@ class ContractLinking extends ChangeNotifier {
isLoading = true;
notifyListeners();
await _client.sendTransaction(
_credentials,
Transaction.callContract(
contract: _contract,
function: _setName,
parameters: [nameToSet],
// gasPrice: EtherAmount.inWei(BigInt.one),
// maxGas: 100000,
// value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 1),
));
getName();
_credentials,
Transaction.callContract(
contract: _contract,
function: _setName,
parameters: <dynamic>[nameToSet],
),
);
await getName();
}
}
88 changes: 88 additions & 0 deletions hello_world/lib/hello.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'package:flutter/material.dart';
import 'package:hello_world/contract_linking.dart';
import 'package:provider/provider.dart';

class Hello extends StatelessWidget {
const Hello({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
// Getting the value and object or contract_linking
final contractLink = Provider.of<ContractLinking>(context);
final yourNameController = TextEditingController();

return Scaffold(
appBar: AppBar(
title: const Text('Hello World !'),
centerTitle: true,
),
body: Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Center(
child: contractLink.isLoading
? const CircularProgressIndicator()
: SingleChildScrollView(
child: Form(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Flexible(
child: Text(
'Hello ',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 32,
),
),
),
Flexible(
child: Text(
contractLink.deployedName,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 32,
color: Colors.tealAccent,
),
),
),
],
),
Padding(
padding: const EdgeInsets.only(top: 29),
child: TextFormField(
controller: yourNameController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Your Name',
hintText: 'What is your name ?',
icon: Icon(Icons.drive_file_rename_outline),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 30),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
),
onPressed: () {
contractLink.setName(yourNameController.text);
yourNameController.clear();
},
child: const Text(
'Set Name',
style: TextStyle(fontSize: 30),
),
),
)
],
),
),
),
),
),
);
}
}
Loading