Created
October 14, 2021 20:57
-
-
Save danielhumgon/2fbb0958bf0b7cd0d1caa4c937f2ea0d to your computer and use it in GitHub Desktop.
Get UTXOs hydrated with SLP token information for 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 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