-
Notifications
You must be signed in to change notification settings - Fork 499
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8f9db75
Add (failing) test to find pools by participating account
Shaptic 17e371b
Refactor the other tests to use the new helper
Shaptic aba58ee
Rename test
Shaptic 367d0fc
Refactor test cases together
Shaptic 759345e
Query liquidity pools by account id
erika-sdf 10563f5
Merge unit tests
erika-sdf 70cd4d5
Merge branch 'liquidity' of github.com:erika-sdf/go into liquidity
Shaptic f586953
Fix join sql syntax
Shaptic bca81f5
Drop redundant test
Shaptic 48c3fa6
Minor refactors to test cases
Shaptic b17eaa9
Clean up print statements
erika-sdf 794b2d0
Update parameter name: accountID -> account.
erika-sdf 51c0681
Update services/horizon/internal/actions/liquidity_pool.go
erika-sdf 5a3c370
Add new liqudity pool test to db2/history
erika-sdf f53e343
Merge branch 'liquidity' of https://github.com/erika-sdf/go into liqu…
erika-sdf e3124bd
Merge branch 'liquidity' of github.com:erika-sdf/go into liquidity
Shaptic aa9008e
Merge branch 'stellar:master' into liquidity
erika-sdf 3c2d2c2
Fix trustline creation helper to always have unique ledgerkey
Shaptic 767f312
Merge branch 'liquidity' of github.com:erika-sdf/go into liquidity
Shaptic 5d3ebf8
Update test to upsert correctly now, and change assertion order
Shaptic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,11 @@ import ( | |
"context" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
"github.com/guregu/null" | ||
"github.com/stellar/go/services/horizon/internal/db2" | ||
"github.com/stellar/go/support/errors" | ||
"github.com/stellar/go/xdr" | ||
|
@@ -15,6 +18,7 @@ import ( | |
type LiquidityPoolsQuery struct { | ||
PageQuery db2.PageQuery | ||
Assets []xdr.Asset | ||
Account string | ||
} | ||
|
||
// LiquidityPool is a row of data from the `liquidity_pools`. | ||
|
@@ -150,22 +154,29 @@ func (q *Q) FindLiquidityPoolByID(ctx context.Context, liquidityPoolID string) ( | |
return lp, err | ||
} | ||
|
||
// GetLiquidityPools finds all liquidity pools where accountID is one of the claimants | ||
// GetLiquidityPools finds all liquidity pools where accountID owns assets | ||
func (q *Q) GetLiquidityPools(ctx context.Context, query LiquidityPoolsQuery) ([]LiquidityPool, error) { | ||
if len(query.Account) > 0 && len(query.Assets) > 0 { | ||
return nil, fmt.Errorf("this endpoint does not support filtering by both accountID and reserve assets.") | ||
} | ||
|
||
sql, err := query.PageQuery.ApplyRawTo(selectLiquidityPools, "lp.id") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not apply query to page") | ||
} | ||
sql = sql.Where("deleted = ?", false) | ||
|
||
for _, asset := range query.Assets { | ||
assetB64, err := xdr.MarshalBase64(asset) | ||
if err != nil { | ||
return nil, err | ||
if len(query.Account) > 0 { | ||
sql = sql.LeftJoin("trust_lines ON id = liquidity_pool_id").Where("trust_lines.account_id = ?", query.Account) | ||
} else if len(query.Assets) > 0 { | ||
for _, asset := range query.Assets { | ||
assetB64, err := xdr.MarshalBase64(asset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sql = sql. | ||
Where(`lp.asset_reserves @> '[{"asset": "` + assetB64 + `"}]'`) | ||
} | ||
sql = sql. | ||
Where(`lp.asset_reserves @> '[{"asset": "` + assetB64 + `"}]'`) | ||
} | ||
sql = sql.Where("lp.deleted = ?", false) | ||
|
||
var results []LiquidityPool | ||
if err := q.Select(ctx, &results, sql); err != nil { | ||
|
@@ -219,3 +230,68 @@ var liquidityPoolsSelectStatement = "lp.id, " + | |
"lp.last_modified_ledger" | ||
|
||
var selectLiquidityPools = sq.Select(liquidityPoolsSelectStatement).From("liquidity_pools lp") | ||
|
||
// 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 { | ||
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. note for reviewers: these are intentionally public as they're used both in these tests and in the actions' tests. |
||
if !A.LessThan(B) { | ||
B, A = A, B | ||
b, a = a, b | ||
} | ||
|
||
poolId, _ := xdr.NewPoolId(A, B, xdr.LiquidityPoolFeeV18) | ||
hexPoolId, _ := xdr.MarshalHex(poolId) | ||
return LiquidityPool{ | ||
PoolID: hexPoolId, | ||
Type: xdr.LiquidityPoolTypeLiquidityPoolConstantProduct, | ||
Fee: xdr.LiquidityPoolFeeV18, | ||
TrustlineCount: 12345, | ||
ShareCount: 67890, | ||
AssetReserves: []LiquidityPoolAssetReserve{ | ||
{Asset: A, Reserve: a}, | ||
{Asset: B, Reserve: b}, | ||
}, | ||
LastModifiedLedger: 123, | ||
} | ||
} | ||
|
||
func MakeTestTrustline(account string, asset xdr.Asset, poolId string) TrustLine { | ||
trustline := TrustLine{ | ||
AccountID: account, | ||
Balance: 1000, | ||
AssetCode: "", | ||
AssetIssuer: "", | ||
LedgerKey: account + asset.StringCanonical() + poolId, // irrelevant, just needs to be unique | ||
LiquidityPoolID: poolId, | ||
Flags: 0, | ||
LastModifiedLedger: 1234, | ||
Sponsor: null.String{}, | ||
} | ||
|
||
if poolId == "" { | ||
trustline.AssetType = asset.Type | ||
switch asset.Type { | ||
case xdr.AssetTypeAssetTypeNative: | ||
trustline.AssetCode = "native" | ||
|
||
case xdr.AssetTypeAssetTypeCreditAlphanum4: | ||
fallthrough | ||
case xdr.AssetTypeAssetTypeCreditAlphanum12: | ||
trustline.AssetCode = strings.TrimRight(asset.GetCode(), "\x00") // no nulls in db string | ||
trustline.AssetIssuer = asset.GetIssuer() | ||
trustline.BuyingLiabilities = 1 | ||
trustline.SellingLiabilities = 1 | ||
|
||
default: | ||
panic("invalid asset type") | ||
} | ||
|
||
trustline.Limit = trustline.Balance * 10 | ||
trustline.BuyingLiabilities = 1 | ||
trustline.SellingLiabilities = 2 | ||
} else { | ||
trustline.AssetType = xdr.AssetTypeAssetTypePoolShare | ||
} | ||
|
||
return trustline | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Worth checking the actual response if the LP is the one we expect.
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.
This check has been added.