11using Anvil . Core . ViewModels ;
2+ using Anvil . Models ;
23using Anvil . Services ;
34using Anvil . Services . Network ;
45using Anvil . Services . Wallets ;
@@ -55,10 +56,13 @@ public class WalletViewModel : ViewModelBase
5556
5657 #endregion
5758
59+ private ApplicationState _appState ;
60+
5861 public WalletViewModel ( IClassicDesktopStyleApplicationLifetime appLifetime ,
5962 InternetConnectionService internetConnectionService ,
6063 IWalletService walletService , IRpcClientProvider rpcClientProvider ,
61- KeyStoreService keyStoreService , AddressBookService addressBookService )
64+ KeyStoreService keyStoreService , AddressBookService addressBookService ,
65+ ApplicationState applicationState )
6266 {
6367 _appLifetime = appLifetime ;
6468 _internetConnectionService = internetConnectionService ;
@@ -70,9 +74,13 @@ public WalletViewModel(IClassicDesktopStyleApplicationLifetime appLifetime,
7074 _walletService . OnWalletServiceStateChanged += OnWalletServiceStateChanged ;
7175 _keyStoreService = keyStoreService ;
7276 _addressBookService = addressBookService ;
77+ _appState = applicationState ;
78+
79+ _appState . PropertyChanged += ApplicationStateChanged ;
7380
7481 DerivationWalletsCollection = new ( ) ;
7582 PrivateKeyWalletsCollection = new ( ) ;
83+ CanRequestAirdrop = _appState . Network != Cluster . MainNet ;
7684 NoConnection = ! _internetConnectionService . IsConnected ;
7785 TransactionSubmission = TransactionSubmissionViewModel . NoShow ( ) ;
7886 TransactionSubmission . WhenAnyValue ( x => x . SubmittingTransaction )
@@ -84,11 +92,36 @@ public WalletViewModel(IClassicDesktopStyleApplicationLifetime appLifetime,
8492 RestoreFromWalletSnapshot ( false ) ;
8593 }
8694
95+ private void ApplicationStateChanged ( object sender , System . ComponentModel . PropertyChangedEventArgs e )
96+ {
97+ if ( e . PropertyName == "Network" )
98+ {
99+ CanRequestAirdrop = _appState . Network != Cluster . MainNet ;
100+ }
101+ }
102+
87103 private void OnNetworkConnectionChanged ( object sender , Services . Network . Events . NetworkConnectionChangedEventArgs e )
88104 {
89105 NoConnection = ! e . Connected ;
90106 }
91107
108+ public async void RequestAirdrop ( )
109+ {
110+ var req = await _rpcClient . RequestAirdropAsync ( _currentWallet . Address , SolHelper . ConvertToLamports ( 1m ) ) ;
111+ if ( req . WasSuccessful )
112+ {
113+ if ( req . Result != null )
114+ {
115+ TransactionSubmission = new TransactionSubmissionViewModel ( _rpcClientProvider , req . Result ) ;
116+ TransactionSubmission . Progress = "Successfully requested airdrop." ;
117+ await TransactionSubmission . PollConfirmation ( ) ;
118+ await GetAccountBalance ( ) ;
119+ await Task . Delay ( 15000 ) ;
120+ TransactionSubmission = TransactionSubmissionViewModel . NoShow ( ) ;
121+ }
122+ }
123+ }
124+
92125 private async Task < SendSolanaDialogViewModel > GetDestinationAddressAndAmount ( )
93126 {
94127 var vm = new SendSolanaDialogViewModel ( )
@@ -330,7 +363,7 @@ private Tuple<bool, string> ValidateMnemonicDelegate(string text)
330363
331364 return new Tuple < bool , string > ( false , "Mnemonic is invalid." ) ;
332365 }
333-
366+
334367 /// <summary>
335368 /// Private key validation for the dialog.
336369 /// </summary>
@@ -784,6 +817,13 @@ private void UpdateTokenCollection()
784817 }
785818 }
786819
820+ private bool _canRequestAirdrop ;
821+ public bool CanRequestAirdrop
822+ {
823+ get => _canRequestAirdrop ;
824+ set => this . RaiseAndSetIfChanged ( ref _canRequestAirdrop , value ) ;
825+ }
826+
787827 private bool _canSubmitTransaction ;
788828 public bool CanSubmitTransaction
789829 {
0 commit comments