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

refactor(backend): move fee calculation to backend #1755

Merged
merged 47 commits into from
Sep 14, 2023

Conversation

BlairCurrey
Copy link
Contributor

@BlairCurrey BlairCurrey commented Aug 21, 2023

Changes proposed in this pull request

  • updates to latest open-payments package and openapi schema
  • renames sendAmount to debitAmount across backend
  • moves fee calculation to backend on quote create
  • fixes fee calculation for cross-currency (different send/receive assets)

Context

Fixes: #1639 and Fixes: #1640

There are lots of file changes, but it's mostly renaming. More interesting code changes are the packages/backend/src/open_payments/quote/service.ts ones.

Checklist

  • Related issues linked using fixes #number
  • Tests added/updated
  • Documentation added
  • Make sure that all checks pass
  • Postman collection updated

@netlify
Copy link

netlify bot commented Aug 21, 2023

Deploy Preview for brilliant-pasca-3e80ec canceled.

Name Link
🔨 Latest commit b9b22cc
🔍 Latest deploy log https://app.netlify.com/sites/brilliant-pasca-3e80ec/deploys/6503340c7245570008c3588f

@github-actions github-actions bot added type: tests Testing related pkg: backend Changes in the backend package. pkg: frontend Changes in the frontend package. type: source Changes business logic pkg: mock-ase pkg: documentation Changes in the documentation package. type: documentation (archived) Improvements or additions to documentation labels Aug 21, 2023
@BlairCurrey BlairCurrey changed the title Bc/1639/add fees to quote refactor(backend): move fee calculation to backend Sep 4, 2023
Comment on lines 2 to 10
// renames sendAmount keys (if any) to debitAmount in limits jsonb column
return knex('accesses')
.update({
limits: knex.raw(
"limits - 'sendAmount' || jsonb_build_object('debitAmount', limits->'sendAmount')"
)
})
.whereRaw("limits \\? 'sendAmount'")
}
Copy link
Contributor Author

@BlairCurrey BlairCurrey Sep 8, 2023

Choose a reason for hiding this comment

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

This is hard to read but I could not find a simpler way. Addresses #1755 (comment)

To test I initialized rafiki with this extra test migration (before 20230907201006_rename-limits-jsonb-keys.js) which seeds some dummy data: https://gist.github.com/BlairCurrey/094b5df41f36f94befacbf727c3535ef

Then I verified the accesses records matched expectations after rebuilding rafiki from migrations.

Copy link
Member

@sabineschaller sabineschaller left a comment

Choose a reason for hiding this comment

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

I have to run but here is a first part of a review.

Copy link
Member

Choose a reason for hiding this comment

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

Is that to be backwards compatible? Do we think that is necessary?

Copy link
Contributor Author

@BlairCurrey BlairCurrey Sep 11, 2023

Choose a reason for hiding this comment

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

Yes, to ensure our renaming efforts dont cause any errors on pre-existing data. There is some discussion in the main body of the PR. My initial reasoning is here: #1755 (comment)

I think it probably is necessary because any existing records will have the old name and the application code has been changed to expect the new one. So it might break something on testnet if we dont do this.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. I wasn't aware of the discussion and didn't think about testnet. My bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries... I fee like higher level discussion on PR's on gh is kind of a mess.

Copy link
Member

Choose a reason for hiding this comment

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

Same question as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this one I guess we could rename it in the model with a get and set but I think this is simpler.

Comment on lines +321 to +322
debitAmountValue < quote.debitAmount.value ||
debitAmountValue <= BigInt(0)
Copy link
Member

Choose a reason for hiding this comment

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

How would that happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check already existed and I considered if it was still necessary since we aren't getting the value from the ASE but I wasn't sure, so I left it. I looked at it again though and think it can be removed.

debitAmountValue < quote.debitAmount.value

  • would be true if fees are negative (aside from fees, debitAmountValue === quote.debitAmount.value). not possible since we're checking that on fee create

debitAmountValue <= BigInt(0)

  • true if quote.debitAmount.value + fees is negative. and if fees cant be negative the the quote.debitAmount.value would have to be, and it cant because we're checking that before returning the ilp quote from startQuote

Copy link
Member

Choose a reason for hiding this comment

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

I didn't realize it was just a refactor. I also looked more closely now and I think you are right. This can probably be removed since it checks the quote after it is returned from the ASE in the current flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could remove this one OK but I tried the same for fixed send and an existing test failed (which was explicitly looking for this case). Perhaps the inputs shouldn't cause these errors but I think it's probably safer to leave them just in case.

Comment on lines +321 to +322
debitAmountValue < quote.debitAmount.value ||
debitAmountValue <= BigInt(0)
Copy link
Member

Choose a reason for hiding this comment

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

I didn't realize it was just a refactor. I also looked more closely now and I think you are right. This can probably be removed since it checks the quote after it is returned from the ASE in the current flow.

@@ -160,7 +160,7 @@ components:
- createdAt
- updatedAt
- expiresAt
- sendAmount
- debitAmount
Copy link
Member

Choose a reason for hiding this comment

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

It seems like we have some validation error here. Check in "files changed" if you can't see it here.

Copy link
Contributor Author

@BlairCurrey BlairCurrey Sep 13, 2023

Choose a reason for hiding this comment

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

@sabineschaller Hmm, I see it. More details from the CI job show the warning:

OpenAPI "servers" must be present and non-empty array

https://github.com/interledger/rafiki/actions/runs/6174411421/job/16758949762?pr=1755#step:6:12

Looks unrelated to this change and we've been getting it for a while:

Seems like it needs the servers attribute defined like we do in some of our other specs. Not sure what would be appropriate. What about something like this (similar to exchange rate yaml)?

servers:
  - url: 'https://account-servicing-entity.com/webhooks'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found the webhook url in some config files and concluded that url is right. I added it which seems to have fixed the warning.

@@ -88,8 +88,6 @@ export const Config = {
incomingPaymentWorkers: envInt('INCOMING_PAYMENT_WORKERS', 1),
incomingPaymentWorkerIdle: envInt('INCOMING_PAYMENT_WORKER_IDLE', 200), // milliseconds

quoteUrl: envString('QUOTE_URL', 'http://127.0.0.1:4001/quote'),
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove that from the integration docs, please?

Copy link
Contributor Author

@BlairCurrey BlairCurrey Sep 13, 2023

Choose a reason for hiding this comment

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

Looks like there were a few places to remove that. I also updated the explanation for setting fees in the integration doc e6f6f34, de3a256

sabineschaller
sabineschaller previously approved these changes Sep 14, 2023
Copy link
Member

@sabineschaller sabineschaller left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@mkurapov mkurapov left a comment

Choose a reason for hiding this comment

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

I would also recommend running pnpm generate:types in the token-introspection package to update the types properly.

Also, I'm not sure if we discussed, but for the documentation API schema, we should be seeing sendAmount be changed to debitAmount as well, correct?

@@ -19,7 +19,7 @@ export type Quote = {
paymentType: PaymentType
Copy link
Contributor

@mkurapov mkurapov Sep 14, 2023

Choose a reason for hiding this comment

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

We could remove this file altogether now, right?
will be removed in Nathan's PR

Accept: string
'Content-Type': string
'Rafiki-Signature'?: string
function getFees(fee: Fee | undefined, principal: bigint): bigint {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this method can live in feeService? just a suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered that as well. I think that's probably a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the method to the model since we already had the fee model here. It felt wrong to pass the fee into the fee service like feeService.calculate(fee, principal) or to re-lookup the fee. Later if we just have the feeId or assetId we can do feeService.calculate(id, principal) which gets the fee then calls the calculate method.

mkurapov
mkurapov previously approved these changes Sep 14, 2023
Copy link
Contributor

@mkurapov mkurapov left a comment

Choose a reason for hiding this comment

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

🚀

Co-authored-by: Max Kurapov <max@interledger.org>
@BlairCurrey BlairCurrey merged commit c29c1a7 into main Sep 14, 2023
@BlairCurrey BlairCurrey deleted the bc/1639/add-fees-to-quote branch September 14, 2023 17:44
@sabineschaller sabineschaller added the breaking Issue/PR that introduces breaking changes label Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Issue/PR that introduces breaking changes pkg: auth Changes in the GNAP auth package. pkg: backend Changes in the backend package. pkg: documentation Changes in the documentation package. pkg: frontend Changes in the frontend package. pkg: mock-ase type: documentation (archived) Improvements or additions to documentation type: source Changes business logic type: tests Testing related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add fees to outgoing payment Calculate and add fees to quote
4 participants