diff --git a/providers/bitcoin/README.md b/providers/bitcoin/README.md index 6abbcde..56cbd1a 100644 --- a/providers/bitcoin/README.md +++ b/providers/bitcoin/README.md @@ -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 \ No newline at end of file +* *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 \ No newline at end of file diff --git a/providers/bitcoin/client.go b/providers/bitcoin/client.go index a2568e8..8479504 100644 --- a/providers/bitcoin/client.go +++ b/providers/bitcoin/client.go @@ -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 } diff --git a/providers/bitcoin/fiat.go b/providers/bitcoin/fiat.go index 184f283..a8c970e 100644 --- a/providers/bitcoin/fiat.go +++ b/providers/bitcoin/fiat.go @@ -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) } diff --git a/providers/bitcoin/providerImpl.go b/providers/bitcoin/providerImpl.go index 3ea9d01..230d514 100644 --- a/providers/bitcoin/providerImpl.go +++ b/providers/bitcoin/providerImpl.go @@ -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 }