bbgo_origin/pkg/datasource/glassnode/datasource.go

79 lines
1.7 KiB
Go
Raw Permalink 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 17:29:29 +00:00
func (d *DataSource) Query(ctx context.Context, category, metric, asset string, options QueryOptions) (glassnodeapi.DataSlice, error) {
2022-06-24 16:49:52 +00:00
req := glassnodeapi.Request{
2022-06-24 17:29:29 +00:00
Client: d.client,
Asset: asset,
Since: options.Since,
Until: options.Until,
Interval: options.Interval,
2022-06-24 16:49:52 +00:00
Category: category,
Metric: metric,
}
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()
2022-06-24 17:29:29 +00:00
since := until.Add(-24 * time.Hour)
2022-06-24 16:49:52 +00:00
2022-06-24 17:29:29 +00:00
options := QueryOptions{
Since: &since,
Until: &until,
}
resp, err := d.Query(ctx, "derivatives", "futures_open_interest_sum", currency, options)
2022-06-24 16:49:52 +00:00
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()
2022-06-24 17:29:29 +00:00
since := until.Add(-24 * time.Hour)
options := QueryOptions{
Since: &since,
Until: &until,
}
2022-06-24 16:49:52 +00:00
2022-06-24 17:29:29 +00:00
resp, err := d.Query(ctx, "market", "marketcap_usd", currency, options)
2022-04-25 08:44:57 +00:00
if err != nil {
return 0, err
}
return resp.Last().Value, nil
}