-
Notifications
You must be signed in to change notification settings - Fork 89
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
feat(open-payments): add create quote #888
Changes from 23 commits
cf98952
5b86967
592e91a
d1164bc
58dbed4
7e91f91
b848718
2245f53
8fd1df0
a2b572e
613d57d
4518098
43f94f6
f2f213a
675b8c3
22ae401
e4b0634
6e93aa0
6e54d15
72ebdbd
9dd6de0
67d0488
1b5c01a
56d5777
f3bf0ac
0f24dfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { Logger } from 'pino' | ||
import { ReadContext, CreateContext } from '../../app' | ||
import { IAppConfig } from '../../config/app' | ||
import { QuoteService } from './service' | ||
import { CreateQuoteOptions, QuoteService } from './service' | ||
import { isQuoteError, errorToCode, errorToMessage } from './errors' | ||
import { Quote } from './model' | ||
import { AmountJSON, parseAmount } from '../amount' | ||
|
@@ -43,25 +43,37 @@ async function getQuote( | |
ctx.body = body | ||
} | ||
|
||
export type CreateBody = { | ||
interface CreateBodyBase { | ||
receiver: string | ||
} | ||
|
||
interface CreateBodyWithSendAmount extends CreateBodyBase { | ||
sendAmount?: AmountJSON | ||
receiveAmount?: never | ||
} | ||
|
||
interface CreateBodyWithReceiveAmount extends CreateBodyBase { | ||
sendAmount?: never | ||
receiveAmount?: AmountJSON | ||
} | ||
|
||
export type CreateBody = CreateBodyWithSendAmount | CreateBodyWithReceiveAmount | ||
|
||
async function createQuote( | ||
deps: ServiceDependencies, | ||
ctx: CreateContext<CreateBody> | ||
): Promise<void> { | ||
const { body } = ctx.request | ||
const options: CreateQuoteOptions = { | ||
paymentPointerId: ctx.paymentPointer.id, | ||
receiver: body.receiver, | ||
clientId: ctx.grant?.clientId | ||
} | ||
if (body.sendAmount) options.sendAmount = parseAmount(body.sendAmount) | ||
if (body.receiveAmount) | ||
options.receiveAmount = parseAmount(body.receiveAmount) | ||
try { | ||
const quoteOrErr = await deps.quoteService.create({ | ||
paymentPointerId: ctx.paymentPointer.id, | ||
receiver: body.receiver, | ||
sendAmount: body.sendAmount && parseAmount(body.sendAmount), | ||
receiveAmount: body.receiveAmount && parseAmount(body.receiveAmount), | ||
clientId: ctx.grant?.clientId | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should be able to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the changes to the But I think we should keep the changes to |
||
const quoteOrErr = await deps.quoteService.create(options) | ||
|
||
if (isQuoteError(quoteOrErr)) { | ||
throw quoteOrErr | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,15 @@ export async function createOutgoingPayment( | |
'quoteId' | ||
> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you change
but then it looks like there's some calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 I think we should keep the type as it was before, since our changes are affecting only the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about refactoring function createOutgoingPayment(
deps: IocContract<AppServices>,
quoteOptions: CreateTestQuoteOptions,
outgoingPaymentOptions: Omit<CreateOutgoingPaymentOptions, 'quoteId'>
): Promise<OutgoingPayment> |
||
): Promise<OutgoingPayment> { | ||
const quote = await createQuote(deps, options) | ||
const quoteOptions: CreateTestQuoteOptions = { | ||
paymentPointerId: options.paymentPointerId, | ||
clientId: options.clientId, | ||
receiver: options.receiver, | ||
validDestination: options.validDestination | ||
} | ||
if (options.sendAmount) quoteOptions.sendAmount = options.sendAmount | ||
if (options.receiveAmount) quoteOptions.receiveAmount = options.receiveAmount | ||
const quote = await createQuote(deps, quoteOptions) | ||
const outgoingPaymentService = await deps.use('outgoingPaymentService') | ||
const receiverService = await deps.use('receiverService') | ||
if (options.validDestination === false) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because graphql doesn't do mutually exclusive types (:disappointed:), I think we need to move the old check from the quote service here
rafiki/packages/backend/src/open_payments/quote/service.ts
Lines 76 to 78 in 4db6f8e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I was looking at the
@oneOf
directive (RFC). Even though it's available in a couple of packages, is still a feature from the future and the behavior could change slightly. It would be nice if we can enforce the types at the GraphQL schema later on.While playing around with it, I got to this point:
I think it partially fulfills our needs for the
CreateQuoteInput
. 🤔