@@ -23,6 +23,8 @@ internal class PaymentService : IPaymentService
2323 IContractService ContractService { get ; set ; }
2424 IUtilities Utilities { get ; set ; }
2525 IAuthenicationService AuthenicationService { get ; set ; }
26+
27+ //Init the constructor and inherit all dependencies.
2628 public PaymentService ( )
2729 {
2830 ContractService = ServiceHelper . GetService < IContractService > ( ) ;
@@ -32,30 +34,25 @@ public PaymentService()
3234
3335 public async Task < TransactionResult > BeginTransaction ( )
3436 {
35-
36- MauiProgram . HideTokenList = "none" ;
37- MauiProgram . HideTokenSend = "none" ;
38- MauiProgram . ShowPinPanel = "none" ;
39-
40-
41- var wallet = AuthenicationService . UnlockWallet ( MauiProgram . Pass ) ;
37+ var wallet = AuthenicationService . UnlockWallet ( MauiProgram . Pass ) ; //One of decrypt the PK encoded on the device and open the wallet.
4238
39+ //If wallet doesn't exist return null
4340 if ( wallet == null )
4441 return null ;
4542
43+ //Check if a contract exists (Native currencies don't have a contract) if false, send native chain token.
4644 if ( string . IsNullOrEmpty ( MauiProgram . SelectedContract . ContractAddress ) )
4745 await ContractService . ExecuteNative ( MauiProgram . ReceiverAddress , MauiProgram . Amount , wallet , MauiProgram . ActiveNetwork . Endpoint , MauiProgram . ActiveNetwork . Chainid ) ;
4846 else
4947 await ContractService . ExecutePayments ( MauiProgram . ReceiverAddress , MauiProgram . SelectedContract , MauiProgram . Amount , wallet , MauiProgram . ActiveNetwork . Endpoint , MauiProgram . ActiveNetwork . Chainid ) ;
5048
51- //Clear PK, password etc.
52-
53-
54-
55- var dateTime = DateTime . UtcNow . AddSeconds ( 30 ) ;
49+ //Defer next check, it will be validated after the next block regardless.
50+ var dateTime = DateTime . UtcNow . AddSeconds ( 30 ) ;
5651
52+ //Loop and wait till the transaction was valid, important no recursion here due to that the transaction hash has already been created we are only checking the status.
5753 while ( TransactionResult == null )
5854 {
55+ //If time is over the check time validate if the transaction is validated
5956 if ( DateTime . UtcNow > dateTime )
6057 {
6158 await ValidateTransaction ( MauiProgram . TxHash ) ;
@@ -70,12 +67,22 @@ public async Task<TransactionResult> BeginTransaction()
7067
7168 public async Task < bool > ValidateTransaction ( string txHash )
7269 {
73-
70+ //We don't need a real wallet here, Pass is empty however due to how Nethereum operates we need to make the request from the object, so we can inherit the chain
71+ //That's why we just instance a object to interact with the chain.
7472 var account = AuthenicationService . UnlockWallet ( MauiProgram . Pass ) ;
7573
74+ //If Hash is empty return false. Return an empty object to avoid recursion in the loop.
7675 if ( string . IsNullOrEmpty ( txHash ) )
76+ {
77+ TransactionResult = new TransactionResult
78+ {
79+ TransactionHash = "-1"
80+ } ;
81+ MauiProgram . TxHash = string . Empty ;
7782 return false ;
78-
83+ }
84+
85+ //If the transaction failed at creation, and there is no hash we return an error object.
7986 if ( txHash == "-" )
8087 {
8188 TransactionResult = new TransactionResult
@@ -84,12 +91,14 @@ public async Task<bool> ValidateTransaction(string txHash)
8491 From = "--" ,
8592 To = "--" ,
8693 Timestamp = DateTime . UtcNow ,
87- TransactionHash = "Transaction failed, internal error, inssuficient balance or gas!"
94+ TransactionHash = "Transaction failed, internal error, insuficient balance or gas!"
8895 } ;
89- MauiProgram . TxHash = String . Empty ;
96+ MauiProgram . TxHash = string . Empty ;
9097 return false ;
9198 }
9299
100+ //Create a new web3 instance, using the account object, for the chain data and the current selected network.
101+ //We send a request to the blockchain to check the receipt of the transaction.
93102 var web3 = new Web3 ( account , MauiProgram . ActiveNetwork . Endpoint ) ;
94103 var transactionReceipt = await web3 . Eth . Transactions . GetTransactionReceipt . SendRequestAsync ( txHash ) ;
95104 if ( transactionReceipt == null ) //Check if transaction is processed in case it fails sent it to quoue;
@@ -98,6 +107,8 @@ public async Task<bool> ValidateTransaction(string txHash)
98107 return false ;
99108 }
100109
110+ //Decode events in case it's a proxy contract, ETC ETH smart contract
111+ //If transferEventOutput is 0, it's a native token transfer.
101112 var transferEventOutput = transactionReceipt . DecodeAllEvents < TransferEventDTO > ( ) ;
102113 if ( transferEventOutput . Count == 0 )
103114 {
@@ -111,21 +122,28 @@ public async Task<bool> ValidateTransaction(string txHash)
111122 Timestamp = DateTime . UtcNow ,
112123 TransactionHash = transactionReceipt . TransactionHash
113124 } ;
114- MauiProgram . TxHash = String . Empty ;
125+ MauiProgram . TxHash = string . Empty ;
115126 return true ;
116127 }
117128 else if ( transactionReceipt . Failed ( ) )
118129 {
119-
120- MauiProgram . TxHash = String . Empty ;
130+ TransactionResult = new TransactionResult
131+ {
132+ Amount = 0 ,
133+ From = "--" ,
134+ To = "--" ,
135+ Timestamp = DateTime . UtcNow ,
136+ TransactionHash = "Transaction failed, internal error, insuficient balance or gas!"
137+ } ;
138+ MauiProgram . TxHash = string . Empty ;
121139 return false ;
122140 }
123141 }
124142
125-
143+ //Get the initial main trainsfer event.
126144 var transferEvent = transferEventOutput . FirstOrDefault ( ) . Event ;
127145
128-
146+ //We get the ETH contract actual tokens by substracting the decimals that are obsolete
129147 var actualTransfer = Utilities . ConvertToDex ( ( decimal ) transferEvent . Value , MauiProgram . SelectedContract . Decimals ) ;
130148 if ( transferEvent . From . ToUpper ( ) != MauiProgram . PublicAddress . ToUpper ( ) || actualTransfer < 0 )
131149 return false ;
@@ -140,7 +158,7 @@ public async Task<bool> ValidateTransaction(string txHash)
140158 Timestamp = DateTime . UtcNow ,
141159 TransactionHash = transactionReceipt . TransactionHash
142160 } ;
143- MauiProgram . TxHash = String . Empty ;
161+ MauiProgram . TxHash = string . Empty ;
144162
145163 return true ;
146164 }
0 commit comments