diff --git a/CHANGELOG.md b/CHANGELOG.md index 684aaaac..98804028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/github.com/stellar/horizon/actions_trade_test.go b/src/github.com/stellar/horizon/actions_trade_test.go index e0cbd20a..51b3a401 100644 --- a/src/github.com/stellar/horizon/actions_trade_test.go +++ b/src/github.com/stellar/horizon/actions_trade_test.go @@ -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") } } diff --git a/src/github.com/stellar/horizon/db2/core/order_book_summary.go b/src/github.com/stellar/horizon/db2/core/order_book_summary.go index a8321cd9..dfe0a8ed 100644 --- a/src/github.com/stellar/horizon/db2/core/order_book_summary.go +++ b/src/github.com/stellar/horizon/db2/core/order_book_summary.go @@ -159,7 +159,7 @@ FROM co.priced, co.price - ORDER BY co.price DESC + ORDER BY co.price ASC LIMIT $1 )) summary diff --git a/src/github.com/stellar/horizon/db2/core/order_book_summary_test.go b/src/github.com/stellar/horizon/db2/core/order_book_summary_test.go index cedec5b2..91da171e 100644 --- a/src/github.com/stellar/horizon/db2/core/order_book_summary_test.go +++ b/src/github.com/stellar/horizon/db2/core/order_book_summary_test.go @@ -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) } diff --git a/src/github.com/stellar/horizon/resource/main.go b/src/github.com/stellar/horizon/resource/main.go index ad8571ef..5bc9a7cc 100644 --- a/src/github.com/stellar/horizon/resource/main.go +++ b/src/github.com/stellar/horizon/resource/main.go @@ -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"` diff --git a/src/github.com/stellar/horizon/test/t.go b/src/github.com/stellar/horizon/test/t.go index 153266ce..8e157220 100644 --- a/src/github.com/stellar/horizon/test/t.go +++ b/src/github.com/stellar/horizon/test/t.go @@ -1,6 +1,10 @@ package test import ( + "io" + + "encoding/json" + "github.com/stellar/go/support/db" "github.com/stellar/horizon/ledger" ) @@ -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