From 8ed1dbcffe2d98d72ab9d52e67d86d77c2d7eafb Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Mar 2022 13:08:14 -0700 Subject: [PATCH] Renamed database model properties --- src/entities/offer.js | 36 ++++----- src/entities/order.js | 16 ++-- test/e2e/manual/01-create-order.js | 4 +- test/e2e/manual/02-take-offer.js | 2 +- test/unit/entities/offer.entity.unit.js | 88 +++++++++++----------- test/unit/entities/order.entity.unit.js | 14 ++-- test/unit/use-cases/offer.use-case.unit.js | 4 +- util/wallet/sweep-funds.js | 2 + 8 files changed, 84 insertions(+), 82 deletions(-) diff --git a/src/entities/offer.js b/src/entities/offer.js index 5f00fdb..c4be346 100644 --- a/src/entities/offer.js +++ b/src/entities/offer.js @@ -6,18 +6,18 @@ class OfferEntity { constructor () { - this.orderStatus = ['posted', 'taken', 'dead'] + this.offerStatus = ['posted', 'taken', 'dead'] } - validate (orderData = {}) { + validate (offerData = {}) { // Throw an error if input object does not have a data property - if (!orderData.data) { + if (!offerData.data) { throw new Error( - 'Input to order.validate() must be an object with a data property.' + 'Input to offer.validate() must be an object with a data property.' ) } - const { messageType, messageClass, tokenId, buyOrSell, rateInSats, minSatsToExchange, numTokens, utxoTxid, utxoVout, orderStatus } = orderData.data + const { messageType, messageClass, tokenId, buyOrSell, rateInBaseUnit, minUnitsToExchange, numTokens, utxoTxid, utxoVout, offerStatus } = offerData.data // Input Validation if (!messageType || typeof messageType !== 'number') { @@ -32,11 +32,11 @@ class OfferEntity { if (!buyOrSell || typeof buyOrSell !== 'string') { throw new Error("Property 'buyOrSell' must be a string.") } - if (!rateInSats || typeof rateInSats !== 'number') { - throw new Error("Property 'rateInSats' must be an integer number.") + if (!rateInBaseUnit || typeof rateInBaseUnit !== 'number') { + throw new Error("Property 'rateInBaseUnit' must be an integer number.") } - if (!minSatsToExchange || typeof minSatsToExchange !== 'number') { - throw new Error("Property 'minSatsToExchange' must be an integer number.") + if (!minUnitsToExchange || typeof minUnitsToExchange !== 'number') { + throw new Error("Property 'minUnitsToExchange' must be an integer number.") } if (!numTokens || typeof numTokens !== 'number') { throw new Error("Property 'numTokens' must be a number.") @@ -47,8 +47,8 @@ class OfferEntity { if (typeof utxoVout !== 'number') { throw new Error("Property 'utxoVout' must be an integer number.") } - if (orderStatus && !this.orderStatus.includes(orderStatus)) { - throw new Error("Property 'orderStatus' must be posted, taken, or dead") + if (offerStatus && !this.offerStatus.includes(offerStatus)) { + throw new Error("Property 'offerStatus' must be posted, taken, or dead") } const validatedOfferData = { @@ -56,16 +56,16 @@ class OfferEntity { messageClass, tokenId, buyOrSell, - rateInSats, - minSatsToExchange, + rateInBaseUnit, + minUnitsToExchange, numTokens, utxoTxid, utxoVout, - timestamp: orderData.timestamp, - localTimestamp: orderData.localTimeStamp, - txid: orderData.txid, - p2wdbHash: orderData.hash, - orderStatus: orderStatus || this.orderStatus[0] + timestamp: offerData.timestamp, + localTimestamp: offerData.localTimeStamp, + txid: offerData.txid, + p2wdbHash: offerData.hash, + offerStatus: offerStatus || this.offerStatus[0] } return validatedOfferData diff --git a/src/entities/order.js b/src/entities/order.js index 24d2aa6..306bfdc 100644 --- a/src/entities/order.js +++ b/src/entities/order.js @@ -13,8 +13,8 @@ class Order { messageClass, tokenId, buyOrSell, - rateInSats, - minSatsToExchange, + rateInBaseUnit, + minUnitsToExchange, numTokens } = data @@ -31,11 +31,11 @@ class Order { if (!buyOrSell || typeof buyOrSell !== 'string') { throw new Error("Property 'buyOrSell' must be a string.") } - if (!rateInSats || typeof rateInSats !== 'number') { - throw new Error("Property 'rateInSats' must be an integer number.") + if (!rateInBaseUnit || typeof rateInBaseUnit !== 'number') { + throw new Error("Property 'rateInBaseUnit' must be an integer number.") } - if (!minSatsToExchange || typeof minSatsToExchange !== 'number') { - throw new Error("Property 'minSatsToExchange' must be an integer number.") + if (!minUnitsToExchange || typeof minUnitsToExchange !== 'number') { + throw new Error("Property 'minUnitsToExchange' must be an integer number.") } if (!numTokens || typeof numTokens !== 'number') { throw new Error("Property 'numTokens' must be a number.") @@ -46,8 +46,8 @@ class Order { messageClass, tokenId, buyOrSell, - rateInSats, - minSatsToExchange, + rateInBaseUnit, + minUnitsToExchange, numTokens } diff --git a/test/e2e/manual/01-create-order.js b/test/e2e/manual/01-create-order.js index 6eb3031..0b101d1 100644 --- a/test/e2e/manual/01-create-order.js +++ b/test/e2e/manual/01-create-order.js @@ -18,8 +18,8 @@ async function start () { tokenId: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2', buyOrSell: 'sell', - rateInSats: 1000, - minSatsToExchange: 1000, + rateInBaseUnit: 1000, + minUnitsToExchange: 1000, numTokens: 1 } diff --git a/test/e2e/manual/02-take-offer.js b/test/e2e/manual/02-take-offer.js index 2ab150d..26e689b 100644 --- a/test/e2e/manual/02-take-offer.js +++ b/test/e2e/manual/02-take-offer.js @@ -12,7 +12,7 @@ async function start () { method: 'post', url: `${LOCALHOST}/offer/take`, data: { - offerCid: 'zdpuAppDJR57Hrn1mAhsFRssSBp5qNrDT6PcSeHL5ndQqiqJc' + offerCid: 'zdpuApygN6JCpkWDcCy7CMBH8gizzR8vXPML769aZwRyKpHGJ' } } diff --git a/test/unit/entities/offer.entity.unit.js b/test/unit/entities/offer.entity.unit.js index 95ddaf9..1d6905f 100644 --- a/test/unit/entities/offer.entity.unit.js +++ b/test/unit/entities/offer.entity.unit.js @@ -31,15 +31,15 @@ describe('#Offer-Entity', () => { // console.log(err) assert.include( err.message, - 'Input to order.validate() must be an object with a data property.' + 'Input to offer.validate() must be an object with a data property.' ) } }) it('should throw an error if messageType is not included', () => { try { - const orderData = { data: {} } - uut.validate(orderData) + const offerData = { data: {} } + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { @@ -53,8 +53,8 @@ describe('#Offer-Entity', () => { it('should throw an error if messageClass is not included', () => { try { - const orderData = { data: { messageType: 1 } } - uut.validate(orderData) + const offerData = { data: { messageType: 1 } } + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { @@ -68,8 +68,8 @@ describe('#Offer-Entity', () => { it('should throw an error if tokenId is not included', () => { try { - const orderData = { data: { messageType: 1, messageClass: 1 } } - uut.validate(orderData) + const offerData = { data: { messageType: 1, messageClass: 1 } } + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { @@ -80,11 +80,11 @@ describe('#Offer-Entity', () => { it('should throw an error if buyOrSell is not included', () => { try { - const orderData = { + const offerData = { data: { messageType: 1, messageClass: 1, tokenId: 'fakeId' } } - uut.validate(orderData) + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { @@ -93,9 +93,9 @@ describe('#Offer-Entity', () => { } }) - it('should throw an error if rateInSats is not included', () => { + it('should throw an error if rateInBaseUnit is not included', () => { try { - const orderData = { + const offerData = { data: { messageType: 1, messageClass: 1, @@ -104,55 +104,55 @@ describe('#Offer-Entity', () => { } } - uut.validate(orderData) + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { // console.log(err) assert.include( err.message, - "Property 'rateInSats' must be an integer number." + "Property 'rateInBaseUnit' must be an integer number." ) } }) - it('should throw an error if minSatsToExchange is not included', () => { + it('should throw an error if minUnitsToExchange is not included', () => { try { - const orderData = { + const offerData = { data: { messageType: 1, messageClass: 1, tokenId: 'fakeId', buyOrSell: 'buy', - rateInSats: 1000 + rateInBaseUnit: 1000 } } - uut.validate(orderData) + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { // console.log(err) assert.include( err.message, - "Property 'minSatsToExchange' must be an integer number." + "Property 'minUnitsToExchange' must be an integer number." ) } }) it('should throw an error if numTokens is not included', () => { try { - const orderData = { + const offerData = { data: { messageType: 1, messageClass: 1, tokenId: 'fakeId', buyOrSell: 'buy', - rateInSats: 1000, - minSatsToExchange: 350 + rateInBaseUnit: 1000, + minUnitsToExchange: 350 } } - uut.validate(orderData) + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { @@ -163,18 +163,18 @@ describe('#Offer-Entity', () => { it('should throw an error if utxoTxid is not included', () => { try { - const orderData = { + const offerData = { data: { messageType: 1, messageClass: 1, tokenId: 'fakeId', buyOrSell: 'buy', - rateInSats: 1000, - minSatsToExchange: 350, + rateInBaseUnit: 1000, + minUnitsToExchange: 350, numTokens: 1 } } - uut.validate(orderData) + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { @@ -185,19 +185,19 @@ describe('#Offer-Entity', () => { it('should throw an error if utxoVout is not included', () => { try { - const orderData = { + const offerData = { data: { messageType: 1, messageClass: 1, tokenId: 'fakeId', buyOrSell: 'buy', - rateInSats: 1000, - minSatsToExchange: 350, + rateInBaseUnit: 1000, + minUnitsToExchange: 350, numTokens: 1, utxoTxid: 'fakeTxid' } } - uut.validate(orderData) + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { @@ -211,34 +211,34 @@ describe('#Offer-Entity', () => { it('should throw an error if proper status is not applied', () => { try { - const orderData = { + const offerData = { data: { messageType: 1, messageClass: 1, tokenId: 'fakeId', buyOrSell: 'buy', - rateInSats: 1000, - minSatsToExchange: 350, + rateInBaseUnit: 1000, + minUnitsToExchange: 350, numTokens: 1, utxoTxid: 'fakeTxid', utxoVout: 0, - orderStatus: 'badStatus' + offerStatus: 'badStatus' } } - uut.validate(orderData) + uut.validate(offerData) assert.fail('Unexpected code path') } catch (err) { // console.log(err) assert.include( err.message, - "Property 'orderStatus' must be posted, taken, or dead" + "Property 'offerStatus' must be posted, taken, or dead" ) } }) - it('should validate a new order', () => { - const orderObj = { + it('should validate a new offer', () => { + const offerObj = { appId: 'swapTest555', data: { messageType: 1, @@ -246,13 +246,13 @@ describe('#Offer-Entity', () => { tokenId: '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0', buyOrSell: 'sell', - rateInSats: 1000, - minSatsToExchange: 10, + rateInBaseUnit: 1000, + minUnitsToExchange: 10, numTokens: 0.02, utxoTxid: '241c06bf61384b8623477e419bf4779edbcc7e3bc862f0f179a9ed2967069b87', utxoVout: 0, - orderStatus: 'posted' + offerStatus: 'posted' }, timestamp: '2021-09-20T17:54:26.395Z', localTimeStamp: '9/20/2021, 10:54:26 AM', @@ -260,15 +260,15 @@ describe('#Offer-Entity', () => { hash: 'zdpuB2X25AZCKo3wpr4sSbw44vqPWJRqcxWQRHZccK5BdtoGD' } - const result = uut.validate(orderObj) + const result = uut.validate(offerObj) // console.log('result: ', result) assert.property(result, 'messageType') assert.property(result, 'messageClass') assert.property(result, 'tokenId') assert.property(result, 'buyOrSell') - assert.property(result, 'rateInSats') - assert.property(result, 'minSatsToExchange') + assert.property(result, 'rateInBaseUnit') + assert.property(result, 'minUnitsToExchange') assert.property(result, 'numTokens') assert.property(result, 'utxoTxid') assert.property(result, 'utxoVout') diff --git a/test/unit/entities/order.entity.unit.js b/test/unit/entities/order.entity.unit.js index ee655c3..3ef8a38 100644 --- a/test/unit/entities/order.entity.unit.js +++ b/test/unit/entities/order.entity.unit.js @@ -77,7 +77,7 @@ describe('#Order-Entity', () => { } }) - it('should throw an error if rateInSats is not included', () => { + it('should throw an error if rateInBaseUnit is not included', () => { try { const data = { messageType: 1, @@ -90,26 +90,26 @@ describe('#Order-Entity', () => { // console.log(err) assert.include( err.message, - "Property 'rateInSats' must be an integer number." + "Property 'rateInBaseUnit' must be an integer number." ) } }) - it('should throw an error if minSatsToExchange is not included', () => { + it('should throw an error if minUnitsToExchange is not included', () => { try { const data = { messageType: 1, messageClass: 1, tokenId: 'fakeId', buyOrSell: 'buy', - rateInSats: 1000 + rateInBaseUnit: 1000 } uut.validate(data) } catch (err) { // console.log(err) assert.include( err.message, - "Property 'minSatsToExchange' must be an integer number." + "Property 'minUnitsToExchange' must be an integer number." ) } }) @@ -121,8 +121,8 @@ describe('#Order-Entity', () => { messageClass: 1, tokenId: 'fakeId', buyOrSell: 'buy', - rateInSats: 1000, - minSatsToExchange: 350 + rateInBaseUnit: 1000, + minUnitsToExchange: 350 } uut.validate(data) } catch (err) { diff --git a/test/unit/use-cases/offer.use-case.unit.js b/test/unit/use-cases/offer.use-case.unit.js index 86b3945..effbfaa 100644 --- a/test/unit/use-cases/offer.use-case.unit.js +++ b/test/unit/use-cases/offer.use-case.unit.js @@ -87,8 +87,8 @@ describe('#offer-use-case', () => { tokenId: '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0', buyOrSell: 'sell', - rateInSats: 1000, - minSatsToExchange: 10, + rateInBaseUnit: 1000, + minUnitsToExchange: 10, numTokens: 0.02, utxoTxid: '241c06bf61384b8623477e419bf4779edbcc7e3bc862f0f179a9ed2967069b87', diff --git a/util/wallet/sweep-funds.js b/util/wallet/sweep-funds.js index ea27250..d05394f 100644 --- a/util/wallet/sweep-funds.js +++ b/util/wallet/sweep-funds.js @@ -70,6 +70,8 @@ async function sweepFunds () { } while (emptyAddrCnt < 5) console.log('5 empty addresses detected. Exiting.') + + console.log('\n\nDo not forget to reset the nextAddress property in the wallet.json file!\n\n') } catch (err) { console.error('Error in sweepFunds(): ', err) }