Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
boufni95 committed Jul 13, 2024
1 parent 3703329 commit d09c2bf
Show file tree
Hide file tree
Showing 10 changed files with 2,832 additions and 2,856 deletions.
484 changes: 241 additions & 243 deletions proto/autogenerated/client.md

Large diffs are not rendered by default.

2,466 changes: 1,224 additions & 1,242 deletions proto/autogenerated/debug.txt

Large diffs are not rendered by default.

2,706 changes: 1,348 additions & 1,358 deletions proto/autogenerated/ts/types.ts

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions proto/service/structs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ message PayInvoiceResponse{
string operation_id = 3;
int64 service_fee = 4;
int64 network_fee = 5;
int64 latest_balance = 6;
}

message OpenChannelRequest{
Expand Down Expand Up @@ -417,7 +416,6 @@ message GetProductBuyLinkResponse {

message LiveUserOperation {
UserOperation operation = 1;
int64 latest_balance = 2;
}
message MigrationUpdate {
optional ClosureMigration closure = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/services/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default class {
getLogger({ appName: app.name })("cannot notify user, not a nostr user")
return
}
const message: Types.LiveUserOperation & { requestId: string, status: 'OK' } = { operation: op, latest_balance: user.user.balance_sats, requestId: "GetLiveUserOperations", status: 'OK' }
const message: Types.LiveUserOperation & { requestId: string, status: 'OK' } = { operation: op, requestId: "GetLiveUserOperations", status: 'OK' }
this.nostrSend({ type: 'app', appId: app.app_id }, { type: 'content', content: JSON.stringify(message), pub: user.nostr_public_key })
}

Expand Down
13 changes: 7 additions & 6 deletions src/services/main/liquidityProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class LiquidityProvider {
queue: ((state: 'ready') => void)[] = []
utils: Utils
pendingPayments: Record<string, number> = {}
updateProviderBalance: (balance: number) => Promise<void>
incrementProviderBalance: (balance: number) => Promise<void>
// make the sub process accept client
constructor(pubDestination: string, utils: Utils, invoicePaidCb: InvoicePaidCb, updateProviderBalance: (balance: number) => Promise<any>) {
constructor(pubDestination: string, utils: Utils, invoicePaidCb: InvoicePaidCb, incrementProviderBalance: (balance: number) => Promise<any>) {
this.utils = utils
if (!pubDestination) {
this.log("No pub provider to liquidity provider, will not be initialized")
Expand All @@ -39,7 +39,7 @@ export class LiquidityProvider {
this.log("connecting to liquidity provider:", pubDestination)
this.pubDestination = pubDestination
this.invoicePaidCb = invoicePaidCb
this.updateProviderBalance = updateProviderBalance
this.incrementProviderBalance = incrementProviderBalance
this.client = newNostrClient({
pubDestination: this.pubDestination,
retrieveNostrUserAuth: async () => this.myPub,
Expand Down Expand Up @@ -84,6 +84,7 @@ export class LiquidityProvider {
if (res.status === 'ERROR') {
return
}
this.incrementProviderBalance(res.balance)
this.ready = true
this.queue.forEach(q => q('ready'))
this.log("subbing to user operations")
Expand All @@ -94,7 +95,7 @@ export class LiquidityProvider {
}
//this.log("got user operation", res.operation)
if (res.operation.type === Types.UserOperationType.INCOMING_INVOICE) {
this.updateProviderBalance(res.latest_balance)
this.incrementProviderBalance(res.operation.amount)
this.invoicePaidCb(res.operation.identifier, res.operation.amount, 'provider')
}
})
Expand Down Expand Up @@ -192,8 +193,8 @@ export class LiquidityProvider {
this.log("error paying invoice", res.reason)
throw new Error(res.reason)
}

this.updateProviderBalance(userInfo.balance).then(() => { delete this.pendingPayments[invoice] })
const totalPaid = res.amount_paid + res.network_fee + res.service_fee
this.incrementProviderBalance(-totalPaid).then(() => { delete this.pendingPayments[invoice] })
this.utils.stateBundler.AddTxPoint('paidAnInvoice', decodedAmount, { used: 'provider', from, timeDiscount: true })
return res
} catch (err) {
Expand Down
1 change: 0 additions & 1 deletion src/services/main/paymentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ export default class {
operation_id: `${Types.UserOperationType.OUTGOING_INVOICE}-${paymentInfo.serialId}`,
network_fee: paymentInfo.networkFee,
service_fee: serviceFee,
latest_balance: user.balance_sats
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/main/rugPullTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class RugPullTracker {
this.rugPulled = false
if (providerTracker.latest_distruption_at_unix !== 0) {
await this.storage.liquidityStorage.UpdateTrackedProviderDisruption('lnPub', pubDst, 0)
getLogger({ component: 'rugPull' })("rugpull from: ", pubDst, "cleared after: ", (Date.now() / 1000) - providerTracker.latest_distruption_at_unix, "seconds")
getLogger({ component: 'rugPull' })("rugpull from: ", pubDst, "cleared after: ", Math.floor(Date.now() / 1000) - providerTracker.latest_distruption_at_unix, "seconds")
}
if (diff > 0) {
this.log("detected excees from: ", pubDst, "provider balance changed from", providerTracker.latest_balance, "to", trackedBalance, "gaining", diff)
Expand Down
4 changes: 2 additions & 2 deletions src/services/main/watchdog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Watchdog {
this.accumulatedHtlcFees = 0

this.interval = setInterval(() => {
if (this.latestCheckStart + (1000 * 60) < Date.now()) {
if (this.latestCheckStart + (1000 * 58) < Date.now()) {
this.PaymentRequested()
}
}, 1000 * 60)
Expand Down Expand Up @@ -160,7 +160,7 @@ export class Watchdog {
} else {
if (tracker.latest_distruption_at_unix !== 0) {
await this.storage.liquidityStorage.UpdateTrackedProviderDisruption('lnd', this.lndPubKey, 0)
getLogger({ component: 'bark' })("loss cleared after: ", (Date.now() / 1000) - tracker.latest_distruption_at_unix, "seconds")
getLogger({ component: 'bark' })("loss cleared after: ", Math.floor(Date.now() / 1000) - tracker.latest_distruption_at_unix, "seconds")
} else if (absoluteDiff > 0) {
this.log("lnd balance increased more than users balance by", absoluteDiff)
}
Expand Down
8 changes: 8 additions & 0 deletions src/services/storage/liquidityStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export class LiquidityStorage {
console.log("updating tracked balance:", latestBalance)
return this.DB.getRepository(TrackedProvider).update({ provider_pubkey: pub, provider_type: providerType }, { latest_balance: latestBalance })
}
async IncrementTrackedProviderBalance(providerType: 'lnd' | 'lnPub', pub: string, amount: number) {
if (amount < 0) {
return this.DB.getRepository(TrackedProvider).increment({ provider_pubkey: pub, provider_type: providerType }, "latest_balance", amount)
} else {
return this.DB.getRepository(TrackedProvider).decrement({ provider_pubkey: pub, provider_type: providerType }, "latest_balance", -amount)
}

}
async UpdateTrackedProviderDisruption(providerType: 'lnd' | 'lnPub', pub: string, latestDisruptionAtUnix: number) {
return this.DB.getRepository(TrackedProvider).update({ provider_pubkey: pub, provider_type: providerType }, { latest_distruption_at_unix: latestDisruptionAtUnix })
}
Expand Down

0 comments on commit d09c2bf

Please sign in to comment.