From 65bffe1ef630d874156d747669c54c5adf7e3d49 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 22 Mar 2022 08:00:08 -0700 Subject: [PATCH] Successfully detecting and routing offers and counter-offers --- src/adapters/wallet.js | 3 +++ src/controllers/rest-api/p2wdb/controller.js | 13 +++++++++++++ src/use-cases/offer/index.js | 3 +++ test/e2e/manual/01-create-order.js | 4 ++-- test/e2e/manual/02-take-offer.js | 2 +- test/unit/adapters/wallet.adapter.unit.js | 3 ++- util/wallet/sweep-funds.js | 8 +++++--- 7 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/adapters/wallet.js b/src/adapters/wallet.js index 3535685..5f85e27 100644 --- a/src/adapters/wallet.js +++ b/src/adapters/wallet.js @@ -350,6 +350,9 @@ class WalletAdapter { }] console.log(`receivers: ${JSON.stringify(receivers, null, 2)}`) + // Update the UTXO store of the wallet. + await this.bchWallet.getUtxos() + const txid = await this.bchWallet.send(receivers) const utxoInfo = { diff --git a/src/controllers/rest-api/p2wdb/controller.js b/src/controllers/rest-api/p2wdb/controller.js index e9440ca..d97eae4 100644 --- a/src/controllers/rest-api/p2wdb/controller.js +++ b/src/controllers/rest-api/p2wdb/controller.js @@ -36,6 +36,19 @@ class P2WDBRESTControllerLib { try { console.log('p2wdb REST API handler: body: ', ctx.request.body) + const dataType = ctx.request.body.data.dataType + + if (dataType.includes('counter')) { + console.log('counter-offer data detected.') + } else if (dataType.includes('offer')) { + console.log('offer data detected') + + const offerObj = ctx.request.body + await _this.useCases.offer.createOffer(offerObj) + } else { + console.log('Could not route P2WDB webhook data.') + } + // const orderObj = ctx.request.body.order // // const hash = await _this.useCases.order.createOrder(orderObj) diff --git a/src/use-cases/offer/index.js b/src/use-cases/offer/index.js index 97c2f72..eb3382d 100644 --- a/src/use-cases/offer/index.js +++ b/src/use-cases/offer/index.js @@ -149,6 +149,9 @@ class OfferUseCases { delete takenOfferInfo._id takenOfferInfo.offerHash = offerInfo.p2wdbHash + // Add P2WDB specific flag for signaling that this is a new offer. + takenOfferInfo.dataType = 'counter-offer' + // Write offer info to the P2WDB // TODO: This will trigger the webhook. Find some way of triggering the // webhook on new offers, but not on counteroffers diff --git a/test/e2e/manual/01-create-order.js b/test/e2e/manual/01-create-order.js index 0b101d1..3095a9f 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', - rateInBaseUnit: 1000, - minUnitsToExchange: 1000, + rateInBaseUnit: 5000, + minUnitsToExchange: 5000, numTokens: 1 } diff --git a/test/e2e/manual/02-take-offer.js b/test/e2e/manual/02-take-offer.js index e01302d..4f2452f 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: 'zdpuB1gqwyto4hJnR2YgnttZiXifzUQ5NLRVBuXEtonwa3f4g' + offerCid: 'zdpuAxgdi3az59KrbscntQR2tXjU7tycjvJk6YNN2usiF2t9m' } } diff --git a/test/unit/adapters/wallet.adapter.unit.js b/test/unit/adapters/wallet.adapter.unit.js index 824735b..a3b72bf 100644 --- a/test/unit/adapters/wallet.adapter.unit.js +++ b/test/unit/adapters/wallet.adapter.unit.js @@ -289,7 +289,8 @@ describe('#wallet', () => { hdIndex: 6 }) uut.bchWallet = { - send: async () => 'fake-txid' + send: async () => 'fake-txid', + getUtxos: async () => {} } const amountSat = 1000 diff --git a/util/wallet/sweep-funds.js b/util/wallet/sweep-funds.js index d05394f..69bdaff 100644 --- a/util/wallet/sweep-funds.js +++ b/util/wallet/sweep-funds.js @@ -35,9 +35,11 @@ async function sweepFunds () { do { // Generate a keypair from the HD wallet. const childNode = masterHDNode.derivePath(`m/44'/245'/0'/0/${hdIndex}`) - // const cashAddress = bchjs.HDNode.toCashAddress(childNode) + const cashAddress = bchjs.HDNode.toCashAddress(childNode) const wifToSweep = bchjs.HDNode.toWIF(childNode) + console.log(`\nSweeping ${cashAddress}`) + try { // Sweep tokens from address const sweeper = new BchTokenSweep( @@ -62,12 +64,12 @@ async function sweepFunds () { // Wait between loop iterations. await bchjs.Util.sleep(3000) } catch (err) { - console.log(`error message: ${err.message}`) + console.log(`error message with index ${hdIndex}: ${err.message}`) emptyAddrCnt++ } hdIndex++ - } while (emptyAddrCnt < 5) + } while (emptyAddrCnt < 10) console.log('5 empty addresses detected. Exiting.')