mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 01:02:05 -07:00
feat(token-sweep): Added token sweep endpoint
This commit is contained in:
+114
-2
@@ -13,6 +13,118 @@ const mockAddress = {
|
||||
isscript: false
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mockAddress
|
||||
const mockBalance = {
|
||||
page: 1,
|
||||
totalPages: 1,
|
||||
itemsOnPage: 1000,
|
||||
address: "bitcoincash:qzp7gdl52edm24xlpkyqnza9rv33u3mdxyc77j3u6k",
|
||||
balance: "2546",
|
||||
totalReceived: "2546",
|
||||
totalSent: "0",
|
||||
unconfirmedBalance: "0",
|
||||
unconfirmedTxs: 0,
|
||||
txs: 2,
|
||||
txids: [
|
||||
"e190d13b88578132608ab912a4d2be3e55aa2792d6042d481ae21d700639de56",
|
||||
"44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6"
|
||||
]
|
||||
}
|
||||
|
||||
const mockUtxos = [
|
||||
{
|
||||
txid: "e190d13b88578132608ab912a4d2be3e55aa2792d6042d481ae21d700639de56",
|
||||
vout: 0,
|
||||
value: "2000",
|
||||
height: 605873,
|
||||
confirmations: 298,
|
||||
satoshis: 2000
|
||||
},
|
||||
{
|
||||
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
|
||||
vout: 1,
|
||||
value: "546",
|
||||
height: 605873,
|
||||
confirmations: 298,
|
||||
satoshis: 546
|
||||
}
|
||||
]
|
||||
|
||||
const mockThreeUtxos = [
|
||||
{
|
||||
txid: "e190d13b88578132608ab912a4d2be3e55aa2792d6042d481ae21d700639de56",
|
||||
vout: 0,
|
||||
value: "2000",
|
||||
height: 605873,
|
||||
confirmations: 298,
|
||||
satoshis: 2000
|
||||
},
|
||||
{
|
||||
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
|
||||
vout: 1,
|
||||
value: "546",
|
||||
height: 605873,
|
||||
confirmations: 298,
|
||||
satoshis: 546
|
||||
},
|
||||
{
|
||||
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
|
||||
vout: 1,
|
||||
value: "546",
|
||||
height: 605873,
|
||||
confirmations: 298,
|
||||
satoshis: 546
|
||||
}
|
||||
]
|
||||
|
||||
const mockIsTokenUtxos = [
|
||||
false,
|
||||
{
|
||||
txid: "44e1f48c4093fc61db1a8fa206aa402fc34e482b3f788cb38c123ca0e1a35db6",
|
||||
vout: 1,
|
||||
value: "546",
|
||||
height: 605873,
|
||||
confirmations: 298,
|
||||
satoshis: 546,
|
||||
utxoType: "token",
|
||||
transactionType: "send",
|
||||
tokenId: "497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7",
|
||||
tokenTicker: "TOK-CH",
|
||||
tokenName: "TokyoCash",
|
||||
tokenDocumentUrl: "",
|
||||
tokenDocumentHash: "",
|
||||
decimals: 8,
|
||||
tokenQty: 2
|
||||
}
|
||||
]
|
||||
|
||||
const tapUtxo = {
|
||||
txid: "2e030df12390186baf817fa2760540b886511e04bc520e88f6b4c2124cc2a7d4",
|
||||
vout: 1,
|
||||
value: "546",
|
||||
height: 606564,
|
||||
confirmations: 3,
|
||||
satoshis: 546,
|
||||
utxoType: "token",
|
||||
transactionType: "send",
|
||||
tokenId: "dd84ca78db4d617221b58eabc6667af8fe2f7eadbfcc213d35be9f1b419beb8d",
|
||||
tokenTicker: "TAP",
|
||||
tokenName: "Thoughts and Prayers",
|
||||
tokenDocumentUrl: "",
|
||||
tokenDocumentHash: "",
|
||||
decimals: 0,
|
||||
tokenQty: 1
|
||||
}
|
||||
|
||||
const tokensOnly = [mockIsTokenUtxos[1], tapUtxo]
|
||||
|
||||
const multipleTokens = [false, mockIsTokenUtxos[1], tapUtxo]
|
||||
|
||||
module.exports = {
|
||||
mockAddress,
|
||||
mockBalance,
|
||||
mockUtxos,
|
||||
mockThreeUtxos,
|
||||
mockIsTokenUtxos,
|
||||
tokensOnly,
|
||||
multipleTokens
|
||||
}
|
||||
|
||||
+212
-7
@@ -12,6 +12,7 @@ const chai = require("chai")
|
||||
const assert = chai.assert
|
||||
const utilRoute = require("../../src/routes/v3/util")
|
||||
const nock = require("nock") // HTTP mocking
|
||||
const sinon = require("sinon")
|
||||
|
||||
let originalEnvVars // Used during transition from integration to unit tests.
|
||||
|
||||
@@ -19,11 +20,15 @@ let originalEnvVars // Used during transition from integration to unit tests.
|
||||
const { mockReq, mockRes } = require("./mocks/express-mocks")
|
||||
const mockData = require("./mocks/util-mocks")
|
||||
|
||||
const util = require("util")
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const UtilRoute = utilRoute.UtilRoute
|
||||
const utilRouteInst = new utilRoute.UtilRoute()
|
||||
|
||||
describe("#Util", () => {
|
||||
let req, res
|
||||
let sandbox
|
||||
|
||||
before(() => {
|
||||
// Save existing environment variables.
|
||||
@@ -57,12 +62,16 @@ describe("#Util", () => {
|
||||
|
||||
// Activate nock if it's inactive.
|
||||
if (!nock.isActive()) nock.activate()
|
||||
|
||||
sandbox = sinon.createSandbox()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up HTTP mocks.
|
||||
nock.cleanAll() // clear interceptor list.
|
||||
nock.restore()
|
||||
|
||||
sandbox.restore()
|
||||
})
|
||||
|
||||
after(() => {
|
||||
@@ -133,7 +142,7 @@ describe("#Util", () => {
|
||||
req.params.address = `bitcoincash:qpujxqra3jmdlzzapwmmt7uspr7q0c9ff5hzljcrnd`
|
||||
|
||||
const result = await validateAddress(req, res)
|
||||
//console.log(`result: ${util.inspect(result)}`)
|
||||
// console.log(`result: ${util.inspect(result)}`)
|
||||
|
||||
assert.hasAnyKeys(result, [
|
||||
"isvalid",
|
||||
@@ -298,14 +307,210 @@ describe("#Util", () => {
|
||||
})
|
||||
|
||||
describe("#sweepWif", () => {
|
||||
// const sweepWif = utilRoute.testableComponents.sweepWif
|
||||
|
||||
it("should do something", async () => {
|
||||
req.body.wif = "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt"
|
||||
req.body.toAddr = "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
|
||||
it("should throw 400 if WIF is not included", async () => {
|
||||
req.body = {}
|
||||
|
||||
const result = await utilRouteInst.sweepWif(req, res)
|
||||
console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(res.statusCode, 400, "HTTP status code 400 expected.")
|
||||
assert.include(
|
||||
result.error,
|
||||
"WIF needs to a proper compressed WIF starting with K or L",
|
||||
"Proper error message"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw 400 if WIF is malformed", async () => {
|
||||
req.body = {
|
||||
wif: `abc123`
|
||||
}
|
||||
|
||||
const result = await utilRouteInst.sweepWif(req, res)
|
||||
|
||||
assert.equal(res.statusCode, 400, "HTTP status code 400 expected.")
|
||||
assert.include(
|
||||
result.error,
|
||||
"WIF needs to a proper compressed WIF starting with K or L",
|
||||
"Proper error message"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw 400 if destination address is not included", async () => {
|
||||
req.body = {
|
||||
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt"
|
||||
}
|
||||
|
||||
const result = await utilRouteInst.sweepWif(req, res)
|
||||
|
||||
assert.equal(res.statusCode, 400, "HTTP status code 400 expected.")
|
||||
assert.include(
|
||||
result.error,
|
||||
"address can not be empty",
|
||||
"Proper error message"
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate transaction for valid token sweep", async () => {
|
||||
// Mock the RPC call for unit tests.
|
||||
if (process.env.TEST === "unit") {
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"balanceFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockBalance)
|
||||
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"utxosFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockUtxos)
|
||||
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
|
||||
.resolves(mockData.mockIsTokenUtxos)
|
||||
}
|
||||
|
||||
// Mock sendRawTransaction() so that the hex does not actually get broadcast
|
||||
// to the network.
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
|
||||
.resolves("test-txid")
|
||||
|
||||
req.body = {
|
||||
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
|
||||
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
|
||||
}
|
||||
|
||||
const result = await utilRouteInst.sweepWif(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(result, "test-txid")
|
||||
})
|
||||
|
||||
// Unit tests only
|
||||
if (process.env.TEST === "unit") {
|
||||
it("should generate transaction for valid BCH-only sweep", async () => {
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"balanceFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockBalance)
|
||||
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"utxosFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockUtxos)
|
||||
|
||||
// Force token utxo to appear as regular BCH utxo.
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
|
||||
.resolves([false, false])
|
||||
|
||||
// Mock sendRawTransaction() so that the hex does not actually get broadcast
|
||||
// to the network.
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
|
||||
.resolves("test-txid")
|
||||
|
||||
req.body = {
|
||||
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
|
||||
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
|
||||
}
|
||||
|
||||
const result = await utilRouteInst.sweepWif(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(result, "test-txid")
|
||||
})
|
||||
|
||||
it("should throw 422 error if no non-token UTXOs", async () => {
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"balanceFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockBalance)
|
||||
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"utxosFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockUtxos)
|
||||
|
||||
// Force token utxo to appear as regular BCH utxo.
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
|
||||
.resolves(mockData.tokensOnly)
|
||||
|
||||
// Mock sendRawTransaction() so that the hex does not actually get broadcast
|
||||
// to the network.
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
|
||||
.resolves("test-txid")
|
||||
|
||||
req.body = {
|
||||
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
|
||||
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
|
||||
}
|
||||
|
||||
const result = await utilRouteInst.sweepWif(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422)
|
||||
assert.property(result, "error")
|
||||
assert.include(
|
||||
result.error,
|
||||
"Tokens found, but no BCH UTXOs found. Add BCH to wallet to move tokens"
|
||||
)
|
||||
})
|
||||
|
||||
it("should detect and throw error for multiple token classes", async () => {
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"balanceFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockBalance)
|
||||
|
||||
sandbox
|
||||
.stub(
|
||||
utilRouteInst.blockbook.testableComponents,
|
||||
"utxosFromBlockbook"
|
||||
)
|
||||
.resolves(mockData.mockThreeUtxos)
|
||||
|
||||
// Force token utxo to appear as regular BCH utxo.
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.SLP.Utils, "tokenUtxoDetails")
|
||||
.resolves(mockData.multipleTokens)
|
||||
|
||||
// Mock sendRawTransaction() so that the hex does not actually get broadcast
|
||||
// to the network.
|
||||
sandbox
|
||||
.stub(utilRouteInst.bchjs.RawTransactions, "sendRawTransaction")
|
||||
.resolves("test-txid")
|
||||
|
||||
req.body = {
|
||||
wif: "L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt",
|
||||
toAddr: "bitcoincash:qz2qn6zt4qmacf4r6c0e2pdcqsgnkxaa3ql2xpee6p"
|
||||
}
|
||||
|
||||
const result = await utilRouteInst.sweepWif(req, res)
|
||||
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
assert.equal(res.statusCode, 422)
|
||||
assert.property(result, "error")
|
||||
assert.include(
|
||||
result.error,
|
||||
"Multiple token classes detected. This function only supports a single class of token"
|
||||
)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user