@@ -54,38 +54,36 @@ public bool checkTransactionReceived()
5454 }
5555
5656 string json = wc . DownloadString ( url ) ;
57+
5758 JsonDocument doc = JsonDocument . Parse ( json ) ;
5859 JsonElement root = doc . RootElement ;
59- JsonElement txs = root . GetProperty ( "txrefs " ) ;
60+ JsonElement txs = root . GetProperty ( "txs " ) ;
6061
6162 foreach ( JsonElement tx in txs . EnumerateArray ( ) )
6263 {
6364
6465 string dateString = tx . GetProperty ( "received" ) . GetString ( ) ;
65- DateTime dateTime = DateTime . ParseExact ( dateString , "yyyy-MM-ddTHH:mm:ssZ" , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal ) ;
66- long unixTime = ( ( DateTimeOffset ) dateTime ) . ToUnixTimeSeconds ( ) ;
67- if ( unixTime >= creationTime )
66+ long unixTime = parseToUnixTime ( dateString ) ;
67+ if ( unixTime > creationTime )
6868 {
6969 JsonElement txAmount = tx . GetProperty ( "outputs" ) ;
7070 foreach ( JsonElement txAmounts in txAmount . EnumerateArray ( ) )
7171 {
72- Console . WriteLine ( txAmounts . GetProperty ( "addresses" ) . GetString ( ) ) ;
73- if ( txAmounts . GetProperty ( "addresses" ) . GetString ( ) == address )
72+ if ( txAmounts . GetProperty ( "addresses" ) . EnumerateArray ( ) . First ( ) . GetString ( ) == address )
7473 {
7574 if ( checkEqualWithMargin ( Bitcoin . ConvertBitcoinToSatoshis ( amount ) , txAmounts . GetProperty ( "value" ) . GetInt32 ( ) , 0 ) )
7675 {
77- transactionHash = tx . GetProperty ( "tx_hash " ) . GetString ( ) ;
76+ transactionHash = tx . GetProperty ( "hash " ) . GetString ( ) ;
7877 receivedTime = unixTime ;
7978 OnTransactionReceived ( ) ;
8079 //checks if confirmation time exists and parses it if it does
8180 try
8281 {
8382 string confirmedString = tx . GetProperty ( "confirmed" ) . GetString ( ) ;
84- DateTime confirmeDateTime = DateTime . ParseExact ( confirmedString , "yyyy-MM-ddTHH:mm:ssZ" , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal ) ;
85- this . confirmationTime = ( ( DateTimeOffset ) confirmeDateTime ) . ToUnixTimeSeconds ( ) ;
83+ this . confirmationTime = parseToUnixTime ( confirmedString ) ;
8684 OnTransactionConfirmed ( ) ;
8785 }
88- catch
86+ catch ( Exception e )
8987 {
9088 }
9189 return true ;
@@ -112,7 +110,6 @@ public bool checkTransactionConfirmed()
112110 else if ( transactionHash == null )
113111 {
114112 checkTransactionReceived ( ) ;
115-
116113 return confirmationTime != 0 ;
117114 }
118115 string url = "" ;
@@ -125,15 +122,13 @@ public bool checkTransactionConfirmed()
125122 throw new Exception ( "Invalid coin type" ) ;
126123 break ;
127124 }
128-
129125 string json = wc . DownloadString ( url ) ;
130126 JsonDocument doc = JsonDocument . Parse ( json ) ;
131127 JsonElement root = doc . RootElement ;
132128 try
133129 {
134130 string confirmedString = root . GetProperty ( "confirmed" ) . GetString ( ) ;
135- DateTime confirmeDateTime = DateTime . ParseExact ( confirmedString , "yyyy-MM-ddTHH:mm:ssZ" , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal ) ;
136- this . confirmationTime = ( ( DateTimeOffset ) confirmeDateTime ) . ToUnixTimeSeconds ( ) ;
131+ this . confirmationTime = parseToUnixTime ( confirmedString ) ;
137132 OnTransactionConfirmed ( ) ;
138133 return true ;
139134 }
@@ -190,6 +185,19 @@ private void OnTransactionConfirmed()
190185 {
191186 TransactionConfirmed ? . Invoke ( this , new TransactionEventArgs ( this ) ) ;
192187 }
188+ public long parseToUnixTime ( string dateString )
189+ {
190+ int fractionalSecondsLength = dateString . Length - dateString . IndexOf ( '.' ) - 2 ;
193191
192+ if ( fractionalSecondsLength != 3 )
193+ {
194+ dateString = dateString . Substring ( 0 , dateString . IndexOf ( '.' ) + 4 ) + dateString . Substring ( dateString . IndexOf ( 'Z' ) ) ;
195+ }
196+
197+ string formatSpecifier = "yyyy-MM-ddTHH:mm:ss.fffZ" ;
198+ DateTimeOffset dateTimeOffset = DateTimeOffset . ParseExact ( dateString , formatSpecifier , CultureInfo . InvariantCulture ) ;
199+
200+ return dateTimeOffset . ToUnixTimeSeconds ( ) ;
201+ }
194202 }
195203}
0 commit comments