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

services/horizon: Add an endpoint that allows querying for which liquidity pools an account is participating in #4043

Merged
merged 20 commits into from
Nov 4, 2021

Conversation

erika-sdf
Copy link
Contributor

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot, or all or doc if the changes are broad or impact many
    packages.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've updated the relevant CHANGELOG (here for Horizon) if
    needed with deprecations, added features, breaking changes, and DB schema changes.
  • I've decided if this PR requires a new major/minor version according to
    semver, or if it's mainly a patch change. The PR is targeted at the next
    release branch if it's not a patch change.

What

GET /liquidity_pools?account=<id>returns a list of pools that have as a participant

Why

Getting this information in the current api requires querying an account for its balances to obtain pool ids, then querying each liquidity pool by id. This simplifies that look up into a single call.

Known limitations

This change does not allow requests that specify both account if and existing reserves parameter in the same request.

@Shaptic Shaptic requested a review from a team November 4, 2021 00:36
Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

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

obviously can't ✔️ this but some minor notes

services/horizon/internal/db2/history/liquidity_pools.go Outdated Show resolved Hide resolved

// MakeTestPool is a helper to make liquidity pools for testing purposes. It's
// public because it's used in other test suites.
func MakeTestPool(A xdr.Asset, a uint64, B xdr.Asset, b uint64) LiquidityPool {
Copy link
Contributor

Choose a reason for hiding this comment

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

note for reviewers: these are intentionally public as they're used both in these tests and in the actions' tests.

services/horizon/internal/db2/history/liquidity_pools.go Outdated Show resolved Hide resolved
@ire-and-curses ire-and-curses requested a review from a team November 4, 2021 03:03
@erika-sdf erika-sdf changed the title services/horizon: Add endpoint allows querying liquidity pools that an account is participating in services/horizon: Add an endpoint that allows querying liquidity pools that an account is participating in Nov 4, 2021
@erika-sdf erika-sdf changed the title services/horizon: Add an endpoint that allows querying liquidity pools that an account is participating in services/horizon: Add an endpoint that allows querying for which liquidity pools an account is participating in Nov 4, 2021
handler := GetLiquidityPoolsHandler{}
response, err := handler.GetResourcePage(httptest.NewRecorder(), request)
assert.NoError(t, err)
assert.Len(t, response, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth checking the actual response if the LP is the one we expect.

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 has been added.

services/horizon/internal/actions/liquidity_pool.go Outdated Show resolved Hide resolved
},
LastModifiedLedger: 123,
}
lp := MakeTestPool(usdAsset, 450, xlmAsset, 450)
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't see a test checking Account filter. Maybe worth adding one?

Copy link
Contributor

Choose a reason for hiding this comment

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

I was curious about this (did most of the test work): the db code path is already checked by using the actions code path. It seems like the tests are very similar to the action ones. Is there a benefit to having them in both places?

Copy link
Contributor

Choose a reason for hiding this comment

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

The main idea we had when doing a refactoring was that actions package will not use DB directly but mocks. And because we have integration tests framework, the entire stack should be tested there. Unfortunately previous action tests did not have this rule so quite a lot new tests access DB too.

I think it's fine if we test it in one place. What we could also do instead here is add a very simple (a few lines of code) test sending a new query and ensuring the result is correct in TestLiquidityPoolHappyPath test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A test has been added here to test querying by account id. Thanks for @Shaptic for helping debug.

}

if len(query.Account) > 0 {
return q.findLiquidityPoolsByAccountId(ctx, query.Account)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there's a need to extract this to a separate method. First, it's easy to miss stuff, for example currently the code does not apply cursor so pagination probably doesn't work. Second, it actually duplicates code: deleted where clause is added in two methods.

I think we can rewrite selectLiquidityPools to have named table lp. Then in a single if statement we can set the query we want to work on and add filters.

Copy link
Contributor Author

@erika-sdf erika-sdf Nov 4, 2021

Choose a reason for hiding this comment

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

I have combined this into a single method, and set the filters in an if/else branch

The order is expected, actual so the error messages make more
sense if we swap the ID order.
Copy link
Contributor

@bartekn bartekn left a comment

Choose a reason for hiding this comment

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

Great!

@erika-sdf erika-sdf merged commit fa20a49 into stellar:master Nov 4, 2021
@bartekn
Copy link
Contributor

bartekn commented Nov 4, 2021

@erika-sdf please remember to squash commits before merging (this can be done via GH GUI). It's easier to explore diffs when PRs are squashed. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Query all liquidity pools an account is participating in.
4 participants