Skip to content

Commit

Permalink
clients/horizonclient: allow Fund on other networks and rely on serve…
Browse files Browse the repository at this point in the history
…r to indicate support (#3891)

Allow the `Fund` function to submit a fund request to any horizon instance, not just the instance defined by `horizonclient.DefaultTestNetClient`, and rely on the horizon instance responding with a 404 as an indicator that funding is not supported.

The only way to construct a `horizonclient.Client` that supports the `Fund` operation is to use and mutate the `horizonclient.DefaultTestNetClient`. This is unnecessarily restrictive because a developer may wish to use the `Fund` operation with a non-default client that they have constructed themselves, or they may wish to use the `Fund` operation with their own private or standalone test network.
  • Loading branch information
leighmcculloch authored Sep 7, 2021
1 parent 3a0bd72 commit ea9fc73
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
6 changes: 6 additions & 0 deletions clients/horizonclient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this
file. This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

* The restriction that `Fund` can only be called on the DefaultTestNetClient has
been removed. Any horizonclient.Client may now call Fund. Horizon instances not
supporting Fund will error with a resource not found error.

## [v7.1.1](https://github.com/stellar/go/releases/tag/horizonclient-v7.1.1) - 2021-06-25

* Added transaction and operation result codes to the horizonclient.Error string for easy glancing at string only errors for underlying cause.
Expand Down
6 changes: 3 additions & 3 deletions clients/horizonclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,11 @@ func (c *Client) Trades(request TradeRequest) (tds hProtocol.TradesPage, err err
// Fund creates a new account funded from friendbot. It only works on test networks. See
// https://www.stellar.org/developers/guides/get-started/create-account.html for more information.
func (c *Client) Fund(addr string) (tx hProtocol.Transaction, err error) {
if !c.isTestNet {
return tx, errors.New("can't fund account from friendbot on production network")
}
friendbotURL := fmt.Sprintf("%sfriendbot?addr=%s", c.fixHorizonURL(), addr)
err = c.sendGetRequest(friendbotURL, &tx)
if IsNotFoundError(err) {
return tx, errors.Wrap(err, "funding is only available on test networks and may not be supported by "+c.fixHorizonURL())
}
return
}

Expand Down
24 changes: 23 additions & 1 deletion clients/horizonclient/client_fund_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestFund(t *testing.T) {
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
isTestNet: true,
}

hmock.On(
Expand All @@ -37,3 +36,26 @@ func TestFund(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, int32(8269), tx.Ledger)
}

func TestFund_notSupported(t *testing.T) {
friendbotFundResponse := `{
"type": "https://stellar.org/horizon-errors/not_found",
"title": "Resource Missing",
"status": 404,
"detail": "The resource at the url requested was not found. This usually occurs for one of two reasons: The url requested is not valid, or no data in our database could be found with the parameters provided."
}`

hmock := httptest.NewClient()
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
}

hmock.On(
"GET",
"https://localhost/friendbot?addr=GBLPP2W3X3PJQXYMC7EFWM5G2QCZL7HTCTFNMONS4ITGAYJ3GNNZIQ4V",
).ReturnString(404, friendbotFundResponse)

_, err := client.Fund("GBLPP2W3X3PJQXYMC7EFWM5G2QCZL7HTCTFNMONS4ITGAYJ3GNNZIQ4V")
assert.EqualError(t, err, "funding is only available on test networks and may not be supported by https://localhost/: horizon error: \"Resource Missing\" - check horizon.Error.Problem for more information")
}
2 changes: 0 additions & 2 deletions clients/horizonclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ type Client struct {
// AppVersion is the version of the application using the horizonclient package
AppVersion string
horizonTimeout time.Duration
isTestNet bool

// clock is a Clock returning the current time.
clock *clock.Clock
Expand Down Expand Up @@ -215,7 +214,6 @@ var DefaultTestNetClient = &Client{
HorizonURL: "https://horizon-testnet.stellar.org/",
HTTP: http.DefaultClient,
horizonTimeout: HorizonTimeout,
isTestNet: true,
}

// DefaultPublicNetClient is a default client to connect to public network.
Expand Down

0 comments on commit ea9fc73

Please sign in to comment.