ynab-portfolio-monitor/providers/bitcoin/fiat.go

38 lines
1002 B
Go
Raw Normal View History

2023-11-12 23:50:46 +00:00
package bitcoin
import (
"fmt"
"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"
type FiatConversion struct {
MarketData struct {
CurrentPrice struct {
CAD int `json:"cad"`
} `json:"current_price"`
} `json:"market_data"`
}
func (c *client) ConvertBTCToCAD(amount int) (int, error) {
fiatConversion := &FiatConversion{}
req, err := http.NewRequest("GET", fiatConvertURL, 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, fiatConversion)
if err != nil {
return 0, fmt.Errorf("failed to process response: %v", err)
}
return (amount * int(fiatConversion.MarketData.CurrentPrice.CAD*1000)) / 100000000, nil // one BTC = one hundred million satoshi's
}