Got Offer creation working

This commit is contained in:
Chris Troutner
2022-03-09 10:08:51 -08:00
parent b19d4da345
commit a13a1c91e8
3 changed files with 20 additions and 24 deletions
+4 -4
View File
@@ -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
+2
View File
@@ -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
}
}
+14 -20
View File
@@ -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