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

clients/horizonclient: fix multi-parameter url for claimable balance query #4248

Merged
merged 16 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
e681b94
bugfix: 4247 - claimable balance - check if one of the parameters is ID
iateadonut Feb 26, 2022
758d6b3
Merge branch 'master' of https://github.com/stellar/go into 4247/clai…
iateadonut Mar 1, 2022
a7e2313
Merge branch 'master' into 4247/claimable_balance
iateadonut Mar 1, 2022
4cec1fb
Merge branch '4247/claimable_balance' of github.com:iateadonut/go int…
iateadonut Mar 1, 2022
3d84568
clients/horizonclient: cursor and limit in ClaimableBalanceRequest
iateadonut Mar 4, 2022
c64d693
Merge branch 'master' into 4247/claimable_balance
iateadonut Mar 4, 2022
dfb520f
clients/horizonclient: cursor and limit in ClaimableBalanceRequest te…
iateadonut Mar 4, 2022
96a920f
Merge branch '4247/claimable_balance' of github.com:iateadonut/go int…
iateadonut Mar 4, 2022
a0be92d
Merge branch 'master' into 4247/claimable_balance
iateadonut Mar 5, 2022
2343187
clients/horizonclient: cursor and limit in ClaimableBalanceRequest - …
iateadonut Mar 5, 2022
c3ed6b2
Merge branch '4247/claimable_balance' of github.com:iateadonut/go int…
iateadonut Mar 5, 2022
3f4f3e7
Merge branch 'master' into 4247/claimable_balance
Shaptic Mar 25, 2022
9ac8dde
add test for limit; move cursor and limit out of params
iateadonut Mar 26, 2022
d54270a
Merge branch '4247/claimable_balance' of github.com:iateadonut/go int…
iateadonut Mar 26, 2022
4d0b6f0
claimable balance request: do not allow a limit of more than 200
iateadonut Mar 26, 2022
c10e167
Merge branch 'master' into 4247/claimable_balance
Shaptic Mar 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions clients/horizonclient/claimable_balance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@ import (
func (cbr ClaimableBalanceRequest) BuildURL() (endpoint string, err error) {
endpoint = "claimable_balances"

//max limit is 200
if cbr.Limit > 200 {
return endpoint, errors.New("invalid request: limit " + fmt.Sprint(cbr.Limit) + " is greater than limit max of 200")
}

// Only one filter parameter is allowed, and you can't mix an ID query and
// filters.
nParams := countParams(cbr.Asset, cbr.Claimant, cbr.Sponsor, cbr.ID)
if nParams > 1 {
if cbr.ID != "" && nParams > 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is necessary? countParams returns 1 if the cbr.ID is set.

Copy link
Contributor Author

@iateadonut iateadonut Mar 26, 2022

Choose a reason for hiding this comment

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

Yes it is necessary; you can have more than one parameter if the ID is not set.

This is valid but returns and error with no cbr.ID != "" condition:

balances, err := client.ClaimableBalances(horizonclient.ClaimableBalanceRequest{
		Claimant: "CLAIMANT_ADDRESS",
		Asset: "AQUA:GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA",
		Limit: 200,
	})

it would query this: https://horizon.stellar.org/claimable_balances/?claimant=GDJCDWRAAB3LA6TT32OBOXV2UYVJEVPKMQXDLUWYH7FQ73MCTH3UNPT7&limit=200&asset=AQUA:GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA

see the associated test, https://github.com/stellar/go/pull/4248/files#diff-368f30ea351d73347fe7c2dac69307ae69e3cdf5f2e92f6d584e097aa256db31line 26

Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, so the comment is a lie then 🤔 can you rephrase it? Then ✔️ from my perspective 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the comment to:
// You can't mix an ID query and filters.

and the error verbiage to:
return endpoint, errors.New("invalid request: you cannot have an ID query with filters")

return endpoint, errors.New("invalid request: too many parameters")
}

if cbr.ID != "" {
endpoint = fmt.Sprintf("%s/%s", endpoint, cbr.ID)
} else {
params := map[string]string{
"claimant": cbr.Claimant,
"sponsor": cbr.Sponsor,
"asset": cbr.Asset,
}
queryParams := addQueryParams(
map[string]string{
"claimant": cbr.Claimant,
"sponsor": cbr.Sponsor,
"asset": cbr.Asset,
},
params, limit(cbr.Limit), cursor(cbr.Cursor),
)

endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams)
}

Expand Down
53 changes: 53 additions & 0 deletions clients/horizonclient/claimable_balance_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package horizonclient

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestClaimableBalanceBuildUrl(t *testing.T) {

cbr := ClaimableBalanceRequest{
ID: "1235",
}
url, err := cbr.BuildURL()
assert.Equal(t, "claimable_balances/1235", url)
assert.NoError(t, err)

//if the ID is included, you cannot include another parameter
cbr = ClaimableBalanceRequest{
ID: "1235",
Claimant: "CLAIMANTADDRESS",
}
_, err = cbr.BuildURL()
assert.EqualError(t, err, "invalid request: too many parameters")

//if you have two parameters, and neither of them are ID, it must use both in the URL
cbr = ClaimableBalanceRequest{
Claimant: "CLAIMANTADDRESS",
Asset: "TEST:ISSUERADDRESS",
}
url, err = cbr.BuildURL()
assert.NoError(t, err)
assert.Equal(t, "claimable_balances?asset=TEST%3AISSUERADDRESS&claimant=CLAIMANTADDRESS", url)

//check limit
cbr = ClaimableBalanceRequest{
Claimant: "CLAIMANTADDRESS",
Asset: "TEST:ISSUERADDRESS",
Limit: 200,
}
url, err = cbr.BuildURL()
assert.NoError(t, err)
assert.Equal(t, "claimable_balances?asset=TEST%3AISSUERADDRESS&claimant=CLAIMANTADDRESS&limit=200", url)

cbr = ClaimableBalanceRequest{
Claimant: "CLAIMANTADDRESS",
Asset: "TEST:ISSUERADDRESS",
Limit: 201,
}
_, err = cbr.BuildURL()
assert.EqualError(t, err, "invalid request: limit 201 is greater than limit max of 200")

}
2 changes: 2 additions & 0 deletions clients/horizonclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ type ClaimableBalanceRequest struct {
Asset string
Sponsor string
Claimant string
Cursor string
Limit uint
}

// ServerTimeRecord contains data for the current unix time of a horizon server instance, and the local time when it was recorded.
Expand Down