@@ -1793,7 +1793,8 @@ function current_block_height(rd, api_data, rdo) {
17931793
17941794// Routes Electrum server API requests between address polling and block height verification for Bitcoin transactions
17951795function electrum_rpc_init ( rd , api_data , rdo ) {
1796- if ( rdo . source === "addr_polling" ) {
1796+ const source = rdo . source ;
1797+ if ( source === "addr_polling" || source === "after_scan" ) {
17971798 electrum_rpc ( rd , api_data , rdo ) ;
17981799 return
17991800 }
@@ -1847,12 +1848,15 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
18471848 script_hash = script_pub . hash ,
18481849 script_pub_key = script_pub . script_pub_key ,
18491850 rpc_url = api_data . url ,
1850- pending = rdo . pending ;
1851+ pending = rdo . pending ,
1852+ cachetime = rdo . cachetime ,
1853+ addr_polling = ( source === "addr_polling" || source === "after_scan" ) ,
1854+ endpoint = addr_polling ? "get_mempool" : "get_history" ;
18511855 let matched_tx = false ;
18521856 if ( pending === "scanning" ) { // scan incoming transactions on address
18531857 api_proxy ( {
18541858 "api" : currency ,
1855- "cachetime" : rdo . cachetime ,
1859+ cachetime,
18561860 "cachefolder" : "1h" ,
18571861 "custom" : "electrum" ,
18581862 "api_url" : rpc_url ,
@@ -1862,7 +1866,7 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
18621866 "cache" : true ,
18631867 "data" : {
18641868 "id" : "scanning" ,
1865- "method" : "blockchain.scripthash.get_history" ,
1869+ "method" : "blockchain.scripthash." + endpoint ,
18661870 "ref" : script_hash ,
18671871 "node" : rpc_url
18681872 }
@@ -1876,19 +1880,81 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
18761880 } , rd , api_data , rdo ) ;
18771881 return
18781882 }
1879- if ( has_tx ( api_result ) ) {
1880- $ . each ( api_result , function ( key , tx ) {
1881- const parsed_tx = electrum_scan_data ( tx , rdo . setconfirmations , rd . currencysymbol , script_pub_key , latest_block ) ;
1882- if ( parsed_tx . transactiontime > rdo . request_timestamp && parsed_tx . ccval ) {
1883- matched_tx = parsed_tx ;
1884- if ( source === "requests" ) {
1885- const tx_item = create_transaction_item ( parsed_tx , rd . requesttype ) ;
1886- if ( tx_item ) {
1887- tx_list . append ( tx_item . data ( parsed_tx ) ) ;
1883+ if ( addr_polling ) {
1884+ if ( empty_obj ( api_result ) ) {
1885+ glob_let . tx_count = 0 ; // set tx count
1886+ process_scan_results ( rd , api_data , rdo , matched_tx ) ;
1887+ return
1888+ }
1889+ const glob_tx_count = glob_let . tx_count ,
1890+ tx_count = api_result . length ;
1891+ if ( tx_count > glob_let . tx_count ) { // new tx detected
1892+ const latest_tx = api_result [ 0 ] . tx_hash ;
1893+ api_proxy ( {
1894+ "api" : currency ,
1895+ cachetime,
1896+ "cachefolder" : "1h" ,
1897+ "custom" : "electrum" ,
1898+ "api_url" : rpc_url ,
1899+ "proxy" : true ,
1900+ "params" : {
1901+ "method" : "POST" ,
1902+ "cache" : true ,
1903+ "data" : {
1904+ "id" : "scanning" ,
1905+ "method" : "blockchain.transaction.get" ,
1906+ "ref" : latest_tx ,
1907+ "node" : rpc_url
18881908 }
18891909 }
1890- }
1891- } ) ;
1910+ } ) . done ( function ( e ) {
1911+ const tx_result = br_result ( e ) ,
1912+ res = q_obj ( tx_result , "result" ) ;
1913+ if ( res ) {
1914+ const parsed_tx = electrum_scan_data ( res , rdo . setconfirmations , rd . currencysymbol , script_pub_key , latest_block , latest_tx ) ;
1915+ if ( parsed_tx . ccval ) {
1916+ matched_tx = parsed_tx ;
1917+ }
1918+ process_scan_results ( rd , api_data , rdo , matched_tx ) ;
1919+ return
1920+ }
1921+ handle_scan_failure ( null , rd , api_data , rdo ) ;
1922+ } ) . fail ( function ( xhr , stat , err ) {
1923+ const is_proxy_error = is_proxy_fail ( this . url ) ,
1924+ error_data = xhr || stat || err ;
1925+ handle_scan_failure ( {
1926+ "error" : error_data ,
1927+ "is_proxy" : is_proxy_error
1928+ } , rd , api_data , rdo , network ) ;
1929+ } ) . always ( function ( ) {
1930+ update_api_source ( rdo , api_data ) ;
1931+ } ) ;
1932+ return
1933+ }
1934+ if ( glob_tx_count === 1000000 ) { // tx count not set
1935+ // set initial tx count
1936+ glob_let . tx_count = tx_count ;
1937+ }
1938+ return
1939+ }
1940+ if ( api_result ) {
1941+ const tx_arr = api_result . tx_hash ? [ api_result ] : api_result ; // convert to array
1942+ if ( has_tx ( tx_arr ) ) {
1943+ $ . each ( tx_arr , function ( key , tx ) {
1944+ const parsed_tx = electrum_scan_data ( tx , rdo . setconfirmations , rd . currencysymbol , script_pub_key , latest_block ) ;
1945+ if ( parsed_tx . transactiontime > rdo . request_timestamp && parsed_tx . ccval ) {
1946+ matched_tx = parsed_tx ;
1947+ if ( source === "requests" ) {
1948+ const tx_item = create_transaction_item ( parsed_tx , rd . requesttype ) ;
1949+ if ( tx_item ) {
1950+ tx_list . append ( tx_item . data ( parsed_tx ) ) ;
1951+ }
1952+ } else {
1953+ return false
1954+ }
1955+ }
1956+ } ) ;
1957+ }
18921958 }
18931959 process_scan_results ( rd , api_data , rdo , matched_tx ) ;
18941960 return
@@ -1910,7 +1976,7 @@ function electrum_rpc(rd, api_data, rdo, latest_block) {
19101976 const tx_hash = rd . txhash ;
19111977 api_proxy ( {
19121978 "api" : currency ,
1913- "cachetime" : rdo . cachetime ,
1979+ cachetime,
19141980 "cachefolder" : "1h" ,
19151981 "custom" : "electrum" ,
19161982 "api_url" : rpc_url ,
@@ -2533,7 +2599,7 @@ function mempoolspace_scan_data(data, setconfirmations, ccsymbol, address, lates
25332599function electrum_scan_data ( data , setconfirmations , ccsymbol , script_pub , latest_block , tx_hash ) {
25342600 const outputs = data . outputs ,
25352601 height = data . height || 0 ,
2536- transactiontime = normalize_timestamp ( data . timestamp , true ) ,
2602+ transactiontime = height ? normalize_timestamp ( data . timestamp , true ) : now_utc ( ) - 3000 ,
25372603 confirmations = latest_block ? get_block_confirmations ( height , latest_block ) : height ;
25382604 let outputsum = 0 ;
25392605 if ( outputs ) {
@@ -2546,7 +2612,7 @@ function electrum_scan_data(data, setconfirmations, ccsymbol, script_pub, latest
25462612 return {
25472613 "ccval" : ( outputsum ) ? outputsum / 100000000 : null ,
25482614 transactiontime,
2549- "txhash" : data . tx_hash || tx_hash ,
2615+ "txhash" : tx_hash || data . tx_hash ,
25502616 confirmations,
25512617 setconfirmations,
25522618 ccsymbol
0 commit comments