Push notification of incoming transaction Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Push notification of incoming transaction without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Is there a way to be notified by bitcoind via the CLI that some incoming transaction has been confirmed?

Answer

A transaction can only be confirmed by a new block being added to the chain. And since that happens on average every ten minutes, it would work just as well to be notified when a block is discovered (and/or the chain is reorganized). You can then check any transactions you care about.

One way to be notified if the chain is changed in any way is with my native long polling patch. This should apply pretty cleanly to any recent version of the client (but is based on 0.4.0):

diff -u orig/init.cpp new/init.cpp
--- orig/init.cpp   2011-09-25 08:29:53.935505617 -0700
+++ new/init.cpp    2011-09-25 08:48:13.667215990 -0700
@@ -199,6 +200,7 @@
             "  -rpcport=<port>  tt  " + _("Listen for JSON-RPC connections on <port> (default: 8332)n") +
             "  -rpcallowip=<ip> tt  " + _("Allow JSON-RPC connections from specified IP addressn") +
             "  -rpcconnect=<ip> t  "   + _("Send commands to node running on <ip> (default: 127.0.0.1)n") +
+            "  -pollpidfile=<f> t  "   + _("Support long pollingn") +
             "  -keypool=<n>     t  "   + _("Set key pool size to <n> (default: 100)n") +
             "  -rescan          t  "   + _("Rescan the block chain for missing wallet transactionsn");

diff -u orig/main.cpp new/main.cpp
--- orig/main.cpp   2011-09-25 08:29:53.937505613 -0700
+++ new/main.cpp    2011-09-25 08:48:13.667215990 -0700
@@ -1118,6 +1118,7 @@
 bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew)
 {
     uint256 hash = GetHash();
+    bool lp = false;

     txdb.TxnBegin();
     if (pindexGenesisBlock == NULL && hash == hashGenesisBlock)
@@ -1160,6 +1161,7 @@
     // Update best block in wallet (so we can detect restored wallets)
     if (!IsInitialBlockDownload())
     {
+        lp = true;
         const CBlockLocator locator(pindexNew);
         ::SetBestChain(locator);
     }
@@ -1173,6 +1175,23 @@
     nTransactionsUpdated++;
     printf("SetBestChain: new best=%s  height=%d  work=%sn", hashBestChain.ToString().substr(0,20).c_str(), nBest

+    if (lp)
+    {
+        // Support long polling
+        string lp_pid = mapArgs["-pollpidfile"];
+        if(lp_pid != "")
+        {
+            FILE *pidFile = fopen(lp_pid.c_str(), "r");
+            if(pidFile!=NULL)
+            {
+                int pid=0;
+                if ((fscanf(pidFile, "%d", &pid) == 1) && (pid > 1))
+                    kill((pid_t) pid, SIGUSR1);
+                fclose(pidFile);
+            }
+        }
+    }
+
     return true;
 }

To use it, just start bitcoin or bitcoind with a command line argument of
-pollpidfile=/path/to/some/file. Then write your monitor process’ PID to that file. You will receive a SIGUSR1 on any block chain change.

We are here to answer your question about Push notification of incoming transaction - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji