Skip to content

Commit d144b13

Browse files
committed
feat(slpindexer): Added bchjs.PsfSlpIndexer.getTokenData()
1 parent 30d58bc commit d144b13

File tree

4 files changed

+170
-2
lines changed

4 files changed

+170
-2
lines changed

src/psf-slp-indexer.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PsfSlpIndexer {
4141
this.rawTransaction = new RawTransaction(config)
4242
this.slpUtils = new SlpUtils(config)
4343

44-
// _this = this
44+
// _this = this
4545
}
4646

4747
/**
@@ -374,6 +374,69 @@ class PsfSlpIndexer {
374374
return false
375375
}
376376
}
377+
378+
/**
379+
* @api PsfSlpIndexer.getTokenData() getTokenData()
380+
* @apiName Token Data
381+
* @apiGroup PSF SLP
382+
* @apiDescription Get mutable and immutable data if the token contains them.
383+
*
384+
* @apiExample Example usage:
385+
* (async () => {
386+
* try {
387+
* let tokenData = await bchjs.PsfSlpIndexer.getTokenData('a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2')
388+
* console.log(tokenData)
389+
* } catch(error) {
390+
* console.error(error)
391+
* }
392+
* })()
393+
*
394+
* {
395+
* genesisData: {
396+
* type: 1,
397+
* ticker: 'TROUT',
398+
* name: "Trout's test token",
399+
* tokenId: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
400+
* documentUri: 'troutsblog.com',
401+
* documentHash: '',
402+
* decimals: 2,
403+
* mintBatonIsActive: true,
404+
* tokensInCirculationBN: '100098953386',
405+
* tokensInCirculationStr: '100098953386',
406+
* blockCreated: 622414,
407+
* totalBurned: '1046614',
408+
* totalMinted: '100100000000'
409+
* ]
410+
* },
411+
* immutableData :{
412+
* issuer:"FullStack.cash.",
413+
* website:"https://fullstack.cash/",
414+
* dateCreated:"2022-01-11"
415+
* },
416+
* mutableData :{
417+
* "tokenIcon":"https://gateway.ipfs.io/ipfs/bafybeiehitanirn5gmhqjg44xrmdtomn4n5lu5yjoepsvgpswk5mggaw6i/LP_logo-1.png",
418+
* "about":"Mutable data managed with npm package: https://www.npmjs.com/package/slp-mutable-data"
419+
* }
420+
* }
421+
*
422+
*/
423+
async getTokenData (tokenId) {
424+
try {
425+
// Handle single address.
426+
if (typeof tokenId === 'string') {
427+
const response = await axios.post(
428+
'https://bchn.fullstack.cash/v5/psf/slp/token/data/',
429+
{ tokenId },
430+
this.axiosOptions
431+
)
432+
return response.data
433+
}
434+
throw new Error('Input tokenId must be a string.')
435+
} catch (error) {
436+
if (error.response && error.response.data) throw error.response.data
437+
else throw error
438+
}
439+
}
377440
}
378441

379442
module.exports = PsfSlpIndexer

test/integration/chains/bchn/psf-slp-indexer.integration.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ describe('#psf-slp-indexer', () => {
124124
}
125125
})
126126
})
127+
128+
describe('#getTokenData', () => {
129+
it('should get token data', async () => {
130+
const tokenId =
131+
'f055256b938f1ecfa270459d6f12c7c8c82b66d3263c03d5074445a2b1a498a3'
132+
133+
const result = await bchjs.PsfSlpIndexer.getTokenData(tokenId)
134+
console.log('result: ', result)
135+
136+
assert.property(result, 'genesisData')
137+
assert.property(result, 'immutableData')
138+
assert.property(result, 'mutableData')
139+
140+
assert.isObject(result.genesisData)
141+
assert.isObject(result.immutableData)
142+
assert.isObject(result.mutableData)
143+
})
144+
})
127145
})
128146

129147
// Promise-based sleep function

test/unit/fixtures/psf-slp-indexer-mock.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,23 @@ const tokenData01 = {
144144
qty: {}
145145
}
146146

147+
const tokenData = {
148+
genesisData: tokenStats.tokenData,
149+
immutableData: {
150+
issuer: 'Launchpad Intellectual Property, Inc.',
151+
website: 'http://launchpadip.com/',
152+
dateCreated: '2022-01-11'
153+
},
154+
mutableData: {
155+
tokenIcon: 'https://gateway.ipfs.io/ipfs/bafybeiehitanirn5gmhqjg44xrmdtomn4n5lu5yjoepsvgpswk5mggaw6i/LP_logo-1.png',
156+
about: 'Mutable data managed with npm package: https://www.npmjs.com/package/slp-mutable-data'
157+
}
158+
}
147159
module.exports = {
148160
tokenStats,
149161
txData,
150162
balance,
151163
status,
152-
tokenData01
164+
tokenData01,
165+
tokenData
153166
}

test/unit/psf-slp-indexer.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,78 @@ describe('#PsfSlpIndexer', () => {
336336
assert.equal(result, false)
337337
})
338338
})
339+
describe('#getTokenData', () => {
340+
it('should GET token data', async () => {
341+
// Stub the network call.
342+
sandbox.stub(axios, 'post').resolves({ data: mockData.tokenData })
343+
344+
const tokenId =
345+
'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
346+
const result = await bchjs.PsfSlpIndexer.getTokenData(tokenId)
347+
assert.property(result, 'genesisData')
348+
assert.property(result, 'immutableData')
349+
assert.property(result, 'mutableData')
350+
351+
assert.isObject(result.genesisData)
352+
assert.isObject(result.immutableData)
353+
assert.isObject(result.mutableData)
354+
355+
assert.property(result.genesisData, 'type')
356+
assert.property(result.genesisData, 'ticker')
357+
assert.property(result.genesisData, 'name')
358+
assert.property(result.genesisData, 'tokenId')
359+
assert.property(result.genesisData, 'documentUri')
360+
assert.property(result.genesisData, 'documentHash')
361+
assert.property(result.genesisData, 'decimals')
362+
assert.property(result.genesisData, 'mintBatonIsActive')
363+
assert.property(result.genesisData, 'tokensInCirculationBN')
364+
assert.property(result.genesisData, 'tokensInCirculationStr')
365+
assert.property(result.genesisData, 'blockCreated')
366+
assert.property(result.genesisData, 'totalBurned')
367+
assert.property(result.genesisData, 'totalMinted')
368+
})
369+
370+
it('should throw an error for improper input', async () => {
371+
try {
372+
const tokenId = 12345
373+
374+
await bchjs.PsfSlpIndexer.getTokenData(tokenId)
375+
assert.equal(true, false, 'Unexpected result!')
376+
} catch (err) {
377+
// console.log(`err: `, err)
378+
assert.include(err.message, 'Input tokenId must be a string.')
379+
}
380+
})
381+
382+
it('should handle axios error', async () => {
383+
try {
384+
// Stub the network call.
385+
sandbox.stub(axios, 'post').throws(new Error('test error'))
386+
387+
const tokenId =
388+
'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
389+
await bchjs.PsfSlpIndexer.getTokenData(tokenId)
390+
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
391+
assert.equal(true, false, 'Unexpected result!')
392+
} catch (err) {
393+
assert.include(err.message, 'test error')
394+
}
395+
})
396+
397+
it('should handle request error', async () => {
398+
try {
399+
// Stub the network call.
400+
const testErr = new Error()
401+
testErr.response = { data: { status: 422 } }
402+
sandbox.stub(axios, 'post').throws(testErr)
403+
404+
const tokenId =
405+
'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
406+
await bchjs.PsfSlpIndexer.getTokenData(tokenId)
407+
assert.equal(true, false, 'Unexpected result!')
408+
} catch (err) {
409+
assert.equal(err.status, 422)
410+
}
411+
})
412+
})
339413
})

0 commit comments

Comments
 (0)