bbgo_origin/pkg/datasource/glassnode/datasource.go

80 lines
1.7 KiB
Go
Raw Normal View History

2022-04-25 08:44:57 +00:00
package glassnode
import (
"context"
"time"
2022-04-26 10:51:06 +00:00
"github.com/c9s/bbgo/pkg/datasource/glassnode/glassnodeapi"
2022-04-25 08:44:57 +00:00
)
2022-04-26 10:51:06 +00:00
type DataSource struct {
2022-04-25 08:44:57 +00:00
client *glassnodeapi.RestClient
}
2022-04-26 10:51:06 +00:00
func New(apiKey string) *DataSource {
2022-04-25 08:44:57 +00:00
client := glassnodeapi.NewRestClient()
client.Auth(apiKey)
2022-04-26 10:51:06 +00:00
return &DataSource{client: client}
2022-04-25 08:44:57 +00:00
}
2022-06-24 16:49:52 +00:00
func (d *DataSource) Query(ctx context.Context, category, metric, asset, interval string, since, until *time.Time) (glassnodeapi.DataSlice, error) {
req := glassnodeapi.Request{
2022-04-26 10:51:06 +00:00
Client: d.client,
2022-06-24 16:49:52 +00:00
Asset: asset,
Format: glassnodeapi.FormatJSON,
Category: category,
Metric: metric,
}
if since != nil {
req.Since = since
}
if until != nil {
req.Until = until
}
if interval != "" {
req.SetInterval(glassnodeapi.Interval(interval))
2022-04-25 17:25:42 +00:00
}
resp, err := req.Do(ctx)
2022-06-24 16:49:52 +00:00
if err != nil {
return nil, err
}
return glassnodeapi.DataSlice(resp), nil
}
// query last futures open interest
// 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)
resp, err := d.Query(ctx, "derivatives", "futures_open_interest_sum", currency, "24h", &since, &until)
2022-04-25 17:25:42 +00:00
if err != nil {
return 0, err
}
return resp.Last().Value, nil
}
// query last market cap in usd
// https://docs.glassnode.com/api/market#market-cap
2022-04-26 10:51:06 +00:00
func (d *DataSource) QueryMarketCapInUSD(ctx context.Context, currency string) (float64, error) {
2022-06-24 16:49:52 +00:00
until := time.Now()
since := until.Add(-25 * time.Hour)
resp, err := d.Query(ctx, "market", "marketcap_usd", currency, "24h", &since, &until)
2022-04-25 08:44:57 +00:00
if err != nil {
return 0, err
}
return resp.Last().Value, nil
}