fix fiat conversion - requires cg api key
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2024-09-09 17:43:14 -06:00
parent c119f1f57c
commit 7689e3e1f2
4 changed files with 14 additions and 16 deletions

View File

@ -5,4 +5,5 @@ A provider for bitcoin, backed by blockstream.info to query address balance and
### Environment Variables
* *bitcoin_address_[0...]* - A series of bitcoin addresses with a suffix of _0 and each additional address counting up.
* *bitcoin_ynab_account* - The YNAB account ID used to keep track of Bitcoin balance
* *bitcoin_ynab_account* - The YNAB account ID used to keep track of Bitcoin balance
* *bitcoin_coingecko_api_key* - Required for fiat conversion, your addresses nor balances are not transmitted. Docs: https://docs.coingecko.com/reference/setting-up-your-api-key

View File

@ -15,8 +15,9 @@ const apiBaseURL = "https://blockstream.info/api/"
// endpoints. It holds the login credentials, http client/transport,
// rate limit information, and the login session timer.
type client struct {
httpClient *http.Client
transport *http.Transport
httpClient *http.Client
transport *http.Transport
coinGeckoApiKey string
}
// Send an HTTP GET request, and return the processed response
@ -61,7 +62,7 @@ func (c *client) processResponse(res *http.Response, out interface{}) error {
}
// newClient is the factory function for clients
func newClient() (*client, error) {
func newClient(coinGeckoApiKey string) *client {
transport := &http.Transport{
ResponseHeaderTimeout: 5 * time.Second,
}
@ -72,8 +73,9 @@ func newClient() (*client, error) {
// Create a new client
c := &client{
httpClient: httpClient,
transport: transport,
httpClient: httpClient,
transport: transport,
coinGeckoApiKey: coinGeckoApiKey,
}
return c, nil
return c
}

View File

@ -5,7 +5,7 @@ import (
"net/http"
)
const fiatConvertURL = "https://api.coingecko.com/api/v3/coins/bitcoin?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false"
const fiatConvertURL = "https://pro-api.coingecko.com/api/v3/coins/bitcoin?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false"
type coinGeckoResponse struct {
MarketData struct {
@ -18,7 +18,7 @@ type coinGeckoResponse struct {
func (c *client) convertBTCToCAD(amount int) (int, error) {
coinGeckoData := &coinGeckoResponse{}
req, err := http.NewRequest("GET", fiatConvertURL, nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s&x_cg_pro_api_key=%s", fiatConvertURL, c.coinGeckoApiKey), nil)
if err != nil {
return 0, fmt.Errorf("failed to create new GET request: %v", err)
}

View File

@ -14,14 +14,12 @@ type Provider struct {
}
func (p *Provider) Name() string {
return "Bitcoin - Blockstream.info"
return "Bitcoin - Blockstream.info / CoinGecko"
}
// Configures the provider for usage via environment variables and persistentData
// If an error is returned, the provider will not be used
func (p *Provider) Configure() error {
var err error
// Load environment variables in continous series with suffix starting at 0
// Multiple addresses can be configured, (eg _1, _2)
// As soon as the series is interrupted, we assume we're done
@ -39,10 +37,7 @@ func (p *Provider) Configure() error {
p.ynabAccountID = os.Getenv("bitcoin_ynab_account")
// Create new HTTP client
p.client, err = newClient()
if err != nil {
return fmt.Errorf("failed to create new bitcoin client: %v", err)
}
p.client = newClient(os.Getenv("bitcoin_coingecko_api_key"))
return nil
}