This commit is contained in:
hatim boufnichel 2024-05-21 21:47:39 +02:00
parent 779d3204c3
commit bbd00b3450
4 changed files with 35 additions and 21 deletions

View file

@ -12,6 +12,7 @@ export class LiquidityProvider {
client: ReturnType<typeof newNostrClient> client: ReturnType<typeof newNostrClient>
clientCbs: Record<string, nostrCallback<any>> = {} clientCbs: Record<string, nostrCallback<any>> = {}
clientId: string = "" clientId: string = ""
myPub: string = ""
log = getLogger({ component: 'liquidityProvider' }) log = getLogger({ component: 'liquidityProvider' })
nostrSend: NostrSend | null = null nostrSend: NostrSend | null = null
ready = false ready = false
@ -22,37 +23,48 @@ export class LiquidityProvider {
if (!pubDestination) { if (!pubDestination) {
this.log("No pub provider to liquidity provider, will not be initialized") this.log("No pub provider to liquidity provider, will not be initialized")
} }
this.pubDestination = pubDestination
this.client = newNostrClient({ this.client = newNostrClient({
pubDestination: pubDestination, pubDestination: this.pubDestination,
retrieveNostrUserAuth: async () => "", retrieveNostrUserAuth: async () => this.myPub,
}, this.clientSend, this.clientSub) }, this.clientSend, this.clientSub)
const interval = setInterval(() => { const interval = setInterval(() => {
if (this.ready) { if (this.ready) {
this.log("ready")
clearInterval(interval) clearInterval(interval)
this.client.GetUserInfo().then(res => { this.CheckUSerState()
}
}, 1000)
}
CheckUSerState = async () => {
await new Promise(res => setTimeout(res, 2000))
this.log("ready")
const res = await this.client.GetUserInfo()
if (res.status === 'ERROR') { if (res.status === 'ERROR') {
this.log("error getting user info", res) this.log("error getting user info", res)
return return
} }
this.log("got user info", res) this.log("got user info", res)
})
}
}, 1000)
} }
setClientId = (clientId: string) => { setNostrInfo = ({ clientId, myPub }: { myPub: string, clientId: string }) => {
this.clientId = clientId this.clientId = clientId
if (this.nostrSend && this.pubDestination !== "") { this.myPub = myPub
this.ready = true this.setSetIfReady()
}
} }
attachNostrSend(f: NostrSend) { attachNostrSend(f: NostrSend) {
this.nostrSend = f this.nostrSend = f
if (this.clientId !== "" && this.pubDestination !== "") { this.setSetIfReady()
}
setSetIfReady = () => {
if (this.nostrSend && !!this.pubDestination && !!this.clientId && !!this.myPub) {
this.ready = true this.ready = true
this.log("ready to send to ", this.pubDestination)
} }
} }

View file

@ -49,7 +49,7 @@ export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings
publicKey: liquidityProviderApp.publicKey, publicKey: liquidityProviderApp.publicKey,
name: "liquidity_provider", clientId: `client_${liquidityProviderApp.appId}` name: "liquidity_provider", clientId: `client_${liquidityProviderApp.appId}`
} }
liquidityProvider.setClientId(liquidityProviderInfo.clientId) liquidityProvider.setNostrInfo({ clientId: liquidityProviderInfo.clientId, myPub: liquidityProviderInfo.publicKey })
const stop = await processArgs(mainHandler) const stop = await processArgs(mainHandler)
if (stop) { if (stop) {
return return

View file

@ -1,14 +1,15 @@
import { LoadTestSettingsFromEnv } from "../services/main/settings.js" import { LoadTestSettingsFromEnv } from "../services/main/settings.js"
import { BitcoinCoreWrapper } from "./bitcoinCore.js" import { BitcoinCoreWrapper } from "./bitcoinCore.js"
import LND from '../services/lnd/lnd.js' import LND from '../services/lnd/lnd.js'
import { LiquidityProvider } from "../services/lnd/liquidityProvider.js"
export const setupNetwork = async () => { export const setupNetwork = async () => {
const settings = LoadTestSettingsFromEnv() const settings = LoadTestSettingsFromEnv()
const core = new BitcoinCoreWrapper(settings) const core = new BitcoinCoreWrapper(settings)
await core.InitAddress() await core.InitAddress()
await core.Mine(1) await core.Mine(1)
const alice = new LND(settings.lndSettings, () => { }, () => { }, () => { }, () => { }) const alice = new LND(settings.lndSettings, new LiquidityProvider(""), () => { }, () => { }, () => { }, () => { })
const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, () => { }, () => { }, () => { }, () => { }) const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, new LiquidityProvider(""), () => { }, () => { }, () => { }, () => { })
await tryUntil<void>(async i => { await tryUntil<void>(async i => {
const peers = await alice.ListPeers() const peers = await alice.ListPeers()
if (peers.peers.length > 0) { if (peers.peers.length > 0) {

View file

@ -10,6 +10,7 @@ import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js'
import SanityChecker from '../services/main/sanityChecker.js' import SanityChecker from '../services/main/sanityChecker.js'
import LND from '../services/lnd/lnd.js' import LND from '../services/lnd/lnd.js'
import { resetDisabledLoggers } from '../services/helpers/logger.js' import { resetDisabledLoggers } from '../services/helpers/logger.js'
import { LiquidityProvider } from '../services/lnd/liquidityProvider.js'
chai.use(chaiString) chai.use(chaiString)
export const expect = chai.expect export const expect = chai.expect
export type Describe = (message: string, failure?: boolean) => void export type Describe = (message: string, failure?: boolean) => void
@ -45,15 +46,15 @@ export const SetupTest = async (d: Describe): Promise<TestBase> => {
const user2 = { userId: u2.info.userId, appUserIdentifier: u2.identifier, appId: app.appId } const user2 = { userId: u2.info.userId, appUserIdentifier: u2.identifier, appId: app.appId }
const externalAccessToMainLnd = new LND(settings.lndSettings, console.log, console.log, () => { }, () => { }) const externalAccessToMainLnd = new LND(settings.lndSettings, new LiquidityProvider(""), console.log, console.log, () => { }, () => { })
await externalAccessToMainLnd.Warmup() await externalAccessToMainLnd.Warmup()
const otherLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.otherNode } const otherLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }
const externalAccessToOtherLnd = new LND(otherLndSetting, console.log, console.log, () => { }, () => { }) const externalAccessToOtherLnd = new LND(otherLndSetting, new LiquidityProvider(""), console.log, console.log, () => { }, () => { })
await externalAccessToOtherLnd.Warmup() await externalAccessToOtherLnd.Warmup()
const thirdLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.thirdNode } const thirdLndSetting = { ...settings.lndSettings, mainNode: settings.lndSettings.thirdNode }
const externalAccessToThirdLnd = new LND(thirdLndSetting, console.log, console.log, () => { }, () => { }) const externalAccessToThirdLnd = new LND(thirdLndSetting, new LiquidityProvider(""), console.log, console.log, () => { }, () => { })
await externalAccessToThirdLnd.Warmup() await externalAccessToThirdLnd.Warmup()