dynamic_sdk_solana 1.1.0
dynamic_sdk_solana: ^1.1.0 copied to clipboard
Dynamic SDK Solana
Dynamic SDK Solana #
The Dynamic SDK Solana package provides convenient methods for interacting with the Solana blockchain. It allows you to create signers, send transactions, and sign messages seamlessly.
Full Example #
Here is a complete example of how to initialize the Dynamic SDK and use the Solana package to sign a message and send a transaction.
1. Initialize the SDK #
First, initialize the DynamicSDK in your main.dart file. This should be done before running your app.
// main.dart
import 'package:dynamic_sdk/dynamic_sdk.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
DynamicSDK.init(
props: ClientProps(
environmentId: 'YOUR_ENVIRONMENT_ID', // Replace with your Environment ID
appLogoUrl: 'https://yourapp.com/logo.png',
appName: 'Your App Name',
redirectUrl: 'yourapp://',
),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
// Your app's widget tree
return MaterialApp(
home: YourMainScreen(),
);
}
}
2. Connect a Wallet and Use Solana Features #
Once the SDK is initialized, you can connect a wallet and then use the Solana-specific functionalities. The following example shows a widget that connects a wallet, and then provides buttons to sign a message and send a transaction.
// your_main_screen.dart
import 'package:flutter/material.dart';
import 'package:dynamic_sdk/dynamic_sdk.dart';
import 'package:dynamic_sdk_solana/dynamic_sdk_solana.dart';
import 'package:solana_web3_latest/solana_web3.dart';
import 'package:solana_web3_latest/programs.dart';
class YourMainScreen extends StatefulWidget {
@override
_YourMainScreenState createState() => _YourMainScreenState();
}
class _YourMainScreenState extends State<YourMainScreen> {
final DynamicSDK _sdk = DynamicSDK.instance;
BaseWallet? _wallet;
@override
void initState() {
super.initState();
_sdk.events.on(
'walletConnected',
(data) => setState(() {
_wallet = data;
}),
);
_sdk.events.on(
'walletDisconnected',
(data) => setState(() {
_wallet = null;
}),
);
}
Future<void> _connectWallet() async {
await _sdk.connectWallet();
}
Future<void> _signSolanaMessage(String message) async {
if (_wallet == null) {
print('Please connect a wallet first.');
return;
}
try {
final signer = _sdk.solana.createSigner(wallet: _wallet!);
final signature = await signer.signMessage(message: message);
print('Message signed with signature: $signature');
// Show success message to the user
} catch (e) {
print('An error occurred while signing the message: $e');
// Show error message to the user
}
}
Future<void> _sendSolanaTransaction(String recipientAddress, double amount) async {
if (_wallet == null) {
print('Please connect a wallet first.');
return;
}
try {
final signer = _sdk.solana.createSigner(wallet: _wallet!);
final connection = _sdk.solana.createConnection();
final fromPubKey = Pubkey.fromString(_wallet!.address);
final toPubKey = Pubkey.fromString(recipientAddress);
final BlockhashWithExpiryBlockHeight recentBlockhash =
await connection.getLatestBlockhash();
final transaction = Transaction.v0(
payer: fromPubKey,
recentBlockhash: recentBlockhash.blockhash,
instructions: [
SystemProgram.transfer(
fromPubkey: fromPubKey,
toPubkey: toPubKey,
lamports: solToLamports(amount),
),
],
);
final signature = await signer.signAndSendTransaction(transaction: transaction);
print('Transaction sent with signature: $signature');
// Show success message to the user
} catch (e) {
print('An error occurred while sending the transaction: $e');
// Show error message to the user
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Dynamic Solana Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_wallet == null)
ElevatedButton(
onPressed: _connectWallet,
child: const Text('Connect Wallet'),
)
else
Text('Connected: ${_wallet!.address}'),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => _signSolanaMessage('Hello from Dynamic!'),
child: const Text('Sign Message'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => _sendSolanaTransaction(
'4ybGGu1vxysaZrBBSLVGfsxLydHREkHDYHUCnFk6os5D', // Example recipient
0.001,
),
child: const Text('Send Transaction'),
),
],
),
),
);
}
}
By following these examples, you can integrate Solana functionalities into your application using the Dynamic SDK.