Skip to content

Commit

Permalink
Adding uploadCounterOffer2() to use Nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
christroutner committed Dec 15, 2024
1 parent dead7ca commit ef72bd9
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/take.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import RetryQueue from '@chris.troutner/retry-queue'

// Local libraries
import BchDexUtil from './bch-dex-util.js'
import NostrAdapter from './adapters/nostr.js'

class Take {
constructor (localConfig = {}) {
Expand All @@ -29,13 +30,14 @@ class Take {
// Encapsulate dependencies
this.util = new BchDexUtil(localConfig)
this.retryQueue = new RetryQueue({ attempts: 3, retryPeriod: 1000 })
this.nostr = new NostrAdapter()

// Bind 'this' object to all subfunctions
this.takeOffer = this.takeOffer.bind(this)
this.ensureFunds = this.ensureFunds.bind(this)
this.moveBch = this.moveBch.bind(this)
this.generatePartialTx = this.generatePartialTx.bind(this)
this.uploadCounterOffer = this.uploadCounterOffer.bind(this)
this.ensureFunds = this.ensureFunds.bind(this)
}

// This is a top-level function that orchestrates several subfunctions, in
Expand Down Expand Up @@ -118,6 +120,38 @@ class Take {
}
}

// Generate a Counter Offer and upload it to Nostr
async uploadCounterOffer2 (inObj = {}) {
try {
const { offerData, partialHex, offerCid } = inObj

console.log(`uploadCounterOffer2() offerData: ${JSON.stringify(offerData, null, 2)}`)

// Scaffold the Counter Offer from the Offer
const counterOfferData = Object.assign({}, offerData.data)
counterOfferData.partialTxHex = partialHex
delete counterOfferData.nostrEventId
delete counterOfferData._id
counterOfferData.offerHash = offerData.data.nostrEventId

// Add P2WDB specific flag for signaling that this is a new Counter Offer.
counterOfferData.dataType = 'counter-offer'
console.log(`counterOfferData: ${JSON.stringify(counterOfferData, null, 2)}`)

const nostrData = {
data: counterOfferData
}
const resultEventId = await this.nostr.post(JSON.stringify(nostrData))

const noteId = this.nostr.eventId2note(resultEventId)

return {eventId: resultEventId, nodeId}
} catch (err) {
console.error('Error in uploadCounterOffer2(): ', err)
throw err
}
}

// Generate a partial transaction to *take* a 'sell' offer.
async generatePartialTx (inObj = {}) {
try {
Expand Down

0 comments on commit ef72bd9

Please sign in to comment.