glassnode: add QueryOptions

This commit is contained in:
なるみ 2022-06-25 01:29:29 +08:00
parent 99d6c0550d
commit cbb3effc42
4 changed files with 91 additions and 51 deletions

View File

@ -18,28 +18,18 @@ func New(apiKey string) *DataSource {
return &DataSource{client: client} return &DataSource{client: client}
} }
func (d *DataSource) Query(ctx context.Context, category, metric, asset, interval string, since, until *time.Time) (glassnodeapi.DataSlice, error) { func (d *DataSource) Query(ctx context.Context, category, metric, asset string, options QueryOptions) (glassnodeapi.DataSlice, error) {
req := glassnodeapi.Request{ req := glassnodeapi.Request{
Client: d.client, Client: d.client,
Asset: asset, Asset: asset,
Format: glassnodeapi.FormatJSON, Since: options.Since,
Until: options.Until,
Interval: options.Interval,
Category: category, Category: category,
Metric: metric, Metric: metric,
} }
if since != nil {
req.Since = since
}
if until != nil {
req.Until = until
}
if interval != "" {
req.SetInterval(glassnodeapi.Interval(interval))
}
resp, err := req.Do(ctx) resp, err := req.Do(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -52,9 +42,13 @@ func (d *DataSource) Query(ctx context.Context, category, metric, asset, interva
// https://docs.glassnode.com/api/derivatives#futures-open-interest // https://docs.glassnode.com/api/derivatives#futures-open-interest
func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency string) (float64, error) { func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency string) (float64, error) {
until := time.Now() until := time.Now()
since := until.Add(-25 * time.Hour) since := until.Add(-24 * time.Hour)
resp, err := d.Query(ctx, "derivatives", "futures_open_interest_sum", currency, "24h", &since, &until) options := QueryOptions{
Since: &since,
Until: &until,
}
resp, err := d.Query(ctx, "derivatives", "futures_open_interest_sum", currency, options)
if err != nil { if err != nil {
return 0, err return 0, err
@ -67,9 +61,14 @@ func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency stri
// https://docs.glassnode.com/api/market#market-cap // https://docs.glassnode.com/api/market#market-cap
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) { func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) {
until := time.Now() until := time.Now()
since := until.Add(-25 * time.Hour) since := until.Add(-24 * time.Hour)
resp, err := d.Query(ctx, "market", "marketcap_usd", currency, "24h", &since, &until) options := QueryOptions{
Since: &since,
Until: &until,
}
resp, err := d.Query(ctx, "market", "marketcap_usd", currency, options)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -13,9 +13,10 @@ type Request struct {
Asset string `param:"a,required,query"` Asset string `param:"a,required,query"`
Since *time.Time `param:"s,query,seconds"` Since *time.Time `param:"s,query,seconds"`
Until *time.Time `param:"u,query,seconds"` Until *time.Time `param:"u,query,seconds"`
Interval Interval `param:"i,query"` Interval *Interval `param:"i,query"`
Format Format `param:"f,query"` Format *Format `param:"f,query" default:"JSON"`
TimestampFormat string `param:"timestamp_format,query"` Currency *string `param:"c,query"`
TimestampFormat *string `param:"timestamp_format,query"`
Category string `param:"category,slug"` Category string `param:"category,slug"`
Metric string `param:"metric,slug"` Metric string `param:"metric,slug"`

View File

@ -29,17 +29,22 @@ func (r *Request) SetUntil(Until time.Time) *Request {
} }
func (r *Request) SetInterval(Interval Interval) *Request { func (r *Request) SetInterval(Interval Interval) *Request {
r.Interval = Interval r.Interval = &Interval
return r return r
} }
func (r *Request) SetFormat(Format Format) *Request { func (r *Request) SetFormat(Format Format) *Request {
r.Format = Format r.Format = &Format
return r
}
func (r *Request) SetCurrency(Currency string) *Request {
r.Currency = &Currency
return r return r
} }
func (r *Request) SetTimestampFormat(TimestampFormat string) *Request { func (r *Request) SetTimestampFormat(TimestampFormat string) *Request {
r.TimestampFormat = TimestampFormat r.TimestampFormat = &TimestampFormat
return r return r
} }
@ -86,42 +91,63 @@ func (r *Request) GetQueryParameters() (url.Values, error) {
} else { } else {
} }
// check Interval field -> json key i // check Interval field -> json key i
Interval := r.Interval if r.Interval != nil {
Interval := *r.Interval
// TEMPLATE check-valid-values // TEMPLATE check-valid-values
switch Interval { switch Interval {
case Interval1h, Interval24h, Interval10m, Interval1w, Interval1m: case Interval1h, Interval24h, Interval10m, Interval1w, Interval1m:
params["i"] = Interval
default:
return nil, fmt.Errorf("i value %v is invalid", Interval)
}
// END TEMPLATE check-valid-values
// assign parameter of Interval
params["i"] = Interval params["i"] = Interval
} else {
default:
return nil, fmt.Errorf("i value %v is invalid", Interval)
} }
// END TEMPLATE check-valid-values
// assign parameter of Interval
params["i"] = Interval
// check Format field -> json key f // check Format field -> json key f
Format := r.Format if r.Format != nil {
Format := *r.Format
// TEMPLATE check-valid-values // TEMPLATE check-valid-values
switch Format { switch Format {
case FormatJSON, FormatCSV: case FormatJSON, FormatCSV:
params["f"] = Format
default:
return nil, fmt.Errorf("f value %v is invalid", Format)
}
// END TEMPLATE check-valid-values
// assign parameter of Format
params["f"] = Format params["f"] = Format
} else {
Format := "JSON"
default: // assign parameter of Format
return nil, fmt.Errorf("f value %v is invalid", Format) params["f"] = Format
} }
// END TEMPLATE check-valid-values // check Currency field -> json key c
if r.Currency != nil {
Currency := *r.Currency
// assign parameter of Format // assign parameter of Currency
params["f"] = Format params["c"] = Currency
} else {
}
// check TimestampFormat field -> json key timestamp_format // check TimestampFormat field -> json key timestamp_format
TimestampFormat := r.TimestampFormat if r.TimestampFormat != nil {
TimestampFormat := *r.TimestampFormat
// assign parameter of TimestampFormat // assign parameter of TimestampFormat
params["timestamp_format"] = TimestampFormat params["timestamp_format"] = TimestampFormat
} else {
}
query := url.Values{} query := url.Values{}
for _k, _v := range params { for _k, _v := range params {

View File

@ -0,0 +1,14 @@
package glassnode
import (
"time"
"github.com/c9s/bbgo/pkg/datasource/glassnode/glassnodeapi"
)
type QueryOptions struct {
Since *time.Time
Until *time.Time
Interval *glassnodeapi.Interval
Currency *string
}