Whale Watch Example - Txns
const Web3Data = require("web3data-js@0.6.1")
// Set the blockchainId of the blockchain to listen on
const blockchainId = '1c9c969065fcd1cf'
// Create the Web3Data instances passing in your API Key (Get one! -> amberdata.io/onboarding)
const w3d = new Web3Data(process.env.API_KEY, {blockchainId})
// Use these parameters to adjust the threshold value and the comparison operator
const thresholdValue = 10
const thresholdSign = '>'
w3d.connect()
w3d.on({eventName: 'transactions'}, txn => {
const txnValue = toBaseDenom[txn.blockchainId](txn.value)
if(meetsThreshold(txnValue, thresholdValue, thresholdSign)) {
const time = txn.timestamp ? (new Date(txn.timestamp) ).toLocaleString() : null
console.log(`New transaction ${txn.hash.slice(0,10)} value - ${round(txnValue, 2)} at ${time}`)
}
})
// Simple helper function for roud
const round = (n, digits) => Number.parseFloat(n).toFixed(digits)
// Function that returns whether or not the threshold gets met
const meetsThreshold = (value, thresholdValue, thresholdSign) => {
switch (thresholdSign) {
case '>': return value > thresholdValue
case '<': return value < thresholdValue
case '≥': return value >= thresholdValue
case '≤': return value <= thresholdValue
default: break
}
}
// Converts values from lowest to base denomination based on the blockchain
const toBaseDenom = {
'1c9c969065fcd1cf': wei => wei / Math.pow(10, 18), // Eth
'408fa195a34b533de9ad9889f076045e': sat => sat / Math.pow(10, 8), // Btc
'f94be61fd9f4fa684f992ddfd4e92272': photon => photon / Math.pow(10, 8), // Ltc
}
no comments