2023-11-12 23:50:46 +00:00
|
|
|
package bitcoin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-12-28 16:33:13 +00:00
|
|
|
/* // CoinConvert BTC to CAD converter
|
2023-11-12 23:50:46 +00:00
|
|
|
// example: https://api.coinconvert.net/convert/btc/cad?amount=1
|
|
|
|
const fiatConvertURL = "https://api.coinconvert.net/convert/btc/cad"
|
|
|
|
|
|
|
|
type FiatConversion struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
BTC int `json:"BTC"`
|
|
|
|
CAD float64 `json:"CAD"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BTC to CAD FIAT conversion - accepts an
|
|
|
|
// amount in satoshi's and returns a CAD amount * 1000
|
2023-11-13 04:40:00 +00:00
|
|
|
func (c *client) ConvertBTCToCAD(amount int) (int, error) {
|
2023-11-12 23:50:46 +00:00
|
|
|
fiatConversion := &FiatConversion{}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fiatConvertURL+"?amount=1", 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fiatConversion.Status != "success" {
|
|
|
|
return 0, fmt.Errorf("fiat conversion status was '%s' but expected 'success'", fiatConversion.Status)
|
|
|
|
}
|
|
|
|
return (amount * int(fiatConversion.CAD*1000)) / 100000000, nil // one BTC = one hundred million satoshi's
|
|
|
|
}
|
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"
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|