Skip to content

Commit

Permalink
feat(background): remove quoting (#371)
Browse files Browse the repository at this point in the history
* Remove quoting

* Update comment
  • Loading branch information
raducristianpopa authored Jun 26, 2024
1 parent 9b5a67f commit ecc81a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 79 deletions.
56 changes: 14 additions & 42 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
createAuthenticatedClient
} from '@interledger/open-payments/dist/client'
import {
IncomingPayment,
isFinalizedGrant,
isPendingGrant,
OutgoingPayment,
Quote,
WalletAddress
} from '@interledger/open-payments/dist/types'
import * as ed from '@noble/ed25519'
Expand Down Expand Up @@ -74,21 +74,16 @@ interface VerifyInteractionHashParams {
authServer: string
}

interface CreateQuoteAndOutgoingPaymentGrantParams {
interface CreateOutgoingPaymentGrantParams {
clientNonce: string
walletAddress: WalletAddress
amount: WalletAmount
}

interface CreateQuoteParams {
walletAddress: WalletAddress
receiver: string
amount: string
}

interface CreateOutgoingPaymentParams {
walletAddress: WalletAddress
quoteId: string
incomingPaymentId: IncomingPayment['id']
amount: string
}

type TabUpdateCallback = Parameters<Tabs.onUpdatedEvent['addListener']>[0]
Expand Down Expand Up @@ -300,7 +295,7 @@ export class OpenPaymentsService {

await this.initClient(walletAddress.id)
const clientNonce = crypto.randomUUID()
const grant = await this.createQuoteAndOutgoingPaymentGrant({
const grant = await this.createOutgoingPaymentGrant({
clientNonce,
walletAddress,
amount: transformedAmount
Expand Down Expand Up @@ -356,24 +351,18 @@ export class OpenPaymentsService {
this.token = token
}

private async createQuoteAndOutgoingPaymentGrant({
private async createOutgoingPaymentGrant({
amount,
walletAddress,
clientNonce
}: CreateQuoteAndOutgoingPaymentGrantParams) {
}: CreateOutgoingPaymentGrantParams) {
const grant = await this.client!.grant.request(
{
url: walletAddress.authServer
},
{
access_token: {
access: [
// TODO: Can be removed when the Test Wallet will be upgraded
// to the latest Rafiki version (or at least alpha.10)
{
type: 'quote',
actions: ['create']
},
{
type: 'outgoing-payment',
actions: ['create'],
Expand Down Expand Up @@ -499,41 +488,24 @@ export class OpenPaymentsService {
})
}

async createQuote({
async createOutgoingPayment({
walletAddress,
receiver,
amount
}: CreateQuoteParams): Promise<Quote> {
return await this.client!.quote.create(
amount,
incomingPaymentId
}: CreateOutgoingPaymentParams): Promise<OutgoingPayment> {
return await this.client!.outgoingPayment.create(
{
accessToken: this.token.value,
url: walletAddress.resourceServer
},
{
method: 'ilp',
incomingPayment: incomingPaymentId,
walletAddress: walletAddress.id,
receiver,
debitAmount: {
value: amount,
assetCode: walletAddress.assetCode,
assetScale: walletAddress.assetScale
}
}
)
}

async createOutgoingPayment({
walletAddress,
quoteId
}: CreateOutgoingPaymentParams): Promise<OutgoingPayment> {
return await this.client!.outgoingPayment.create(
{
accessToken: this.token.value,
url: walletAddress.resourceServer
},
{
quoteId,
walletAddress: walletAddress.id,
},
metadata: {
source: 'Web Monetization'
}
Expand Down
42 changes: 6 additions & 36 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { OpenPaymentsService } from './openPayments'
import {
IncomingPayment,
OutgoingPayment,
Quote,
WalletAddress,
isPendingGrant
} from '@interledger/open-payments/dist/types'
Expand Down Expand Up @@ -121,27 +120,14 @@ export class PaymentSession {

await this.setIncomingPaymentUrl()

let quote: Quote | undefined
let outgoingPayment: OutgoingPayment | undefined

while (this.active) {
try {
// Quote can be removed once the Test Wallet upgrades to alpha-10.
// We will be able to create an outgoing payment with an incoming payment,
// making the quoting unnecessary through OP.
//
// Note: Under the hood, Rafiki is still quoting.
if (!quote) {
quote = await this.openPaymentsService.createQuote({
walletAddress: this.sender,
receiver: this.incomingPaymentUrl,
amount: this.amount
})
}

outgoingPayment = await this.openPaymentsService.createOutgoingPayment({
walletAddress: this.sender,
quoteId: quote.id
incomingPaymentId: this.incomingPaymentUrl,
amount: this.amount
})
} catch (e) {
if (e instanceof OpenPaymentsClientError) {
Expand All @@ -151,9 +137,8 @@ export class PaymentSession {
continue
}

// Is there a better way to handle this - expired incoming
// payment?
if (e.status === 400 && quote === undefined) {
// We need better error handling.
if (e.status === 400) {
await this.setIncomingPaymentUrl()
continue
}
Expand All @@ -166,7 +151,6 @@ export class PaymentSession {
if (outgoingPayment) {
const { receiveAmount, receiver: incomingPayment } = outgoingPayment

quote = undefined
outgoingPayment = undefined

sendMonetizationEvent({
Expand Down Expand Up @@ -252,29 +236,15 @@ export class PaymentSession {
async pay(amount: number) {
const incomingPayment = await this.createIncomingPayment()

let quote: Quote | undefined
let outgoingPayment: OutgoingPayment | undefined

try {
if (!quote) {
quote = await this.openPaymentsService.createQuote({
walletAddress: this.sender,
receiver: incomingPayment.id,
amount: (amount * 10 ** this.sender.assetScale).toFixed(0)
})
}

outgoingPayment = await this.openPaymentsService.createOutgoingPayment({
walletAddress: this.sender,
quoteId: quote.id
incomingPaymentId: incomingPayment.id,
amount: (amount * 10 ** this.sender.assetScale).toFixed(0)
})
} catch (e) {
/**
* Unhandled exceptions:
* - Expired incoming payment: if the incoming payment is expired when
* trying to create a quote, create a new incoming payment
*
*/
if (e instanceof OpenPaymentsClientError) {
// Status code 403 -> expired access token
if (e.status === 403) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface Storage {
walletAddress?: WalletAddress | undefined | null
/** Overall amount */
amount?: WalletAmount | undefined | null
/** Access token for quoting & outgoing payments */
/** Access token for outgoing payments */
token?: AccessToken | undefined | null
/** Grant details - continue access token & uri for canceling the grant */
grant?: GrantDetails | undefined | null
Expand Down

0 comments on commit ecc81a1

Please sign in to comment.