18
18
19
19
// ** RPC Interactions: **
20
20
//current_block_height
21
+ //electrum_rpc_init
21
22
//electrum_rpc_blockheight
23
+ //electrum_rpc
22
24
//mempoolspace_rpc_init
23
25
//mempoolspace_rpc_blockheight
24
26
//mempoolspace_blockheight_fails
25
- //electrum_rpc
26
27
//mempoolspace_rpc
27
28
//infura_txd_rpc
28
29
//eth_params
@@ -1810,7 +1811,7 @@ function process_dash_transactions(rd, api_data, rdo) {
1810
1811
function current_block_height ( rd , api_data , rdo ) {
1811
1812
const api_name = api_data . name ;
1812
1813
if ( api_name === "electrum" ) {
1813
- electrum_rpc_blockheight ( rd , api_data , rdo ) ;
1814
+ electrum_rpc_init ( rd , api_data , rdo ) ;
1814
1815
return
1815
1816
}
1816
1817
if ( api_name === "blockchain.info" ) {
@@ -1820,6 +1821,15 @@ function current_block_height(rd, api_data, rdo) {
1820
1821
mempoolspace_rpc_init ( rd , api_data , rdo , true ) ;
1821
1822
}
1822
1823
1824
+ // Routes Electrum server API requests between address polling and block height verification for Bitcoin transactions
1825
+ function electrum_rpc_init ( rd , api_data , rdo ) {
1826
+ if ( rdo . source === "addr_polling" ) {
1827
+ electrum_rpc ( rd , api_data , rdo ) ;
1828
+ return
1829
+ }
1830
+ electrum_rpc_blockheight ( rd , api_data , rdo ) ;
1831
+ }
1832
+
1823
1833
// Fetches current Bitcoin block height from Electrum servers for confirmation calculations
1824
1834
function electrum_rpc_blockheight ( rd , api_data , rdo ) {
1825
1835
const rpc_url = api_data . url ;
@@ -1857,60 +1867,6 @@ function electrum_rpc_blockheight(rd, api_data, rdo) {
1857
1867
} ) ;
1858
1868
}
1859
1869
1860
- // Routes mempool.space API requests between address polling and block height verification for Bitcoin transactions
1861
- function mempoolspace_rpc_init ( rd , api_data , rdo , rpc ) {
1862
- if ( rdo . source === "addr_polling" ) {
1863
- mempoolspace_rpc ( rd , api_data , rdo , rpc , false ) ;
1864
- return
1865
- }
1866
- mempoolspace_rpc_blockheight ( rd , api_data , rdo , rpc ) ;
1867
- }
1868
-
1869
- // Fetches current Bitcoin block height from mempool.space API for confirmation calculations
1870
- function mempoolspace_rpc_blockheight ( rd , api_data , rdo , rpc ) {
1871
- const currency = rd . payment ,
1872
- btc_base = glob_const . mempool_space [ currency ] ;
1873
- if ( ! btc_base ) {
1874
- mempoolspace_blockheight_fails ( rd , api_data , rdo , rpc ) ;
1875
- return
1876
- }
1877
- const api_url = api_data . url ,
1878
- base_url = rpc ? api_url : btc_base ,
1879
- api_name = api_data . name ;
1880
- let block_height = null ;
1881
- api_proxy ( { // get latest blockheight
1882
- "api_url" : base_url + "/api/blocks/tip/height" ,
1883
- "proxy" : base_url . includes ( ".onion" ) ,
1884
- "params" : {
1885
- "method" : "GET"
1886
- }
1887
- } ) . done ( function ( response ) {
1888
- const api_result = br_result ( response ) . result ;
1889
- if ( api_result ) {
1890
- if ( ! api_result . error ) {
1891
- block_height = api_result ;
1892
- }
1893
- }
1894
- if ( api_name === "electrum" ) {
1895
- electrum_rpc ( rd , api_data , rdo , block_height ) ;
1896
- return
1897
- }
1898
- mempoolspace_rpc ( rd , api_data , rdo , rpc , block_height ) ;
1899
- } ) . fail ( function ( xhr , stat , err ) {
1900
- mempoolspace_blockheight_fails ( rd , api_data , rdo , rpc ) ;
1901
- } ) . always ( function ( ) {
1902
- update_api_source ( rdo , api_data ) ;
1903
- } ) ;
1904
- }
1905
-
1906
- function mempoolspace_blockheight_fails ( rd , api_data , rdo , rpc ) {
1907
- if ( api_data . name === "electrum" ) {
1908
- electrum_rpc ( rd , api_data , rdo ) ;
1909
- return
1910
- }
1911
- mempoolspace_rpc ( rd , api_data , rdo , rpc ) ;
1912
- }
1913
-
1914
1870
// Processes Electrum transactions via RPC with support for account scanning and block verification
1915
1871
function electrum_rpc ( rd , api_data , rdo , latest_block ) {
1916
1872
const currency = rd . payment ,
@@ -1921,8 +1877,7 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
1921
1877
script_hash = script_pub . hash ,
1922
1878
script_pub_key = script_pub . script_pub_key ,
1923
1879
rpc_url = api_data . url ,
1924
- pending = rdo . pending ,
1925
- set_confirmations = latest_block ? rdo . setconfirmations : 1 ;
1880
+ pending = rdo . pending ;
1926
1881
let tx_count = 0 ,
1927
1882
matched_tx = false ;
1928
1883
if ( pending === "scanning" ) { // scan incoming transactions on address
@@ -1954,7 +1909,7 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
1954
1909
}
1955
1910
if ( has_tx ( api_result ) ) {
1956
1911
$ . each ( api_result , function ( key , tx ) {
1957
- const parsed_tx = electrum_scan_data ( tx , set_confirmations , rd . currencysymbol , script_pub_key , latest_block ) ;
1912
+ const parsed_tx = electrum_scan_data ( tx , rdo . setconfirmations , rd . currencysymbol , script_pub_key , latest_block ) ;
1958
1913
if ( parsed_tx . transactiontime > rdo . request_timestamp && parsed_tx . ccval ) {
1959
1914
matched_tx = parsed_tx ;
1960
1915
if ( source === "requests" ) {
@@ -1977,7 +1932,7 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
1977
1932
handle_scan_failure ( {
1978
1933
"error" : error_data ,
1979
1934
"is_proxy" : is_proxy_error
1980
- } , rd , api_data , rdo , network ) ;
1935
+ } , rd , api_data , rdo ) ;
1981
1936
} ) . always ( function ( ) {
1982
1937
update_api_source ( rdo , api_data ) ;
1983
1938
} ) ;
@@ -2012,7 +1967,7 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
2012
1967
} , rd , api_data , rdo ) ;
2013
1968
return
2014
1969
}
2015
- const parsed_tx = electrum_scan_data ( api_result , set_confirmations , rd . currencysymbol , script_pub_key , latest_block , tx_hash ) ;
1970
+ const parsed_tx = electrum_scan_data ( api_result , rdo . setconfirmations , rd . currencysymbol , script_pub_key , latest_block , tx_hash ) ;
2016
1971
if ( parsed_tx . ccval ) {
2017
1972
matched_tx = parsed_tx ;
2018
1973
tx_count = 1 ;
@@ -2040,6 +1995,60 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
2040
1995
}
2041
1996
}
2042
1997
1998
+ // Routes mempool.space API requests between address polling and block height verification for Bitcoin transactions
1999
+ function mempoolspace_rpc_init ( rd , api_data , rdo , rpc ) {
2000
+ if ( rdo . source === "addr_polling" ) {
2001
+ mempoolspace_rpc ( rd , api_data , rdo , rpc , false ) ;
2002
+ return
2003
+ }
2004
+ mempoolspace_rpc_blockheight ( rd , api_data , rdo , rpc ) ;
2005
+ }
2006
+
2007
+ // Fetches current Bitcoin block height from mempool.space API for confirmation calculations
2008
+ function mempoolspace_rpc_blockheight ( rd , api_data , rdo , rpc ) {
2009
+ const currency = rd . payment ,
2010
+ btc_base = glob_const . mempool_space [ currency ] ;
2011
+ if ( ! btc_base ) {
2012
+ mempoolspace_blockheight_fails ( rd , api_data , rdo , rpc ) ;
2013
+ return
2014
+ }
2015
+ const api_url = api_data . url ,
2016
+ base_url = rpc ? api_url : btc_base ,
2017
+ api_name = api_data . name ;
2018
+ let block_height = null ;
2019
+ api_proxy ( { // get latest blockheight
2020
+ "api_url" : base_url + "/api/blocks/tip/height" ,
2021
+ "proxy" : base_url . includes ( ".onion" ) ,
2022
+ "params" : {
2023
+ "method" : "GET"
2024
+ }
2025
+ } ) . done ( function ( response ) {
2026
+ const api_result = br_result ( response ) . result ;
2027
+ if ( api_result ) {
2028
+ if ( ! api_result . error ) {
2029
+ block_height = api_result ;
2030
+ }
2031
+ }
2032
+ if ( api_name === "electrum" ) {
2033
+ electrum_rpc ( rd , api_data , rdo , block_height ) ;
2034
+ return
2035
+ }
2036
+ mempoolspace_rpc ( rd , api_data , rdo , rpc , block_height ) ;
2037
+ } ) . fail ( function ( xhr , stat , err ) {
2038
+ mempoolspace_blockheight_fails ( rd , api_data , rdo , rpc ) ;
2039
+ } ) . always ( function ( ) {
2040
+ update_api_source ( rdo , api_data ) ;
2041
+ } ) ;
2042
+ }
2043
+
2044
+ function mempoolspace_blockheight_fails ( rd , api_data , rdo , rpc ) {
2045
+ if ( api_data . name === "electrum" ) {
2046
+ electrum_rpc ( rd , api_data , rdo ) ;
2047
+ return
2048
+ }
2049
+ mempoolspace_rpc ( rd , api_data , rdo , rpc ) ;
2050
+ }
2051
+
2043
2052
// Processes Bitcoin transactions through mempool.space API with transaction filtering and block height validation
2044
2053
function mempoolspace_rpc ( rd , api_data , rdo , rpc , latest_block ) {
2045
2054
const tx_list = rdo . transactionlist ,
@@ -2070,11 +2079,10 @@ function mempoolspace_rpc(rd, api_data, rdo, rpc, latest_block) {
2070
2079
return
2071
2080
}
2072
2081
if ( has_tx ( api_result ) ) {
2073
- const sorted_txs = sort_transactions_by_date ( mempoolspace_scan_data , api_result ) ,
2074
- set_confirmations = latest_block ? rdo . setconfirmations : 1 ;
2082
+ const sorted_txs = sort_transactions_by_date ( mempoolspace_scan_data , api_result ) ;
2075
2083
$ . each ( sorted_txs , function ( date , tx ) {
2076
2084
if ( tx . txid ) { // filter outgoing transactions
2077
- const parsed_tx = mempoolspace_scan_data ( tx , set_confirmations , rd . currencysymbol , rd . address , latest_block ) ;
2085
+ const parsed_tx = mempoolspace_scan_data ( tx , rdo . setconfirmations , rd . currencysymbol , rd . address , latest_block ) ;
2078
2086
if ( parsed_tx . transactiontime > rdo . request_timestamp && parsed_tx . ccval ) {
2079
2087
matched_tx = parsed_tx ;
2080
2088
if ( source === "requests" ) {
@@ -2459,7 +2467,8 @@ function calculate_total_outputs(outputs, address, value_processor) {
2459
2467
function normalize_timestamp ( raw_timestamp , convert_utc ) {
2460
2468
if ( ! raw_timestamp ) return convert_utc ? now_utc ( ) : now ( ) ;
2461
2469
const timestamp_ms = raw_timestamp * 1000 ;
2462
- return convert_utc ? timestamp_ms + glob_const . timezone : timestamp_ms ;
2470
+ tx_time_margin = timestamp_ms - 3000 ; // 3 second margin for unknown timestamps in mempool default to now()
2471
+ return convert_utc ? tx_time_margin + glob_const . timezone : tx_time_margin ;
2463
2472
}
2464
2473
2465
2474
// Computes blockchain confirmation count with validation against latest block height
@@ -2532,7 +2541,7 @@ function mempoolspace_ws_data(data, setconfirmations, ccsymbol, address) {
2532
2541
// Processes mempool.space transaction data with block height confirmation calculation
2533
2542
function mempoolspace_scan_data ( data , setconfirmations , ccsymbol , address , latest_block ) {
2534
2543
const status = data . status ,
2535
- tx_timestamp = status . block_time ? status . block_time * 1000 : now ( ) ,
2544
+ tx_timestamp = normalize_timestamp ( status . block_time ) ,
2536
2545
transactiontime = tx_timestamp ? tx_timestamp + glob_const . timezone : null ;
2537
2546
if ( setconfirmations === "sort" ) {
2538
2547
return transactiontime ;
@@ -2544,7 +2553,7 @@ function mempoolspace_scan_data(data, setconfirmations, ccsymbol, address, lates
2544
2553
const total_output = calculate_total_outputs ( data . vout , address , process_value ) ,
2545
2554
height = status . block_height ,
2546
2555
min_confs = status . confirmed ? 1 : 0 ,
2547
- confirmations = latest_block ? get_block_confirmations ( status . block_height , latest_block ) : min_confs || height ;
2556
+ confirmations = ( height && latest_block ) ? get_block_confirmations ( status . block_height , latest_block ) : min_confs || height || 0 ;
2548
2557
return {
2549
2558
"ccval" : total_output ? total_output / 1e8 : null ,
2550
2559
transactiontime,
0 commit comments