In the last part, we pointed out that it wouldn’t be a bad idea to find out about the fact of payment from bitcoind, instead of going through all the issued addresses.
Read the full series of articles for developers of a payment gateway for bitcoin here:
ACCEPT PAYMENT IN BITCOIN: PART ONE, THEORETICAL
WE ACCEPT PAYMENT IN BITCOIN: PART TWO. TOOLS AND PREPARATION
WE ACCEPT PAYMENT IN BITCOIN: PART THREE, YOUR TESTNET. WITH BLACKJACK
ACCEPT PAYMENT IN BITCOIN: PART FOURTH, CLOSER TO THE BUSINESS!
WE ACCEPT PAYMENT IN BITCOIN: PART FIVE, THERE ARE NUANCES
Firstly, by doing this we slow down the entire system, and with a large number of payments this becomes a serious problem. And secondly, “firstly” is quite enough.
I mentioned in the last article that bitcoind can still send notifications, although it does it in a somewhat unique way. Technically, when certain events occur, a system command (program) is launched, passing data as a command line argument. Offhand, there are three such events: alertnotify, blocknotify and walletnotify. We are interested in the last two.
In order to receive notifications, we need to specify the following in the bitcoin.conf configuration file:
walletnotify=command1 %s blocknotify=command2 %s
What will this give us, and how does it work? For any transaction associated with our wallet, that is, with any address issued by our bitcoind, the command will be executed
command1 specified for walletnotify, and the transaction hash will be substituted as the %s parameter. Of course, I would like to have both the address and the amount - but we have what we have. Having TxId, we can already obtain complete information about the transaction, including all addresses and amounts.
Now about the nuances.
The command will be run a maximum of two times. Sometimes - alone. The first time is when receiving an unconfirmed transaction and adding it to the mempool. The second time is when the transaction is included in the block, that is, when the first confirmation is received. In some cases, the first notification may not arrive at all.. But what should we do if we want to consider the payment completed after the canonical six confirmations?
This is where blocknotify comes to our aid. The command2 command will be executed when each new signed block is added, and the hash of the block will be returned instead of the %s parameter. Next, we can either check all of our registered transactions for the number of confirmations (yes, such information in a transaction comes from bitcoind), or view each block to see if it contains transactions with our addresses (long, ineffective, but sometimes necessary)
How the software part that implements notifications will be implemented is entirely up to the developer. It could be a bash script, a C/C++ program, or PHP. The main thing is that your application can be launched from the command line.
You May Also Like
Lity. New smart contract language
There are currently more than 1,700 decentralized applications (DApps) published on the Ethereum network, and their number continues to increase. And although all Dapps rely on smart contracts, the reliability of smart contracts themselves is questionable - cybercriminals have already earned more than a billion dollars from hacking them.
EthBackNode: why does your application need a “spacer” between it and the Ethereum node
If you have ever tried to write a crypto-wallet, a payment gateway, or simply a “backend that can send and receive ETH,” then an unpleasant thing quickly becomes clear: the Ethereum node is not your backend
