offers liquidity

This commit is contained in:
shocknet-justin 2025-05-11 16:37:17 -04:00
parent ab76283131
commit fa2a2a593d
4 changed files with 22 additions and 6 deletions

1
.gitignore vendored
View file

@ -22,3 +22,4 @@ app.nprofile
admin.connect admin.connect
debug.txt debug.txt
proto/autogenerated/debug.txt proto/autogenerated/debug.txt
.specstory

View file

@ -75,7 +75,7 @@ export default class {
this.applicationManager = new ApplicationManager(this.storage, this.settings, this.paymentManager) this.applicationManager = new ApplicationManager(this.storage, this.settings, this.paymentManager)
this.appUserManager = new AppUserManager(this.storage, this.settings, this.applicationManager) this.appUserManager = new AppUserManager(this.storage, this.settings, this.applicationManager)
this.debitManager = new DebitManager(this.storage, this.lnd, this.applicationManager) this.debitManager = new DebitManager(this.storage, this.lnd, this.applicationManager)
this.offerManager = new OfferManager(this.storage, this.lnd, this.applicationManager, this.productManager) this.offerManager = new OfferManager(this.storage, this.lnd, this.applicationManager, this.productManager, this.liquidityManager)
this.webRTC = new webRTC(this.storage, this.utils) this.webRTC = new webRTC(this.storage, this.utils)
} }

View file

@ -17,6 +17,7 @@ import { UserOffer } from '../storage/entity/UserOffer.js';
import { DeepPartial } from 'typeorm'; import { DeepPartial } from 'typeorm';
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { LoadNosrtSettingsFromEnv } from '../nostr/index.js'; import { LoadNosrtSettingsFromEnv } from '../nostr/index.js';
import { LiquidityManager } from "./liquidityManager.js"
const mapToOfferConfig = (appUserId: string, offer: UserOffer, { pubkey, relay }: { pubkey: string, relay: string }): Types.OfferConfig => { const mapToOfferConfig = (appUserId: string, offer: UserOffer, { pubkey, relay }: { pubkey: string, relay: string }): Types.OfferConfig => {
if (offer.expected_data) { if (offer.expected_data) {
@ -50,12 +51,14 @@ export class OfferManager {
productManager: ProductManager productManager: ProductManager
storage: Storage storage: Storage
lnd: LND lnd: LND
liquidityManager: LiquidityManager
logger = getLogger({ component: 'DebitManager' }) logger = getLogger({ component: 'DebitManager' })
constructor(storage: Storage, lnd: LND, applicationManager: ApplicationManager, productManager: ProductManager) { constructor(storage: Storage, lnd: LND, applicationManager: ApplicationManager, productManager: ProductManager, liquidityManager: LiquidityManager) {
this.storage = storage this.storage = storage
this.lnd = lnd this.lnd = lnd
this.applicationManager = applicationManager this.applicationManager = applicationManager
this.productManager = productManager this.productManager = productManager
this.liquidityManager = liquidityManager
} }
attachNostrSend = (nostrSend: NostrSend) => { attachNostrSend = (nostrSend: NostrSend) => {
@ -219,14 +222,18 @@ export class OfferManager {
async getNofferInvoice(offerReq: NofferData, appId: string): Promise<{ success: true, invoice: string } | { success: false, code: number, max: number }> { async getNofferInvoice(offerReq: NofferData, appId: string): Promise<{ success: true, invoice: string } | { success: false, code: number, max: number }> {
try { try {
const { remote } = await this.lnd.ChannelBalance() const { remote } = await this.lnd.ChannelBalance()
let maxSendable = remote
if (remote === 0 && (await this.liquidityManager.liquidityProvider.IsReady())) {
maxSendable = 10_000_000
}
const split = offerReq.offer.split(':') const split = offerReq.offer.split(':')
if (split.length === 1) { if (split.length === 1) {
return this.HandleUserOffer(offerReq, appId, remote) return this.HandleUserOffer(offerReq, appId, maxSendable)
} else if (split[0] === 'p') { } else if (split[0] === 'p') {
const product = await this.productManager.NewProductInvoice(split[1]) const product = await this.productManager.NewProductInvoice(split[1])
return { success: true, invoice: product.invoice } return { success: true, invoice: product.invoice }
} else { } else {
return { success: false, code: 1, max: remote } return { success: false, code: 1, max: maxSendable }
} }
} catch (e: any) { } catch (e: any) {
getLogger({ component: "noffer" })(ERROR, e.message || e) getLogger({ component: "noffer" })(ERROR, e.message || e)

View file

@ -497,10 +497,14 @@ export default class {
const payK1 = await this.storage.paymentStorage.AddUserEphemeralKey(userId, 'pay', linkedApplication) const payK1 = await this.storage.paymentStorage.AddUserEphemeralKey(userId, 'pay', linkedApplication)
const url = baseUrl ? baseUrl : `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle` const url = baseUrl ? baseUrl : `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle`
const { remote } = await this.lnd.ChannelBalance() const { remote } = await this.lnd.ChannelBalance()
let maxSendable = remote * 1000
if (remote === 0 && (await this.liquidityManager.liquidityProvider.IsReady())) {
maxSendable = 10_000_000 * 1000
}
return { return {
tag: 'payRequest', tag: 'payRequest',
callback: `${url}?k1=${payK1.key}`, callback: `${url}?k1=${payK1.key}`,
maxSendable: remote * 1000, maxSendable: maxSendable,
minSendable: 10000, minSendable: 10000,
metadata: metadata ? metadata : defaultLnurlPayMetadata(this.settings.lnurlMetaText), metadata: metadata ? metadata : defaultLnurlPayMetadata(this.settings.lnurlMetaText),
allowsNostr: !!linkedApplication.nostr_public_key, allowsNostr: !!linkedApplication.nostr_public_key,
@ -517,10 +521,14 @@ export default class {
throw new Error("invalid lnurl request") throw new Error("invalid lnurl request")
} }
const { remote } = await this.lnd.ChannelBalance() const { remote } = await this.lnd.ChannelBalance()
let maxSendable = remote * 1000
if (remote === 0 && (await this.liquidityManager.liquidityProvider.IsReady())) {
maxSendable = 10_000_000 * 1000
}
return { return {
tag: 'payRequest', tag: 'payRequest',
callback: `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle?k1=${payInfoK1}`, callback: `${this.settings.serviceUrl}/api/guest/lnurl_pay/handle?k1=${payInfoK1}`,
maxSendable: remote * 1000, maxSendable: maxSendable,
minSendable: 10000, minSendable: 10000,
metadata: defaultLnurlPayMetadata(this.settings.lnurlMetaText), metadata: defaultLnurlPayMetadata(this.settings.lnurlMetaText),
allowsNostr: !!key.linkedApplication.nostr_public_key, allowsNostr: !!key.linkedApplication.nostr_public_key,