Merge pull request #149 from christroutner/unstable

fix(generateSendOpReturn): returns hex string now
This commit is contained in:
Chris Troutner
2020-07-18 19:47:24 -07:00
committed by GitHub
4 changed files with 61 additions and 7 deletions
+1 -1
View File
@@ -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",
+24 -1
View File
@@ -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)
@@ -1647,7 +1665,12 @@ class Slp {
tokenUtxos,
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)
+24 -2
View File
@@ -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)
})
})
})
+12 -3
View File
@@ -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'
@@ -817,6 +821,7 @@ describe('#SLP', () => {
assert.hasAllKeys(result, ['error'])
assert.include(result.error, 'tokenUtxos needs to be an array.')
})
it('should throw 400 if tokenUtxos is empty array', async () => {
req.body.tokenUtxos = []
@@ -826,6 +831,7 @@ describe('#SLP', () => {
assert.hasAllKeys(result, ['error'])
assert.include(result.error, 'tokenUtxos array can not be empty.')
})
// Validate sendQty input
it('should throw 400 if sendQty is missing', async () => {
req.body.tokenUtxos = [{}, {}, {}]
@@ -846,6 +852,7 @@ describe('#SLP', () => {
assert.hasAllKeys(result, ['error'])
assert.include(result.error, 'sendQty must be a number')
})
it('should throw 400 if sendQty is not a number', async () => {
req.body.tokenUtxos = [{}, {}, {}]
req.body.sendQty = 'sendQty'
@@ -856,10 +863,12 @@ describe('#SLP', () => {
assert.hasAllKeys(result, ['error'])
assert.include(result.error, 'sendQty must be a number')
})
it('should return OP_RETURN script', async () => {
req.body.tokenUtxos = [
{
tokenId: '0a321bff9761f28e06a268b14711274bb77617410a16807bd0437ef234a072b1',
tokenId:
'0a321bff9761f28e06a268b14711274bb77617410a16807bd0437ef234a072b1',
decimals: 0,
tokenQty: 2
}