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

fix: set the value from incoming payment in quote #862

Merged
merged 4 commits into from
Oct 17, 2023
Merged
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this approach. The users should be able send their desired amounts. If the incoming payment has a receive amount of 100EUR, we should not enforce to send 100EUR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I agree, the request might be 100EUR but the user ca decide to pay only 50EUR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue is that converting the value is not exact, so 20.15 euro will result in 100.02 roni (instead of expected 100)
I added a check that if the amount is off by less then 1% to set it to the amount in incoming payment
open to other suggestions if this does not make sense

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