Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Add /simple/token_price endpoint #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions v3/json/simple_token_price.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"0x5218e472cfcfe0b64a064f055b43b4cdc9efd3a6": {
"usd": 0.10674
},
"0x6b175474e89094c44da98b954eedeac495271d0f": {
"usd": 1.01
},
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": {
"usd": 1
}
}
53 changes: 53 additions & 0 deletions v3/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/superoo7/go-gecko/format"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions v3/v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down