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

Heartbeat #201

Merged
merged 6 commits into from
Oct 17, 2023
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## 2.1.4 - UNRELEASED
## 2.1.5

- Added a core heartbeat to check and expire on pairing and sessions.

## 2.1.4

- Core defaults to `Logger.level = Level.nothing` to prevent logs from being printed by default
- Resolved errors with pairings and sessions trying to subscribe even when the relay wasn't connected
Expand Down
2 changes: 1 addition & 1 deletion example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class _MyHomePageState extends State<MyHomePage> {

Future<void> initialize() async {
// try {
print('Project ID: ${DartDefines.projectId}');
debugPrint('Project ID: ${DartDefines.projectId}');
_web3App = await Web3App.createInstance(
projectId: DartDefines.projectId,
logLevel: LogLevel.info,
Expand Down
55 changes: 20 additions & 35 deletions example/dapp/lib/pages/connect_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ class ConnectPageState extends State<ConnectPage> {
vertical: StyleConstants.linear8,
),
child: ElevatedButton(
onPressed: () => _onConnect(_selectedChains),
onPressed: () => _onConnect(
_selectedChains,
showToast: (m) async {
await showPlatformToast(child: Text(m), context: context);
},
),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
StyleConstants.primaryColor,
Expand Down Expand Up @@ -155,9 +160,8 @@ class ConnectPageState extends State<ConnectPage> {
);
}

Future<void> _onConnect(
List<ChainMetadata> chains,
) async {
Future<void> _onConnect(List<ChainMetadata> chains,
{Function(String message)? showToast}) async {
// Use the chain metadata to build the required namespaces:
// Get the methods, get the events
final Map<String, RequiredNamespace> requiredNamespaces = {};
Expand Down Expand Up @@ -192,12 +196,7 @@ class ConnectPageState extends State<ConnectPage> {
final _ = await res.session.future;
// print(sessionData);

showPlatformToast(
child: const Text(
StringConstants.connectionEstablished,
),
context: context,
);
showToast?.call(StringConstants.connectionEstablished);

// Send off an auth request now that the pairing/session is established
debugPrint('Requesting authentication');
Expand All @@ -216,19 +215,9 @@ class ConnectPageState extends State<ConnectPage> {

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

if (_shouldDismissQrCode) {
Expand All @@ -241,12 +230,7 @@ class ConnectPageState extends State<ConnectPage> {
// ignore: use_build_context_synchronously
Navigator.pop(context);
}
await showPlatformToast(
child: const Text(
StringConstants.connectionFailed,
),
context: context,
);
showToast?.call(StringConstants.connectionFailed);
}
}

Expand Down Expand Up @@ -279,17 +263,18 @@ class ConnectPageState extends State<ConnectPage> {
height: StyleConstants.linear16,
),
ElevatedButton(
onPressed: () async {
await Clipboard.setData(
onPressed: () {
Clipboard.setData(
ClipboardData(
text: response.uri!.toString(),
),
);
await showPlatformToast(
child: const Text(
StringConstants.copiedToClipboard,
).then(
(_) => showPlatformToast(
child: const Text(
StringConstants.copiedToClipboard,
),
context: context,
),
context: context,
);
},
child: const Text(
Expand Down
35 changes: 12 additions & 23 deletions example/dapp/lib/widgets/method_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,25 @@ class MethodDialogState extends State<MethodDialog> {
if (snapshot.hasData) {
final String t = jsonEncode(snapshot.data);
return InkWell(
onTap: () async {
await Clipboard.setData(
ClipboardData(
text: t,
onTap: () {
Clipboard.setData(ClipboardData(text: t)).then(
(_) => showPlatformToast(
child: const Text(StringConstants.copiedToClipboard),
context: context,
),
);
showPlatformToast(
child: const Text(
StringConstants.copiedToClipboard,
),
context: context,
);
},
child: Text(
t,
),
child: Text(t),
);
} else if (snapshot.hasError) {
return InkWell(
onTap: () async {
await Clipboard.setData(
ClipboardData(
text: snapshot.data.toString(),
),
);
showPlatformToast(
child: const Text(
StringConstants.copiedToClipboard,
onTap: () {
Clipboard.setData(ClipboardData(text: snapshot.data.toString()))
.then(
(_) => showPlatformToast(
child: const Text(StringConstants.copiedToClipboard),
context: context,
),
context: context,
);
},
child: Text(
Expand Down
2 changes: 1 addition & 1 deletion example/wallet/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:name="com.walletconnect.flutter_wallet.MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
Expand Down
8 changes: 4 additions & 4 deletions example/wallet/lib/dependencies/chains/evm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,24 +247,24 @@ class EVMService extends IChain {
final transaction = Transaction(
from: EthereumAddress.fromHex(ethTransaction.from),
to: EthereumAddress.fromHex(ethTransaction.to),
value: EtherAmount.fromUnitAndValue(
value: EtherAmount.fromBigInt(
EtherUnit.wei,
BigInt.tryParse(ethTransaction.value) ?? BigInt.zero,
),
gasPrice: ethTransaction.gasPrice != null
? EtherAmount.fromUnitAndValue(
? EtherAmount.fromBigInt(
EtherUnit.wei,
BigInt.tryParse(ethTransaction.gasPrice!) ?? BigInt.zero,
)
: null,
maxFeePerGas: ethTransaction.maxFeePerGas != null
? EtherAmount.fromUnitAndValue(
? EtherAmount.fromBigInt(
EtherUnit.wei,
BigInt.tryParse(ethTransaction.maxFeePerGas!) ?? BigInt.zero,
)
: null,
maxPriorityFeePerGas: ethTransaction.maxPriorityFeePerGas != null
? EtherAmount.fromUnitAndValue(
? EtherAmount.fromBigInt(
EtherUnit.wei,
BigInt.tryParse(ethTransaction.maxPriorityFeePerGas!) ??
BigInt.zero,
Expand Down
2 changes: 1 addition & 1 deletion example/wallet/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MyApp extends StatelessWidget {
const nearWhite = Color(0xFFE0E0E0);

return baseTheme.copyWith(
backgroundColor: Colors.black,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.black),
scaffoldBackgroundColor: Colors.black,
textTheme: baseTheme.textTheme.apply(
bodyColor: nearWhite,
Expand Down
13 changes: 8 additions & 5 deletions lib/apis/core/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:walletconnect_flutter_v2/apis/core/crypto/i_crypto.dart';
import 'package:walletconnect_flutter_v2/apis/core/echo/echo.dart';
import 'package:walletconnect_flutter_v2/apis/core/echo/echo_client.dart';
import 'package:walletconnect_flutter_v2/apis/core/echo/i_echo.dart';
import 'package:walletconnect_flutter_v2/apis/core/heartbit/heartbeat.dart';
import 'package:walletconnect_flutter_v2/apis/core/heartbit/i_heartbeat.dart';
import 'package:walletconnect_flutter_v2/apis/core/i_core.dart';
import 'package:walletconnect_flutter_v2/apis/core/pairing/expirer.dart';
import 'package:walletconnect_flutter_v2/apis/core/pairing/i_expirer.dart';
Expand Down Expand Up @@ -49,17 +51,17 @@ class Core implements ICore {
@override
late IExpirer expirer;

// @override
// late IJsonRpcHistory history;

@override
late IPairing pairing;

@override
late IEcho echo;

@override
late IHeartBeat heartbeat;

Logger _logger = Logger(
level: Level.nothing,
level: Level.off,
printer: PrettyPrinter(),
);
@override
Expand All @@ -81,6 +83,7 @@ class Core implements ICore {
level: logLevel.toLevel(),
printer: PrettyPrinter(),
);
heartbeat = HeartBeat();
storage = SharedPrefsStores(
memoryStore: memoryStore,
);
Expand Down Expand Up @@ -111,7 +114,6 @@ class Core implements ICore {
),
socketHandler: webSocketHandler,
);

expirer = Expirer(
storage: storage,
context: StoreVersions.CONTEXT_EXPIRER,
Expand Down Expand Up @@ -157,5 +159,6 @@ class Core implements ICore {
await relayClient.init();
await expirer.init();
await pairing.init();
heartbeat.init();
}
}
31 changes: 31 additions & 0 deletions lib/apis/core/heartbit/heartbeat.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dart:async';

import 'package:event/event.dart';
import 'package:walletconnect_flutter_v2/apis/core/heartbit/i_heartbeat.dart';

class HeartBeat implements IHeartBeat {
Timer? _heartbeatTimer;

@override
int interval;

HeartBeat({this.interval = 5});

@override
void init() {
if (_heartbeatTimer != null) return;
_heartbeatTimer = Timer.periodic(
Duration(seconds: interval),
(timer) => onPulse.broadcast(),
);
}

@override
void stop() {
_heartbeatTimer?.cancel();
_heartbeatTimer = null;
}

@override
final Event<EventArgs> onPulse = Event();
}
9 changes: 9 additions & 0 deletions lib/apis/core/heartbit/i_heartbeat.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:event/event.dart';

abstract class IHeartBeat {
abstract int interval;
abstract final Event onPulse;

void init();
void stop();
}
5 changes: 3 additions & 2 deletions lib/apis/core/i_core.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:logger/logger.dart';
import 'package:walletconnect_flutter_v2/apis/core/crypto/i_crypto.dart';
import 'package:walletconnect_flutter_v2/apis/core/echo/i_echo.dart';
import 'package:walletconnect_flutter_v2/apis/core/heartbit/i_heartbeat.dart';
import 'package:walletconnect_flutter_v2/apis/core/pairing/i_expirer.dart';
import 'package:walletconnect_flutter_v2/apis/core/pairing/i_pairing.dart';
import 'package:walletconnect_flutter_v2/apis/core/relay_client/i_relay_client.dart';
Expand All @@ -14,11 +15,11 @@ abstract class ICore {
abstract final String projectId;
abstract final String pushUrl;

// abstract IHeartBeat heartbeat;
abstract IHeartBeat heartbeat;
abstract ICrypto crypto;
abstract IRelayClient relayClient;
abstract IStore<Map<String, dynamic>> storage;
// abstract IJsonRpcHistory history;

abstract IExpirer expirer;
abstract IPairing pairing;
abstract IEcho echo;
Expand Down
2 changes: 2 additions & 0 deletions lib/apis/core/pairing/i_pairing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ abstract class IPairing {
required String topic,
required PairingMetadata metadata,
});
Future<void> checkAndExpire();
List<PairingInfo> getPairings();
PairingInfo? getPairing({required String topic});
Future<void> ping({required String topic});
Future<void> disconnect({required String topic});
IPairingStore getStore();
Expand Down
Loading