Skip to content

Commit

Permalink
feat: Added mainnet to solana_client example (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
Merculiar authored Aug 14, 2024
1 parent 0aab92d commit d286d66
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 37 deletions.
25 changes: 22 additions & 3 deletions packages/solana_mobile_client/example/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,32 @@ import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:solana/encoder.dart';
import 'package:solana/solana.dart';
import 'package:solana_mobile_client/solana_mobile_client.dart';
import 'package:solana_mobile_client_example/config.dart';

part 'client.freezed.dart';

// ignore: avoid-cubits, just an example
class ClientBloc extends Cubit<ClientState> {
ClientBloc(this._solanaClient) : super(const ClientState());
ClientBloc() : super(const ClientState()) {
_initializeClient();
}

late SolanaClient _solanaClient;

void _initializeClient() {
final rpcUrl = state.isMainnet ? mainnetRpcUrl : testnetRpcUrl;
final websocketUrl = state.isMainnet ? mainnetWsUrl : testnetWsUrl;
_solanaClient = SolanaClient(
rpcUrl: Uri.parse(rpcUrl),
websocketUrl: Uri.parse(websocketUrl),
);
}

final SolanaClient _solanaClient;
void updateNetwork({required bool isMainnet}) {
if (state.isMainnet == isMainnet) return;
_initializeClient();
emit(ClientState(isMainnet: isMainnet));
}

Future<bool> isWalletAvailable() => LocalAssociationScenario.isAvailable();

Expand Down Expand Up @@ -180,7 +198,7 @@ class ClientBloc extends Cubit<ClientState> {
identityUri: Uri.parse('https://solana.com'),
iconUri: Uri.parse('favicon.ico'),
identityName: 'Solana',
cluster: 'testnet',
cluster: state.isMainnet ? mainnetCluster : testnetCluster,
);

emit(state.copyWith(authorizationResult: result));
Expand Down Expand Up @@ -211,6 +229,7 @@ class ClientState with _$ClientState {
GetCapabilitiesResult? capabilities,
AuthorizationResult? authorizationResult,
@Default(false) bool isRequestingAirdrop,
@Default(false) bool isMainnet,
}) = _ClientState;

const ClientState._();
Expand Down
77 changes: 50 additions & 27 deletions packages/solana_mobile_client/example/lib/client.freezed.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/solana_mobile_client/example/lib/config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const String testnetRpcUrl = 'https://api.testnet.solana.com';
const String testnetWsUrl = 'wss://api.testnet.solana.com';
const String testnetCluster = 'testnet';
const String mainnetRpcUrl = 'https://api.mainnet-beta.solana.com';
const String mainnetWsUrl = 'wss://api.mainnet-beta.solana.com';
const String mainnetCluster = 'mainnet-beta';
37 changes: 30 additions & 7 deletions packages/solana_mobile_client/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:solana/solana.dart';
import 'package:solana_mobile_client_example/client.dart';

void main() {
runApp(
BlocProvider(
create: (_) => ClientBloc(
SolanaClient(
rpcUrl: Uri.parse('https://api.testnet.solana.com'),
websocketUrl: Uri.parse('wss://api.testnet.solana.com'),
),
),
create: (_) => ClientBloc(),
child: const MyApp(),
),
);
Expand All @@ -37,6 +31,7 @@ class MyApp extends StatelessWidget {
builder: (context, state) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const NetworkToggleButtons(),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8,
Expand Down Expand Up @@ -144,6 +139,34 @@ class MyApp extends StatelessWidget {
);
}

class NetworkToggleButtons extends StatelessWidget {
const NetworkToggleButtons({super.key});

@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: ToggleButtons(
isSelected: [
!context.watch<ClientBloc>().state.isMainnet,
context.watch<ClientBloc>().state.isMainnet,
],
onPressed: (index) {
context.read<ClientBloc>().updateNetwork(isMainnet: index == 1);
},
children: const [
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Text('Testnet'),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Text('Mainnet'),
),
],
),
);
}

class SignAndSendTxButton extends StatelessWidget {
const SignAndSendTxButton({
super.key,
Expand Down

0 comments on commit d286d66

Please sign in to comment.