From bbd00b3450c722d8e6119a7cf41c84c84e6b7279 Mon Sep 17 00:00:00 2001 From: hatim boufnichel Date: Tue, 21 May 2024 21:47:39 +0200 Subject: [PATCH] wired --- src/services/lnd/liquidityProvider.ts | 42 +++++++++++++++++---------- src/services/main/init.ts | 2 +- src/tests/networkSetup.ts | 5 ++-- src/tests/testBase.ts | 7 +++-- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/services/lnd/liquidityProvider.ts b/src/services/lnd/liquidityProvider.ts index 7e672861..9e34510e 100644 --- a/src/services/lnd/liquidityProvider.ts +++ b/src/services/lnd/liquidityProvider.ts @@ -12,6 +12,7 @@ export class LiquidityProvider { client: ReturnType clientCbs: Record> = {} clientId: string = "" + myPub: string = "" log = getLogger({ component: 'liquidityProvider' }) nostrSend: NostrSend | null = null ready = false @@ -22,37 +23,48 @@ export class LiquidityProvider { if (!pubDestination) { this.log("No pub provider to liquidity provider, will not be initialized") } + this.pubDestination = pubDestination this.client = newNostrClient({ - pubDestination: pubDestination, - retrieveNostrUserAuth: async () => "", + pubDestination: this.pubDestination, + retrieveNostrUserAuth: async () => this.myPub, }, this.clientSend, this.clientSub) const interval = setInterval(() => { if (this.ready) { - this.log("ready") clearInterval(interval) - this.client.GetUserInfo().then(res => { - if (res.status === 'ERROR') { - this.log("error getting user info", res) - return - } - this.log("got user info", res) - }) + this.CheckUSerState() } }, 1000) } - setClientId = (clientId: string) => { - this.clientId = clientId - if (this.nostrSend && this.pubDestination !== "") { - this.ready = true + CheckUSerState = async () => { + await new Promise(res => setTimeout(res, 2000)) + this.log("ready") + const res = await this.client.GetUserInfo() + if (res.status === 'ERROR') { + this.log("error getting user info", res) + return } + this.log("got user info", res) } + setNostrInfo = ({ clientId, myPub }: { myPub: string, clientId: string }) => { + this.clientId = clientId + this.myPub = myPub + this.setSetIfReady() + } + + + attachNostrSend(f: NostrSend) { this.nostrSend = f - if (this.clientId !== "" && this.pubDestination !== "") { + this.setSetIfReady() + } + + setSetIfReady = () => { + if (this.nostrSend && !!this.pubDestination && !!this.clientId && !!this.myPub) { this.ready = true + this.log("ready to send to ", this.pubDestination) } } diff --git a/src/services/main/init.ts b/src/services/main/init.ts index 80ef8cc5..fb21b170 100644 --- a/src/services/main/init.ts +++ b/src/services/main/init.ts @@ -49,7 +49,7 @@ export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings publicKey: liquidityProviderApp.publicKey, name: "liquidity_provider", clientId: `client_${liquidityProviderApp.appId}` } - liquidityProvider.setClientId(liquidityProviderInfo.clientId) + liquidityProvider.setNostrInfo({ clientId: liquidityProviderInfo.clientId, myPub: liquidityProviderInfo.publicKey }) const stop = await processArgs(mainHandler) if (stop) { return diff --git a/src/tests/networkSetup.ts b/src/tests/networkSetup.ts index 9bcdba1c..90ddc7a5 100644 --- a/src/tests/networkSetup.ts +++ b/src/tests/networkSetup.ts @@ -1,14 +1,15 @@ import { LoadTestSettingsFromEnv } from "../services/main/settings.js" import { BitcoinCoreWrapper } from "./bitcoinCore.js" import LND from '../services/lnd/lnd.js' +import { LiquidityProvider } from "../services/lnd/liquidityProvider.js" export const setupNetwork = async () => { const settings = LoadTestSettingsFromEnv() const core = new BitcoinCoreWrapper(settings) await core.InitAddress() await core.Mine(1) - const alice = new LND(settings.lndSettings, () => { }, () => { }, () => { }, () => { }) - const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, () => { }, () => { }, () => { }, () => { }) + const alice = new LND(settings.lndSettings, new LiquidityProvider(""), () => { }, () => { }, () => { }, () => { }) + const bob = new LND({ ...settings.lndSettings, mainNode: settings.lndSettings.otherNode }, new LiquidityProvider(""), () => { }, () => { }, () => { }, () => { }) await tryUntil(async i => { const peers = await alice.ListPeers() if (peers.peers.length > 0) { diff --git a/src/tests/testBase.ts b/src/tests/testBase.ts index 246aceb6..248a9117 100644 --- a/src/tests/testBase.ts +++ b/src/tests/testBase.ts @@ -10,6 +10,7 @@ import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js' import SanityChecker from '../services/main/sanityChecker.js' import LND from '../services/lnd/lnd.js' import { resetDisabledLoggers } from '../services/helpers/logger.js' +import { LiquidityProvider } from '../services/lnd/liquidityProvider.js' chai.use(chaiString) export const expect = chai.expect export type Describe = (message: string, failure?: boolean) => void @@ -45,15 +46,15 @@ export const SetupTest = async (d: Describe): Promise => { 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() 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() 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()