Litecoin
$43.78 -0.06%
LTC · 2w

Litecoin transaction monitoring is a breeze 💨 with Web3data.js!

const w3d = new Web3Data("YOUR API KEY", { blockchainId: 'f94be61fd9f4fa684f992ddfd4e92272'})

First we create the Web3Data instance and pass in all of the necessary configuration options. You’ll see here that we are specifying the Litecoin blockchain Id. In our documentation you’ll find a complete list of supported blockchains and their corresponding blockchain Ids.

w3d.connect()

Next, we establish a connection with the websocket server.

const {result: txnHashes} = await w3d.ltc.rpc('getrawmempool')

We make the getrawmempool RPC request to the Litecoin node which returns an array of the transaction hashes of the transactions within the mempool. We’ll object destructure the result property, which contains the array, into the txnHashes variable.

As you can see making RPC requests with Web3data.js is super simple! To see the full list of available RPC requests you can check out our documentation.

txnHashes.forEach( txnHash => { console.log('Subscribing to transaction: ' + txnHash.slice(0,8)) subscribe(txnHash)})

Iterate over each transaction hash and log that we are now subscribing to this transaction; thus watching for it’s exit from the mempool. We only log the first 8 characters for readability.

const subscribe = hash => { w3d.on({eventName: 'transaction', filters: {hash}}, (txn) => { console.log(`Transaction: ${hash.slice(0,8)} exited mempool.`) })}

Finally, we have our subscribe method which sends a websocket subscription request to the websocket server. We specify the name of the event we want to subscribe to using the eventName property. Using the filters option we pass in the hash of a pending transaction that we want to watch for. In this case, it’s the has of the pending transaction we got from the Litecoin mempool. Then we log that the transaction left the mempool and got confirmed.

You can see more details in our documentation about websockets and the transaction event and be sure ...

Continue on medium.com
Recent news