diff --git a/dev-docs/specification.md b/dev-docs/specification.md index d5d3d4a..51c1b60 100644 --- a/dev-docs/specification.md +++ b/dev-docs/specification.md @@ -34,6 +34,9 @@ Order entities have the following properties: - For AVAX, the min currency is nano-Avax. - for eCash, the min currency is bits. - _minUnitsToExchange_ - The minimum order size accepted. + - _makerAddr_ - The address for the taker to send money to. + - _p2wdbTxid_ - The TXID proof-of-burn used to add the order to the P2WDB. + - _p2wdbHash_ - The CID used to identify the order entry in the P2WDB. - Authentication Data: - _signature_ - A message signed by the address which created the order. @@ -70,6 +73,7 @@ Offer entities have the following properties: - For AVAX, the min currency is nano-Avax. - for eCash, the min currency is bits. - _minUnitsToExchange_ - The minimum order size accepted. + - _makerAddr_ - The address for the taker to send money to. - _p2wdbTxid_ - The TXID proof-of-burn used to add the order to the P2WDB. - _p2wdbHash_ - The CID used to identify the order entry in the P2WDB. - _offerStatus_ - The state of the offer. When the data is added to the P2WDB, it gets a value of 'posted', but the database model internal to bch-dex can have the following properties: diff --git a/package.json b/package.json index 717655a..d33ba31 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node index.js", "test": "npm run test:all", - "test:all": "export BCH_DEX=test && nyc --reporter=text mocha --exit --timeout 15000 --recursive test/unit test/e2e/automated/", + "test:all": "export BCH_DEX=test && nyc --reporter=text mocha --exit --timeout 30000 --recursive test/unit test/e2e/automated/", "test:unit": "export BCH_DEX=test && mocha --exit --timeout 15000 --recursive test/unit/", "test:e2e:auto": "export BCH_DEX=test && mocha --exit --timeout 30000 test/e2e/automated/", "test:integration": "export BCH_DEX=test && mocha --exit --timeout 45000 --recursive test/integration", diff --git a/src/adapters/localdb/models/offer.js b/src/adapters/localdb/models/offer.js index d75c526..51a4855 100644 --- a/src/adapters/localdb/models/offer.js +++ b/src/adapters/localdb/models/offer.js @@ -15,6 +15,7 @@ const Offer = new mongoose.Schema({ p2wdbTxid: { type: String }, p2wdbHash: { type: String }, offerStatus: { type: String }, + makerAddr: { type: String }, // Authentication data signature: { type: String }, diff --git a/src/adapters/localdb/models/order.js b/src/adapters/localdb/models/order.js index f2b9fd8..f89e604 100644 --- a/src/adapters/localdb/models/order.js +++ b/src/adapters/localdb/models/order.js @@ -2,6 +2,8 @@ Order Model. Orders are 'internal' to the system and track the HD wallet index that contains funds for that Order. This is in contrast to Offers, which are 'external' to the system and mirrored by every instance of bch-dex. + + See the dev-docs/specification.md for details on each property. */ const mongoose = require('mongoose') @@ -19,6 +21,7 @@ const Order = new mongoose.Schema({ minUnitsToExchange: { type: String }, p2wdbTxid: { type: String }, p2wdbHash: { type: String }, + makerAddr: { type: String }, // Authentication data signature: { type: String }, diff --git a/src/entities/offer.js b/src/entities/offer.js index c4be346..c82f079 100644 --- a/src/entities/offer.js +++ b/src/entities/offer.js @@ -17,7 +17,19 @@ class OfferEntity { ) } - const { messageType, messageClass, tokenId, buyOrSell, rateInBaseUnit, minUnitsToExchange, numTokens, utxoTxid, utxoVout, offerStatus } = offerData.data + const { + messageType, + messageClass, + tokenId, + buyOrSell, + rateInBaseUnit, + minUnitsToExchange, + numTokens, + utxoTxid, + utxoVout, + offerStatus, + makerAddr + } = offerData.data // Input Validation if (!messageType || typeof messageType !== 'number') { @@ -50,6 +62,9 @@ class OfferEntity { if (offerStatus && !this.offerStatus.includes(offerStatus)) { throw new Error("Property 'offerStatus' must be posted, taken, or dead") } + if (!makerAddr || typeof makerAddr !== 'string') { + throw new Error("Property 'makerAddr' must be a string.") + } const validatedOfferData = { messageType, @@ -65,7 +80,8 @@ class OfferEntity { localTimestamp: offerData.localTimeStamp, txid: offerData.txid, p2wdbHash: offerData.hash, - offerStatus: offerStatus || this.offerStatus[0] + offerStatus: offerStatus || this.offerStatus[0], + makerAddr } return validatedOfferData diff --git a/src/use-cases/order.js b/src/use-cases/order.js index 32a026f..da989c4 100644 --- a/src/use-cases/order.js +++ b/src/use-cases/order.js @@ -52,6 +52,10 @@ class OrderLib { orderEntity.utxoTxid = utxoInfo.txid orderEntity.utxoVout = utxoInfo.vout + // Specify the address to send payment. + orderEntity.makerAddr = this.adapters.wallet.bchWallet.walletInfo.cashAddress + console.log('orderEntity.makerAddr: ', orderEntity.makerAddr) + // Add P2WDB specific flag for signaling that this is a new offer. orderEntity.dataType = 'offer' diff --git a/test/e2e/manual/02-take-offer.js b/test/e2e/manual/02-take-offer.js index 4f2452f..1224c9d 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: 'zdpuAxgdi3az59KrbscntQR2tXjU7tycjvJk6YNN2usiF2t9m' + offerCid: 'zdpuAs5Djp9VrnheYTSDVes6f4KJVds2u3juM3MxwFYbTV8m2' } } diff --git a/test/unit/entities/offer.entity.unit.js b/test/unit/entities/offer.entity.unit.js index 1d6905f..520605f 100644 --- a/test/unit/entities/offer.entity.unit.js +++ b/test/unit/entities/offer.entity.unit.js @@ -252,7 +252,8 @@ describe('#Offer-Entity', () => { utxoTxid: '241c06bf61384b8623477e419bf4779edbcc7e3bc862f0f179a9ed2967069b87', utxoVout: 0, - offerStatus: 'posted' + offerStatus: 'posted', + makerAddr: 'bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00' }, timestamp: '2021-09-20T17:54:26.395Z', localTimeStamp: '9/20/2021, 10:54:26 AM', diff --git a/test/unit/use-cases/offer.use-case.unit.js b/test/unit/use-cases/offer.use-case.unit.js index 228c919..7c0b210 100644 --- a/test/unit/use-cases/offer.use-case.unit.js +++ b/test/unit/use-cases/offer.use-case.unit.js @@ -94,7 +94,8 @@ describe('#offer-use-case', () => { numTokens: 0.02, utxoTxid: '241c06bf61384b8623477e419bf4779edbcc7e3bc862f0f179a9ed2967069b87', - utxoVout: 0 + utxoVout: 0, + makerAddr: 'bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00' }, timestamp: '2021-09-20T17:54:26.395Z', localTimeStamp: '9/20/2021, 10:54:26 AM', diff --git a/util/wallet/sweep-funds.js b/util/wallet/sweep-funds.js index 69bdaff..b5a1534 100644 --- a/util/wallet/sweep-funds.js +++ b/util/wallet/sweep-funds.js @@ -13,6 +13,9 @@ const BchTokenSweep = require('bch-token-sweep/index') // Local libraries const WalletAdapter = require('../../src/adapters/wallet') +// Constants +const EMTPY_ADDR_CUTOFF = 5 + async function sweepFunds () { try { // Open the wallet files. @@ -69,9 +72,9 @@ async function sweepFunds () { } hdIndex++ - } while (emptyAddrCnt < 10) + } while (emptyAddrCnt < EMTPY_ADDR_CUTOFF) - console.log('5 empty addresses detected. Exiting.') + console.log(`${EMTPY_ADDR_CUTOFF} empty addresses detected. Exiting.`) console.log('\n\nDo not forget to reset the nextAddress property in the wallet.json file!\n\n') } catch (err) {