From 86988e99d5838ece6c882a5d4a1fd136a176835f Mon Sep 17 00:00:00 2001 From: PR Date: Tue, 21 Mar 2023 18:37:04 -0700 Subject: [PATCH] book offers test --- model/client/common/ledger.go | 3 + model/client/path/book_offers_response.go | 16 ++- model/client/path/book_offers_test.go | 147 ++++++++++++++++++++++ 3 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 model/client/path/book_offers_test.go diff --git a/model/client/common/ledger.go b/model/client/common/ledger.go index d7223dba..e3ad6c74 100644 --- a/model/client/common/ledger.go +++ b/model/client/common/ledger.go @@ -11,6 +11,9 @@ type LedgerSpecifier interface { } func UnmarshalLedgerSpecifier(data []byte) (LedgerSpecifier, error) { + if len(data) == 0 { + return nil, nil + } switch data[0] { case '"': var s string diff --git a/model/client/path/book_offers_response.go b/model/client/path/book_offers_response.go index 151c208f..e99e1ae6 100644 --- a/model/client/path/book_offers_response.go +++ b/model/client/path/book_offers_response.go @@ -17,27 +17,31 @@ type BookOffersResponse struct { type BookOffer struct { ledger.Offer - OwnerFunds string `json:"offer_funds"` + OwnerFunds string `json:"owner_funds,omitempty"` TakerGetsFunded types.CurrencyAmount `json:"taker_gets_funded,omitempty"` TakerPaysFunded types.CurrencyAmount `json:"taker_pays_funded,omitempty"` - Quality string `json:"quality"` + Quality string `json:"quality,omitempty"` } func (o *BookOffer) UnmarshalJSON(data []byte) error { type boHelper struct { - ledger.Offer - OwnerFunds string `json:"offer_funds"` + OwnerFunds string `json:"offer_funds,omitempty"` TakerGetsFunded json.RawMessage `json:"taker_gets_funded,omitempty"` TakerPaysFunded json.RawMessage `json:"taker_pays_funded,omitempty"` - Quality string `json:"quality"` + Quality string `json:"quality,omitempty"` } var h boHelper err := json.Unmarshal(data, &h) if err != nil { return err } + var offer ledger.Offer + err = json.Unmarshal(data, &offer) + if err != nil { + return err + } *o = BookOffer{ - Offer: h.Offer, + Offer: offer, OwnerFunds: h.OwnerFunds, Quality: h.Quality, } diff --git a/model/client/path/book_offers_test.go b/model/client/path/book_offers_test.go new file mode 100644 index 00000000..f48d797a --- /dev/null +++ b/model/client/path/book_offers_test.go @@ -0,0 +1,147 @@ +package path + +import ( + "testing" + + "github.com/xyield/xrpl-go/model/ledger" + "github.com/xyield/xrpl-go/model/transactions/types" + "github.com/xyield/xrpl-go/test" +) + +func TestBookOffersRequest(t *testing.T) { + s := BookOffersRequest{ + Taker: "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", + TakerGets: types.IssuedCurrencyAmount{ + Currency: "XRP", + }, + TakerPays: types.IssuedCurrencyAmount{ + Currency: "USD", + Issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + }, + Limit: 10, + } + j := `{ + "taker_gets": { + "currency": "XRP" + }, + "taker_pays": { + "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + "currency": "USD" + }, + "limit": 10, + "taker": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59" +}` + + if err := test.SerializeAndDeserialize(t, s, j); err != nil { + t.Error(err) + } +} + +func TestBookOffersResponse(t *testing.T) { + s := BookOffersResponse{ + LedgerCurrentIndex: 7035305, + Offers: []BookOffer{ + { + Offer: ledger.Offer{ + Account: "rM3X3QSr8icjTGpaF52dozhbT2BZSXJQYM", + BookDirectory: "7E5F614417C2D0A7CEFEB73C4AA773ED5B078DE2B5771F6D55055E4C405218EB", + BookNode: "0000000000000000", + Flags: 0, + LedgerEntryType: ledger.OfferEntry, + OwnerNode: "0000000000000AE0", + PreviousTxnID: "6956221794397C25A53647182E5C78A439766D600724074C99D78982E37599F1", + PreviousTxnLgrSeq: 7022646, + Sequence: 264542, + TakerGets: types.IssuedCurrencyAmount{ + Currency: "EUR", + Issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + Value: "17.90363633316433", + }, + TakerPays: types.IssuedCurrencyAmount{ + Currency: "USD", + Issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + Value: "27.05340557506234", + }, + }, + Quality: "1.511056473200875", + }, + { + Offer: ledger.Offer{ + Account: "rhsxKNyN99q6vyYCTHNTC1TqWCeHr7PNgp", + BookDirectory: "7E5F614417C2D0A7CEFEB73C4AA773ED5B078DE2B5771F6D5505DCAA8FE12000", + BookNode: "0000000000000000", + Flags: 131072, + LedgerEntryType: ledger.OfferEntry, + OwnerNode: "0000000000000001", + PreviousTxnID: "8AD748CD489F7FF34FCD4FB73F77F1901E27A6EFA52CCBB0CCDAAB934E5E754D", + PreviousTxnLgrSeq: 7007546, + Sequence: 265, + TakerGets: types.IssuedCurrencyAmount{ + Currency: "EUR", + Issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + Value: "2.542743233917848", + }, + TakerPays: types.IssuedCurrencyAmount{ + Currency: "USD", + Issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + Value: "4.19552633596446", + }, + }, + Quality: "1.65", + }, + }, + } + + j := `{ + "ledger_current_index": 7035305, + "offers": [ + { + "Account": "rM3X3QSr8icjTGpaF52dozhbT2BZSXJQYM", + "BookDirectory": "7E5F614417C2D0A7CEFEB73C4AA773ED5B078DE2B5771F6D55055E4C405218EB", + "BookNode": "0000000000000000", + "Flags": 0, + "LedgerEntryType": "Offer", + "OwnerNode": "0000000000000AE0", + "PreviousTxnID": "6956221794397C25A53647182E5C78A439766D600724074C99D78982E37599F1", + "PreviousTxnLgrSeq": 7022646, + "Sequence": 264542, + "TakerPays": { + "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + "currency": "USD", + "value": "27.05340557506234" + }, + "TakerGets": { + "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + "currency": "EUR", + "value": "17.90363633316433" + }, + "quality": "1.511056473200875" + }, + { + "Account": "rhsxKNyN99q6vyYCTHNTC1TqWCeHr7PNgp", + "BookDirectory": "7E5F614417C2D0A7CEFEB73C4AA773ED5B078DE2B5771F6D5505DCAA8FE12000", + "BookNode": "0000000000000000", + "Flags": 131072, + "LedgerEntryType": "Offer", + "OwnerNode": "0000000000000001", + "PreviousTxnID": "8AD748CD489F7FF34FCD4FB73F77F1901E27A6EFA52CCBB0CCDAAB934E5E754D", + "PreviousTxnLgrSeq": 7007546, + "Sequence": 265, + "TakerPays": { + "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + "currency": "USD", + "value": "4.19552633596446" + }, + "TakerGets": { + "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", + "currency": "EUR", + "value": "2.542743233917848" + }, + "quality": "1.65" + } + ] +}` + if err := test.SerializeAndDeserialize(t, s, j); err != nil { + t.Error(err) + } +}