Skip to content

Commit 08e03c1

Browse files
Merge pull request #234 from Permissionless-Software-Foundation/dh-psfSlpIndexer
feat(slpindexer): Added bchjs.PsfSlpIndexer.getTokenData()
2 parents 843f5ea + 7da010e commit 08e03c1

File tree

4 files changed

+175
-2
lines changed

4 files changed

+175
-2
lines changed

src/psf-slp-indexer.js

Lines changed: 68 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,73 @@ 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+
const url = `${this.restURL}psf/slp/token/data`
426+
// console.log(`url: ${url}`)
427+
428+
// Handle single address.
429+
if (typeof tokenId === 'string') {
430+
const response = await axios.post(
431+
// 'https://bchn.fullstack.cash/v5/psf/slp/token/data/',
432+
url,
433+
{ tokenId },
434+
this.axiosOptions
435+
)
436+
return response.data
437+
}
438+
throw new Error('Input tokenId must be a string.')
439+
} catch (error) {
440+
if (error.response && error.response.data) throw error.response.data
441+
else throw error
442+
}
443+
}
377444
}
378445

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

0 commit comments

Comments
 (0)