From 3b205b519924d0d667da9d24d5bb2131a254798c Mon Sep 17 00:00:00 2001 From: Sagleft Date: Thu, 7 Jul 2022 00:28:42 +0300 Subject: [PATCH] add `GetPairPrice` method --- structs.go | 6 ++++++ trade.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/structs.go b/structs.go index b60c6cb..b8688e5 100644 --- a/structs.go +++ b/structs.go @@ -265,3 +265,9 @@ type OperationsHistoryDataContainer struct { } type mapTable map[string]interface{} + +type PairPriceData struct { + PairSymbol string + BestAskPrice float64 + BestBidPrice float64 +} diff --git a/trade.go b/trade.go index 0cee300..488adac 100644 --- a/trade.go +++ b/trade.go @@ -148,3 +148,33 @@ func (c *Client) GetMarketCurrenciesList(pairSymbol string) (*CurrenciesListData } return &response.Result, nil } + +func (c *Client) GetPairPrice(pairCode string) (PairPriceData, error) { + result := PairPriceData{} + pairs, err := c.GetPairs() + if err != nil { + return result, err + } + + for i := 0; i < len(pairs); i++ { + pairData := pairs[i] + if pairData.Pair.PairCode == pairCode { + bookData, err := c.GetOrderBook(pairData.Pair.PairCode) + if err != nil { + return result, err + } + + if len(bookData.Buy) > 0 { + result.BestAskPrice = bookData.Buy[0].Price + } + if len(bookData.Sell) > 0 { + result.BestBidPrice = bookData.Sell[0].Price + } + return result, nil + } else { + continue + } + } + + return result, errors.New("pair " + pairCode + " not found") +}