Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full web support and request contracts fixes #286

Merged
merged 7 commits into from
Apr 23, 2024
Merged
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.2.3-beta01
## 2.2.3-beta03

- Web Support is here!
- Full web support
- Fix on `requestWriteContract()` function where parameters were not parsed properly

## 2.2.2

Expand Down
2 changes: 1 addition & 1 deletion example/dapp/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="dapp"
android:label="Web3Dapp Flutter"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down
2 changes: 1 addition & 1 deletion example/dapp/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
package_info_plus: 58f0028419748fad15bf008b270aaa8e54380b1c
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126
url_launcher_ios: 6116280ddcfe98ab8820085d8d76ae7449447586
url_launcher_ios: bf5ce03e0e2088bad9cc378ea97fa0ed5b49673b

PODFILE CHECKSUM: c4c93c5f6502fe2754f48404d3594bf779584011

Expand Down
6 changes: 0 additions & 6 deletions example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class _MyHomePageState extends State<MyHomePage> {
}

// Register event handlers
_web3App!.onSessionConnect.subscribe(_onSessionConnect);
_web3App!.onSessionPing.subscribe(_onSessionPing);
_web3App!.onSessionEvent.subscribe(_onSessionEvent);
_web3App!.onSessionUpdate.subscribe(_onSessionUpdate);
Expand Down Expand Up @@ -134,7 +133,6 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void dispose() {
// Unregister event handlers
_web3App!.onSessionConnect.unsubscribe(_onSessionConnect);
_web3App!.onSessionPing.unsubscribe(_onSessionPing);
_web3App!.onSessionEvent.unsubscribe(_onSessionEvent);
_web3App!.onSessionUpdate.unsubscribe(_onSessionUpdate);
Expand Down Expand Up @@ -256,10 +254,6 @@ class _MyHomePageState extends State<MyHomePage> {
);
}

void _onSessionConnect(SessionConnect? event) {
debugPrint(jsonEncode(event?.session.toJson()));
}

void _onSessionUpdate(SessionUpdate? args) {
debugPrint('[$runtimeType] _onSessionUpdate $args');
}
Expand Down
138 changes: 94 additions & 44 deletions example/dapp/lib/pages/connect_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: use_build_context_synchronously

import 'dart:async';

import 'package:fl_toast/fl_toast.dart';
Expand Down Expand Up @@ -31,6 +33,18 @@ class ConnectPageState extends State<ConnectPage> {
final List<ChainMetadata> _selectedChains = [];
bool _shouldDismissQrCode = true;

@override
void initState() {
super.initState();
widget.web3App.onSessionConnect.subscribe(_onSessionConnect);
}

@override
void dispose() {
widget.web3App.onSessionConnect.unsubscribe(_onSessionConnect);
super.dispose();
}

void setTestnet(bool value) {
if (value != _testnetOnly) {
_selectedChains.clear();
Expand Down Expand Up @@ -75,7 +89,9 @@ class ConnectPageState extends State<ConnectPage> {
: () => _onConnect(showToast: (m) async {
await showPlatformToast(child: Text(m), context: context);
}, closeModal: () {
Navigator.of(context).pop();
if (Navigator.canPop(context)) {
Navigator.of(context).pop();
}
}),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
Expand Down Expand Up @@ -192,7 +208,6 @@ class ConnectPageState extends State<ConnectPage> {
// final uri = 'metamask://wc?uri=$encodedUri';
if (await canLaunchUrlString(uri)) {
final openApp = await showDialog(
// ignore: use_build_context_synchronously
context: context,
builder: (BuildContext context) {
return AlertDialog(
Expand All @@ -219,49 +234,10 @@ class ConnectPageState extends State<ConnectPage> {
_showQrCode(res);
}

try {
debugPrint('Awaiting session proposal settlement');
final _ = await res.session.future;

showToast?.call(StringConstants.connectionEstablished);
debugPrint('Awaiting session proposal settlement');
final _ = await res.session.future;

// Send off an auth request now that the pairing/session is established
debugPrint('Requesting authentication');
final AuthRequestResponse authRes = await widget.web3App.requestAuth(
pairingTopic: res.pairingTopic,
params: AuthRequestParams(
chainId: _selectedChains[0].chainId,
domain: Constants.domain,
aud: Constants.aud,
// statement: 'Welcome to example flutter app',
),
);

debugPrint('Awaiting authentication response');
final authResponse = await authRes.completer.future;

if (authResponse.error != null) {
debugPrint('Authentication failed: ${authResponse.error}');
showToast?.call(StringConstants.authFailed);
} else {
showToast?.call(StringConstants.authSucceeded);
closeModal?.call();
}

// ignore: use_build_context_synchronously
if (_shouldDismissQrCode && Navigator.canPop(context)) {
// ignore: use_build_context_synchronously
Navigator.pop(context);
}
} catch (e) {
// ignore: use_build_context_synchronously
if (_shouldDismissQrCode && Navigator.canPop(context)) {
// ignore: use_build_context_synchronously
Navigator.pop(context);
}
showToast?.call(StringConstants.connectionFailed);
closeModal?.call();
}
showToast?.call(StringConstants.connectionEstablished);
}

Future<void> _showQrCode(ConnectResponse response) async {
Expand Down Expand Up @@ -309,6 +285,80 @@ class ConnectPageState extends State<ConnectPage> {
),
);
}

void _onSessionConnect(SessionConnect? event) async {
if (event == null) return;

if (_shouldDismissQrCode && Navigator.canPop(context)) {
_shouldDismissQrCode = false;
Navigator.pop(context);
}

final shouldAuth = await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
insetPadding: const EdgeInsets.all(0.0),
contentPadding: const EdgeInsets.all(0.0),
backgroundColor: Colors.white,
title: const Text('Request Auth?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(context, true);
},
child: const Text('Yes!'),
),
],
);
},
);
if (!shouldAuth) return;

try {
final scheme = event.session.peer.metadata.redirect?.native ?? '';
launchUrlString(scheme, mode: LaunchMode.externalApplication);

final pairingTopic = event.session.pairingTopic;
// Send off an auth request now that the pairing/session is established
debugPrint('Requesting authentication');
final authRes = await widget.web3App.requestAuth(
pairingTopic: pairingTopic,
params: AuthRequestParams(
chainId: _selectedChains[0].chainId,
domain: Constants.domain,
aud: Constants.aud,
// statement: 'Welcome to example flutter app',
),
);

debugPrint('Awaiting authentication response');
final authResponse = await authRes.completer.future;

if (authResponse.error != null) {
debugPrint('Authentication failed: ${authResponse.error}');
showPlatformToast(
child: const Text(StringConstants.authFailed),
context: context,
);
} else {
showPlatformToast(
child: const Text(StringConstants.authSucceeded),
context: context,
);
}
} catch (e) {
showPlatformToast(
child: const Text(StringConstants.connectionFailed),
context: context,
);
}
}
}

class QRCodeScreen extends StatefulWidget {
Expand Down
48 changes: 16 additions & 32 deletions example/dapp/lib/utils/crypto/eip155.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'dart:convert';
// ignore: depend_on_referenced_packages
import 'package:convert/convert.dart';

import 'package:flutter/foundation.dart';

import 'package:intl/intl.dart';
import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart';
import 'package:walletconnect_flutter_v2_dapp/models/chain_metadata.dart';
Expand Down Expand Up @@ -96,7 +94,8 @@ class EIP155 {
transaction: Transaction(
from: EthereumAddress.fromHex(address),
to: EthereumAddress.fromHex(
'0x59e2f66C0E96803206B6486cDb39029abAE834c0'),
'0x59e2f66C0E96803206B6486cDb39029abAE834c0',
),
value: EtherAmount.fromInt(EtherUnit.finney, 12), // == 0.012
),
);
Expand All @@ -108,7 +107,8 @@ class EIP155 {
transaction: Transaction(
from: EthereumAddress.fromHex(address),
to: EthereumAddress.fromHex(
'0x59e2f66C0E96803206B6486cDb39029abAE834c0'),
'0x59e2f66C0E96803206B6486cDb39029abAE834c0',
),
value: EtherAmount.fromInt(EtherUnit.finney, 11), // == 0.011
),
);
Expand Down Expand Up @@ -139,19 +139,23 @@ class EIP155 {
address: address,
);
case 'write':
return writeToSmartContract(
web3App: web3App,
rpcUrl: ChainData.testChains.first.rpc.first,
address: address,
return web3App.requestWriteContract(
topic: topic,
chainId: ChainData.testChains.first.chainId,
contract: deployedContract,
rpcUrl: ChainData.testChains.first.rpc.first,
deployedContract: deployedContract,
functionName: 'transfer',
transaction: Transaction(
from: EthereumAddress.fromHex(address),
to: EthereumAddress.fromHex(
'0x59e2f66C0E96803206B6486cDb39029abAE834c0'),
value: EtherAmount.fromInt(EtherUnit.finney, 10), // == 0.010
),
parameters: [
// Recipient
EthereumAddress.fromHex(
'0x59e2f66C0E96803206B6486cDb39029abAE834c0',
),
// Amount to Transfer
EtherAmount.fromInt(EtherUnit.finney, 10).getInWei, // == 0.010
],
);
default:
return Future.value();
Expand All @@ -167,7 +171,6 @@ class EIP155 {
}) async {
final bytes = utf8.encode(message);
final encoded = '0x${hex.encode(bytes)}';
debugPrint('personalSign $encoded');

return await web3App.request(
topic: topic,
Expand Down Expand Up @@ -286,23 +289,4 @@ class EIP155 {
'balance': oCcy.format(balance),
};
}

static Future<dynamic> writeToSmartContract({
required Web3App web3App,
required String rpcUrl,
required String topic,
required String chainId,
required String address,
required DeployedContract contract,
required Transaction transaction,
}) async {
return await web3App.requestWriteContract(
topic: topic,
chainId: chainId,
rpcUrl: rpcUrl,
deployedContract: contract,
functionName: 'transfer',
transaction: transaction,
);
}
}
Loading
Loading