Skip to content

Instantly share code, notes, and snippets.

@christroutner
Forked from danielhumgon/hydrated-utxos.js
Created October 18, 2021 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christroutner/dcb25a895900e557d7b43341ee85fa79 to your computer and use it in GitHub Desktop.
Save christroutner/dcb25a895900e557d7b43341ee85fa79 to your computer and use it in GitHub Desktop.
Get UTXOs hydrated with SLP token information for an address.
const axios = require('axios').default
const bchAddress = 'bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk'
const SERVER = process.env.SERVER
// Get hydrated UTXOs from an address
const hydrateSLP = 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 address UTXOs
const result = await axios(options)
const utxos = result.data
// Hydrate UTXOs
const hydratedUtxos = await hydrateUTXOS(utxos)
console.log(hydratedUtxos)
console.log(`Hydrated UTXOs : ${hydratedUtxos.length}`)
return hydratedUtxos
} catch (error) {
console.log(error)
throw error
}
}
// Maps and filter the SLP UTXOs of an UTXOs array
// get information about the SLP UTXOs
const hydrateUTXOS = async (UTXOS) => {
try {
if (!Array.isArray(UTXOS)) {
throw new Error('UTXOS must be an array of slp utxos')
}
const slpUtxos = UTXOS.filter((val) => val.slp)
const hydrated = []
// Find information of each token
for (let i = 0; i < slpUtxos.length; i++) {
const slp = slpUtxos[i].slp
const info = await getTokenInfo(slp.tokenId)
const obj = Object.assign(slp, info)
slpUtxos[i].slp = obj
hydrated.push(slpUtxos[i])
}
return hydrated
} catch (error) {
console.log(error)
throw error
}
}
// Get information of a token
const getTokenInfo = async (tokenId) => {
try {
if (!tokenId || typeof tokenId !== 'string') {
throw new Error('tokenID must be string')
}
const options = {
method: 'GET',
url: `${SERVER}token/${tokenId}`
}
const result = await axios(options)
const tokenInfo = result.data
// console.log('token info', tokenInfo)
return tokenInfo
} catch (error) {
console.log(error)
throw error
}
}
hydrateSLP(bchAddress)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment