fix fiat conversion - requires cg api key
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		@@ -6,3 +6,4 @@ A provider for bitcoin, backed by blockstream.info to query address balance and
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
* *bitcoin_address_[0...]* - A series of bitcoin addresses with a suffix of _0 and each additional address counting up.
 | 
					* *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
 | 
				
			||||||
@@ -17,6 +17,7 @@ const apiBaseURL = "https://blockstream.info/api/"
 | 
				
			|||||||
type client struct {
 | 
					type client struct {
 | 
				
			||||||
	httpClient      *http.Client
 | 
						httpClient      *http.Client
 | 
				
			||||||
	transport       *http.Transport
 | 
						transport       *http.Transport
 | 
				
			||||||
 | 
						coinGeckoApiKey string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Send an HTTP GET request, and return the processed response
 | 
					// 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
 | 
					// newClient is the factory function for clients
 | 
				
			||||||
func newClient() (*client, error) {
 | 
					func newClient(coinGeckoApiKey string) *client {
 | 
				
			||||||
	transport := &http.Transport{
 | 
						transport := &http.Transport{
 | 
				
			||||||
		ResponseHeaderTimeout: 5 * time.Second,
 | 
							ResponseHeaderTimeout: 5 * time.Second,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -74,6 +75,7 @@ func newClient() (*client, error) {
 | 
				
			|||||||
	c := &client{
 | 
						c := &client{
 | 
				
			||||||
		httpClient:      httpClient,
 | 
							httpClient:      httpClient,
 | 
				
			||||||
		transport:       transport,
 | 
							transport:       transport,
 | 
				
			||||||
 | 
							coinGeckoApiKey: coinGeckoApiKey,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return c, nil
 | 
						return c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,7 +5,7 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"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 {
 | 
					type coinGeckoResponse struct {
 | 
				
			||||||
	MarketData struct {
 | 
						MarketData struct {
 | 
				
			||||||
@@ -18,7 +18,7 @@ type coinGeckoResponse struct {
 | 
				
			|||||||
func (c *client) convertBTCToCAD(amount int) (int, error) {
 | 
					func (c *client) convertBTCToCAD(amount int) (int, error) {
 | 
				
			||||||
	coinGeckoData := &coinGeckoResponse{}
 | 
						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 {
 | 
						if err != nil {
 | 
				
			||||||
		return 0, fmt.Errorf("failed to create new GET request: %v", err)
 | 
							return 0, fmt.Errorf("failed to create new GET request: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,14 +14,12 @@ type Provider struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *Provider) Name() string {
 | 
					func (p *Provider) Name() string {
 | 
				
			||||||
	return "Bitcoin - Blockstream.info"
 | 
						return "Bitcoin - Blockstream.info / CoinGecko"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Configures the provider for usage via environment variables and persistentData
 | 
					// Configures the provider for usage via environment variables and persistentData
 | 
				
			||||||
// If an error is returned, the provider will not be used
 | 
					// If an error is returned, the provider will not be used
 | 
				
			||||||
func (p *Provider) Configure() error {
 | 
					func (p *Provider) Configure() error {
 | 
				
			||||||
	var err error
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Load environment variables in continous series with suffix starting at 0
 | 
						// Load environment variables in continous series with suffix starting at 0
 | 
				
			||||||
	// Multiple addresses can be configured, (eg _1, _2)
 | 
						// Multiple addresses can be configured, (eg _1, _2)
 | 
				
			||||||
	// As soon as the series is interrupted, we assume we're done
 | 
						// 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")
 | 
						p.ynabAccountID = os.Getenv("bitcoin_ynab_account")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Create new HTTP client
 | 
						// Create new HTTP client
 | 
				
			||||||
	p.client, err = newClient()
 | 
						p.client = newClient(os.Getenv("bitcoin_coingecko_api_key"))
 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("failed to create new bitcoin client: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user