Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bg/OpenPayments): extract OutgoingPaymentGrantService #775

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ networkidle
webextensions
firefoxUserPrefs
textbox
keyid

# packages and 3rd party tools/libraries
awilix
Expand Down
14 changes: 14 additions & 0 deletions src/background/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import browser, { type Browser } from 'webextension-polyfill';
import {
OpenPaymentsService,
StorageService,
WalletService,
MonetizationService,
Background,
TabEvents,
Expand All @@ -17,6 +18,7 @@ import {
EventsService,
Heartbeat,
Deduplicator,
OutgoingPaymentGrantService,
} from './services';
import { createLogger, Logger } from '@/shared/logger';
import { LOG_LEVEL } from '@/shared/defines';
Expand All @@ -39,7 +41,9 @@ export interface Cradle {
events: EventsService;
deduplicator: Deduplicator;
storage: StorageService;
grantService: OutgoingPaymentGrantService;
openPaymentsService: OpenPaymentsService;
walletService: WalletService;
monetizationService: MonetizationService;
message: MessageManager<BackgroundToContentMessage>;
sendToPopup: SendToPopup;
Expand Down Expand Up @@ -75,11 +79,21 @@ export const configureContainer = () => {
.inject(() => ({
logger: logger.getLogger('storage'),
})),
grantService: asClass(OutgoingPaymentGrantService)
.singleton()
.inject(() => ({
logger: logger.getLogger('outgoing-payment-grant'),
})),
openPaymentsService: asClass(OpenPaymentsService)
.singleton()
.inject(() => ({
logger: logger.getLogger('open-payments'),
})),
walletService: asClass(WalletService)
.singleton()
.inject(() => ({
logger: logger.getLogger('wallet'),
})),
monetizationService: asClass(MonetizationService)
.singleton()
.inject(() => ({
Expand Down
22 changes: 11 additions & 11 deletions src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ALARM_RESET_OUT_OF_FUNDS = 'reset-out-of-funds';

export class Background {
private browser: Cradle['browser'];
private openPaymentsService: Cradle['openPaymentsService'];
private walletService: Cradle['walletService'];
private monetizationService: Cradle['monetizationService'];
private storage: Cradle['storage'];
private logger: Cradle['logger'];
Expand All @@ -32,7 +32,7 @@ export class Background {

constructor({
browser,
openPaymentsService,
walletService,
monetizationService,
storage,
logger,
Expand All @@ -44,7 +44,7 @@ export class Background {
}: Cradle) {
Object.assign(this, {
browser,
openPaymentsService,
walletService,
monetizationService,
storage,
sendToPopup,
Expand Down Expand Up @@ -196,34 +196,34 @@ export class Background {
),
);

case 'CONNECT_WALLET':
await this.openPaymentsService.connectWallet(message.payload);
case 'CONNECT_WALLET': {
await this.walletService.connectWallet(message.payload);
if (message.payload?.recurring) {
this.scheduleResetOutOfFundsState();
}
return success(undefined);

}
case 'RECONNECT_WALLET': {
await this.openPaymentsService.reconnectWallet(message.payload);
await this.walletService.reconnectWallet(message.payload);
await this.monetizationService.resumePaymentSessionActiveTab();
await this.updateVisualIndicatorsForCurrentTab();
return success(undefined);
}

case 'UPDATE_BUDGET':
await this.openPaymentsService.updateBudget(message.payload);
await this.walletService.updateBudget(message.payload);
return success(undefined);

case 'ADD_FUNDS':
await this.openPaymentsService.addFunds(message.payload);
await this.walletService.addFunds(message.payload);
await this.browser.alarms.clear(ALARM_RESET_OUT_OF_FUNDS);
if (message.payload.recurring) {
this.scheduleResetOutOfFundsState();
}
return;

case 'DISCONNECT_WALLET':
await this.openPaymentsService.disconnectWallet();
await this.walletService.disconnectWallet();
await this.browser.alarms.clear(ALARM_RESET_OUT_OF_FUNDS);
await this.updateVisualIndicatorsForCurrentTab();
this.sendToPopup.send('SET_STATE', { state: {}, prevState: {} });
Expand Down Expand Up @@ -343,7 +343,7 @@ export class Background {
this.logger.info(data);
if (details.reason === 'install') {
await this.storage.populate();
await this.openPaymentsService.generateKeys();
await this.walletService.generateKeys();
await this.browser.tabs.create({
url: this.browser.runtime.getURL(`${APP_URL}#/post-install`),
});
Expand Down
2 changes: 2 additions & 0 deletions src/background/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export { OpenPaymentsService } from './openPayments';
export { StorageService } from './storage';
export { MonetizationService } from './monetization';
export { OutgoingPaymentGrantService } from './outgoingPaymentGrant';
export { WalletService } from './wallet';
export { Background } from './background';
export { TabEvents } from './tabEvents';
export { TabState } from './tabState';
Expand Down
18 changes: 12 additions & 6 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export class MonetizationService {
private logger: Cradle['logger'];
private t: Cradle['t'];
private openPaymentsService: Cradle['openPaymentsService'];
private walletService: Cradle['walletService'];
private grantService: Cradle['grantService'];
private storage: Cradle['storage'];
private browser: Cradle['browser'];
private events: Cradle['events'];
private tabState: Cradle['tabState'];
private windowState: Cradle['windowState'];
Expand All @@ -39,10 +40,11 @@ export class MonetizationService {
constructor({
logger,
t,
browser,
openPaymentsService,
walletService,
grantService,
storage,
events,
openPaymentsService,
tabState,
windowState,
message,
Expand All @@ -51,8 +53,9 @@ export class MonetizationService {
logger,
t,
openPaymentsService,
walletService,
grantService,
storage,
browser,
events,
tabState,
windowState,
Expand Down Expand Up @@ -114,7 +117,10 @@ export class MonetizationService {
requestId,
tabId,
frameId,
this.storage,
this.openPaymentsService,
this.walletService,
this.grantService,
this.events,
this.tabState,
removeQueryParams(url!),
Expand Down Expand Up @@ -346,7 +352,7 @@ export class MonetizationService {
[...outgoingPayments]
.filter(([, outgoingPayment]) => outgoingPayment !== null)
.map(async ([sessionId, outgoingPaymentInitial]) => {
for await (const outgoingPayment of this.openPaymentsService.pollOutgoingPayment(
for await (const outgoingPayment of this.walletService.pollOutgoingPayment(
// Null assertion: https://github.com/microsoft/TypeScript/issues/41173
outgoingPaymentInitial!.id,
{ signal, maxAttempts: OUTGOING_PAYMENT_POLLING_MAX_ATTEMPTS },
Expand Down Expand Up @@ -418,7 +424,7 @@ export class MonetizationService {
if (!connected) return false;
if (isOkState(state)) return true;

if (state.out_of_funds && this.openPaymentsService.isAnyGrantUsable()) {
if (state.out_of_funds && this.grantService.isAnyGrantUsable()) {
// if we're in out_of_funds state, we still try to make payments hoping we
// have funds available now. If a payment succeeds, we move out from
// of_out_funds state.
Expand Down
Loading