Skip to content

Commit

Permalink
fix: set the value from incoming payment in quote (#862)
Browse files Browse the repository at this point in the history
* fix: set the value from incoming payment in quote

* chore: update pnpm-locky

* fix: adjust the send amount only if it is off by a small margin
  • Loading branch information
dragosp1011 authored Oct 17, 2023
1 parent f249dfd commit f8b86b6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/wallet/backend/src/createContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ export const createContainer = (config: Env): Container<Bindings> => {
async () =>
new IncomingPaymentService({
accountService: await container.resolve('accountService'),
rafikiClient: await container.resolve('rafikiClient')
rafikiClient: await container.resolve('rafikiClient'),
logger: await container.resolve('logger')
})
)

Expand Down
11 changes: 11 additions & 0 deletions packages/wallet/backend/src/incomingPayment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Transaction } from '@/transaction/model'
import { extractUuidFromUrl, transformAmount } from '@/utils/helpers'
import { Asset } from '@/rafiki/backend/generated/graphql'
import { add } from 'date-fns'
import { Logger } from 'winston'

interface IIncomingPaymentService {
create: (
Expand All @@ -21,6 +22,7 @@ interface IIncomingPaymentService {
interface IncomingPaymentServiceDependencies {
accountService: AccountService
rafikiClient: RafikiClient
logger: Logger
}

interface CreateReceiverParams {
Expand Down Expand Up @@ -125,6 +127,15 @@ export class IncomingPaymentService implements IIncomingPaymentService {
}
}

public async getReceiver(receiver: string) {
try {
// @TODO: replace with get receiver from rafiki when implemented
return await this.getPaymentDetailsByUrl(receiver)
} catch (_e) {
this.deps.logger.info(`Could not find transaction for ${receiver}`)
}
}

public async createReceiver(params: CreateReceiverParams): Promise<string> {
const response = await this.deps.rafikiClient.createReceiver(params)
// id is the incoming payment url
Expand Down
14 changes: 4 additions & 10 deletions packages/wallet/backend/src/outgoingPayment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ export class OutgoingPaymentService implements IOutgoingPaymentService {
const quote = await this.deps.rafikiClient.getQuote(quoteId)
const paymentPointerId = quote.paymentPointerId

let description
try {
const incomingPayment =
await this.deps.incomingPaymentService.getPaymentDetailsByUrl(
quote.receiver
)
description = incomingPayment.description
} catch (_e) {
// @todo: find another way to get payment description
}
const incomingPayment = await this.deps.incomingPaymentService.getReceiver(
quote.receiver
)
const description = incomingPayment?.description

await this.deps.rafikiClient.createOutgoingPayment({
paymentPointerId,
Expand Down
26 changes: 24 additions & 2 deletions packages/wallet/backend/src/quote/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { IncomingPaymentService } from '@/incomingPayment/service'
import { PaymentPointer } from '@/paymentPointer/model'
import { Asset, Quote } from '@/rafiki/backend/generated/graphql'
import { RafikiClient } from '@/rafiki/rafiki-client'
import { incomingPaymentRegexp, urlToPaymentPointer } from '@/utils/helpers'
import {
incomingPaymentRegexp,
transformBalance,
urlToPaymentPointer
} from '@/utils/helpers'
import {
createPaymentPointerIfFalsy,
PaymentPointerService
Expand Down Expand Up @@ -109,11 +113,29 @@ export class QuoteService implements IQuoteService {
params.isReceive &&
destinationPaymentPointer.assetCode !== asset.code
) {
const convertedValue = await this.convert({
let convertedValue = await this.convert({
from: assetCode,
to: destinationPaymentPointer.assetCode,
amount: value
})
if (isIncomingPayment) {
const payment = await this.deps.incomingPaymentService.getReceiver(
params.receiver
)

const amount = payment?.value
? transformBalance(
payment?.value,
destinationPaymentPointer.assetScale
)
: undefined

// adjust the amount in case that after converting it to the receiver currency it is off by a small margin
if (amount && 1 - Number(amount) / Number(convertedValue) < 0.01) {
convertedValue = amount
}
}

value = convertedValue

//* This next check is for first-party transfers. Future Third party transfers will need to go through another flow.
Expand Down

0 comments on commit f8b86b6

Please sign in to comment.