Skip to content
This repository has been archived by the owner on Jan 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #324 from stellar/more-fixes
Browse files Browse the repository at this point in the history
More fixes
  • Loading branch information
nullstyle authored Jan 12, 2017
2 parents 6a4ac29 + f5bd48c commit 4e37310
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ As this project is pre 1.0, breaking changes may happen for minor version
bumps. A breaking change will get clearly notified in this log.


## [Unreleased]

### Bug fixes

- Trade resources now include "bought_amount" and "sold_amount" fields when being viewed through the "Orderbook Trades" endpoint.
- Fixes #322: orderbook summaries with over 20 bids now return the correct price levels, starting with the closest to the spread.

## [v0.7.0] - 2017-01-10

### Added
Expand Down
6 changes: 6 additions & 0 deletions src/github.com/stellar/horizon/actions_trade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func TestTradeActions_Index(t *testing.T) {
w = ht.Get("/order_book/trades?" + q.Encode())
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(1, w.Body)

records := []map[string]interface{}{}
ht.UnmarshalPage(w.Body, &records)

ht.Assert.Contains(records[0], "bought_amount")
ht.Assert.Contains(records[0], "sold_amount")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ FROM
co.priced,
co.price
ORDER BY co.price DESC
ORDER BY co.price ASC
LIMIT $1
)) summary
Expand Down
25 changes: 12 additions & 13 deletions src/github.com/stellar/horizon/db2/core/order_book_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,21 @@ func TestGetOrderBookSummary_Regress310(t *testing.T) {
tt.Require.NoError(err)
tt.Require.Len(summary, 20)

// In the order_books_310 scenario, the orders were
// placed in such a way that three orders at prices
// 0.1, 0.2, and 0.3 should appear first, when the
// query is correct. In a failing scenario the 0.2
// transaction should not appear.

tt.Assert.Equal(0.1, summary[0].Pricef)
tt.Assert.Equal(0.2, summary[1].Pricef)
tt.Assert.Equal(0.3, summary[2].Pricef)
// In the order_books_310 scenario, the orders were placed in such a way that
// three orders at prices 10.2, 10.1, and 10.0 should appear first, when the
// query is correct. In a failing scenario the 10.2 transaction should not
// appear, because it was inserted after the first 20 rows
bids := summary.Bids()
tt.Assert.Equal(10.2, bids[0].Pricef)
tt.Assert.Equal(10.1, bids[1].Pricef)
tt.Assert.Equal(10.0, bids[2].Pricef)

// validate the inverse order book is correct as well
err = q.GetOrderBookSummary(&summary, buying, selling)
tt.Require.NoError(err)
tt.Require.Len(summary, 20)

tt.Assert.Equal(1.0/10.2, summary[0].Pricef)
tt.Assert.Equal(1.0/10.1, summary[1].Pricef)
tt.Assert.Equal(1.0/10.0, summary[2].Pricef)
asks := summary.Asks()
tt.Assert.Equal(1.0/10.2, asks[0].Pricef)
tt.Assert.Equal(1.0/10.1, asks[1].Pricef)
tt.Assert.Equal(1.0/10.0, asks[2].Pricef)
}
2 changes: 2 additions & 0 deletions src/github.com/stellar/horizon/resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ type Trade struct {
ID string `json:"id"`
PT string `json:"paging_token"`
Seller string `json:"seller"`
SoldAmount string `json:"sold_amount"`
SoldAssetType string `json:"sold_asset_type"`
SoldAssetCode string `json:"sold_asset_code,omitempty"`
SoldAssetIssuer string `json:"sold_asset_issuer,omitempty"`
Buyer string `json:"buyer"`
BoughtAmount string `json:"bought_amount"`
BoughtAssetType string `json:"bought_asset_type"`
BoughtAssetCode string `json:"bought_asset_code,omitempty"`
BoughtAssetIssuer string `json:"bought_asset_issuer,omitempty"`
Expand Down
20 changes: 20 additions & 0 deletions src/github.com/stellar/horizon/test/t.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package test

import (
"io"

"encoding/json"

"github.com/stellar/go/support/db"
"github.com/stellar/horizon/ledger"
)
Expand Down Expand Up @@ -47,6 +51,22 @@ func (t *T) ScenarioWithoutHorizon(name string) *T {
return t
}

// UnmarshalPage populates dest with the records contained in the json-encoded
// page in r.
func (t *T) UnmarshalPage(r io.Reader, dest interface{}) {
var env struct {
Embedded struct {
Records json.RawMessage `json:"records"`
} `json:"_embedded"`
}

err := json.NewDecoder(r).Decode(&env)
t.Require.NoError(err, "failed to decode page")

err = json.Unmarshal(env.Embedded.Records, dest)
t.Require.NoError(err, "failed to decode records")
}

// UpdateLedgerState updates the cached ledger state (or panicing on failure).
func (t *T) UpdateLedgerState() {
var next ledger.State
Expand Down

0 comments on commit 4e37310

Please sign in to comment.