Skip to content

Commit

Permalink
protocols/horizon: Change JSON response from int64 to string in multi…
Browse files Browse the repository at this point in the history
…ple places. (#2021)

* Change type from int64 to strings for multiple fields in protocol.

Change the type for multiple JSON fields using `int64` to be of type `string`:

  - Attribute `offer_id` in [manage buy offer](https://www.stellar.org/developers/horizon/reference/resources/operation.html#manage-buy-offer) and [manage sell offer](https://www.stellar.org/developers/horizon/reference/resources/operation.html#manage-sell-offer) operations.
  - Attribute `offer_id` in [Trade](https://www.stellar.org/developers/horizon/reference/resources/effect.html#trading-effects) effect.
  - Attribute `id` in [Offer](https://www.stellar.org/developers/horizon/reference/resources/offer.html) resource.
  - Attribute `timestamp` and `trade_count` in [Trade Aggregation](https://www.stellar.org/developers/horizon/reference/resources/trade_aggregation.html) resource.
  - Attribute `new_seq` in [Sequence Bumpeed](https://www.stellar.org/developers/horizon/reference/resources/effect.html#misc-effects) effect.

* Add a custom ManageOffer struct to unmarshall  DetailsString for manage_offer ops.

* Add regression test for trade effect.

* Add a custom struct for TradeEffectDetails.

* Remove test for int64 values.

* Remove int64 or string workaround in protocol.

* Remove sequence bumped test with int64.

* Rename vars on test.
  • Loading branch information
abuiles authored Jan 20, 2020
1 parent 8b7bea6 commit 2e9e667
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 316 deletions.
4 changes: 2 additions & 2 deletions clients/horizon/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ var accountOffersResponse = `{
"href": "https://horizon.stellar.org/accounts/GC2BQYBXFOVPRDH35D5HT2AFVCDGXJM5YVTAF5THFSAISYOWAJQKRESK"
}
},
"id": 161,
"id": "161",
"paging_token": "161",
"seller": "GC2BQYBXFOVPRDH35D5HT2AFVCDGXJM5YVTAF5THFSAISYOWAJQKRESK",
"selling": {
Expand All @@ -731,7 +731,7 @@ var accountOffersResponse = `{
"href": "https://horizon.stellar.org/accounts/GC2BQYBXFOVPRDH35D5HT2AFVCDGXJM5YVTAF5THFSAISYOWAJQKRESK"
}
},
"id": 2539,
"id": "2539",
"paging_token": "2539",
"seller": "GC2BQYBXFOVPRDH35D5HT2AFVCDGXJM5YVTAF5THFSAISYOWAJQKRESK",
"selling": {
Expand Down
90 changes: 4 additions & 86 deletions clients/horizonclient/effect_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ func TestSequenceBumpedNewSeq(t *testing.T) {
}{
{
desc: "new_seq as a string",
payload: sequenceBumpedAsStringPage,
},
{
desc: "new_seq as a number",
payload: sequenceBumpedAsNumberPage,
payload: sequenceBumpedPage,
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -209,13 +205,9 @@ func TestTradeEffectOfferID(t *testing.T) {
desc string
payload string
}{
{
desc: "offer_id as a number",
payload: tradeEffectNumberOfferID,
},
{
desc: "offer_id as a string",
payload: tradeEffectStringOfferID,
payload: tradeEffectPage,
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -301,45 +293,7 @@ var firstEffectsPage = `{
}
}`

var sequenceBumpedAsNumberPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=&limit=10&order=asc"
},
"next": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=asc"
},
"prev": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-1&limit=10&order=desc"
}
},
"_embedded": {
"records": [
{
"_links": {
"operation": {
"href": "https://horizon-testnet.stellar.org/operations/249108107265"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=249108107265-1"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=249108107265-1"
}
},
"id": "0000000249108107265-0000000001",
"paging_token": "249108107265-1",
"account": "GCQZP3IU7XU6EJ63JZXKCQOYT2RNXN3HB5CNHENNUEUHSMA4VUJJJSEN",
"type": "sequence_bumped",
"type_i": 43,
"created_at": "2019-06-03T16:36:24Z",
"new_seq": 300000000000
}
]
}
}`

var sequenceBumpedAsStringPage = `{
var sequenceBumpedPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=&limit=10&order=asc"
Expand Down Expand Up @@ -377,43 +331,7 @@ var sequenceBumpedAsStringPage = `{
}
}`

var tradeEffectNumberOfferID = `
{
"_embedded": {
"records": [
{
"_links": {
"operation": {
"href": "https://horizon-testnet.stellar.org/operations/224209713045979100"
},
"succeeds": {
"href": "https://horizon-testnet.stellar.org/effects?order=desc&cursor=224209713045979100-3"
},
"precedes": {
"href": "https://horizon-testnet.stellar.org/effects?order=asc&cursor=224209713045979100-3"
}
},
"id": "2214209713045979100-0000000003",
"paging_token": "224209713045979100-3",
"account": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD",
"type": "trade",
"type_i": 33,
"created_at": "2019-11-01T23:05:58Z",
"seller": "GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX",
"offer_id": 127538672,
"sold_amount": "14.5984123",
"sold_asset_type": "native",
"bought_amount": "1.0000000",
"bought_asset_type": "credit_alphanum4",
"bought_asset_code": "USD",
"bought_asset_issuer": "GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX"
}
]
}
}
`

var tradeEffectStringOfferID = `
var tradeEffectPage = `
{
"_embedded": {
"records": [
Expand Down
4 changes: 2 additions & 2 deletions clients/horizonclient/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ var offersResponse = `{
"href": "https://horizon-testnet.stellar.org/accounts/GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F"
}
},
"id": 432323,
"id": "432323",
"paging_token": "432323",
"seller": "GDOJCPYIB66RY4XNDLRRHQQXB27YLNNAGAYV5HMHEYNYY4KUNV5FDV2F",
"selling": {
Expand Down Expand Up @@ -1302,7 +1302,7 @@ var multipleOpsResponse = `{
"selling_asset_type": "credit_alphanum4",
"selling_asset_code": "XRP",
"selling_asset_issuer": "GBVOL67TMUQBGL4TZYNMY3ZQ5WGQYFPFD5VJRWXR72VA33VFNL225PL5",
"offer_id": 73938565
"offer_id": "73938565"
},
{
"_links": {
Expand Down
6 changes: 3 additions & 3 deletions clients/horizonclient/offer_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestStringOfferID(t *testing.T) {
assert.Equal(t, int64(2946580), offers.Embedded.Records[0].ID)
}

var offerStreamResponse = `data: {"_links":{"self":{"href":"https://horizon-testnet.stellar.org/offers/5269100"},"offer_maker":{"href":"https://horizon-testnet.stellar.org/accounts/GAQHWQYBBW272OOXNQMMLCA5WY2XAZPODGB7Q3S5OKKIXVESKO55ZQ7C"}},"id":5269100,"paging_token":"5269100","seller":"GAQHWQYBBW272OOXNQMMLCA5WY2XAZPODGB7Q3S5OKKIXVESKO55ZQ7C","selling":{"asset_type":"credit_alphanum4","asset_code":"DSQ","asset_issuer":"GBDQPTQJDATT7Z7EO4COS4IMYXH44RDLLI6N6WIL5BZABGMUOVMLWMQF"},"buying":{"asset_type":"credit_alphanum4","asset_code":"XCS6","asset_issuer":"GBH2V47NOZRC56QAYCPV5JUBG5NVFJQF5AQTUNFNWNDHSWWTKH2MWR2L"},"amount":"20.4266087","price_r":{"n":24819,"d":10000000},"price":"0.0024819","last_modified_ledger":674449,"last_modified_time":"2019-04-08T11:56:41Z"}
var offerStreamResponse = `data: {"_links":{"self":{"href":"https://horizon-testnet.stellar.org/offers/5269100"},"offer_maker":{"href":"https://horizon-testnet.stellar.org/accounts/GAQHWQYBBW272OOXNQMMLCA5WY2XAZPODGB7Q3S5OKKIXVESKO55ZQ7C"}},"id":"5269100","paging_token":"5269100","seller":"GAQHWQYBBW272OOXNQMMLCA5WY2XAZPODGB7Q3S5OKKIXVESKO55ZQ7C","selling":{"asset_type":"credit_alphanum4","asset_code":"DSQ","asset_issuer":"GBDQPTQJDATT7Z7EO4COS4IMYXH44RDLLI6N6WIL5BZABGMUOVMLWMQF"},"buying":{"asset_type":"credit_alphanum4","asset_code":"XCS6","asset_issuer":"GBH2V47NOZRC56QAYCPV5JUBG5NVFJQF5AQTUNFNWNDHSWWTKH2MWR2L"},"amount":"20.4266087","price_r":{"n":24819,"d":10000000},"price":"0.0024819","last_modified_ledger":674449,"last_modified_time":"2019-04-08T11:56:41Z"}
`
var emptyOffersPage = `{
"_links": {
Expand Down Expand Up @@ -172,7 +172,7 @@ var firstOffersPage = `{
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG"
}
},
"id": 2946580,
"id": "2946580",
"paging_token": "2946580",
"seller": "GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG",
"selling": {
Expand Down Expand Up @@ -203,7 +203,7 @@ var firstOffersPage = `{
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG"
}
},
"id": 2946581,
"id": "2946581",
"paging_token": "2946581",
"seller": "GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG",
"selling": {
Expand Down
8 changes: 2 additions & 6 deletions clients/horizonclient/operation_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,9 @@ func TestManageSellManageBuyOfferOfferID(t *testing.T) {
desc string
payload string
}{
{
desc: "offer_id as a number",
payload: numberManageSellBuyOfferOperations,
},
{
desc: "offer_id as a string",
payload: stringManageSellBuyOfferOperations,
payload: manageSellBuyOfferOperationsPage,
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -411,7 +407,7 @@ var numberManageSellBuyOfferOperations = `{
}
}`

var stringManageSellBuyOfferOperations = `{
var manageSellBuyOfferOperationsPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/operations?cursor=661424967682&limit=2&order=asc"
Expand Down
16 changes: 8 additions & 8 deletions clients/horizonclient/trade_aggregation_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ var tradeAggsResponse = `{
"_embedded": {
"records": [
{
"timestamp": 1517522400000,
"trade_count": 26,
"timestamp": "1517522400000",
"trade_count": "26",
"base_volume": "27575.0201596",
"counter_volume": "5085.6410385",
"avg": "0.1844293",
Expand All @@ -246,8 +246,8 @@ var tradeAggsResponse = `{
}
},
{
"timestamp": 1517526000000,
"trade_count": 15,
"timestamp": "1517526000000",
"trade_count": "15",
"base_volume": "3913.8224543",
"counter_volume": "719.4993608",
"avg": "0.1838355",
Expand Down Expand Up @@ -291,8 +291,8 @@ var firstTradeAggsPage = `{
"_embedded": {
"records": [
{
"timestamp": 1565026860000,
"trade_count": 3,
"timestamp": "1565026860000",
"trade_count": "3",
"base_volume": "23781.2128418",
"counter_volume": "2.0000000",
"avg": "0.0000841",
Expand All @@ -318,8 +318,8 @@ var firstTradeAggsPage = `{
}
},
{
"timestamp": 1565026920000,
"trade_count": 1,
"timestamp": "1565026920000",
"trade_count": "1",
"base_volume": "11890.6052319",
"counter_volume": "0.9999999",
"avg": "0.0000841",
Expand Down
21 changes: 0 additions & 21 deletions protocols/horizon/base/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package base

import "encoding/json"

type Price struct {
N int32 `json:"n"`
D int32 `json:"d"`
Expand All @@ -23,22 +21,3 @@ type Asset struct {
type Rehydratable interface {
Rehydrate() error
}

// ExtractOfferID extracts offer_id from a JSON payload
// Action needed in release: horizon-v0.25.0: remove
func ExtractOfferID(data []byte) (int64, error) {
var temp struct {
OfferID json.Number `json:"offer_id"`
}

if err := json.Unmarshal(data, &temp); err != nil {
return 0, err
}

offerID, err := temp.OfferID.Int64()
if err != nil {
return 0, err
}

return offerID, nil
}
65 changes: 4 additions & 61 deletions protocols/horizon/effects/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,33 +198,7 @@ type AccountFlagsUpdated struct {

type SequenceBumped struct {
Base
// Action needed in release: horizon-v0.25.0: make new_seq as string
NewSeq int64 `json:"new_seq"`
}

// UnmarshalJSON is the custom unmarshal method for SequenceBumped. It allows
// parsing of new_seq as a string or an int64.
// Action needed in release: horizon-v0.25.0: Delete
func (effect *SequenceBumped) UnmarshalJSON(data []byte) error {
var temp struct {
NewSeq json.Number `json:"new_seq"`
}

if err := json.Unmarshal(data, &effect.Base); err != nil {
return err
}

if err := json.Unmarshal(data, &temp); err != nil {
return err
}

newSeq, err := temp.NewSeq.Int64()
if err != nil {
return err
}
effect.NewSeq = newSeq

return nil
NewSeq int64 `json:"new_seq,string"`
}

type SignerCreated struct {
Expand Down Expand Up @@ -280,10 +254,10 @@ type TrustlineDeauthorized struct {
AssetCode string `json:"asset_code,omitempty"`
}

// Action needed in release: horizon-v0.25.0: move back to Trade, remove
// embedded struct
type tradeBase struct {
type Trade struct {
Base
Seller string `json:"seller"`
OfferID int64 `json:"offer_id,string"`
SoldAmount string `json:"sold_amount"`
SoldAssetType string `json:"sold_asset_type"`
SoldAssetCode string `json:"sold_asset_code,omitempty"`
Expand All @@ -294,37 +268,6 @@ type tradeBase struct {
BoughtAssetIssuer string `json:"bought_asset_issuer,omitempty"`
}

type Trade struct {
Base
tradeBase
// Action needed in release: horizon-v0.25.0: Make offer_id a string
OfferID int64 `json:"offer_id"`
}

// UnmarshalJSON is the custom unmarshal method for Trade. It allows
// parsing of offer_id as a string or an int64.
// Action needed in release: horizon-v0.25.0: Delete
func (effect *Trade) UnmarshalJSON(data []byte) error {
var tradeWithoutOfferID struct {
Base
tradeBase
}
if err := json.Unmarshal(data, &tradeWithoutOfferID); err != nil {
return err
}

offerID, err := base.ExtractOfferID(data)
if err != nil {
return err
}

effect.Base = tradeWithoutOfferID.Base
effect.tradeBase = tradeWithoutOfferID.tradeBase
effect.OfferID = offerID

return nil
}

// Effect contains methods that are implemented by all effect types.
type Effect interface {
PagingToken() string
Expand Down
Loading

0 comments on commit 2e9e667

Please sign in to comment.