add balance updated event
This commit is contained in:
parent
0aec49b25b
commit
15202e5339
2 changed files with 37 additions and 4 deletions
33
index.ts
33
index.ts
|
|
@ -89,7 +89,6 @@ export default class PortalBtcWallet {
|
|||
this.cashuSDK = new PortalBtcWalletCashu(
|
||||
mnemonic,
|
||||
cashuStore,
|
||||
cashuMeltThreshold,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -105,6 +104,7 @@ export default class PortalBtcWallet {
|
|||
console.log(e);
|
||||
}
|
||||
this.refreshBreezBalances();
|
||||
this.emitEvent("balanceUpdated");
|
||||
},
|
||||
});
|
||||
setLogger({
|
||||
|
|
@ -114,7 +114,7 @@ export default class PortalBtcWallet {
|
|||
}
|
||||
},
|
||||
});
|
||||
await this.cashuSDK.init();
|
||||
await this.cashuSDK.init(() => this.emitEvent("balanceUpdated"));
|
||||
await this.redeemCashuQuotes();
|
||||
await this.maybeMelt();
|
||||
}
|
||||
|
|
@ -134,6 +134,7 @@ export default class PortalBtcWallet {
|
|||
(this.cashuBalance % this.cashuMeltThreshold);
|
||||
const invoice = await this.generateBolt11Invoice(cashuAmountToSwapToLiquid);
|
||||
await this.cashuSDK.meltProofsToPayInvoice(invoice);
|
||||
this.emitEvent("balanceUpdated");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -173,6 +174,7 @@ export default class PortalBtcWallet {
|
|||
async redeemToken(token: string): Promise<void> {
|
||||
await this.cashuSDK.redeemToken(token);
|
||||
await this.maybeMelt();
|
||||
this.emitEvent("balanceUpdated");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -317,6 +319,7 @@ export default class PortalBtcWallet {
|
|||
try {
|
||||
if (parsedPayment.type === "bolt11") {
|
||||
yield* this.payBolt11Invoice(parsedPayment, destination, amount);
|
||||
this.emitEvent("balanceUpdated");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -342,6 +345,7 @@ export default class PortalBtcWallet {
|
|||
parsedPayment.type === "liquidAddress"
|
||||
) {
|
||||
yield* this.payChainAddress(parsedPayment, amount);
|
||||
this.emitEvent("balanceUpdated");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -369,6 +373,7 @@ export default class PortalBtcWallet {
|
|||
status: PaymentStatus.PaymentSent,
|
||||
error: null,
|
||||
};
|
||||
this.emitEvent("balanceUpdated");
|
||||
return;
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
|
|
@ -406,4 +411,28 @@ export default class PortalBtcWallet {
|
|||
);
|
||||
return sortedPayments.slice(offset, offset + limit);
|
||||
}
|
||||
|
||||
private eventListeners: Record<string, (() => void)[]> = {};
|
||||
|
||||
/**
|
||||
* Adds an event listener
|
||||
*
|
||||
* @param event - The event to listen for
|
||||
* @param listener - The listener function
|
||||
*/
|
||||
addEventListeners(event: "balanceUpdated", listener: () => void) {
|
||||
if (!this.eventListeners[event]) {
|
||||
this.eventListeners[event] = [];
|
||||
}
|
||||
this.eventListeners[event].push(listener);
|
||||
}
|
||||
|
||||
private emitEvent(event: "balanceUpdated") {
|
||||
if (this.loggingEnabled) {
|
||||
console.log(`[${event}]`);
|
||||
}
|
||||
for (const listener of this.eventListeners[event] ?? []) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue