From 179d92489903aee7e57fc08b3422e6f2fe6fa7b5 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sat, 27 Feb 2021 13:10:33 -0800 Subject: [PATCH] Adding respond.data in error --- src/slp/utils.js | 19 ++++++++----- test/unit/fixtures/slp/mock-utils.js | 40 +++++++++++++++++----------- test/unit/slp-utils.js | 25 ++++++++++++++--- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/slp/utils.js b/src/slp/utils.js index 917d158..bd70131 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -1017,13 +1017,18 @@ class Utils { console.log('decodeOpReturn error: ', error) console.log(`decodeOpReturn error.message: ${error.message}`) - // if (error.response && error.response.data) { - // // throw error.response.data - // - // // error.bchApiMessage = error.message - // // error.message = error.response.data - // // error.data = error.response.data - // } + if (error.response && error.response.data) { + // throw error.response.data + + // error.bchApiMessage = error.message + // error.message = error.response.data + // error.data = error.response.data + console.log( + `decodeOpReturn error.response.data: ${JSON.stringify( + error.response.data + )}` + ) + } throw error } } diff --git a/test/unit/fixtures/slp/mock-utils.js b/test/unit/fixtures/slp/mock-utils.js index 56e2e90..c37fd18 100644 --- a/test/unit/fixtures/slp/mock-utils.js +++ b/test/unit/fixtures/slp/mock-utils.js @@ -1301,8 +1301,10 @@ const txDetailsSLPNftChild = { txid: 'a9a2458a0f9f0761d5b8725c256f2e7fa35b9de4dec6f47b46e9f20d92d0e395', vout: 1, scriptSig: { - asm: '3045022100890ff2f80f3c184419ba03a575b9d159c0a5edfa817e4548cfdb6c74d16ce8d202205a8237d2139196716c273fe80506896d369a0022bb54401e2eadb364286a9f07[ALL|FORKID] 03136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b', - hex: '483045022100890ff2f80f3c184419ba03a575b9d159c0a5edfa817e4548cfdb6c74d16ce8d202205a8237d2139196716c273fe80506896d369a0022bb54401e2eadb364286a9f07412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b' + asm: + '3045022100890ff2f80f3c184419ba03a575b9d159c0a5edfa817e4548cfdb6c74d16ce8d202205a8237d2139196716c273fe80506896d369a0022bb54401e2eadb364286a9f07[ALL|FORKID] 03136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b', + hex: + '483045022100890ff2f80f3c184419ba03a575b9d159c0a5edfa817e4548cfdb6c74d16ce8d202205a8237d2139196716c273fe80506896d369a0022bb54401e2eadb364286a9f07412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b' }, sequence: 4294967295 }, @@ -1310,8 +1312,10 @@ const txDetailsSLPNftChild = { txid: 'a9a2458a0f9f0761d5b8725c256f2e7fa35b9de4dec6f47b46e9f20d92d0e395', vout: 3, scriptSig: { - asm: '304402203905a61ec4e3567f7cad7eb514132e5b32d8a1d77c8838baac1b34b735282a5f022044da6130b5cc41b65615b17df6610087fe1baedd1a3a9f37552b56ea8d36d026[ALL|FORKID] 03136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b', - hex: '47304402203905a61ec4e3567f7cad7eb514132e5b32d8a1d77c8838baac1b34b735282a5f022044da6130b5cc41b65615b17df6610087fe1baedd1a3a9f37552b56ea8d36d026412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b' + asm: + '304402203905a61ec4e3567f7cad7eb514132e5b32d8a1d77c8838baac1b34b735282a5f022044da6130b5cc41b65615b17df6610087fe1baedd1a3a9f37552b56ea8d36d026[ALL|FORKID] 03136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b', + hex: + '47304402203905a61ec4e3567f7cad7eb514132e5b32d8a1d77c8838baac1b34b735282a5f022044da6130b5cc41b65615b17df6610087fe1baedd1a3a9f37552b56ea8d36d026412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623b' }, sequence: 4294967295 } @@ -1321,8 +1325,10 @@ const txDetailsSLPNftChild = { value: 0, n: 0, scriptPubKey: { - asm: 'OP_RETURN 5262419 65 47454e45534953 4e4654303034 4e4654204368696c64 68747470733a2f2f46756c6c537461636b2e63617368 0 0 0 0000000000000001', - hex: '6a04534c500001410747454e45534953064e4654303034094e4654204368696c641668747470733a2f2f46756c6c537461636b2e636173684c0001004c00080000000000000001', + asm: + 'OP_RETURN 5262419 65 47454e45534953 4e4654303034 4e4654204368696c64 68747470733a2f2f46756c6c537461636b2e63617368 0 0 0 0000000000000001', + hex: + '6a04534c500001410747454e45534953064e4654303034094e4654204368696c641668747470733a2f2f46756c6c537461636b2e636173684c0001004c00080000000000000001', type: 'nulldata' } }, @@ -1330,36 +1336,37 @@ const txDetailsSLPNftChild = { value: 0.00000546, n: 1, scriptPubKey: { - asm: 'OP_DUP OP_HASH160 f6fc6bce702ef779d2373aa4356a32113d95e674 OP_EQUALVERIFY OP_CHECKSIG', + asm: + 'OP_DUP OP_HASH160 f6fc6bce702ef779d2373aa4356a32113d95e674 OP_EQUALVERIFY OP_CHECKSIG', hex: '76a914f6fc6bce702ef779d2373aa4356a32113d95e67488ac', reqSigs: 1, type: 'pubkeyhash', - addresses: [ - 'bitcoincash:qrm0c67wwqh0w7wjxua2gdt2xggnm90xws00a3lezv' - ] + addresses: ['bitcoincash:qrm0c67wwqh0w7wjxua2gdt2xggnm90xws00a3lezv'] } }, { value: 0.0003923, n: 2, scriptPubKey: { - asm: 'OP_DUP OP_HASH160 f6fc6bce702ef779d2373aa4356a32113d95e674 OP_EQUALVERIFY OP_CHECKSIG', + asm: + 'OP_DUP OP_HASH160 f6fc6bce702ef779d2373aa4356a32113d95e674 OP_EQUALVERIFY OP_CHECKSIG', hex: '76a914f6fc6bce702ef779d2373aa4356a32113d95e67488ac', reqSigs: 1, type: 'pubkeyhash', - addresses: [ - 'bitcoincash:qrm0c67wwqh0w7wjxua2gdt2xggnm90xws00a3lezv' - ] + addresses: ['bitcoincash:qrm0c67wwqh0w7wjxua2gdt2xggnm90xws00a3lezv'] } } ], - hex: '020000000295e3d0920df2e9467bf4c6dee49d5ba37f2e6f255c72b8d561079f0f8a45a2a9010000006b483045022100890ff2f80f3c184419ba03a575b9d159c0a5edfa817e4548cfdb6c74d16ce8d202205a8237d2139196716c273fe80506896d369a0022bb54401e2eadb364286a9f07412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623bffffffff95e3d0920df2e9467bf4c6dee49d5ba37f2e6f255c72b8d561079f0f8a45a2a9030000006a47304402203905a61ec4e3567f7cad7eb514132e5b32d8a1d77c8838baac1b34b735282a5f022044da6130b5cc41b65615b17df6610087fe1baedd1a3a9f37552b56ea8d36d026412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623bffffffff030000000000000000476a04534c500001410747454e45534953064e4654303034094e4654204368696c641668747470733a2f2f46756c6c537461636b2e636173684c0001004c0008000000000000000122020000000000001976a914f6fc6bce702ef779d2373aa4356a32113d95e67488ac3e990000000000001976a914f6fc6bce702ef779d2373aa4356a32113d95e67488ac00000000', + hex: + '020000000295e3d0920df2e9467bf4c6dee49d5ba37f2e6f255c72b8d561079f0f8a45a2a9010000006b483045022100890ff2f80f3c184419ba03a575b9d159c0a5edfa817e4548cfdb6c74d16ce8d202205a8237d2139196716c273fe80506896d369a0022bb54401e2eadb364286a9f07412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623bffffffff95e3d0920df2e9467bf4c6dee49d5ba37f2e6f255c72b8d561079f0f8a45a2a9030000006a47304402203905a61ec4e3567f7cad7eb514132e5b32d8a1d77c8838baac1b34b735282a5f022044da6130b5cc41b65615b17df6610087fe1baedd1a3a9f37552b56ea8d36d026412103136d9ec06adef1c9154105950e82d470d290aa019ed34a5e3bbd0b679805623bffffffff030000000000000000476a04534c500001410747454e45534953064e4654303034094e4654204368696c641668747470733a2f2f46756c6c537461636b2e636173684c0001004c0008000000000000000122020000000000001976a914f6fc6bce702ef779d2373aa4356a32113d95e67488ac3e990000000000001976a914f6fc6bce702ef779d2373aa4356a32113d95e67488ac00000000', blockhash: '000000000000000001360fe42629f4136e9adec53546b278de0e7ef788eed328', confirmations: 2113, time: 1613181364, blocktime: 1613181364 } +const mock429Error = new Error('Request failed with status code 429') + module.exports = { mockList, mockToken, @@ -1391,5 +1398,6 @@ module.exports = { mockValidateTxid3Invalid, whitelist, slpdbStatus, - mockValidateTxidArray + mockValidateTxidArray, + mock429Error } diff --git a/test/unit/slp-utils.js b/test/unit/slp-utils.js index dda7d54..c93cb55 100644 --- a/test/unit/slp-utils.js +++ b/test/unit/slp-utils.js @@ -456,14 +456,14 @@ describe('#SLP Utils', () => { it('should decode a NFT Child transaction', async () => { // Mock the call to the REST API. sandbox - .stub(uut.Utils.axios, 'get') - .resolves({ data: mockData.txDetailsSLPNftChild }) + .stub(uut.Utils.axios, 'post') + .resolves({ data: [mockData.txDetailsSLPNftChild] }) const txid = 'eeddccc4d716f04157ea132ac93a48040fea34a6b57f3d8f0cccb7d1a731ab2b' const data = await uut.Utils.decodeOpReturn(txid) - console.log(`data: ${JSON.stringify(data, null, 2)}`) + // console.log(`data: ${JSON.stringify(data, null, 2)}`) assert.property(data, 'tokenType') assert.property(data, 'txType') @@ -480,6 +480,25 @@ describe('#SLP Utils', () => { assert.equal(data.mintBatonVout, 0) assert.equal(data.qty, '1') }) + + // 429 means the user is exceeding the rate limits. + it('should throw error when axios returns 429 status', async () => { + try { + // Mock the call to the REST API + sandbox.stub(uut.Utils.axios, 'post').rejects(mockData.mock429Error) + + const txid = + 'a60a522cc11ad7011b74e57fbabbd99296e4b9346bcb175dcf84efb737030415' + + await uut.Utils.decodeOpReturn(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.fail('Unexpect result') + } catch (err) { + console.log('err: ', err) + // assert.include(err.message, 'amount string size not 8 bytes') + } + }) }) describe('#tokenUtxoDetails', () => {