fix(GET /offer/list/addr/:addr): Get offers by an address

This commit is contained in:
Chris Troutner
2025-07-28 11:12:04 -07:00
parent 578aade39b
commit dda6cb2aa4
3 changed files with 41 additions and 10 deletions
@@ -1,5 +1,8 @@
/*
REST API Controller library for the /offer route
TODO:
- Add api-doc documentation for all endpoints in this file.
*/
import wlogger from '../../../adapters/wlogger.js'
@@ -30,6 +33,7 @@ class OfferRESTControllerLib {
this.listNftOffers = this.listNftOffers.bind(this)
this.listFungibleOffers = this.listFungibleOffers.bind(this)
this.takeOffer = this.takeOffer.bind(this)
this.listOffersByAddress = this.listOffersByAddress.bind(this)
this.handleError = this.handleError.bind(this)
}
@@ -115,6 +119,21 @@ class OfferRESTControllerLib {
}
}
// List all offers being made by a given address.
// curl -X GET http://localhost:5700/offer/list/addr/bitcoincash:qrpxtnrlhfz9wsuuse7z5k2mxmvw0r3pu5qepyhmq2
async listOffersByAddress (ctx) {
try {
const addr = ctx.params.addr
const offers = await this.useCases.offer.listOffersByAddress(addr)
ctx.body = offers
} catch (err) {
console.log('Error in listOffersByAddress REST API handler: ', err)
this.handleError(ctx, err)
}
}
// DRY error handler
handleError (ctx, err) {
console.log('err', err.message)
+10 -10
View File
@@ -8,8 +8,6 @@ import Router from 'koa-router'
// Local libraries.
import OfferRESTControllerLib from './controller.js'
let _this
class OfferRouter {
constructor (localConfig = {}) {
// Dependency Injection.
@@ -38,7 +36,8 @@ class OfferRouter {
const baseUrl = '/offer'
this.router = new Router({ prefix: baseUrl })
_this = this
// Bind 'this' object to all subfunctions.
this.attach = this.attach.bind(this)
}
attach (app) {
@@ -50,19 +49,20 @@ class OfferRouter {
// 12/3/24 CT:
// Note: The createOffer() path was used by a P2WDB webhook to generate an
// Offer from and Order. This has been deprecated and Offers are now created
// Offer from an Order. This has been deprecated and Offers are now created
// by a Timer Controller monitoring a Nostr topic.
// Define the routes and attach the controller.
// this.router.post('/', _this.offerRESTController.createOffer) // Deprecated.
this.router.post('/take', _this.offerRESTController.takeOffer)
this.router.get('/list/all/:page', _this.offerRESTController.listOffers)
this.router.get('/list/nft/:page', _this.offerRESTController.listNftOffers)
this.router.get('/list/fungible/:page', _this.offerRESTController.listFungibleOffers)
this.router.post('/take', this.offerRESTController.takeOffer)
this.router.get('/list/all/:page', this.offerRESTController.listOffers)
this.router.get('/list/nft/:page', this.offerRESTController.listNftOffers)
this.router.get('/list/fungible/:page', this.offerRESTController.listFungibleOffers)
this.router.get('/list/addr/:addr', this.offerRESTController.listOffersByAddress)
// Attach the Controller routes to the Koa app.
app.use(_this.router.routes())
app.use(_this.router.allowedMethods())
app.use(this.router.routes())
app.use(this.router.allowedMethods())
}
}
+12
View File
@@ -64,6 +64,7 @@ class OfferUseCases {
this.removeStaleOffers = this.removeStaleOffers.bind(this)
this.flagOffer = this.flagOffer.bind(this)
this.loadOffers = this.loadOffers.bind(this)
this.listOffersByAddress = this.listOffersByAddress.bind(this)
// State
this.seenOffers = []
@@ -808,6 +809,17 @@ class OfferUseCases {
throw error
}
}
// List all offers being made by a given address.
async listOffersByAddress (addr) {
try {
const offers = await this.OfferModel.find({ makerAddr: addr })
return offers
} catch (err) {
console.error('Error in listOffersByAddress(): ', err)
throw err
}
}
}
export default OfferUseCases