2023-11-12 23:50:46 +00:00
|
|
|
package bitcoin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-12-28 16:33:13 +00:00
|
|
|
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"
|
|
|
|
|
2024-03-23 20:06:38 +00:00
|
|
|
type coinGeckoResponse struct {
|
2023-12-28 16:33:13 +00:00
|
|
|
MarketData struct {
|
|
|
|
CurrentPrice struct {
|
|
|
|
CAD int `json:"cad"`
|
|
|
|
} `json:"current_price"`
|
|
|
|
} `json:"market_data"`
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:06:38 +00:00
|
|
|
func (c *client) convertBTCToCAD(amount int) (int, error) {
|
|
|
|
coinGeckoData := &coinGeckoResponse{}
|
2023-12-28 16:33:13 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:06:38 +00:00
|
|
|
err = c.processResponse(res, coinGeckoData)
|
2023-12-28 16:33:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to process response: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:06:38 +00:00
|
|
|
return (amount * int(coinGeckoData.MarketData.CurrentPrice.CAD*1000)) / 100000000, nil // one BTC = one hundred million satoshi's
|
2023-12-28 16:33:13 +00:00
|
|
|
}
|