From f88a24c1b579bfdc098ef9fb14e59d2e0c5a4cb6 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 10 Aug 2025 10:44:45 -0700 Subject: [PATCH] Updating unit tests --- src/use-cases/offer/index.js | 20 +++++------ test/unit/entities/offer.entity.unit.js | 4 ++- test/unit/mocks/use-cases/offer-mock-data.js | 36 +++++++++++++++++++- test/unit/use-cases/offer.use-case.unit.js | 18 ++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/use-cases/offer/index.js b/src/use-cases/offer/index.js index e8aa821..9f8d250 100644 --- a/src/use-cases/offer/index.js +++ b/src/use-cases/offer/index.js @@ -591,16 +591,6 @@ class OfferUseCases { const txObj = await this.adapters.wallet.deseralizeTx(txHex) console.log(`txObj: ${JSON.stringify(txObj, null, 2)}`) - // Ensure the Counter Offer has an output for the Operator of bch-dex. - if (!txObj.vout[3]) { - console.log('The Counter Offer does not have an output for the Operator.') - - // Add order to list of seen orders, so that we don't spent time trying to validate it again. - this.seenOffers.push(eventId) - - return 'N/A' - } - // Ensure the 3rd output (vout=2) contains the required amount of BCH. const satsToReceive = Math.ceil(orderData.numTokens * parseInt(orderData.rateInBaseUnit)) console.log('Ceil', satsToReceive) @@ -613,6 +603,16 @@ class OfferUseCases { throw new Error(`The Counter Offer has an output of ${satsOut}, which does not match the required ${satsToReceive} in the Offer.`) } + // Ensure the Counter Offer has an output for the Operator of bch-dex. + if (!txObj.vout[3]) { + console.log('The Counter Offer does not have an output for the Operator.') + + // Add order to list of seen orders, so that we don't spent time trying to validate it again. + this.seenOffers.push(eventId) + + return 'N/A' + } + // Ensure the 3rd output (vout=2) is going to the maker address specified // in the Offer. const addrInCounterOffer = txObj.vout[2].scriptPubKey.addresses[0] diff --git a/test/unit/entities/offer.entity.unit.js b/test/unit/entities/offer.entity.unit.js index 090fcb8..dea0243 100644 --- a/test/unit/entities/offer.entity.unit.js +++ b/test/unit/entities/offer.entity.unit.js @@ -286,7 +286,9 @@ describe('#Offer-Entity', () => { makerAddr: 'bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00', ticker: 'TROUT', tokenType: 1, - nostrEventId: 'test' + nostrEventId: 'test', + operatorAddress: 'bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00', + operatorPercentage: 10 }, timestamp: '2021-09-20T17:54:26.395Z', localTimeStamp: '9/20/2021, 10:54:26 AM', diff --git a/test/unit/mocks/use-cases/offer-mock-data.js b/test/unit/mocks/use-cases/offer-mock-data.js index c2cc82f..071e82e 100644 --- a/test/unit/mocks/use-cases/offer-mock-data.js +++ b/test/unit/mocks/use-cases/offer-mock-data.js @@ -142,10 +142,37 @@ const offerMockData = { utxoVout: 0, makerAddr: 'address', tokenType: 1, - nostrEventId: 'test' + nostrEventId: 'test', + operatorAddress: 'bitcoincash:qzy97glp47ut7tstm5g0tlrmkhk742795gkmyc7478', + operatorPercentage: 10 } } +const deserealizeTxMockNoOperatorOut = { + //... + vout: [ + { + value: 0, + scriptPubKey: { + addresses: ['bitcoincash:qzy97glp47ut7tstm5g0tlrmkhk742795gkmyc7478'] + } + }, + { + value: 0, + scriptPubKey: { + addresses: ['bitcoincash:qzy97glp47ut7tstm5g0tlrmkhk742795gkmyc7478'] + } + }, + { + value: 0, + scriptPubKey: { + addresses: ['bitcoincash:qzy97glp47ut7tstm5g0tlrmkhk742795gkmyc7478'] + } + }, + //... + ] +} + const deserealizeTxMock = { //... vout: [ @@ -167,6 +194,12 @@ const deserealizeTxMock = { addresses: ['bitcoincash:qzy97glp47ut7tstm5g0tlrmkhk742795gkmyc7478'] } }, + { + value: 0, + scriptPubKey: { + addresses: ['bitcoincash:qzy97glp47ut7tstm5g0tlrmkhk742795gkmyc7478'] + } + }, //... ] } @@ -179,5 +212,6 @@ export default { fungibleOffer01, fungibleTokenData01, offerMockData, + deserealizeTxMockNoOperatorOut, deserealizeTxMock }; diff --git a/test/unit/use-cases/offer.use-case.unit.js b/test/unit/use-cases/offer.use-case.unit.js index 05fed70..a047efc 100644 --- a/test/unit/use-cases/offer.use-case.unit.js +++ b/test/unit/use-cases/offer.use-case.unit.js @@ -746,6 +746,24 @@ describe('#offer-use-case', () => { } }) + it('should skip transactions that do not have an output for the operator', async () => { + // Mock data + const mock = Object.assign({}, mockData.offerMockData.data) + mock.makerAddr = 'bitcoincash:qzy97glp47ut7tstm5g0tlrmkhk742795gkmyc7477' // Unknow Adress + mock.rateInBaseUnit = 0 + mock.numTokens = 0 + + // Mock dependencies + sandbox.stub(uut.orderUseCase, 'findOrderByEvent').resolves(mock) + sandbox.stub(uut.orderUseCase, 'findOrderByUtxo').resolves(mock) + sandbox.stub(uut.adapters.wallet.bchWallet.bchjs.BitcoinCash, 'toSatoshi').returns(0) + + sandbox.stub(uut.adapters.wallet, 'deseralizeTx').resolves(mockData.deserealizeTxMockNoOperatorOut) + + const result = await uut.acceptCounterOffer({ data: { /** .... */ } }) + assert.equal(result, 'N/A') + }) + it('should handle error for wrong transaction output address', async () => { try { // Mock data