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

Add Trades endpoint in Go SDK #408

Closed
wants to merge 11 commits into from

Conversation

0xMichaelRan
Copy link
Contributor

As titled, add a new function LoadTrades()

Testing:

	trades, err = LocalhostCoreClient.LoadTrades(
		horizon.Asset{Type: "native"},
		horizon.Asset{"credit_alphanum4", "SLT", "GCKA6K5PCQ6PNF5RQBF7PQDJWRHO6UOGFMRLK3DYHDOI244V47XKQ4GP"},
		0,
		300000,
		horizon.Limit(3),
		horizon.Order(horizon.OrderAsc),
	)

	fmt.Println("Start printing trades: ")
	for i, rr := range trades.Embedded.Records {
		.... .... 
		fmt.Println(rr)
	}

Result

Start printing trades: 
0
{{ false} {http://localhost:8000/accounts/GBY6A3UQ4RSGWMFDXSIYEDUKJUCXQNDCYPBVLILUZXMJRPGI4PNYA5D7 false} {http://localhost:8000/accounts/GANTILTFDYGNWWJ67PRHTOFYF4ALXBIZ2IVGQHABNN5LBD7MXM4FCH43 false} {http://localhost:8000/operations/73662146575085569 false}}
{{{ false} { false} { false} { false}} 73662146575085569-0 73662146575085569-0 2018-04-03 02:20:33 +0000 UTC 5151620 GBY6A3UQ4RSGWMFDXSIYEDUKJUCXQNDCYPBVLILUZXMJRPGI4PNYA5D7 0.4999200 native   GANTILTFDYGNWWJ67PRHTOFYF4ALXBIZ2IVGQHABNN5LBD7MXM4FCH43 0.1055800 credit_alphanum4 SLT GCKA6K5PCQ6PNF5RQBF7PQDJWRHO6UOGFMRLK3DYHDOI244V47XKQ4GP true 0xc420156398}
1
{{ false} {http://localhost:8000/accounts/GCSBMZV2BYD35GZZVM327APFZEOCHVXWT35ZKHKBSQIEV2H3ATNYWSQA false} {http://localhost:8000/accounts/GAS6QKMRGS76XGKNNQ3MGWUNVQZCBLQAS2KPOITSGD6DPWQZRUQEY5BF false} {http://localhost:8000/operations/73663447950188545 false}}
{{{ false} { false} { false} { false}} 73663447950188545-0 73663447950188545-0 2018-04-03 02:45:45 +0000 UTC 5146821 GCSBMZV2BYD35GZZVM327APFZEOCHVXWT35ZKHKBSQIEV2H3ATNYWSQA 200.7511450 native   GAS6QKMRGS76XGKNNQ3MGWUNVQZCBLQAS2KPOITSGD6DPWQZRUQEY5BF 50.0000000 credit_alphanum4 SLT GCKA6K5PCQ6PNF5RQBF7PQDJWRHO6UOGFMRLK3DYHDOI244V47XKQ4GP true 0xc420156420}
2
{{ false} {http://localhost:8000/accounts/GBGMFKBQ6LVLDUQDROQBRA73VX7LJ3TC4MDRQAYEIPUTJU32OSILAVW4 false} {http://localhost:8000/accounts/GANTILTFDYGNWWJ67PRHTOFYF4ALXBIZ2IVGQHABNN5LBD7MXM4FCH43 false} {http://localhost:8000/operations/73664324123504641 false}}
{{{ false} { false} { false} { false}} 73664324123504641-0 73664324123504641-0 2018-04-03 03:02:45 +0000 UTC 5152692 GBGMFKBQ6LVLDUQDROQBRA73VX7LJ3TC4MDRQAYEIPUTJU32OSILAVW4 0.9999999 native   GANTILTFDYGNWWJ67PRHTOFYF4ALXBIZ2IVGQHABNN5LBD7MXM4FCH43 0.2178649 credit_alphanum4 SLT GCKA6K5PCQ6PNF5RQBF7PQDJWRHO6UOGFMRLK3DYHDOI244V47XKQ4GP false 0xc420156470}

http://localhost:8000/accounts/GBTYBQW5GLRSYMT4OJQGVAIH5KBIE5ITEWIVDZHVHIZT7XSKU6DLB6QI/payments?cursor=-213&order=asc&limit=2
```
{
  "type": "https://stellar.org/horizon-errors/bad_request",
  "title": "Bad Request",
  "status": 400,
  "detail": "The request you sent was invalid in some way",
  "extras": {
    "invalid_field": "cursor",
    "reason": "the cursor -213 is a negative number: "
  }
}
```
https://www.stellar.org/developers/horizon/reference/endpoints/trades.html
http://localhost:8000/trades?limit=3&order=desc&base_asset_type=native&counter_asset_code=SLT&counter_asset_issuer=GCKA6K5PCQ6PNF5RQBF7PQDJWRHO6UOGFMRLK3DYHDOI244V47XKQ4GP&counter_asset_type=credit_alphanum4
offerID int64,
resolution int64,
params ...interface{},
) (tradesPage TradesPage, err error)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably create code-style guide for our Go code.

resolution int64,
params ...interface{},
) (tradesPage TradesPage, err error) {
a := m.Called(baseAsset, counterAsset, offerID, resolution, params)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be params.... Right now, for the following params: ["a", "b", "c"] will call:

m.Called(baseAsset, counterAsset, offerID, resolution, ["a", "b", "c"])

instead of:

m.Called(baseAsset, counterAsset, offerID, resolution, "a", "b", "c")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean this?

a := m.Called(baseAsset, counterAsset, offerID, resolution, params...)

I won't get this compiled.

Copy link
Contributor

Choose a reason for hiding this comment

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

What errors are you getting? It should work.

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 is the error message I got.

screen shot 2018-04-18 at 23 55 17

Copy link
Contributor

@bartekn bartekn Apr 20, 2018

Choose a reason for hiding this comment

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

OK, I pulled your code and it seems we're falling into golang/go#18605.

The only possible workaround is to do it like:

	args := []interface{}{baseAsset, counterAsset, offerID, resolution}
	args = append(args, params...)
	a := m.Called(args...)
	return a.Get(0).(TradesPage), a.Error(1)

which looks strange but works as expected (allows mocking params properly).

@nikhilsaraf
Copy link
Contributor

Is the test you've listed in your comment above checked in? @michaelran16
review looks good to me. Deferring to @bartekn for review approval based on this review's history.

@0xMichaelRan
Copy link
Contributor Author

Thanks, @bartekn! I started writing Go since last week, so I was assuming lots of styling guide from Java, which I then realize shouldn't be the case.

If there's no guide now, I will refer to this for now:
https://github.com/golang/go/wiki/CodeReviewComments

@0xMichaelRan
Copy link
Contributor Author

0xMichaelRan commented Apr 17, 2018

@nikhilsaraf No the test code is an external project, calling horizon go API, thus not checked in.

@bartekn
Copy link
Contributor

bartekn commented Apr 17, 2018

Thanks, @bartekn! I started writing Go since last week, so I was assuming lots of styling guide from Java, which I then realize shouldn't be the case.

No worries. I think the best solution is to connect go fmt to your code editor. Without having the one code-style we will end up with a lot of code changes connected with styling in diffs and sometimes conflicts when merging branches.

Would be great to have tests for a new method. Check main_test.go file for examples.

@nikhilsaraf
Copy link
Contributor

+1 on the test. they should be part of the project and checked in, that way we can run them as part of the build.

@0xMichaelRan
Copy link
Contributor Author

0xMichaelRan commented Apr 20, 2018

@bartekn and @nikhilsaraf
Testing code updated.

@@ -266,6 +266,80 @@ var _ = Describe("Horizon", func() {
})
})

Describe("LoadTrades", func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do not add tests using gingko. As mentioned previously in slack, we should be writing our tests using the standard library's testing package and testify.

@0xMichaelRan
Copy link
Contributor Author

@nullstyle I think this PR is good to go, the testing code I move to a different discussion thread: #421

@nullstyle
Copy link
Contributor

@michaelran16 You misunderstood me: I didn't mean that you should remove the test code. I meant that you should be porting that test code into our most modern convention for go tests: the standard test library and testify.

We are doing this work such that we can remove goconvey, gingko, and gomega from our list of dependencies.

@nullstyle
Copy link
Contributor

Whoops! I see your comment now. nvm.

@0xMichaelRan
Copy link
Contributor Author

replaced with #428

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

Successfully merging this pull request may close these issues.

5 participants