-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
123 lines (115 loc) · 3.5 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import 'package:flutter/material.dart';
import 'package:fuse_wallet_sdk/fuse_wallet_sdk.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fuse Staking App',
home: WalletScreen(),
);
}
}
class WalletScreen extends StatefulWidget {
@override
_WalletScreenState createState() => _WalletScreenState();
}
class _WalletScreenState extends State<WalletScreen> {
final apiKey = 'YOUR_API_KEY';
final privateKey = EthPrivateKey.fromHex('YOUR_PRIVATE_KEY');
late FuseSDK fuseSDK;
String _address = '';
final tokenController = TextEditingController();
String selectedToken = "Fuse"; // Default to Fuse
bool _isStaking = false;
String _transactionHash = "";
@override
void initState() {
super.initState();
initFuseSDK();
}
Future<void> initFuseSDK() async {
fuseSDK = await FuseSDK.init(apiKey, privateKey);
setState(() {
_address = fuseSDK.wallet.getSender();
});
}
Future<void> stakeTokens() async {
setState(() {
_isStaking = true;
});
try {
String tokenAddress = selectedToken == "Fuse" ? "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" : "0x34Ef2Cc892a88415e9f02b91BfA9c91fC0bE6bD4"; // Use actual addresses
final res = await fuseSDK.stakeToken(
StakeRequestBody(
accountAddress: _address,
tokenAmount: tokenController.text,
tokenAddress: tokenAddress,
),
);
print('UserOpHash: ${res.userOpHash}');
print('Waiting for transaction...');
final ev = await res.wait();
if (ev != null && ev is FilterEvent) {
String transactionHash = ev.transactionHash ?? "Unknown"; // Extracting the transaction hash from the event
setState(() {
_transactionHash = 'Transaction Hash: $transactionHash';
_isStaking = false;
});
print(_transactionHash);
} else {
throw Exception("Failed to retrieve transaction details.");
}
} catch (e) {
setState(() {
_transactionHash = "Error: ${e.toString()}";
_isStaking = false;
});
print(_transactionHash);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fuse Wallet and Staking App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (_address.isNotEmpty)
Text('Wallet Address: $_address'),
if (_transactionHash.isNotEmpty)
Text(_transactionHash),
TextField(
controller: tokenController,
decoration: InputDecoration(labelText: 'Token Amount'),
),
DropdownButton<String>(
value: selectedToken,
onChanged: (String? newValue) {
setState(() {
selectedToken = newValue!;
});
},
items: <String>['Fuse', 'Volt']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
ElevatedButton(
onPressed: _isStaking ? null : stakeTokens,
child: _isStaking ? CircularProgressIndicator() : Text('Stake Tokens'),
),
],
),
),
);
}
}