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

protocols/horizon: Add custom UnmarshalJSON to struct with int64 fields. #1915

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 232 additions & 3 deletions clients/horizonclient/effect_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestNextEffectsPage(t *testing.T) {
efp, err := client.Effects(effectRequest)

if assert.NoError(t, err) {
assert.Equal(t, len(efp.Embedded.Records), 2)
assert.Len(t, efp.Embedded.Records, 2)
}

hmock.On(
Expand All @@ -153,7 +153,88 @@ func TestNextEffectsPage(t *testing.T) {

nextPage, err := client.NextEffectsPage(efp)
if assert.NoError(t, err) {
assert.Equal(t, len(nextPage.Embedded.Records), 0)
assert.Len(t, nextPage.Embedded.Records, 0)
}
}

func TestSequenceBumpedNewSeq(t *testing.T) {
hmock := httptest.NewClient()
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
}
effectRequest := EffectRequest{ForAccount: "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD"}
testCases := []struct {
desc string
payload string
}{
{
desc: "new_seq as a string",
payload: sequenceBumpedAsStringPage,
},
{
desc: "new_seq as a number",
payload: sequenceBumpedAsNumberPage,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
hmock.On(
"GET",
"https://localhost/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects",
).ReturnString(200, tc.payload)

efp, err := client.Effects(effectRequest)

if assert.NoError(t, err) {
assert.Len(t, efp.Embedded.Records, 1)
}

effect, ok := efp.Embedded.Records[0].(effects.SequenceBumped)
assert.True(t, ok)
assert.Equal(t, int64(300000000000), effect.NewSeq)

})
}
}

func TestTradeEffectOfferID(t *testing.T) {
hmock := httptest.NewClient()
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
}
effectRequest := EffectRequest{ForAccount: "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD"}
testCases := []struct {
desc string
payload string
}{
{
desc: "offer_id as a number",
payload: tradeEffectNumberOfferID,
},
{
desc: "offer_id as a string",
payload: tradeEffectStringOfferID,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
hmock.On(
"GET",
"https://localhost/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects",
).ReturnString(200, tc.payload)

efp, err := client.Effects(effectRequest)

if assert.NoError(t, err) {
assert.Len(t, efp.Embedded.Records, 1)
}

effect, ok := efp.Embedded.Records[0].(effects.Trade)
assert.True(t, ok)
assert.Equal(t, int64(127538672), effect.OfferID)
})
}
}

Expand Down Expand Up @@ -215,11 +296,159 @@ var firstEffectsPage = `{
"weight": 1,
"public_key": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD",
"key": ""
}
}
]
}
}`

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 = `{
"_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 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 = `
{
"_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 emptyEffectsPage = `{
"_links": {
"self": {
Expand Down
88 changes: 75 additions & 13 deletions clients/horizonclient/offer_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func TestNextOffersPage(t *testing.T) {
assert.Equal(t, len(offers.Embedded.Records), 2)
}

assert.Equal(t, int64(2946580), offers.Embedded.Records[0].ID)

hmock.On(
"GET",
"https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?cursor=2946581&limit=2&order=asc",
Expand Down Expand Up @@ -105,8 +107,47 @@ func TestOfferRequestStreamOffers(t *testing.T) {
}
}

func TestStringOfferID(t *testing.T) {
hmock := httptest.NewClient()
client := &Client{
HorizonURL: "https://localhost/",
HTTP: hmock,
}

offerRequest := OfferRequest{ForAccount: "GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG", Limit: 1}

hmock.On(
"GET",
"https://localhost/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?limit=1",
).ReturnString(200, stringOffersPage)

offers, err := client.Offers(offerRequest)

if assert.NoError(t, err) {
assert.Equal(t, len(offers.Embedded.Records), 1)
}

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 emptyOffersPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?cursor=2946581&limit=2&order=asc"
},
"next": {
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?cursor=2946583&limit=2&order=asc"
},
"prev": {
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?cursor=2946582&limit=2&order=desc"
}
},
"_embedded": {
"records": []
}
}`

var firstOffersPage = `{
"_links": {
Expand Down Expand Up @@ -188,19 +229,40 @@ var firstOffersPage = `{
}
}`

var emptyOffersPage = `{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?cursor=2946581&limit=2&order=asc"
},
"next": {
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?cursor=2946583&limit=2&order=asc"
},
"prev": {
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG/offers?cursor=2946582&limit=2&order=desc"
}
},
var stringOffersPage = `{
"_embedded": {
"records": []
"records": [
{
"_links": {
"self": {
"href": "https://horizon-testnet.stellar.org/offers/2946580"
},
"offer_maker": {
"href": "https://horizon-testnet.stellar.org/accounts/GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG"
}
},
"id": "2946580",
"paging_token": "2946580",
"seller": "GBZ5OD56VRTRQKMNADD6VUZUG3FCILMAMYQY5ZSC3AW3GBXNEPIK76IG",
"selling": {
"asset_type": "credit_alphanum4",
"asset_code": "HT",
"asset_issuer": "GCNSGHUCG5VMGLT5RIYYZSO7VQULQKAJ62QA33DBC5PPBSO57LFWVV6P"
},
"buying": {
"asset_type": "credit_alphanum4",
"asset_code": "BTC",
"asset_issuer": "GCNSGHUCG5VMGLT5RIYYZSO7VQULQKAJ62QA33DBC5PPBSO57LFWVV6P"
},
"amount": "33.7252478",
"price_r": {
"n": 15477,
"d": 43975000
},
"price": "0.0003519",
"last_modified_ledger": 363492,
"last_modified_time": "2019-05-16T08:35:22Z"
}
]
}
}`
Loading