Skip to content

Commit

Permalink
chore: upgrade rafiki to alpha 6 (#1176)
Browse files Browse the repository at this point in the history
* chore: upgrade rafiki to alpha 6

* chore: upgrade open-payments

* feat: get receiver from rafiki instead of local transaction

* fix: remove optional check from receivedAmount

* test: add mock method for get receiver

* chore: set wait seconds env var
  • Loading branch information
dragosp1011 authored Mar 5, 2024
1 parent d29af27 commit 75d5663
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 149 deletions.
12 changes: 6 additions & 6 deletions docker/dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,30 @@ services:
# Rafiki
rafiki-auth:
container_name: rafiki-auth
image: ghcr.io/interledger/rafiki-auth:v1.0.0-alpha.5
image: ghcr.io/interledger/rafiki-auth:v1.0.0-alpha.6
restart: always
networks:
- testnet
ports:
- '3006:3006'
- '3008:3008'
environment:
PORT: 3006
AUTH_PORT: 3006
INTROSPECTION_PORT: 3007
ADMIN_PORT: 3008
NODE_ENV: development
AUTH_SERVER_DOMAIN: http://localhost:3006
AUTH_DATABASE_URL: postgresql://rafiki_auth:rafiki_auth@postgres/rafiki_auth
IDENTITY_SERVER_DOMAIN: http://localhost:4003/grant-interactions
IDENTITY_SERVER_SECRET: ${AUTH_IDENTITY_SERVER_SECRET}
WAIT_SECONDS: 1
depends_on:
- postgres
<<: *logging

rafiki-backend:
container_name: rafiki-backend
image: ghcr.io/interledger/rafiki-backend:v1.0.0-alpha.5
image: ghcr.io/interledger/rafiki-backend:v1.0.0-alpha.6
restart: always
privileged: true
volumes:
Expand Down Expand Up @@ -140,7 +141,6 @@ services:
ILP_ADDRESS: test.net
STREAM_SECRET: BjPXtnd00G2mRQwP/8ZpwyZASOch5sUXT5o0iR5b5wU=
ADMIN_KEY: admin
PUBLIC_HOST: http://rafiki-backend
OPEN_PAYMENTS_URL: http://rafiki-backend
REDIS_URL: redis://redis:6379/0
WALLET_ADDRESS_URL: https://rafiki-backend/.well-known/pay
Expand All @@ -159,7 +159,7 @@ services:

rafiki-frontend:
container_name: rafiki-frontend
image: ghcr.io/interledger/rafiki-frontend:v1.0.0-alpha.5
image: ghcr.io/interledger/rafiki-frontend:v1.0.0-alpha.6
depends_on:
- rafiki-backend
restart: always
Expand All @@ -175,7 +175,7 @@ services:
<<: *logging

tigerbeetle:
image: ghcr.io/tigerbeetle/tigerbeetle:0.13.57
image: ghcr.io/tigerbeetle/tigerbeetle:0.14.176
privileged: true
volumes:
- tigerbeetle-data:/var/lib/tigerbeetle
Expand Down
2 changes: 1 addition & 1 deletion docker/prod/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ RAFIKI_AUTH_DATABASE_URL=
RAFIKI_AUTH_IDENTITY_SERVER_DOMAIN=
RAFIKI_AUTH_IDENTITY_SERVER_SECRET=
RAFIKI_AUTH_SERVER_DOMAIN=
RAFIKI_AUTH_WAIT_SECONDS=

# RAFIKI BACKEND
RAFIKI_BACKEND_LOG_LEVEL=
Expand All @@ -71,7 +72,6 @@ RAFIKI_BACKEND_AUTH_SERVER_INTROSPECTION_URL=
RAFIKI_BACKEND_ILP_ADDRESS=
RAFIKI_BACKEND_STREAM_SECRET=
RAFIKI_BACKEND_ADMIN_KEY=
RAFIKI_BACKEND_PUBLIC_HOST=
RAFIKI_BACKEND_OPEN_PAYMENTS_URL=
RAFIKI_BACKEND_REDIS_URL=
RAFIKI_BACKEND_WALLET_ADDRESS_URL=
Expand Down
12 changes: 6 additions & 6 deletions docker/prod/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,18 @@ services:
<<: *logging

rafiki-auth:
image: ghcr.io/interledger/rafiki-auth:v1.0.0-alpha.5
image: ghcr.io/interledger/rafiki-auth:v1.0.0-alpha.6
container_name: rafiki-auth
environment:
NODE_ENV: ${NODE_ENV}
PORT: ${RAFIKI_AUTH_PORT}
AUTH_PORT: ${RAFIKI_AUTH_PORT}
ADMIN_PORT: ${RAFIKI_AUTH_ADMIN_PORT}
INTROSPECTION_PORT: ${RAFIKI_AUTH_INTROSPECTION_PORT}
AUTH_DATABASE_URL: ${RAFIKI_AUTH_DATABASE_URL}
IDENTITY_SERVER_SECRET: ${RAFIKI_AUTH_IDENTITY_SERVER_SECRET}
IDENTITY_SERVER_DOMAIN: ${RAFIKI_AUTH_IDENTITY_SERVER_DOMAIN}
AUTH_SERVER_DOMAIN: ${RAFIKI_AUTH_SERVER_DOMAIN}
WAIT_SECONDS: ${RAFIKI_AUTH_WAIT_SECONDS}
TRUST_PROXY: true
depends_on:
- postgres
Expand All @@ -142,7 +143,7 @@ services:
<<: *logging

rafiki-backend:
image: ghcr.io/interledger/rafiki-backend:v1.0.0-alpha.5
image: ghcr.io/interledger/rafiki-backend:v1.0.0-alpha.6
container_name: rafiki-backend
depends_on:
- postgres
Expand All @@ -163,7 +164,6 @@ services:
ILP_ADDRESS: ${RAFIKI_BACKEND_ILP_ADDRESS}
STREAM_SECRET: ${RAFIKI_BACKEND_STREAM_SECRET}
ADMIN_KEY: ${RAFIKI_BACKEND_ADMIN_KEY}
PUBLIC_HOST: ${RAFIKI_BACKEND_PUBLIC_HOST}
OPEN_PAYMENTS_URL: ${RAFIKI_BACKEND_OPEN_PAYMENTS_URL}
REDIS_URL: ${RAFIKI_BACKEND_REDIS_URL}
WALLET_ADDRESS_URL: ${RAFIKI_BACKEND_WALLET_ADDRESS_URL}
Expand All @@ -190,7 +190,7 @@ services:
<<: *logging

rafiki-frontend:
image: ghcr.io/interledger/rafiki-frontend:v1.0.0-alpha.5
image: ghcr.io/interledger/rafiki-frontend:v1.0.0-alpha.6
container_name: rafiki-frontend
depends_on:
- rafiki-backend
Expand All @@ -207,7 +207,7 @@ services:
<<: *logging

tigerbeetle:
image: ghcr.io/tigerbeetle/tigerbeetle:0.13.57
image: ghcr.io/tigerbeetle/tigerbeetle:0.14.176
privileged: true
volumes:
- tigerbeetle-data:/var/lib/tigerbeetle
Expand Down
2 changes: 1 addition & 1 deletion packages/boutique/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@google-cloud/logging-winston": "^6.0.0",
"@interledger/open-payments": "^6.1.1",
"@interledger/open-payments": "^6.6.0",
"awilix": "^10.0.1",
"axios": "^1.6.7",
"cors": "^2.8.5",
Expand Down
15 changes: 14 additions & 1 deletion packages/boutique/backend/src/open-payments/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
WalletAddress,
PendingGrant,
Quote,
isPendingGrant
isPendingGrant,
GrantContinuation
} from '@interledger/open-payments'
import { randomUUID } from 'crypto'
import { Logger } from 'winston'
Expand Down Expand Up @@ -309,6 +310,12 @@ export class OpenPayments implements IOpenPayments {
throw new InternalServerError()
})

if (!this.isGrant(continuation)) {
this.logger.error('Expected grant response.')
this.logger.debug(continuation)
throw new InternalServerError()
}

return {
accessToken: continuation.access_token.value,
manageUrl: continuation.access_token.manage.replace(
Expand All @@ -318,6 +325,12 @@ export class OpenPayments implements IOpenPayments {
}
}

private isGrant(
continuation: GrantContinuation | Grant
): continuation is Grant {
return (continuation as Grant).access_token !== undefined
}

public async instantBuy(
params: InstantBuyParams
): Promise<TokenInfo & { walletAddressurl: string }> {
Expand Down
56 changes: 14 additions & 42 deletions packages/wallet/backend/src/incomingPayment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { NotFound } from '@/errors'
import { PaymentDetails } from '@/incomingPayment/controller'
import { WalletAddress } from '@/walletAddress/model'
import { RafikiClient } from '@/rafiki/rafiki-client'
import { Transaction } from '@/transaction/model'
import { extractUuidFromUrl, transformAmount } from '@/utils/helpers'
import { transformAmount } from '@/utils/helpers'
import { Amount, Asset } from '@/rafiki/backend/generated/graphql'
import { add, Duration } from 'date-fns'
import { Logger } from 'winston'
import axios from 'axios'
import { Env } from '@/config/env'

Expand Down Expand Up @@ -50,7 +48,6 @@ export class IncomingPaymentService implements IIncomingPaymentService {
constructor(
private accountService: AccountService,
private rafikiClient: RafikiClient,
private logger: Logger,
private env: Env
) {}

Expand Down Expand Up @@ -96,49 +93,24 @@ export class IncomingPaymentService implements IIncomingPaymentService {
return response.id
}

// Instead of querying our Transaction model, should we fetch this information from Rafiki?
// Reasoning:
// - An incoming payment can be fulfilled by multiple outgoing payments;
// - If we have an incoming payment that awaits $10 and we send $5 initially,
// inserting the IP URL in the receiver field again, currently shows that the incoming amount
// is still $10.
// - By fetching the IP details from Rafiki, we can calculate how much more is needed
// to fulfill this specific IP (after making an initial outgoing payment of $5).
async getPaymentDetailsByUrl(url: string): Promise<PaymentDetails> {
const id = extractUuidFromUrl(url)

const transaction = await Transaction.query()
.where('paymentId', id)
.where('status', 'PENDING')
.first()
.withGraphFetched({ walletAddress: { account: true } })

if (!transaction) {
throw new NotFound(
'The provided incoming payment URL could not be found.'
)
const receiver = await this.rafikiClient.getReceiverById(url)
const asset = {
scale:
receiver.incomingAmount?.assetScale ??
receiver.receivedAmount.assetScale,
code:
receiver.incomingAmount?.assetCode ?? receiver.receivedAmount.assetCode
}

const asset = await this.rafikiClient.getAssetById(
transaction.walletAddress?.account.assetId
)
if (!asset) {
throw new NotFound()
}
const value = receiver.incomingAmount?.value
? receiver.incomingAmount.value - receiver.receivedAmount.value
: 0n

return {
description: transaction.description,
value: parseFloat(transformAmount(transaction.value ?? 0n, asset.scale)),
assetCode: transaction.assetCode
}
}

public async getReceiver(receiver: string) {
try {
// @TODO: replace with get receiver from rafiki when implemented
return await this.getPaymentDetailsByUrl(receiver)
} catch (_e) {
this.logger.info(`Could not find transaction for ${receiver}`)
description: receiver.metadata.description,
value: parseFloat(transformAmount(value, asset.scale)),
assetCode: asset.code
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/wallet/backend/src/outgoingPayment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ export class OutgoingPaymentService implements IOutgoingPaymentService {
const quote = await this.rafikiClient.getQuote(quoteId)
const walletAddressId = quote.walletAddressId

const incomingPayment = await this.incomingPaymentService.getReceiver(
quote.receiver
)
const incomingPayment =
await this.incomingPaymentService.getPaymentDetailsByUrl(quote.receiver)
const description = incomingPayment?.description

await this.rafikiClient.createOutgoingPayment({
Expand Down
7 changes: 4 additions & 3 deletions packages/wallet/backend/src/quote/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ export class QuoteService implements IQuoteService {
amount: value
})
if (isIncomingPayment) {
const payment = await this.incomingPaymentService.getReceiver(
params.receiver
)
const payment =
await this.incomingPaymentService.getPaymentDetailsByUrl(
params.receiver
)

const amount = payment?.value
? transformBalance(payment?.value, assetDetails.assetScale)
Expand Down
8 changes: 8 additions & 0 deletions packages/wallet/backend/src/rafiki/auth/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export type QueryGrantsArgs = {
filter?: InputMaybe<GrantFilter>;
first?: InputMaybe<Scalars['Int']['input']>;
last?: InputMaybe<Scalars['Int']['input']>;
sortOrder?: InputMaybe<SortOrder>;
};

export type RevokeGrantInput = {
Expand All @@ -188,6 +189,13 @@ export type RevokeGrantMutationResponse = MutationResponse & {
success: Scalars['Boolean']['output'];
};

export enum SortOrder {
/** Choose ascending order for results. */
Asc = 'ASC',
/** Choose descending order for results. */
Desc = 'DESC'
}

export type GetGrantsQueryVariables = Exact<{
after?: InputMaybe<Scalars['String']['input']>;
before?: InputMaybe<Scalars['String']['input']>;
Expand Down
Loading

0 comments on commit 75d5663

Please sign in to comment.