diff --git a/src/controllers/rest-api/bch/controller.js b/src/controllers/rest-api/bch/controller.js index d480bca..68eed4d 100644 --- a/src/controllers/rest-api/bch/controller.js +++ b/src/controllers/rest-api/bch/controller.js @@ -585,6 +585,41 @@ class BchRESTControllerLib { } } + /** + * @api {REST} /bch getTokenData2 + * @apiPermission public + * @apiName getTokenData2 + * @apiGroup REST BCH + * @apiDescription Get token icon and other media + * + * Get the icon for a token, given it's token ID. + * This function expects a string input of a token ID property. + * This function returns an object with a tokenIcon property that contains + * the URL to the icon. + * + * The output object always have these properties: + * - tokenIcon: A url to the token icon, if it exists. + * - tokenStats: Data about the token from psf-slp-indexer. + * - optimizedTokenIcon: An alternative, potentially more optimal, url to the token icon, if it exists. + * - iconRepoCompatible: true if the token icon is available via token.bch.sx + * - ps002Compatible: true if the token icon is compatible with PS007 specification. + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X POST -d '{ "tokenId": "43eddfb11c9941edffb8c8815574bb0a43969a7b1de39ad14cd043eaa24fd38d" }' https://bc01-ca-bch-consumer.fullstackcash.nl/bch/getTokenData2 + */ + async getTokenData2 (ctx) { + try { + const tokenId = ctx.request.body.tokenId + + const data = await this.adapters.bch.getTokenData2(tokenId) + console.log(`getTokenData2 data: ${JSON.stringify(data, null, 2)}`) + + ctx.body = data + } catch (err) { + this.handleError(ctx, err) + } + } + // DRY error handler handleError (ctx, err) { // If an HTTP status is specified by the buisiness logic, use that. diff --git a/src/controllers/rest-api/bch/index.js b/src/controllers/rest-api/bch/index.js index 245462b..971235e 100644 --- a/src/controllers/rest-api/bch/index.js +++ b/src/controllers/rest-api/bch/index.js @@ -61,6 +61,7 @@ class BchRouter { this.router.post('/pubkey', this.postPubKey) this.router.post('/utxoIsValid', this.utxoIsValid) this.router.post('/getTokenData', this.getTokenData) + this.router.post('/getTokenData2', this.getTokenData2) // Attach the Controller routes to the Koa app. app.use(this.router.routes()) @@ -106,6 +107,10 @@ class BchRouter { async getTokenData (ctx, next) { await _this.bchRESTController.getTokenData(ctx, next) } + + async getTokenData2 (ctx, next) { + await _this.bchRESTController.getTokenData2(ctx, next) + } } module.exports = BchRouter diff --git a/test/unit/controllers/rest-api/bch/bch.rest.controller.unit.js b/test/unit/controllers/rest-api/bch/bch.rest.controller.unit.js index e379417..c3e52ff 100644 --- a/test/unit/controllers/rest-api/bch/bch.rest.controller.unit.js +++ b/test/unit/controllers/rest-api/bch/bch.rest.controller.unit.js @@ -394,4 +394,40 @@ describe('#BCH-REST-Controller', () => { assert.equal(ctx.status, 200) }) }) + + describe('#getTokenData2', () => { + it('should return 422 status on arbitrary error', async () => { + try { + // Force an error + sandbox + .stub(uut.adapters.bch, 'getTokenData2') + .rejects(new Error('test error')) + + ctx.request.body = { + tokenId: 'blah' + } + + await uut.getTokenData2(ctx) + + assert.fail('Unexpected result') + } catch (err) { + console.log('err: ', err) + assert.equal(err.status, 422) + assert.include(err.message, 'test error') + } + }) + + it('should return 200 status on success', async () => { + sandbox.stub(uut.adapters.bch, 'getTokenData2').resolves({ status: 200 }) + + ctx.request.body = { + tokenId: 'blah' + } + + await uut.getTokenData2(ctx) + + // Assert the expected HTTP response + assert.equal(ctx.status, 200) + }) + }) }) diff --git a/test/unit/mocks/adapters/index.js b/test/unit/mocks/adapters/index.js index c828c00..802ec93 100644 --- a/test/unit/mocks/adapters/index.js +++ b/test/unit/mocks/adapters/index.js @@ -117,6 +117,10 @@ class BchUseCaseMock { return {} } + async getTokenData2() { + return {} + } + async waitForRPCResponse () { return {} }