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}
}
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{
Client: d.client,
Asset: asset,
Format: glassnodeapi.FormatJSON,
Since: options.Since,
Until: options.Until,
Interval: options.Interval,
Category: category,
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)
if err != nil {
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
func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency string) (float64, error) {
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 {
return 0, err
@ -67,9 +61,14 @@ func (d *DataSource) QueryFuturesOpenInterest(ctx context.Context, currency stri
// https://docs.glassnode.com/api/market#market-cap
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) {
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 {
return 0, err

View File

@ -13,9 +13,10 @@ type Request struct {
Asset string `param:"a,required,query"`
Since *time.Time `param:"s,query,seconds"`
Until *time.Time `param:"u,query,seconds"`
Interval Interval `param:"i,query"`
Format Format `param:"f,query"`
TimestampFormat string `param:"timestamp_format,query"`
Interval *Interval `param:"i,query"`
Format *Format `param:"f,query" default:"JSON"`
Currency *string `param:"c,query"`
TimestampFormat *string `param:"timestamp_format,query"`
Category string `param:"category,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 {
r.Interval = Interval
r.Interval = &Interval
return r
}
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
}
func (r *Request) SetTimestampFormat(TimestampFormat string) *Request {
r.TimestampFormat = TimestampFormat
r.TimestampFormat = &TimestampFormat
return r
}
@ -86,7 +91,8 @@ func (r *Request) GetQueryParameters() (url.Values, error) {
} else {
}
// check Interval field -> json key i
Interval := r.Interval
if r.Interval != nil {
Interval := *r.Interval
// TEMPLATE check-valid-values
switch Interval {
@ -101,8 +107,11 @@ func (r *Request) GetQueryParameters() (url.Values, error) {
// assign parameter of Interval
params["i"] = Interval
} else {
}
// check Format field -> json key f
Format := r.Format
if r.Format != nil {
Format := *r.Format
// TEMPLATE check-valid-values
switch Format {
@ -117,11 +126,28 @@ func (r *Request) GetQueryParameters() (url.Values, error) {
// assign parameter of Format
params["f"] = Format
} else {
Format := "JSON"
// assign parameter of Format
params["f"] = Format
}
// check Currency field -> json key c
if r.Currency != nil {
Currency := *r.Currency
// assign parameter of Currency
params["c"] = Currency
} else {
}
// check TimestampFormat field -> json key timestamp_format
TimestampFormat := r.TimestampFormat
if r.TimestampFormat != nil {
TimestampFormat := *r.TimestampFormat
// assign parameter of TimestampFormat
params["timestamp_format"] = TimestampFormat
} else {
}
query := url.Values{}
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
}