read.cash is a platform where you can earn money for your articles and comments. You can get paid upvotes
from other users or just earn points for writing articles and comments, which are converted daily to
Bitcoin Cash (BCH) cryptocurrency, which can be used on the Internet or converted to your local money.
Takes one minute, no documents required
Advanced BCH Monitoring with Tasker (Part 4: Retrieving your data from the block explorer API)
This interface will allow us to query information about anything related to the addresses we manage.
It needs three variables to keep its state, the last JSON retrieved, the balance in satoshis and a list of funded addresses, the last two are meant to cache results so we don't need to recalculate them once we already have it.
Its functions are as follows:
load() : gets a list of addresses and an API key (if needed) as a parameter and will retrieve information of those addresses from the REST API and save it in the lastJson variable.
getTotalBalance() : after calling load this will return the total balance from the queried addresses.
getFundedAddresses() : a utility function that returns a list of the addresses that have UTXO, it will serve a purpose on later posts.
As the Perform Task action only gets up to two parameters we will separate different parameters with the semicolon character ';' and use the %args variable to store the %par2 content and split it in an array where each element will have one parameter. This pretty much sums up the first five actions, that get the %par2 parameter and turns it into two different local variables: %input_addresses and %input_apikey.
Blockchair is one of the most used block explorers and has a very professional and convenient REST API, that makes retrieving information from the Blockchain really easy.
The steps used to retrieve information using the Blockchair API may be different on other Block Explorer API, but the idea behind it remains the same: Querying the sum for each UTXO value for each address under monitoring.
The first step will be cloning the IBlockExplorerAPI task into a new BlockChairBlockExplorerAPI task.
As many BCH users use to have more than 20 addresses on their wallets thanks to the BIP39 address derivation, retrieving the information could be a very cumbersome task even programmatically, because of that pre-loading the JSON data once and treating it later is the most efficient strategy to follow.
An HTTP Request to the https://api.blockchair.com/bitcoin-cash/dashboards/addresses/ endpoint, to query a list of comma-separated CashAddr addresses %input_addresses using an (optional if you are just doing your own experiments) API key ?key=%input_apikey
Like in the last chapter, a conditional return if the HTTP response is an error
The Blockchair API, and in general, any other block explorer APIs have a consumption rate that can lead your IP to be blacklisted, temporarily blocked or if you have a paid subscription, charged for requests.
Because of that and because some of the APIs need to call each address individually you might want to know which addresses are funded.
The first test will load a JSON and check if returns SUCCESS, the second one will raise a push notification with your balance in satoshi, the third one will raise another notification with the list of funded addresses.
In the next part, we'll be preparing a notification builder object.