File size: 43,005 Bytes
a9dca88
 
0edef69
a9dca88
 
 
 
df5a009
8cc803b
a9dca88
 
 
8d53432
 
a9dca88
37f012e
 
 
a9dca88
 
 
 
 
ebcc066
 
 
 
 
 
 
 
 
 
a9dca88
 
 
8d53432
 
df5a009
 
a9dca88
 
df5a009
a9dca88
 
 
8cc803b
 
df5a009
 
 
 
 
 
37f012e
 
 
 
 
 
 
 
8cc803b
df5a009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9dca88
8d53432
df5a009
 
 
 
8d53432
 
8cc803b
 
8d53432
 
 
 
 
8cc803b
8d53432
8cc803b
8d53432
 
 
 
 
 
 
 
8cc803b
8d53432
8cc803b
 
37f012e
 
 
 
 
 
 
 
ebcc066
37f012e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0edef69
37f012e
 
ebcc066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37f012e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcc066
 
 
 
 
 
 
37f012e
 
 
 
 
 
 
 
 
ebcc066
 
 
 
 
 
 
 
 
37f012e
 
 
 
 
 
 
 
 
 
 
ebcc066
37f012e
 
ebcc066
 
 
 
 
 
 
 
37f012e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcc066
37f012e
 
ebcc066
37f012e
 
 
 
 
 
 
 
 
 
 
ebcc066
37f012e
 
 
8cc803b
 
 
 
df5a009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc803b
 
 
df5a009
8d53432
8cc803b
 
8d53432
8cc803b
df5a009
8cc803b
 
df5a009
 
 
 
 
 
8cc803b
8d53432
8cc803b
 
 
 
 
 
 
 
 
8d53432
8cc803b
 
 
 
 
 
 
 
 
 
 
 
df5a009
 
8cc803b
 
 
 
 
 
 
df5a009
8cc803b
 
 
8d53432
8cc803b
df5a009
8cc803b
 
df5a009
 
 
8cc803b
 
8d53432
8cc803b
 
 
 
 
8d53432
8cc803b
 
 
 
 
 
df5a009
a9dca88
8cc803b
 
 
 
8d53432
8cc803b
df5a009
8cc803b
 
df5a009
 
 
 
 
 
 
 
 
0edef69
df5a009
 
8cc803b
 
8d53432
8cc803b
 
 
 
 
 
 
 
 
 
 
 
 
 
a9dca88
8d53432
8cc803b
37f012e
8cc803b
a9dca88
8cc803b
 
 
 
 
 
a9dca88
d705826
8cc803b
 
 
 
 
df5a009
 
a9dca88
8cc803b
df5a009
8cc803b
 
df5a009
 
 
8cc803b
 
 
 
 
 
 
 
df5a009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc803b
 
 
 
 
 
 
df5a009
8d53432
8cc803b
df5a009
8cc803b
 
df5a009
 
 
 
 
 
 
 
8cc803b
 
 
 
 
 
 
 
8d53432
8cc803b
 
 
df5a009
 
8cc803b
 
 
8d53432
8cc803b
 
df5a009
 
8cc803b
 
 
 
 
df5a009
8cc803b
 
df5a009
 
 
 
 
 
8cc803b
 
 
 
 
 
 
 
 
 
df5a009
 
8cc803b
 
 
8d53432
37f012e
 
 
 
 
 
 
 
ebcc066
37f012e
 
 
 
 
 
 
 
df5a009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9dca88
 
 
 
8cc803b
a9dca88
 
 
 
 
 
8cc803b
a9dca88
 
8cc803b
a9dca88
 
 
8cc803b
a9dca88
 
df5a009
 
 
 
 
 
 
a9dca88
8cc803b
 
 
 
df5a009
 
 
 
 
 
 
 
8d53432
 
 
 
 
8cc803b
8d53432
 
37f012e
 
 
ebcc066
37f012e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9dca88
 
 
 
8cc803b
0edef69
 
a9dca88
 
df5a009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9dca88
8cc803b
df5a009
 
a9dca88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc803b
a9dca88
 
 
df5a009
 
 
 
 
a9dca88
 
 
 
 
 
 
 
df5a009
 
 
 
 
 
 
 
 
 
 
a9dca88
 
 
df5a009
a9dca88
 
 
 
 
 
 
 
8cc803b
a9dca88
 
 
df5a009
 
 
 
a9dca88
8d53432
 
df5a009
 
 
 
 
 
 
 
 
8d53432
df5a009
8d53432
 
 
 
 
 
 
 
 
df5a009
8cc803b
 
 
 
 
8d53432
37f012e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc803b
a9dca88
0edef69
df5a009
 
 
 
 
37f012e
a9dca88
 
 
df5a009
a9dca88
df5a009
8cc803b
 
 
0edef69
8d53432
df5a009
8cc803b
 
8d53432
 
 
68e14c4
 
8d53432
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
#!/usr/bin/env python3
"""
Ethereum MCP Server with Gradio Interface 
"""

import json
import logging
import os
from typing import Any, Dict, List, Optional
from dataclasses import dataclass
import gradio as gr
from web3 import Web3
from eth_tester import EthereumTester
from web3.providers.eth_tester import EthereumTesterProvider
from eth_account import Account
from solcx import compile_source, install_solc
import solcx
from eth_utils import to_checksum_address

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def validate_ethereum_address(address: str) -> bool:
    """Validate Ethereum address format"""
    try:
        if not address or not isinstance(address, str):
            return False
        Web3.to_checksum_address(address)
        return True
    except (ValueError):
        return False

@dataclass
class EthereumConfig:
    """Configuration for Ethereum connections"""
    use_testnet: bool = True
    default_network: str = "local_testnet"
    infura_project_id: Optional[str] = None
    alchemy_api_key: Optional[str] = None

class EthereumClient:
    """Ethereum blockchain client with support for multiple networks"""
    
    def __init__(self, config: EthereumConfig):
        self.config = config
        self.current_network = "local_testnet"
        self.accounts = {}
        self.w3 = None
        self.eth_tester = None
        self.is_mainnet_readonly = False
        
        # Initialize with local testnet by default
        self._connect_local_testnet()

        # Install Solidity compiler for contract compilation
        try:
            install_solc('0.8.19')
            solcx.set_solc_version('0.8.19')
        except Exception as e:
            logger.warning(f"Could not install Solidity compiler: {e}")

        
    def _connect_local_testnet(self):
        """Connect to local test blockchain"""
        try:
            self.eth_tester = EthereumTester()
            self.w3 = Web3(EthereumTesterProvider(self.eth_tester))
            self.current_network = "local_testnet"
            self.is_mainnet_readonly = False
            self.accounts = {}
            
            # Fund some test accounts
            self._setup_test_accounts()
            logger.info(f"βœ… Connected to Local TestNet: {self.w3.is_connected()}")
            logger.info(f"πŸ“Š Latest block: {self.w3.eth.block_number}")
            return True
        except Exception as e:
            logger.error(f"Error connecting to local testnet: {e}")
            return False
    
    def _connect_ethereum_mainnet(self, provider_url: str):
        """Connect to Ethereum mainnet (read-only for safety)"""
        try:
            self.w3 = Web3(Web3.HTTPProvider(provider_url))
            if self.w3.is_connected():
                self.current_network = "ethereum_mainnet"
                self.is_mainnet_readonly = True
                self.accounts = {}  # Clear test accounts for safety
                self.eth_tester = None
                logger.info(f"βœ… Connected to Ethereum Mainnet: {self.w3.is_connected()}")
                logger.info(f"πŸ“Š Latest block: {self.w3.eth.block_number}")
                logger.info("⚠️ MAINNET MODE: Read-only operations only for safety")
                return True
            return False
        except Exception as e:
            logger.error(f"Error connecting to mainnet: {e}")
            return False
    
    def _connect_ethereum_sepolia(self, provider_url: str):
        """Connect to Ethereum Sepolia testnet"""
        try:
            self.w3 = Web3(Web3.HTTPProvider(provider_url))
            if self.w3.is_connected():
                self.current_network = "ethereum_sepolia"
                self.is_mainnet_readonly = False
                self.accounts = {}  # Clear local test accounts
                self.eth_tester = None
                logger.info(f"βœ… Connected to Ethereum Sepolia: {self.w3.is_connected()}")
                logger.info(f"πŸ“Š Latest block: {self.w3.eth.block_number}")
                return True
            return False
        except Exception as e:
            logger.error(f"Error connecting to Sepolia: {e}")
            return False
    
    def switch_network(self, network: str, api_key: str = None) -> dict:
        """Switch between different Ethereum networks"""
        try:
            if network == "local_testnet":
                success = self._connect_local_testnet()
                return {
                    "success": success,
                    "network": self.current_network,
                    "readonly": self.is_mainnet_readonly,
                    "message": "Connected to local testnet" if success else "Failed to connect"
                }
            
            elif network == "ethereum_mainnet":
                if not api_key:
                    return {"success": False, "error": "API key required for mainnet"}
                
                # Try Infura first, then Alchemy
                provider_url = f"https://mainnet.infura.io/v3/{api_key}"
                success = self._connect_ethereum_mainnet(provider_url)
                
                if not success:
                    provider_url = f"https://eth-mainnet.g.alchemy.com/v2/{api_key}"
                    success = self._connect_ethereum_mainnet(provider_url)
                
                return {
                    "success": success,
                    "network": self.current_network,
                    "readonly": self.is_mainnet_readonly,
                    "message": "Connected to Ethereum mainnet (read-only)" if success else "Failed to connect to mainnet"
                }
            
            elif network == "ethereum_sepolia":
                if not api_key:
                    return {"success": False, "error": "API key required for Sepolia"}
                
                # Try Infura first, then Alchemy
                provider_url = f"https://sepolia.infura.io/v3/{api_key}"
                success = self._connect_ethereum_sepolia(provider_url)
                
                if not success:
                    provider_url = f"https://eth-sepolia.g.alchemy.com/v2/{api_key}"
                    success = self._connect_ethereum_sepolia(provider_url)
                
                return {
                    "success": success,
                    "network": self.current_network,
                    "readonly": self.is_mainnet_readonly,
                    "message": "Connected to Ethereum Sepolia testnet" if success else "Failed to connect to Sepolia"
                }
            
            else:
                return {"success": False, "error": f"Unknown network: {network}"}
                
        except Exception as e:
            return {"success": False, "error": str(e)}
    
    def _setup_test_accounts(self):
        """Set up test accounts with initial funding (local testnet only)"""
        if self.current_network != "local_testnet" or not self.eth_tester:
            return
            
        test_accounts = self.eth_tester.get_accounts()
        
        # Create and fund some test accounts
        for i in range(3):
            account_name = f"test_account_{i+1}"
            new_account = Account.create()
            
            try:
                tx_hash = self.w3.eth.send_transaction({
                    'from': test_accounts[0],
                    'to': new_account.address,
                    'value': self.w3.to_wei(100, 'ether'),
                    'gas': 21000,
                    'gasPrice': self.w3.to_wei(20, 'gwei')
                })
                
                self.accounts[account_name] = {
                    "address": new_account.address,
                    "private_key": new_account.key.hex()
                }
                logger.info(f"Created and funded: {account_name} - {new_account.address}")
            except Exception as e:
                logger.error(f"Error funding test account: {e}")

    def compile_and_deploy_contract(self, source_code: str, contract_name: str, from_account: str, constructor_args: list = None) -> dict:
        """
        Compile and deploy a smart contract.
        Only works on local testnet for safety.
        
        Args:
            source_code (str): Solidity source code
            contract_name (str): Name of the contract to deploy
            from_account (str): Account name or Eth address to deploy from
            constructor_args (list): Constructor arguments if any
            
        Returns:
            dict: Deployment result
        """
        try:
            if self.is_mainnet_readonly:
                return {
                    "success": False, 
                    "error": "Contract deployment disabled on mainnet for safety. Switch to testnet."
                }
            
            if self.current_network != "local_testnet":
                return {
                    "success": False, 
                    "error": "Contract deployment only available on local testnet for safety"
                }
            
            # Determine if from_account is a name or address
            private_key = None
            from_address = None
            
            if validate_ethereum_address(from_account):
                # It's an address - check if we have the private key for it
                from_address = Web3.to_checksum_address(from_account)
                
                # Look for this address in our accounts
                account_found = False
                for account_name, account_data in self.accounts.items():
                    if account_data["address"].lower() == from_address.lower():
                        private_key = account_data["private_key"]
                        account_found = True
                        break
                
                if not account_found:
                    return {
                        "success": False, 
                        "error": f"Private key not available for address '{from_address}'. Use an account created in this session."
                    }
            else:
                # It's an account name
                if from_account not in self.accounts:
                    available_accounts = list(self.accounts.keys())
                    return {
                        "success": False, 
                        "error": f"Account '{from_account}' not found. Available accounts: {available_accounts}"
                    }
                
                account_data = self.accounts[from_account]
                private_key = account_data["private_key"]
                from_address = account_data["address"]
            
            
            # Compile the contract
            try:
                compiled_sol = compile_source(source_code)
                contract_interface = None
                
                # Find the contract in compiled output
                for contract_id, contract_data in compiled_sol.items():
                    if contract_name in contract_id:
                        contract_interface = contract_data
                        break
                
                if not contract_interface:
                    return {"success": False, "error": f"Contract '{contract_name}' not found in source code"}
                
            except Exception as e:
                return {"success": False, "error": f"Compilation failed: {str(e)}"}
            
            # # Get account details
            # account_data = self.accounts[from_account]
            # private_key = account_data["private_key"]
            # from_address = account_data["address"]
            # Verify we have the necessary account details
            if not private_key or not from_address:
                return {"success": False, "error": "Unable to retrieve account details for deployment"}
            
            # Create contract instance
            contract = self.w3.eth.contract(
                abi=contract_interface['abi'],
                bytecode=contract_interface['bin']
            )
            
            # Prepare constructor arguments
            constructor_args = constructor_args or []

            # Check balance before deployment
            balance_wei = self.w3.eth.get_balance(from_address)
            if balance_wei == 0:
                return {
                    "success": False, 
                    "error": f"Account {from_address} has zero balance. Cannot deploy contract."
                }
                        
            # Build deployment transaction
            nonce = self.w3.eth.get_transaction_count(from_address)
            
            # Estimate gas for deployment
            try:
                gas_estimate = contract.constructor(*constructor_args).estimate_gas({
                    'from': from_address
                })
                # Add 20% buffer to gas estimate
                gas_limit = int(gas_estimate * 1.2)
            except Exception as e:
                logger.warning(f"Gas estimation failed: {e}")
                gas_limit = 3000000  # Default gas limit
            
            # Check if account has enough gas
            gas_cost = gas_limit * self.w3.eth.gas_price
            if balance_wei < gas_cost:
                return {
                    "success": False,
                    "error": f"Insufficient balance for gas. Required: {self.w3.from_wei(gas_cost, 'ether')} ETH, Available: {self.w3.from_wei(balance_wei, 'ether')} ETH"
                }
            
            # Build and sign transaction
            transaction = contract.constructor(*constructor_args).build_transaction({
                'from': from_address,
                'gas': gas_limit,
                'gasPrice': self.w3.eth.gas_price,
                'nonce': nonce,
            })
            
            signed_txn = self.w3.eth.account.sign_transaction(transaction, private_key)
            tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
            
            # Wait for transaction receipt
            tx_receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash)
            
            if tx_receipt.status == 1:
                return {
                    "success": True,
                    "contract_address": tx_receipt.contractAddress,
                    "transaction_hash": tx_hash.hex(),
                    "gas_used": tx_receipt.gasUsed,
                    "gas_cost_eth": str(self.w3.from_wei(tx_receipt.gasUsed * self.w3.eth.gas_price, 'ether')),
                    "contract_name": contract_name,
                    "deployed_by": from_address,
                    "deployer_input": from_account, 
                    "network": self.current_network,
                    "abi": contract_interface['abi']
                }
            else:
                return {
                    "success": False,
                    "error": "Contract deployment failed",
                    "transaction_hash": tx_hash.hex()
                }
                
        except Exception as e:
            logger.error(f"Contract deployment error: {e}")
            return {"success": False, "error": str(e)}


# Initialize the Ethereum client
config = EthereumConfig(use_testnet=True)
eth_client = EthereumClient(config)

# MCP Tool Functions
def switch_ethereum_network(network: str, api_key: str = None) -> dict:
    """
    Switch between different Ethereum networks.
    
    Args:
        network (str): Network to switch to ('local_testnet', 'ethereum_mainnet', 'ethereum_sepolia')
        api_key (str): API key for Infura or Alchemy (required for mainnet/sepolia)
        
    Returns:
        dict: Network switch result
    """
    return eth_client.switch_network(network, api_key)

def get_network_info() -> dict:
    """
    Get current network information.
    
    Returns:
        dict: Current network details
    """
    try:
        if not eth_client.w3 or not eth_client.w3.is_connected():
            return {"success": False, "error": "Not connected to any network"}
            
        latest_block = eth_client.w3.eth.block_number
        chain_id = eth_client.w3.eth.chain_id if hasattr(eth_client.w3.eth, 'chain_id') else None
        
        return {
            "success": True,
            "network": eth_client.current_network,
            "connected": eth_client.w3.is_connected(),
            "latest_block": latest_block,
            "chain_id": chain_id,
            "readonly_mode": eth_client.is_mainnet_readonly
        }
    except Exception as e:
        return {"success": False, "error": str(e)}

def create_ethereum_account(name: str) -> dict:
    """
    Create a new Ethereum account with automatic funding on testnet.
    Note: Only works on local testnet for safety.
    
    Args:
        name (str): Name for the new account
        
    Returns:
        dict: Account creation result
    """
    try:
        if eth_client.current_network != "local_testnet":
            return {
                "success": False, 
                "error": "Account creation only available on local testnet for safety"
            }
        
        account = Account.create()
        
        # Fund the new account from test accounts
        test_accounts = eth_client.eth_tester.get_accounts()
        if test_accounts:
            tx_hash = eth_client.w3.eth.send_transaction({
                'from': test_accounts[0],
                'to': account.address,
                'value': eth_client.w3.to_wei(10, 'ether'),
                'gas': 21000,
                'gasPrice': eth_client.w3.to_wei(20, 'gwei')
            })
            
            eth_client.accounts[name] = {
                "address": account.address,
                "private_key": account.key.hex()
            }
            
            return {
                "success": True,
                "name": name,
                "address": account.address,
                "balance_eth": "10.0",
                "funded": True,
                "transaction_hash": tx_hash.hex(),
                "network": eth_client.current_network
            }
    except Exception as e:
        return {"success": False, "error": str(e)}

def get_ethereum_balance(address: str) -> dict:
    """
    Get ETH balance for an Ethereum address.
    Works on all networks.
    
    Args:
        address (str): Ethereum address to check balance for
        
    Returns:
        dict: Balance information
    """
    try:
        if not eth_client.w3 or not eth_client.w3.is_connected():
            return {"success": False, "error": "Not connected to any network"}
            
        balance_wei = eth_client.w3.eth.get_balance(address)
        balance_eth = eth_client.w3.from_wei(balance_wei, 'ether')
        return {
            "success": True,
            "address": address,
            "balance_wei": str(balance_wei),
            "balance_eth": str(balance_eth),
            "network": eth_client.current_network
        }
    except Exception as e:
        return {"success": False, "error": str(e)}

def send_ethereum_transaction(from_account: str, to_address: str, amount_eth: float) -> dict:
    """
    Send ETH from one account to another.
    Note: Only works on local testnet for safety.
    
    Args:
        from_account (str): Name of the account to send from
        to_address (str): Ethereum address to send to
        amount_eth (float): Amount of ETH to send
        
    Returns:
        dict: Transaction result
    """
    try:
        if eth_client.is_mainnet_readonly:
            return {
                "success": False, 
                "error": "Transactions disabled on mainnet for safety. Switch to testnet for transactions."
            }
        
        if eth_client.current_network != "local_testnet":
            return {
                "success": False, 
                "error": "Transactions only available on local testnet for safety"
            }
        
        if from_account not in eth_client.accounts:
            return {"success": False, "error": "Account not found"}
        
        account_data = eth_client.accounts[from_account]
        private_key = account_data["private_key"]
        from_address = account_data["address"]
        
        # Build and send transaction
        nonce = eth_client.w3.eth.get_transaction_count(from_address)
        amount_wei = eth_client.w3.to_wei(amount_eth, 'ether')
        
        transaction = {
            'to': to_address,
            'value': amount_wei,
            'gas': 21000,
            'gasPrice': eth_client.w3.eth.gas_price,
            'nonce': nonce,
        }
        
        signed_txn = eth_client.w3.eth.account.sign_transaction(transaction, private_key)
        tx_hash = eth_client.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
        
        return {
            "success": True,
            "transaction_hash": tx_hash.hex(),
            "from": from_address,
            "to": to_address,
            "amount_eth": amount_eth,
            "network": eth_client.current_network
        }
        
    except Exception as e:
        return {"success": False, "error": str(e)}

def get_latest_ethereum_block() -> dict:
    """
    Get information about the latest block.
    Works on all networks.
    
    Returns:
        dict: Latest block information
    """
    try:
        if not eth_client.w3 or not eth_client.w3.is_connected():
            return {"success": False, "error": "Not connected to any network"}
            
        block = eth_client.w3.eth.get_block("latest")
        return {
            "success": True,
            "block_number": block.number,
            "block_hash": block.hash.hex(),
            "timestamp": block.timestamp,
            "transaction_count": len(block.transactions),
            "gas_used": block.gasUsed,
            "gas_limit": block.gasLimit,
            "network": eth_client.current_network
        }
    except Exception as e:
        return {"success": False, "error": str(e)}

def get_transaction_info(tx_hash: str) -> dict:
    """
    Get information about a specific transaction.
    Works on all networks.
    
    Args:
        tx_hash (str): Transaction hash to look up
        
    Returns:
        dict: Transaction information
    """
    try:
        if not eth_client.w3 or not eth_client.w3.is_connected():
            return {"success": False, "error": "Not connected to any network"}
            
        tx = eth_client.w3.eth.get_transaction(tx_hash)
        tx_receipt = eth_client.w3.eth.get_transaction_receipt(tx_hash)
        
        return {
            "success": True,
            "hash": tx.hash.hex(),
            "from": tx["from"],
            "to": tx.to,
            "value_eth": str(eth_client.w3.from_wei(tx.value, 'ether')),
            "gas": tx.gas,
            "gas_price": tx.gasPrice,
            "block_number": tx.blockNumber,
            "status": tx_receipt.status,
            "gas_used": tx_receipt.gasUsed,
            "network": eth_client.current_network
        }
    except Exception as e:
        return {"success": False, "error": str(e)}

def list_ethereum_accounts() -> dict:
    """
    List all created Ethereum accounts with their balances.
    Note: Only shows local testnet accounts for safety.
    
    Returns:
        dict: List of accounts
    """
    try:
        if eth_client.current_network != "local_testnet":
            return {
                "success": True,
                "accounts": [],
                "total_accounts": 0,
                "message": "Account list only available on local testnet for safety"
            }
            
        accounts_info = []
        for name, data in eth_client.accounts.items():
            balance = get_ethereum_balance(data['address'])
            accounts_info.append({
                "name": name,
                "address": data['address'],
                "balance_eth": balance.get('balance_eth', '0') if balance.get('success') else '0'
            })
        
        return {
            "success": True,
            "accounts": accounts_info,
            "total_accounts": len(accounts_info),
            "network": eth_client.current_network
        }
    except Exception as e:
        return {"success": False, "error": str(e)}

def mine_ethereum_blocks(num_blocks: int = 1) -> dict:
    """
    Mine blocks on the local testnet (for testing purposes).
    Note: Only works on local testnet.
    
    Args:
        num_blocks (int): Number of blocks to mine
        
    Returns:
        dict: Mining result
    """
    try:
        if eth_client.current_network != "local_testnet" or not eth_client.eth_tester:
            return {
                "success": False, 
                "error": "Block mining only available on local testnet"
            }
        
        initial_block = eth_client.w3.eth.block_number
        for _ in range(num_blocks):
            eth_client.eth_tester.mine_block()
        
        final_block = eth_client.w3.eth.block_number
        
        return {
            "success": True,
            "initial_block": initial_block,
            "final_block": final_block,
            "blocks_mined": num_blocks,
            "network": eth_client.current_network
        }
    except Exception as e:
        return {"success": False, "error": str(e)}

def deploy_smart_contract(source_code: str, contract_name: str, from_account: str, constructor_args: list = None) -> dict:
    """
    Compile and deploy a smart contract to the blockchain.
    Note: Only works on local testnet for safety.
    
    Args:
        source_code (str): Solidity source code
        contract_name (str): Name of the contract to deploy  
        from_account (str): Name of the account or eth address to deploy from
        constructor_args (list): Constructor arguments if any
        
    Returns:
        dict: Contract deployment result
    """
    return eth_client.compile_and_deploy_contract(source_code, contract_name, from_account, constructor_args)


# Gradio Interface Functions
def switch_network_ui(network: str, api_key: str):
    """Gradio wrapper for network switching"""
    result = switch_ethereum_network(network, api_key if api_key.strip() else None)
    return json.dumps(result, indent=2)

def get_network_info_ui():
    """Get current network info for UI"""
    result = get_network_info()
    if result.get('success'):
        info = f"Network: {result['network']}\n"
        info += f"Connected: {result['connected']}\n"
        info += f"Latest Block: {result['latest_block']}\n"
        info += f"Chain ID: {result.get('chain_id', 'N/A')}\n"
        info += f"Read-Only Mode: {result['readonly_mode']}"
        return info
    return f"Error: {result.get('error', 'Unknown error')}"

def create_account_ui(name: str):
    """Gradio wrapper for create account"""
    if not name:
        return "Please provide an account name"
    result = create_ethereum_account(name)
    return json.dumps(result, indent=2)

def get_balance_ui(address: str):
    """Gradio wrapper for get balance"""
    if not address:
        return "Please provide an address"
    result = get_ethereum_balance(address)
    return json.dumps(result, indent=2)

def send_transaction_ui(from_account: str, to_address: str, amount_eth: float):
    """Gradio wrapper for send transaction"""
    if not from_account or not to_address or amount_eth <= 0:
        return "Please provide valid transaction details"
    result = send_ethereum_transaction(from_account, to_address, amount_eth)
    return json.dumps(result, indent=2)

def get_tx_info_ui(tx_hash: str):
    """Gradio wrapper for transaction info"""
    if not tx_hash:
        return "Please provide a transaction hash"
    result = get_transaction_info(tx_hash)
    return json.dumps(result, indent=2)

def get_accounts_list():
    """Get list of current accounts with balances"""
    result = list_ethereum_accounts()
    if result.get('success'):
        accounts = result.get('accounts', [])
        if accounts:
            account_list = []
            for acc in accounts:
                account_list.append(f"{acc['name']}: {acc['address']} ({acc['balance_eth']} ETH)")
            return "\n".join(account_list)
        else:
            return result.get('message', 'No accounts available')
    return f"Error: {result.get('error', 'Unknown error')}"

def mine_blocks_ui(num_blocks: int):
    """Mine blocks UI wrapper"""
    if num_blocks <= 0:
        return "Please provide a positive number of blocks"
    result = mine_ethereum_blocks(num_blocks)
    return json.dumps(result, indent=2)

def deploy_contract_ui(source_code: str, contract_name: str, from_account: str, constructor_args_str: str):
    """Gradio wrapper for contract deployment"""
    if not source_code or not contract_name or not from_account:
        return "Please provide source code, contract name, and from account or address"
    
    # Parse constructor arguments if provided
    constructor_args = []
    if constructor_args_str.strip():
        try:
            # Parse constructor arguments with type conversion
            args_list = [arg.strip() for arg in constructor_args_str.split(',') if arg.strip()]
            
            for arg in args_list:
                # Try to convert to appropriate types
                if arg.lower() in ['true', 'false']:
                    # Boolean
                    constructor_args.append(arg.lower() == 'true')
                elif arg.startswith('0x') and len(arg) == 42:
                    # Ethereum address
                    constructor_args.append(to_checksum_address(arg))
                elif arg.startswith('"') and arg.endswith('"'):
                    # String (remove quotes)
                    constructor_args.append(arg[1:-1])
                elif arg.startswith("'") and arg.endswith("'"):
                    # String (remove quotes)
                    constructor_args.append(arg[1:-1])
                elif '.' in arg:
                    # Float/Decimal - convert to int if it's actually a whole number
                    try:
                        float_val = float(arg)
                        if float_val.is_integer():
                            constructor_args.append(int(float_val))
                        else:
                            constructor_args.append(float_val)
                    except ValueError:
                        constructor_args.append(arg)  # Keep as string if conversion fails
                else:
                    # Try integer first, then keep as string
                    try:
                        constructor_args.append(int(arg))
                    except ValueError:
                        constructor_args.append(arg)
                        
        except Exception as e:
            return f"Error parsing constructor arguments: {str(e)}"
    
    result = deploy_smart_contract(source_code, contract_name, from_account, constructor_args)
    return json.dumps(result, indent=2)

# Create Gradio Interface
def create_gradio_app():
    """Create the Gradio web interface"""
    
    with gr.Blocks(title="Ethereum MCP Server", theme=gr.themes.Soft()) as app:
        gr.Markdown("# ⛓️ Ethereum MCP Server ")
        gr.Markdown("🌐 **Safety** - Mainnet is read-only, transactions only on local testnet")
        
        with gr.Tabs():
            # Network Management Tab
            with gr.Tab("🌐 Network Management"):
                gr.Markdown("### Switch Ethereum Networks")
                gr.Markdown("**Available Networks:**")
                gr.Markdown("- **Local TestNet**: Full functionality, safe for testing")
                gr.Markdown("- **Ethereum Mainnet**: Read-only access (balance checks, block info)")
                gr.Markdown("- **Ethereum Sepolia**: Testnet with real network conditions")
                
                with gr.Row():
                    with gr.Column():
                        network_choice = gr.Radio(
                            choices=["local_testnet", "ethereum_mainnet", "ethereum_sepolia"],
                            value="local_testnet",
                            label="Select Network"
                        )
                        api_key_input = gr.Textbox(
                            label="API Key (Infura/Alchemy)", 
                            placeholder="Required for mainnet/sepolia",
                            type="password"
                        )
                        switch_btn = gr.Button("Switch Network", variant="primary")
                        switch_output = gr.Textbox(label="Result", lines=8)
                        
                        switch_btn.click(
                            switch_network_ui,
                            inputs=[network_choice, api_key_input],
                            outputs=[switch_output]
                        )
                    
                    with gr.Column():
                        gr.Markdown("#### Current Network Status")
                        network_info = gr.Textbox(label="Network Info", lines=6, interactive=False)
                        refresh_network = gr.Button("Refresh Network Info")
                        refresh_network.click(
                            get_network_info_ui,
                            outputs=[network_info]
                        )
            
            # Account Management Tab
            with gr.Tab("πŸ‘€ Account Management"):
                gr.Markdown("### Ethereum Accounts")
                gr.Markdown("⚠️ **Safety Note**: Account creation only available on local testnet")
                
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("#### Create New Account")
                        create_name = gr.Textbox(label="Account Name", placeholder="my-account")
                        create_btn = gr.Button("Create Account", variant="primary")
                        create_output = gr.Textbox(label="Result", lines=10)
                        
                        create_btn.click(
                            create_account_ui,
                            inputs=[create_name],
                            outputs=[create_output]
                        )
                
                gr.Markdown("#### Current Accounts")
                accounts_display = gr.Textbox(label="Accounts", lines=5, interactive=False)
                refresh_accounts = gr.Button("Refresh Accounts")
                refresh_accounts.click(
                    get_accounts_list,
                    outputs=[accounts_display]
                )
            
            # Balance & Transactions Tab
            with gr.Tab("πŸ’° Balance & Transactions"):
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("#### Check Balance")
                        gr.Markdown("*Works on all networks*")
                        balance_address = gr.Textbox(
                            label="Address", 
                            placeholder="0x... (works with any Ethereum address)"
                        )
                        balance_btn = gr.Button("Get Balance", variant="primary")
                        balance_output = gr.Textbox(label="Balance Info", lines=8)
                        
                        balance_btn.click(
                            get_balance_ui,
                            inputs=[balance_address],
                            outputs=[balance_output]
                        )
                        
                        gr.Markdown("#### Transaction Info")
                        tx_hash_input = gr.Textbox(label="Transaction Hash", placeholder="0x...")
                        tx_info_btn = gr.Button("Get Transaction Info")
                        tx_info_output = gr.Textbox(label="Transaction Details", lines=8)
                        
                        tx_info_btn.click(
                            get_tx_info_ui,
                            inputs=[tx_hash_input],
                            outputs=[tx_info_output]
                        )
                    
                    with gr.Column():
                        gr.Markdown("#### Send Transaction")
                        gr.Markdown("*⚠️ Only available on local testnet for safety*")
                        tx_from = gr.Textbox(label="From Account Name", placeholder="my-account")
                        tx_to = gr.Textbox(label="To Address", placeholder="0x...")
                        tx_amount = gr.Number(label="Amount (ETH)", minimum=0, step=0.001)
                        tx_btn = gr.Button("Send Transaction", variant="primary")
                        tx_output = gr.Textbox(label="Transaction Result", lines=8)
                        
                        tx_btn.click(
                            send_transaction_ui,
                            inputs=[tx_from, tx_to, tx_amount],
                            outputs=[tx_output]
                        )
            
            # Blockchain Info Tab
            with gr.Tab("πŸ“Š Blockchain Info"):
                gr.Markdown("### Blockchain Information")
                gr.Markdown("*Available on all networks*")
                
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("#### Latest Block Info")
                        latest_block_btn = gr.Button("Get Latest Block", variant="primary")
                        latest_block_output = gr.JSON(label="Latest Block")
                        latest_block_btn.click(
                            fn=get_latest_ethereum_block, 
                            outputs=latest_block_output
                        )
                        
                        gr.Markdown("#### Mine Blocks (Local TestNet Only)")
                        mine_blocks_input = gr.Number(label="Number of Blocks", value=1, minimum=1, step=1)
                        mine_btn = gr.Button("Mine Blocks")
                        mine_output = gr.Textbox(label="Mining Result", lines=5)
                        
                        mine_btn.click(
                            mine_blocks_ui,
                            inputs=[mine_blocks_input],
                            outputs=[mine_output]
                        )
                    
                    with gr.Column():
                        gr.Markdown("#### All Accounts Info")
                        list_accounts_btn = gr.Button("List All Accounts") 
                        accounts_json_output = gr.JSON(label="All Accounts")
                        list_accounts_btn.click(
                            fn=list_ethereum_accounts,
                            outputs=accounts_json_output
                        )
            with gr.Tab("πŸ“„ Smart Contracts"):
                gr.Markdown("### Deploy Smart Contracts")
                gr.Markdown("*⚠️ Only available on local testnet for safety*")
                
                with gr.Row():
                    with gr.Column():
                        gr.Markdown("#### Contract Deployment")
                        contract_source = gr.Textbox(
                            label="Solidity Source Code",
                            placeholder="""pragma solidity ^0.8.19;

contract SimpleStorage {
    uint256 public storedData;
    
    constructor(uint256 _initialValue) {
        storedData = _initialValue;
    }
    
    function set(uint256 _value) public {
        storedData = _value;
    }
    
    function get() public view returns (uint256) {
        return storedData;
    }
}""",
                            lines=15
                        )
                        
                        with gr.Row():
                            contract_name_input = gr.Textbox(
                                label="Contract Name", 
                                placeholder="SimpleStorage"
                            )
                            deploy_from_account = gr.Textbox(
                                label="Deploy From Account", 
                                placeholder="test_account_1"
                            )
                        
                        constructor_args_input = gr.Textbox(
                            label="Constructor Arguments (comma-separated)", 
                            placeholder="100",
                            info="Examples: 100 (number), \"Hello\" (string), 0x1234...abcd (address), true/false (boolean)"
                        )
                        
                        deploy_btn = gr.Button("Deploy Contract", variant="primary")
                        deploy_output = gr.Textbox(label="Deployment Result", lines=12)
                        
                        deploy_btn.click(
                            deploy_contract_ui,
                            inputs=[contract_source, contract_name_input, deploy_from_account, constructor_args_input],
                            outputs=[deploy_output]
                        )
                    
                    with gr.Column():
                        gr.Markdown("#### Example Contracts")
                        gr.Markdown("""
**Simple Storage Contract:**
```solidity
pragma solidity ^0.8.19;

contract SimpleStorage {
    uint256 public storedData;
    
    constructor(uint256 _initialValue) {
        storedData = _initialValue;
    }
    
    function set(uint256 _value) public {
        storedData = _value;
    }
}""")
        
        gr.Markdown("---")
        gr.Markdown("### πŸ”’ Safety Features")
        gr.Markdown("- **Mainnet Read-Only**: No transactions on mainnet, only balance/block queries")
        gr.Markdown("- **No Private Key Storage**: Keys only exist in local testnet memory")
        gr.Markdown("- **API Key Security**: Keys entered are not logged or stored")
        
        gr.Markdown("### πŸ€– MCP Integration")
        gr.Markdown("**Available MCP Tools**: `switch_ethereum_network`, `get_network_info`, `create_ethereum_account`, `get_ethereum_balance`, `send_ethereum_transaction`, `get_latest_ethereum_block`, `get_transaction_info`, `list_ethereum_accounts`, `mine_ethereum_blocks`, `deploy_smart_contract`")
    
    return app

# Launch the interface
if __name__ == "__main__":
    print("πŸš€ Starting Enhanced Ethereum MCP Server...")
    print(f"πŸ“Š Network: {eth_client.current_network}")
    print(f"βœ… Connected: {eth_client.w3.is_connected()}")
    print(f"🧱 Latest block: {eth_client.w3.eth.block_number}")
    print("πŸ”’ Prototype - mainnet is read-only for security reason")
    
    # Create and launch the app
    demo = create_gradio_app()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True,
        mcp_server=True 
    )