Updated tokenstats endpoint

This commit is contained in:
Chris Troutner
2020-05-12 17:40:42 -07:00
parent 299945df0d
commit 7ce8a0632a
6 changed files with 212 additions and 101 deletions
+62 -4
View File
@@ -75,16 +75,24 @@ class Slpdb {
}
async getTokenStats (tokenId) {
const [totalMinted, totalBurned, tokenDetails] = await Promise.all([
const [
totalMinted,
totalBurned,
tokenDetails,
circulatingSupply
] = await Promise.all([
this.getTotalMinted(tokenId),
this.getTotalBurned(tokenId),
this.getTokenDetails(tokenId)
this.getTokenDetails(tokenId),
this.getTotalCirculating(tokenId)
])
tokenDetails.totalMinted = tokenDetails.initialTokenQty + totalMinted
tokenDetails.totalBurned = totalBurned
tokenDetails.circulatingSupply =
tokenDetails.totalMinted - tokenDetails.totalBurned
// tokenDetails.circulatingSupply =
// tokenDetails.totalMinted - tokenDetails.totalBurned
tokenDetails.circulatingSupply = circulatingSupply
return tokenDetails
}
@@ -164,6 +172,53 @@ class Slpdb {
return parseFloat(result.data.g[0].count)
}
async getTotalCirculating (tokenId) {
const query = {
v: 3,
q: {
db: ['g'],
aggregate: [
{
$match: {
'tokenDetails.tokenIdHex': tokenId,
'graphTxn.outputs': {
$elemMatch: {
status: 'UNSPENT',
slpAmount: { $gte: 0 }
}
}
}
},
{ $unwind: '$graphTxn.outputs' },
{
$match: {
'graphTxn.outputs.status': 'UNSPENT',
'graphTxn.outputs.slpAmount': { $gte: 0 }
}
},
{
$group: {
_id: null,
circulating_supply: {
$sum: '$graphTxn.outputs.slpAmount'
}
}
}
],
limit: 100000
}
}
const result = await this.runQuery(query)
// console.log(`result.data: ${JSON.stringify(result.data, null, 2)}`)
if (!result.data.g.length) {
return 0
}
return parseFloat(result.data.g[0].circulating_supply)
}
async getTotalBurned (tokenId) {
const query = {
v: 3,
@@ -255,6 +310,8 @@ class Slpdb {
}
formatTokenOutput (token) {
// console.log(`token: ${JSON.stringify(token, null, 2)}`)
token.tokenDetails.id = token.tokenDetails.tokenIdHex
delete token.tokenDetails.tokenIdHex
token.tokenDetails.documentHash = token.tokenDetails.documentSha256Hex
@@ -286,6 +343,7 @@ class Slpdb {
token.tokenDetails.timestampUnix = token.tokenDetails.timestamp_unix
delete token.tokenDetails.timestamp_unix
return token.tokenDetails
}
}
+11 -46
View File
@@ -1284,7 +1284,9 @@ class Slp {
txid: concatArray[0].tx.h,
valid: concatArray[0].slp.valid
}
if (!result.valid) { result.invalidReason = concatArray[0].slp.invalidReason }
if (!result.valid) {
result.invalidReason = concatArray[0].slp.invalidReason
}
}
res.status(200)
@@ -1405,55 +1407,17 @@ class Slp {
*
*/
async tokenStats (req, res, next) {
const tokenId = req.params.tokenId
if (!tokenId || tokenId === '') {
res.status(400)
return res.json({ error: 'tokenId can not be empty' })
}
try {
const query = {
v: 3,
q: {
db: ['t'],
find: {
$query: {
'tokenDetails.tokenIdHex': tokenId
}
},
project: { tokenDetails: 1, tokenStats: 1, _id: 0 },
limit: 10
}
const tokenId = req.params.tokenId
if (!tokenId || tokenId === '') {
res.status(400)
return res.json({ error: 'tokenId can not be empty' })
}
const s = JSON.stringify(query)
const b64 = Buffer.from(s).toString('base64')
const url = `${process.env.SLPDB_URL}q/${b64}`
const options = _this.generateCredentials()
// Request options
const opt = {
method: 'get',
baseURL: url,
headers: options.headers,
timeout: options.timeout
}
// Get data from BitDB.
const tokenRes = await _this.axios.request(opt)
const formattedTokens = []
if (tokenRes.data.t.length) {
tokenRes.data.t.forEach((token) => {
token = _this.formatTokenOutput(token)
formattedTokens.push(token.tokenDetails)
})
}
const tokenStats = await _this.slpdb.getTokenStats(tokenId)
res.status(200)
return res.json(formattedTokens[0])
return res.json(tokenStats)
} catch (err) {
wlogger.error('Error in slp.ts/tokenStats().', err)
return _this.errorHandler(err, res)
@@ -1617,7 +1581,8 @@ class Slp {
if (!networkIsValid) {
res.status(400)
return res.json({
error: 'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
error:
'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
})
}