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(backend): allow admin api to query all receivers and move checks #3314

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

cozminu
Copy link
Contributor

@cozminu cozminu commented Feb 21, 2025

Changes proposed in this pull request

  • moved validation checks (completed, expired and methods availability) to createOutgoingPayment, handleSending, resolveReceiver via Resolver.isInvalidExpiredOrCompleted

Context

fixes #3253

Checklist

  • Related issues linked using fixes #number
  • Tests added/updated
  • Make sure that all checks pass
  • Bruno collection updated (if necessary)
  • Documentation issue created with user-docs label (if necessary)
  • OpenAPI specs updated (if necessary)

@cozminu cozminu linked an issue Feb 21, 2025 that may be closed by this pull request
3 tasks
@cozminu cozminu requested a review from mkurapov February 21, 2025 08:54
@github-actions github-actions bot added pkg: backend Changes in the backend package. type: source Changes business logic labels Feb 21, 2025
Copy link

netlify bot commented Feb 21, 2025

Deploy Preview for brilliant-pasca-3e80ec canceled.

Name Link
🔨 Latest commit 33e5f8d
🔍 Latest deploy log https://app.netlify.com/sites/brilliant-pasca-3e80ec/deploys/67c1b6c9e4e3db0008a6828c

@cozminu
Copy link
Contributor Author

cozminu commented Feb 21, 2025

tests tbd after review

@cozminu cozminu changed the title fix(backend): enable admin api to query all receiver and move checks fix(backend): enable admin api to query all receivers and move checks Feb 21, 2025
@cozminu cozminu changed the title fix(backend): enable admin api to query all receivers and move checks fix(backend): allow admin api to query all receivers and move checks Feb 21, 2025
@@ -120,4 +108,30 @@ export class Receiver {
requestCounter: Counter.from(0) as Counter
}
}

public static isInvalidExpiredOrCompleted(
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be a static method? Or can we just make it a regular method, and use this to refer to the receiver instead of passing it in? This allows us to not import the Receiver model across our different services and leverages the receiver instance we already have.

Then instead of handling undefined in this method we check where this is called like:

So this:

      if (!receiver || receiver.isInvalidExpiredOrCompleted()) {
        throw OutgoingPaymentError.InvalidQuote
      }

Instead of

      if (Receiver.isInvalidExpiredOrCompleted(receiver)) {
        throw OutgoingPaymentError.InvalidQuote
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not, I chose static for simplicity only. Changed to non-static method.

Comment on lines 130 to 133
if (!incomingPayment.methods.length) {
// throw new Error('Missing payment method(s) on incoming payment')
return true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably remove this payment method check. Its not going to do anything because the Receiver constructor will error if it's empty. It looks for ilp method then throws if it cant find it.

I am considering if it should be returned to the constructor but it seems kind of redundant in the first place. With or without a check for this, if method is empty, it will error as described here (missing ilp payment method). I guess the method.length check was just slightly more informative. I think just remove it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to constructor for now because it makes more sense since there's already a check there and ILP method is hard-coupled to the Receiver via ilpAddress property.
Checked if this will affect querying the receiver via Admin API and it doesn't.


public static isInvalidExpiredOrCompleted(
receiver: Receiver | undefined
): receiver is undefined {
Copy link
Contributor

@BlairCurrey BlairCurrey Feb 21, 2025

Choose a reason for hiding this comment

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

Can you speak more to this return type? I think maybe it should just be boolean instead? Perhaps it was working differently with the pattern of throwing errors but I don't think its really doing anything here. Nor could TS really know if its undefined or not.

This is what I see when using it (no effect on the receiver passed in).

const maybeReceiver = {} as Receiver | undefined

const r = Receiver.isInvalidExpiredOrCompleted(maybeReceiver)
r // type is boolean
maybeReceiver //type is still Receiver | undefined

Copy link
Contributor Author

@cozminu cozminu Feb 25, 2025

Choose a reason for hiding this comment

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

first time around I did it, function signature response was boolean

public static isActive(receiver: Receiver | undefined): boolean {

the issue was that I had to double check for type:
Screenshot 2025-02-25 at 10 38 03

but if we do the non-static method that you suggested, then we won't have this problem because we will check for type before using the method

Copy link
Contributor Author

@cozminu cozminu Feb 25, 2025

Choose a reason for hiding this comment

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

when the signature response is receiver is Receiver and you have an if with a return or throw just like above, then typescript will know that the receiver below the if will be just of type Receiver (instead of Receiver | undefined)

Copy link
Contributor Author

@cozminu cozminu Feb 25, 2025

Choose a reason for hiding this comment

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

// function will return boolean, but typescript knows what to do with it
function isActive(r: Receiver | undefined): receiver is Receiver

const maybeReceiver = {} as Receiver | undefined

const r = Receiver.isActive(maybeReceiver)
r // type is boolean
maybeReceiver //type is still Receiver | undefined

if (!r) {
  return;
}

maybeReceiver //type is Receiver only

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense, I see how its working now.

@@ -33,9 +33,6 @@ function getStreamCredentials(
deps: ServiceDependencies,
payment: IncomingPayment
): IlpStreamCredentials | undefined {
if (payment.isExpiredOrComplete()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IncomingPayment.isExpiredOrComplete() isn't used anywhere else in the code with this deletion - perhaps it can be cleaned up?

}
const incomingPayment = receiver.incomingPayment
if (incomingPayment.completed) {
// throw new Error('Cannot create receiver from completed incoming payment')
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: clean up comments

@github-actions github-actions bot added the type: tests Testing related label Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: backend Changes in the backend package. type: source Changes business logic type: tests Testing related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow querying completed and expired receivers (incoming payments)
3 participants