From 695072dfbf417de98aaee4bff68e28759a742867 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sat, 18 Jul 2020 19:40:17 -0700 Subject: [PATCH] fix(generateSendOpReturn): returns hex string now --- package.json | 2 +- src/routes/v3/slp.js | 24 +++++++++++++++++++++++- test/v3/integration/slp.js | 26 ++++++++++++++++++++++++-- test/v3/slp.js | 11 ++++++++--- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index b614cec..996f04d 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "npm run lint && npm run test-v3", "lint": "standard --env mocha --fix", "test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/", - "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/slp.js", + "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/integration/slp.js", "test:integration": "mocha test/v3/integration", "test:integration:slpdb": "mocha --timeout 25000 test/v3/integration/slp*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", diff --git a/src/routes/v3/slp.js b/src/routes/v3/slp.js index cd0a9ca..8225dba 100644 --- a/src/routes/v3/slp.js +++ b/src/routes/v3/slp.js @@ -1611,14 +1611,32 @@ class Slp { } } + /** + * @api {post} /slp/generateSendOpReturn/ generateSendOpReturn + * @apiName SLP generateSendOpReturn + * @apiGroup SLP + * @apiDescription Generate the hex required for a SLP Send OP_RETURN. + * + * This will return a hexidecimal representation of the OP_RETURN code that + * can be used to generate an SLP Send transaction. The number of outputs + * (1 or 2) will also be returned. + * + * @apiExample Example usage: + * curl -X POST "https://api.fullstack.cash/v3/slp/generateSendOpReturn" -H "accept:application/json" -H "Content-Type: application/json" -d '{"tokenUtxos":[{"tokenId": "0a321bff9761f28e06a268b14711274bb77617410a16807bd0437ef234a072b1","decimals": 0, "tokenQty": 2}], "sendQty": 1.5}' + * + * + */ // Get OP_RETURN script and outputs async generateSendOpReturn (req, res, next) { try { const tokenUtxos = req.body.tokenUtxos + // console.log(`tokenUtxos: `, tokenUtxos) const _sendQty = req.body.sendQty const sendQty = Number(_sendQty) + // console.log(`sendQty: `, sendQty) + // Reject if tokenUtxos is not an array. if (!Array.isArray(tokenUtxos)) { res.status(400) @@ -1648,7 +1666,11 @@ class Slp { sendQty ) - return res.json(opReturn) + const script = opReturn.script.toString('hex') + // console.log(`script: ${script}`) + + res.status(200) + return res.json({ script, outputs: opReturn.outputs }) } catch (err) { wlogger.error('Error in slp.js/generateSendOpReturn().', err) diff --git a/test/v3/integration/slp.js b/test/v3/integration/slp.js index 7fc8923..2ce5bbe 100644 --- a/test/v3/integration/slp.js +++ b/test/v3/integration/slp.js @@ -14,7 +14,9 @@ util.inspect.defaultOptions = { depth: 1 } // Exit if SLPDB URL is not defined. if (!process.env.SLPDB_URL) { - throw new Error('SLPDB_URL and SLPDB_PASS must be defined in order to run these tests.') + throw new Error( + 'SLPDB_URL and SLPDB_PASS must be defined in order to run these tests.' + ) } const SLP = require('../../../src/routes/v3/slp') @@ -35,7 +37,7 @@ describe('#slp', () => { it('should get token stats for token with no mint baton', async () => { req.params.tokenId = '497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7' - // 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2' + // 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2' const result = await slp.tokenStats(req, res) // console.log(`result: ${util.inspect(result)}`) @@ -99,4 +101,24 @@ describe('#slp', () => { assert.equal(result.containsBaton, true) }) }) + + describe('generateSendOpReturn', () => { + it('should return OP_RETURN script', async () => { + req.body.tokenUtxos = [ + { + tokenId: + '0a321bff9761f28e06a268b14711274bb77617410a16807bd0437ef234a072b1', + decimals: 0, + tokenQty: 2 + } + ] + req.body.sendQty = 1.5 + + const result = await slp.generateSendOpReturn(req, res) + // console.log(`result: ${util.inspect(result)}`) + + assert.hasAllKeys(result, ['script', 'outputs']) + assert.isNumber(result.outputs) + }) + }) }) diff --git a/test/v3/slp.js b/test/v3/slp.js index b59bc68..89e1dbc 100644 --- a/test/v3/slp.js +++ b/test/v3/slp.js @@ -474,7 +474,9 @@ describe('#SLP', () => { it('returns proper error when downstream service stalls', async () => { // Mock the timeout error. - sandbox.stub(slpRoute.slpdb, 'getTokenStats').throws({ code: 'ECONNABORTED' }) + sandbox + .stub(slpRoute.slpdb, 'getTokenStats') + .throws({ code: 'ECONNABORTED' }) req.params.tokenId = '497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7' @@ -492,7 +494,9 @@ describe('#SLP', () => { it('returns proper error when downstream service is down', async () => { // Mock the timeout error. - sandbox.stub(slpRoute.slpdb, 'getTokenStats').throws({ code: 'ECONNREFUSED' }) + sandbox + .stub(slpRoute.slpdb, 'getTokenStats') + .throws({ code: 'ECONNREFUSED' }) req.params.tokenId = '497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7' @@ -863,7 +867,8 @@ describe('#SLP', () => { it('should return OP_RETURN script', async () => { req.body.tokenUtxos = [ { - tokenId: '0a321bff9761f28e06a268b14711274bb77617410a16807bd0437ef234a072b1', + tokenId: + '0a321bff9761f28e06a268b14711274bb77617410a16807bd0437ef234a072b1', decimals: 0, tokenQty: 2 }