Created
October 13, 2021 19:06
-
-
Save danielhumgon/ba996fdbb2addcefbc61f742b59b24bd to your computer and use it in GitHub Desktop.
JS code example for querying the SLP balance of an address.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios').default | |
const bchAddress = 'bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk' | |
const SERVER = process.env.SERVER | |
// Get SLP balance of an address | |
const slpBalances = async (address) => { | |
try { | |
if (!address || typeof address !== 'string') { | |
throw new Error('address must be string') | |
} | |
const options = { | |
method: 'GET', | |
url: `${SERVER}coin/address/${address}?slp=true` | |
} | |
// Get UTXOs of an address | |
const result = await axios(options) | |
const utxos = result.data | |
console.log(`UTXOS : ${utxos.length}`) | |
// Filter SLP UTXOs | |
const slpUtxos = utxos.filter((val) => val.slp) | |
console.log(`slpUtxos : ${slpUtxos.length}`) | |
console.log(slpUtxos) | |
return slpUtxos | |
} catch (error) { | |
console.log(error) | |
throw error | |
} | |
} | |
slpBalances(bchAddress) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment