From ccc67a2dccbe2098bc5e44f7ca567508e90b9e1b Mon Sep 17 00:00:00 2001 From: Georgy Shapchits Date: Sat, 4 Dec 2021 16:59:53 +0200 Subject: [PATCH] Add /simple/token_price endpoint --- v3/json/simple_token_price.json | 11 +++++++ v3/v3.go | 53 +++++++++++++++++++++++++++++++++ v3/v3_test.go | 28 +++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 v3/json/simple_token_price.json diff --git a/v3/json/simple_token_price.json b/v3/json/simple_token_price.json new file mode 100644 index 0000000..5f737d6 --- /dev/null +++ b/v3/json/simple_token_price.json @@ -0,0 +1,11 @@ +{ + "0x5218e472cfcfe0b64a064f055b43b4cdc9efd3a6": { + "usd": 0.10674 + }, + "0x6b175474e89094c44da98b954eedeac495271d0f": { + "usd": 1.01 + }, + "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": { + "usd": 1 + } +} diff --git a/v3/v3.go b/v3/v3.go index bf893e7..4e94731 100755 --- a/v3/v3.go +++ b/v3/v3.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "net/url" + "strconv" "strings" "github.com/superoo7/go-gecko/format" @@ -19,6 +20,22 @@ type Client struct { httpClient *http.Client } +// TokenPriceOpts not required options for /simple/token_price request +type TokenPriceOpts struct { + IncludeMarketCap bool + Include24HoursVol bool + Include24HoursChange bool + IncludeLastUpdatedAt bool +} + +type TokenPrice struct { + USD float64 `json: "usd"` + USDMarketCap float64 `json: "usd_market_cap"` + USD24hVol float64 `json: "usd_24h_vol"` + USD24hChange float64 `json: "usd_24h_change"` + LastUpdatedAt int64 `json: "last_updated_at"` +} + // NewClient create new client object func NewClient(httpClient *http.Client) *Client { if httpClient == nil { @@ -117,6 +134,42 @@ func (c *Client) SimplePrice(ids []string, vsCurrencies []string) (*map[string]m return &t, nil } +// SimplePrice /simple/price Multiple ID and Currency (ids, vs_currencies) +func (c *Client) SimpleTokenPrice( + platformID string, + tokenAddresses []string, + currencies []string, + opts *TokenPriceOpts, +) (map[string]TokenPrice, error) { + params := url.Values{} + addresssesParam := strings.Join(tokenAddresses[:], ",") + currenciesParam := strings.Join(currencies[:], ",") + + params.Add("contract_addresses", addresssesParam) + params.Add("vs_currencies", currenciesParam) + + if opts != nil { + params.Add("include_market_cap", strconv.FormatBool(opts.IncludeMarketCap)) + params.Add("include_24hr_vol", strconv.FormatBool(opts.Include24HoursVol)) + params.Add("include_24hr_change", strconv.FormatBool(opts.Include24HoursChange)) + params.Add("include_last_updated_at", strconv.FormatBool(opts.IncludeLastUpdatedAt)) + } + + url := fmt.Sprintf("%s/simple/token_price/%s?%s", baseURL, platformID, params.Encode()) + resp, err := c.MakeReq(url) + if err != nil { + return nil, err + } + + tokens := make(map[string]TokenPrice) + err = json.Unmarshal(resp, &tokens) + if err != nil { + return nil, err + } + + return tokens, nil +} + // SimpleSupportedVSCurrencies /simple/supported_vs_currencies func (c *Client) SimpleSupportedVSCurrencies() (*types.SimpleSupportedVSCurrencies, error) { url := fmt.Sprintf("%s/simple/supported_vs_currencies", baseURL) diff --git a/v3/v3_test.go b/v3/v3_test.go index 5e0ae2f..24b7e28 100755 --- a/v3/v3_test.go +++ b/v3/v3_test.go @@ -62,6 +62,34 @@ func TestSimplePrice(t *testing.T) { } } +func TestSimpleTokenPrice(t *testing.T) { + err := setupGock("json/simple_token_price.json", "/simple/token_price") + if err != nil { + t.FailNow() + } + + platformID := "ethereum" + contractAddresses := []string{ + "0x5218E472cFCFE0b64A064F055B43b4cdC9EfD3A6", + "0x6b175474e89094c44da98b954eedeac495271d0f", + "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", + } + currencies := []string{"usd"} + + tokens, err := c.SimpleTokenPrice(platformID, contractAddresses, currencies, nil) + if err != nil { + t.FailNow() + } + + if tokens == nil { + t.FailNow() + } + + if len(tokens) != 3 { + t.FailNow() + } +} + func TestSimpleSupportedVSCurrencies(t *testing.T) { err := setupGock("json/simple_supported_vs_currencies.json", "/simple/supported_vs_currencies") s, err := c.SimpleSupportedVSCurrencies()