Skip to content

Commit

Permalink
rename grantService
Browse files Browse the repository at this point in the history
  • Loading branch information
darian committed Jan 8, 2025
1 parent 69cbef4 commit 26ffd3d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/background/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface Cradle {
events: EventsService;
deduplicator: Deduplicator;
storage: StorageService;
grantService: OutgoingPaymentGrantService;
outgoingPaymentGrantService: OutgoingPaymentGrantService;
openPaymentsService: OpenPaymentsService;
walletService: WalletService;
monetizationService: MonetizationService;
Expand Down Expand Up @@ -79,7 +79,7 @@ export const configureContainer = () => {
.inject(() => ({
logger: logger.getLogger('storage'),
})),
grantService: asClass(OutgoingPaymentGrantService)
outgoingPaymentGrantService: asClass(OutgoingPaymentGrantService)
.singleton()
.inject(() => ({
logger: logger.getLogger('outgoing-payment-grant'),
Expand Down
15 changes: 9 additions & 6 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MonetizationService {
private logger: Cradle['logger'];
private t: Cradle['t'];
private openPaymentsService: Cradle['openPaymentsService'];
private grantService: Cradle['grantService'];
private outgoingPaymentGrantService: Cradle['outgoingPaymentGrantService'];
private storage: Cradle['storage'];
private events: Cradle['events'];
private tabState: Cradle['tabState'];
Expand All @@ -40,7 +40,7 @@ export class MonetizationService {
logger,
t,
openPaymentsService,
grantService,
outgoingPaymentGrantService,
storage,
events,
tabState,
Expand All @@ -51,7 +51,7 @@ export class MonetizationService {
logger,
t,
openPaymentsService,
grantService,
outgoingPaymentGrantService,
storage,
events,
tabState,
Expand Down Expand Up @@ -116,7 +116,7 @@ export class MonetizationService {
frameId,
this.storage,
this.openPaymentsService,
this.grantService,
this.outgoingPaymentGrantService,
this.events,
this.tabState,
removeQueryParams(url!),
Expand Down Expand Up @@ -360,7 +360,7 @@ export class MonetizationService {
)) {
outgoingPayments.set(sessionId, outgoingPayment);
}
return outgoingPayments.get(sessionId)!;
return outgoingPayments.get(sessionId);
}),
);

Expand Down Expand Up @@ -425,7 +425,10 @@ export class MonetizationService {
if (!connected) return false;
if (isOkState(state)) return true;

if (state.out_of_funds && this.grantService.isAnyGrantUsable()) {
if (
state.out_of_funds &&
this.outgoingPaymentGrantService.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
17 changes: 4 additions & 13 deletions src/background/services/outgoingPaymentGrant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,16 @@ export class OutgoingPaymentGrantService {
);
}

const grantDetails = this.buildGrantDetails(
continuation,
recurring,
walletAmount,
);

await this.persistGrantDetails(grantDetails);
this.grant = this.buildGrantDetails(continuation, recurring, walletAmount);
await this.persistGrantDetails(this.grant);

await this.redirectToWelcomeScreen(
tabId,
GrantResult.GRANT_SUCCESS,
intent,
);

return grantDetails;
return this.grant;
}

public async cancelGrant(grantContinuation: GrantDetails['continue']) {
Expand Down Expand Up @@ -292,9 +287,7 @@ export class OutgoingPaymentGrantService {
}
}

public async addPublicKeyToWalletWithRotateToken(
walletAddress: WalletAddress,
) {
public async retryAddPublicKeyToWallet(walletAddress: WalletAddress) {
let tabId: number | undefined;

try {
Expand Down Expand Up @@ -539,8 +532,6 @@ export class OutgoingPaymentGrantService {
});
this.isGrantUsable.oneTime = true;
}

this.grant = grantDetails;
}

private async redirectToWelcomeScreen(
Expand Down
34 changes: 17 additions & 17 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class PaymentSession {
private frameId: number,
private storage: StorageService,
private openPaymentsService: OpenPaymentsService,
private grantService: OutgoingPaymentGrantService,
private outgoingPaymentGrantService: OutgoingPaymentGrantService,
private events: EventsService,
private tabState: TabState,
private url: string,
Expand Down Expand Up @@ -147,7 +147,7 @@ export class PaymentSession {
throw new DOMException('Aborting existing probing', 'AbortError');
}
try {
await this.probeDebitAmount(
await this.createPaymentQuote(
amountToSend.toString(),
this.incomingPaymentUrl,
this.sender,
Expand All @@ -156,7 +156,7 @@ export class PaymentSession {
break;
} catch (e) {
if (isTokenExpiredError(e)) {
await this.grantService.rotateToken();
await this.outgoingPaymentGrantService.rotateToken();
} else if (isNonPositiveAmountError(e)) {
amountToSend = BigInt(amountIter.next().value);
continue;
Expand Down Expand Up @@ -397,10 +397,10 @@ export class PaymentSession {
amount,
incomingPaymentId,
}: CreateOutgoingPaymentParams): Promise<OutgoingPayment> {
const client = this.openPaymentsService.client;
const { client } = this.openPaymentsService;
const outgoingPayment = await client.outgoingPayment.create(
{
accessToken: this.grantService.accessToken(),
accessToken: this.outgoingPaymentGrantService.accessToken(),
url: walletAddress.resourceServer,
},
{
Expand All @@ -419,7 +419,7 @@ export class PaymentSession {

if (outgoingPayment.grantSpentDebitAmount) {
this.storage.updateSpentAmount(
this.grantService.grantType(),
this.outgoingPaymentGrantService.grantType(),
outgoingPayment.grantSpentDebitAmount.value,
);
}
Expand All @@ -428,15 +428,15 @@ export class PaymentSession {
return outgoingPayment;
}

private async probeDebitAmount(
private async createPaymentQuote(
amount: AmountValue,
incomingPayment: IncomingPayment['id'],
sender: WalletAddress,
): Promise<void> {
await this.openPaymentsService.client.quote.create(
{
url: sender.resourceServer,
accessToken: this.grantService.accessToken(),
accessToken: this.outgoingPaymentGrantService.accessToken(),
},
{
method: 'ilp',
Expand Down Expand Up @@ -493,7 +493,7 @@ export class PaymentSession {
this.events.emit('open_payments.key_revoked');
throw e;
} else if (isTokenExpiredError(e)) {
await this.grantService.rotateToken();
await this.outgoingPaymentGrantService.rotateToken();
return await this.pay(amount); // retry
} else {
throw e;
Expand All @@ -513,11 +513,11 @@ export class PaymentSession {
while (++attempt <= maxAttempts) {
try {
signal?.throwIfAborted();
const outgoingPayment =
await this.openPaymentsService.client.outgoingPayment.get({
url: outgoingPaymentId,
accessToken: this.grantService.accessToken(),
});
const { client } = this.openPaymentsService;
const outgoingPayment = await client.outgoingPayment.get({
url: outgoingPaymentId,
accessToken: this.outgoingPaymentGrantService.accessToken(),
});
yield outgoingPayment;
if (
outgoingPayment.failed &&
Expand All @@ -540,7 +540,7 @@ export class PaymentSession {
// TODO: We can remove the token `actions` check once we've proper RS
// errors in place. Then we can handle insufficient grant error
// separately clearly.
const token = await this.grantService.rotateToken();
const token = await this.outgoingPaymentGrantService.rotateToken();
const hasReadAccess = token.access_token.access.find(
(e) => e.type === 'outgoing-payment' && e.actions.includes('read'),
);
Expand Down Expand Up @@ -601,10 +601,10 @@ export class PaymentSession {
if (isKeyRevokedError(e)) {
this.events.emit('open_payments.key_revoked');
} else if (isTokenExpiredError(e)) {
await this.grantService.rotateToken();
await this.outgoingPaymentGrantService.rotateToken();
this.shouldRetryImmediately = true;
} else if (isOutOfBalanceError(e)) {
const switched = await this.grantService.switchGrant();
const switched = await this.outgoingPaymentGrantService.switchGrant();
if (switched === null) {
this.events.emit('open_payments.out_of_funds');
} else {
Expand Down
Loading

0 comments on commit 26ffd3d

Please sign in to comment.