package config import ( "context" "encoding/json" "time" "github.com/shopspring/decimal" "git.lehouerou.net/laurent/sorare/graphql" ) type Config struct { c *graphql.Client exchangeRate *graphql.Query[exchangeRate, graphql.EmptyParams] } func NewConfig(c *graphql.Client) *Config { return &Config{ c: c, exchangeRate: graphql.NewQuery[exchangeRate, graphql.EmptyParams]( c, "exchangeRate", []string{"config"}, ), } } type exchangeRate struct { Id graphql.Id `graphql:"id"` Time time.Time `graphql:"time"` Rates json.RawMessage `graphql:"rates"` } type RateForCurrency struct { Eur decimal.Decimal `json:"eur"` Usd decimal.Decimal `json:"usd"` Gbp decimal.Decimal `json:"gbp"` } type Rates struct { Eth RateForCurrency `json:"eth"` Wei RateForCurrency `json:"wei"` } type ExchangeRate struct { Id string `json:"id"` Time time.Time `json:"time"` Rates Rates `json:"rates"` } func parseRates(rates json.RawMessage) Rates { var r Rates err := json.Unmarshal(rates, &r) if err != nil { panic(err) } return r } func (c *Config) ExchangeRate(ctx context.Context) (*ExchangeRate, error) { raw, err := c.exchangeRate.Get(ctx, graphql.EmptyParams{}) if err != nil { return nil, err } return &ExchangeRate{ Id: raw.Id.String(), Time: raw.Time, Rates: parseRates(raw.Rates), }, nil }