From a13a1c91e8a8ea581997c9e09b5bb81cdc6e3168 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 9 Mar 2022 10:08:51 -0800 Subject: [PATCH] Got Offer creation working --- dev-docs/README.md | 8 ++++---- src/adapters/localdb/index.js | 2 ++ src/use-cases/offer.js | 34 ++++++++++++++-------------------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/dev-docs/README.md b/dev-docs/README.md index c763c26..99f2b03 100644 --- a/dev-docs/README.md +++ b/dev-docs/README.md @@ -47,15 +47,15 @@ These are just a brief, high-level overview. Review the [Specifications](./speci ## Writing to the Global Database -Adding data to the global P2WDB is triggered by the _Client_ calling a REST API endpoint on `bch-dex`. `bch-dex` [p2wdb npm library](https://www.npmjs.com/package/p2wdb) can be leveraged for easy reading and writing to the P2WDB. +Adding data to the global P2WDB is triggered by the _Client_ calling a REST API endpoint on `bch-dex`. The [p2wdb npm library](https://www.npmjs.com/package/p2wdb) can be leveraged for easy reading and writing to the P2WDB. Writing data follows these steps: - Tokens and BCH are held by a wallet which is under the controlled of `bch-dex`. - The _Client_ submits data to the POST `/offer` REST API endpoint to create a new Offer. -- `bch-dex` will move the funds into a segregated UTXO, and will use that UTXO to create an Offer. The Offer data is written to the P2WDB. -- `bch-dex` will receive a webhook call to its POST `/order` endpoint by the P2WDB. This event will trigger the import of the new data into the apps local Mongo database, and generate a new Order model. -- This webhook event is mirrored by every instance of `bch-dex` on the network. Each P2WDB peer on the network will independently validate the new database entry. +- `bch-dex` will move the funds into a segregated UTXO, and will use that UTXO to create an Offer. The Offer data is written to the P2WDB. The Offer data is also saved to the local MongoDB. +- After submitting the data to the P2WDB, `bch-dex` will receive a webhook call to its POST `/order` endpoint by the P2WDB. This event will trigger the import of the new data into the apps local Mongo database, and generate a new Order model. +- This webhook event is mirrored by every instance of `bch-dex` on the network. Each P2WDB peer on the network will independently validate the new database entry and create a new Order model. ## Reading from the Local Database diff --git a/src/adapters/localdb/index.js b/src/adapters/localdb/index.js index df0595b..2a4fc18 100644 --- a/src/adapters/localdb/index.js +++ b/src/adapters/localdb/index.js @@ -6,6 +6,7 @@ const Users = require('./models/users') const Entries = require('./models/entry') const Order = require('./models/order') +const Offer = require('./models/offer') class LocalDB { constructor () { @@ -13,6 +14,7 @@ class LocalDB { this.Users = Users this.Entry = Entries this.Order = Order + this.Offer = Offer } } diff --git a/src/use-cases/offer.js b/src/use-cases/offer.js index 70042ac..085a9c6 100644 --- a/src/use-cases/offer.js +++ b/src/use-cases/offer.js @@ -47,30 +47,23 @@ class OfferLib { // Update the offer with the new UTXO information. offerEntity.utxoTxid = utxoInfo.txid offerEntity.utxoVout = utxoInfo.vout - - // Burn PSF token to pay for P2WDB write. - const txid = await this.adapters.wallet.burnPsf() - console.log('burn txid: ', txid) - console.log(`https://simpleledger.info/tx/${txid}`) - - // generate signature. - const now = new Date() - const message = now.toISOString() - const signature = await this.adapters.wallet.generateSignature(message) - // console.log('signature: ', signature) - - const p2wdbObj = { - txid, - signature, - message, - appId: this.config.p2wdbAppId, - data: offerEntity - } + offerEntity.hdIndex = utxoInfo.hdIndex // Add offer to P2WDB. + const p2wdbObj = { + wif: this.adapters.wallet.bchWallet.walletInfo.privateKey, + data: offerEntity, + appId: this.config.p2wdbAppId + } const hash = await this.adapters.p2wdb.write(p2wdbObj) // console.log('hash: ', hash) + // Create a MongoDB model to hold the Offer + offerEntity.p2wdbHash = hash + console.log(`creating new offer model: ${JSON.stringify(offerEntity, null, 2)}`) + const offer = new this.OfferModel(offerEntity) + await offer.save() + return hash } catch (err) { // console.log("Error in use-cases/entry.js/createEntry()", err.message) @@ -97,7 +90,8 @@ class OfferLib { const utxoInfo = { txid, - vout: 0 + vout: 0, + hdIndex: keyPair.hdIndex } return utxoInfo