All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			
		
			
				
	
	
		
			38 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package bitcoin
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"net/http"
 | 
						|
)
 | 
						|
 | 
						|
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 {
 | 
						|
		CurrentPrice struct {
 | 
						|
			CAD int `json:"cad"`
 | 
						|
		} `json:"current_price"`
 | 
						|
	} `json:"market_data"`
 | 
						|
}
 | 
						|
 | 
						|
func (c *client) convertBTCToCAD(amount int) (int, error) {
 | 
						|
	coinGeckoData := &coinGeckoResponse{}
 | 
						|
 | 
						|
	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)
 | 
						|
	}
 | 
						|
 | 
						|
	res, err := c.httpClient.Do(req)
 | 
						|
	if err != nil {
 | 
						|
		return 0, fmt.Errorf("http GET request failed: %v", err)
 | 
						|
	}
 | 
						|
 | 
						|
	err = c.processResponse(res, coinGeckoData)
 | 
						|
	if err != nil {
 | 
						|
		return 0, fmt.Errorf("failed to process response: %v", err)
 | 
						|
	}
 | 
						|
 | 
						|
	return (amount * int(coinGeckoData.MarketData.CurrentPrice.CAD*1000)) / 100000000, nil // one BTC = one hundred million satoshi's
 | 
						|
}
 |