From 315cb4f25478cdad75a91443c26c5ff5b6499799 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sat, 5 Dec 2020 14:02:39 -0800 Subject: [PATCH] feat(whitelist): Added method for getting SLPDB whitelist --- src/slp/utils.js | 66 +++++++++++++++++++++++++++- test/integration/bchn/slp.js | 11 +++++ test/integration/slp.js | 11 +++++ test/unit/fixtures/slp/mock-utils.js | 26 ++++++++++- test/unit/slp-utils.js | 28 ++++++++++++ 5 files changed, 140 insertions(+), 2 deletions(-) diff --git a/src/slp/utils.js b/src/slp/utils.js index f8a6bfb..2653ad5 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -485,7 +485,7 @@ class Utils { * // validate single SLP txid * (async () => { * try { - * let validated = await bchjs.SLP.Utils.validateTxid( + * let validated = await bchjs.SLP.Utils.validateTxid2( * "df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb" * ); * console.log(validated); @@ -525,6 +525,70 @@ class Utils { } } + /** + * @api SLP.Utils.whitelist() whitelist() + * @apiName whitelist + * @apiGroup SLP Utils + * @apiDescription Get SLP tokens in whitelist + * Retrieves a list of the SLP tokens that in the whitelist. Tokens in the + * whitelist can be validated with the validateTxid3() function. validateTxid3() + * will still work when the SLP network is under stress. + * + * @apiExample Example usage: + * + * // validate single SLP txid + * (async () => { + * try { + * let list = await bchjs.SLP.Utils.whitelit(); + * console.log(list); + * } catch (error) { + * console.error(error); + * } + * })(); + * + * // returns + * [ + * { + * name: 'USDH', + * tokenId: + * 'c4b0d62156b3fa5c8f3436079b5394f7edc1bef5dc1cd2f9d0c4d46f82cca479' + * }, + * { + * name: 'SPICE', + * tokenId: + * '4de69e374a8ed21cbddd47f2338cc0f479dc58daa2bbe11cd604ca488eca0ddf' + * }, + * { + * name: 'PSF', + * tokenId: + * '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0' + * }, + * { + * name: 'TROUT', + * tokenId: + * 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2' + * }, + * { + * name: 'PSFTEST', + * tokenId: + * 'd0ef4de95b78222bfee2326ab11382f4439aa0855936e2fe6ac129a8d778baa0' + * } + * ] + */ + async getWhitelist(txid) { + try { + const path = `${this.restURL}slp/whitelist` + + const response = await axios.get(path, _this.axiosOptions) + // console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`) + + return response.data + } catch (error) { + if (error.response && error.response.data) throw error.response.data + throw error + } + } + /** * @api SLP.Utils.tokenStats() tokenStats() * @apiName tokenStats diff --git a/test/integration/bchn/slp.js b/test/integration/bchn/slp.js index 9c64a16..a9d7c34 100644 --- a/test/integration/bchn/slp.js +++ b/test/integration/bchn/slp.js @@ -663,6 +663,17 @@ describe(`#SLP`, () => { assert.equal(result.isValid, true) }) }) + + describe("#getWhitelist", () => { + it("should get the whitelist", async () => { + const result = await bchjs.SLP.Utils.getWhitelist() + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "name") + assert.property(result[1], "tokenId") + }) + }) }) describe("#tokentype1", () => { diff --git a/test/integration/slp.js b/test/integration/slp.js index 9d39392..3cc4a30 100644 --- a/test/integration/slp.js +++ b/test/integration/slp.js @@ -634,6 +634,17 @@ describe(`#SLP`, () => { assert.equal(result.isValid, true) }) }) + + describe("#getWhitelist", () => { + it("should get the whitelist", async () => { + const result = await bchjs.SLP.Utils.getWhitelist() + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], "name") + assert.property(result[1], "tokenId") + }) + }) }) describe("#tokentype1", () => { diff --git a/test/unit/fixtures/slp/mock-utils.js b/test/unit/fixtures/slp/mock-utils.js index 8e145f0..f249b41 100644 --- a/test/unit/fixtures/slp/mock-utils.js +++ b/test/unit/fixtures/slp/mock-utils.js @@ -1107,6 +1107,29 @@ const txDetailsSLPNftGenesis = { blocktime: 1591329189 } +const mockWhitelist = [ + { + name: "USDH", + tokenId: "c4b0d62156b3fa5c8f3436079b5394f7edc1bef5dc1cd2f9d0c4d46f82cca479" + }, + { + name: "SPICE", + tokenId: "4de69e374a8ed21cbddd47f2338cc0f479dc58daa2bbe11cd604ca488eca0ddf" + }, + { + name: "PSF", + tokenId: "38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0" + }, + { + name: "TROUT", + tokenId: "a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2" + }, + { + name: "PSFTEST", + tokenId: "d0ef4de95b78222bfee2326ab11382f4439aa0855936e2fe6ac129a8d778baa0" + } +] + module.exports = { mockList, mockToken, @@ -1131,5 +1154,6 @@ module.exports = { mockTxDetails, mockDualValidation, mockDualOpData, - mockInvalidSlpSend + mockInvalidSlpSend, + mockWhitelist } diff --git a/test/unit/slp-utils.js b/test/unit/slp-utils.js index f04c7ae..b964e09 100644 --- a/test/unit/slp-utils.js +++ b/test/unit/slp-utils.js @@ -1944,4 +1944,32 @@ describe("#SLP Utils", () => { } }) }) + + describe("#getWhitelist", () => { + it("should return the list", async () => { + sandbox.stub(axios, "get").resolves({ data: mockData.mockWhitelist }) + + const result = await slp.Utils.getWhitelist() + + assert2.isArray(result) + assert2.property(result[0], "name") + assert2.property(result[1], "tokenId") + }) + + it("catches and throws an error", async () => { + try { + sandbox.stub(axios, "get").rejects({ + error: + "Network error: Could not communicate with full node or other external service." + }) + + await slp.Utils.getWhitelist() + + assert2.equal(true, false, "Unexpected result") + } catch (err) { + // console.log("err: ", err) + assert2.include(err.error, "Network error") + } + }) + }) })