From 92a89a543d1c59f779da222ebd8cf5fa4c58d94c Mon Sep 17 00:00:00 2001 From: Stoyan Zhekov Date: Fri, 14 May 2021 21:17:07 +0900 Subject: [PATCH] List NFT children per group --- src/slp/nft1.js | 51 ++++++++++++++++++++++++++-- src/slp/slp.js | 2 +- test/integration/chains/bchn/slp.js | 11 ++++++ test/unit/fixtures/slp/mock-utils.js | 8 +++++ test/unit/slp-nft1.js | 47 +++++++++++++++++++++++-- 5 files changed, 114 insertions(+), 5 deletions(-) diff --git a/src/slp/nft1.js b/src/slp/nft1.js index 762f817..9b7741b 100644 --- a/src/slp/nft1.js +++ b/src/slp/nft1.js @@ -8,16 +8,21 @@ (Parent) token. */ +// Public npm libraries +const axios = require('axios') + +// Local libraries. const Address = require('./address') // const BigNumber = require('bignumber.js') const slpMdm = require('slp-mdm') +let _this // const addy = new Address() let addy const TransactionBuilder = require('../transaction-builder') -class TokenType1 { +class Nft1 { constructor (config) { this.restURL = config.restURL @@ -25,6 +30,8 @@ class TokenType1 { // Instantiate the transaction builder. TransactionBuilder.setAddress(addy) + + _this = this } /** @@ -412,6 +419,46 @@ class TokenType1 { throw err } } + + /** + * @api SLP.Nft1.listNFTGroupChildren() listNFTGroupChildren() + * @apiName listNFTGroupChildren + * @apiGroup SLP Nft1 + * @apiDescription Return list of NFT children tokens in a NFT Group. + * It's assumed provided groupId parameter is for an NFT Group token (type=129) + * + * Returns an Array with GENESIS transaction IDs of the children tokens. + * + * @apiExample Example usage: + * + * const groupId = '68cd33ecd909068fbea318ae5ff1d6207cf754e53b191327d6d73b6916424c0a' + * const children = await bchjs.SLP.Nft1.listNFTGroupChildren(groupId) + * + * children = { + * "success": true, + * "children": [ + * "45a30085691d6ea586e3ec2aa9122e9b0e0d6c3c1fd357decccc15d8efde48a9", + * "928ce61fe1006b1325a0ba0dce700bf83986a6f0691ba26e121c9ac035d12a55" + * ] + * } + */ + async listNFTGroupChildren (groupId) { + try { + if (typeof groupId === 'string') { + const response = await axios.get( + `${this.restURL}slp/nftChildren/${groupId}`, + _this.axiosOptions + ) + return response.data + } + + throw new Error('groupId must be a string.') + } catch (error) { + // console.log('Error in listNFTGroupChildren()') + if (error.response && error.response.data) throw error.response.data + else throw error + } + } } -module.exports = TokenType1 +module.exports = Nft1 diff --git a/src/slp/slp.js b/src/slp/slp.js index e535650..35e8acc 100644 --- a/src/slp/slp.js +++ b/src/slp/slp.js @@ -41,7 +41,7 @@ class SLP { this.Address = new Address(config) this.ECPair = ECPair this.TokenType1 = new TokenType1(config) - this.NFT1 = new NFT1(this.restURL) + this.NFT1 = new NFT1(config) this.Utils = new Utils(config) } } diff --git a/test/integration/chains/bchn/slp.js b/test/integration/chains/bchn/slp.js index 55aa960..884ce66 100644 --- a/test/integration/chains/bchn/slp.js +++ b/test/integration/chains/bchn/slp.js @@ -264,6 +264,17 @@ describe('#SLP', () => { }) }) }) + describe('#nft1', () => { + describe('#listNFTGroupChildren', () => { + it.only('should return array of children GENESIS transactions IDs', async () => { + const groupId = '68cd33ecd909068fbea318ae5ff1d6207cf754e53b191327d6d73b6916424c0a' + const result = await bchjs.SLP.NFT1.listNFTGroupChildren(groupId) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + assert.property(result, 'nftChildren') + assert.isArray(result.nftChildren) + }) + }) + }) }) // Promise-based sleep function diff --git a/test/unit/fixtures/slp/mock-utils.js b/test/unit/fixtures/slp/mock-utils.js index a5d672e..5eb1d83 100644 --- a/test/unit/fixtures/slp/mock-utils.js +++ b/test/unit/fixtures/slp/mock-utils.js @@ -119,6 +119,13 @@ const mockTokens = [ } ] +const mockNftChildrenList = { + nftChildren: [ + '45a30085691d6ea586e3ec2aa9122e9b0e0d6c3c1fd357decccc15d8efde48a9', + '928ce61fe1006b1325a0ba0dce700bf83986a6f0691ba26e121c9ac035d12a55', + ] +} + const balancesForAddress = [ { tokenId: 'df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb', @@ -1388,6 +1395,7 @@ module.exports = { mockList, mockToken, mockTokens, + mockNftChildrenList, balancesForAddress, balancesForAddresses, mockRawTx, diff --git a/test/unit/slp-nft1.js b/test/unit/slp-nft1.js index 00b4342..ec8af00 100644 --- a/test/unit/slp-nft1.js +++ b/test/unit/slp-nft1.js @@ -4,7 +4,7 @@ const assert = require('chai').assert const sinon = require('sinon') -// const axios = require("axios") +const axios = require("axios") // Default to unit tests unless some other value for TEST is passed. if (!process.env.TEST) process.env.TEST = 'unit' @@ -14,7 +14,7 @@ const BCHJS = require('../../src/bch-js') const bchjs = new BCHJS() // Mock data used for unit tests -// const mockData = require("./fixtures/slp/mock-utils") +const mockData = require("./fixtures/slp/mock-utils") // Default to unit tests unless some other value for TEST is passed. if (!process.env.TEST) process.env.TEST = 'unit' @@ -253,4 +253,47 @@ describe('#SLP NFT1', () => { assert.isNumber(result.outputs) }) }) + describe('#listNFTGroupChildren', () => { + it('should throw an error for improper input', async () => { + try { + const groupId = 12345 + + await bchjs.SLP.NFT1.listNFTGroupChildren(groupId) + assert.equal(true, false, 'Unexpected result!') + } catch (err) { + // console.log(`err: `, err) + assert.include( + err.message, + 'groupId must be a string' + ) + } + }) + it('should handle API response error', async () => { + try { + const error = new Error() + error.response = { + error: 'NFT group does not exists' + } + sandbox.stub(axios, 'get').throws(error) + + const groupId = 'non-exists' + await bchjs.SLP.NFT1.listNFTGroupChildren(groupId) + } catch (err) { + assert.include( + err.response, + { error: 'NFT group does not exists' } + ) + } + }) + it('should return array of children GENESIS transactions IDs', async () => { + sandbox.stub(axios, 'get').resolves({ data: mockData.mockNftChildrenList }) + + const groupId = '68cd33ecd909068fbea318ae5ff1d6207cf754e53b191327d6d73b6916424c0a' + const result = await bchjs.SLP.NFT1.listNFTGroupChildren(groupId) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, 'nftChildren') + assert.isArray(result.nftChildren) + }) + }) })