-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: include daily percentage variation
- Loading branch information
Showing
5 changed files
with
137 additions
and
70 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package coinapi | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/araujo88/bitcoin-price-bot-nostr/pkg/config" | ||
"github.com/araujo88/bitcoin-price-bot-nostr/pkg/responses" | ||
) | ||
|
||
const BASE_URL = "https://rest.coinapi.io/v1/" | ||
|
||
var API_KEY = config.GetDotEnvVariable("API_KEY") | ||
|
||
// makeRequest handles the common operations of making an HTTP request to the CoinAPI | ||
func makeRequest(url string) ([]byte, error) { | ||
client := &http.Client{} | ||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating request: %w", err) | ||
} | ||
|
||
req.Header.Set("X-CoinAPI-Key", API_KEY) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("executing request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
response, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading response: %w", err) | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// FetchRate retrieves the current rate of Bitcoin in the specified currency | ||
func FetchRate(currency string) (float64, error) { | ||
url := BASE_URL + "exchangerate/BTC/" + currency | ||
response, err := makeRequest(url) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var message responses.CurrencyRate | ||
if err := json.Unmarshal(response, &message); err != nil { | ||
return 0, fmt.Errorf("unmarshal response: %w", err) | ||
} | ||
|
||
return message.Rate, nil | ||
} | ||
|
||
// FetchDailyVariation retrieves the daily variation in percentage of the Bitcoin price for a specified currency | ||
func FetchDailyVariation(currency string) (float64, error) { | ||
url := fmt.Sprintf("%sohlcv/BTC/%s/latest?period_id=1DAY&limit=1", BASE_URL, currency) | ||
response, err := makeRequest(url) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var data []responses.OHLCVData | ||
if err := json.Unmarshal(response, &data); err != nil { | ||
return 0, fmt.Errorf("unmarshal OHLCV data: %w", err) | ||
} | ||
|
||
if len(data) > 0 { | ||
ohlcv := data[0] | ||
variation := ((ohlcv.PriceClose - ohlcv.PriceOpen) / ohlcv.PriceOpen) * 100 | ||
return variation, nil | ||
} | ||
|
||
return 0, fmt.Errorf("no data found") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package responses | ||
|
||
type CurrencyRate struct { | ||
Time string `json:"time"` | ||
AssetIdBase string `json:"asset_id_base"` | ||
AssetIdQuote string `json:"asset_id_quote"` | ||
Rate float64 `json:"rate"` | ||
} | ||
|
||
type OHLCVData struct { | ||
TimePeriodStart string `json:"time_period_start"` | ||
TimePeriodEnd string `json:"time_period_end"` | ||
TimeOpen string `json:"time_open"` | ||
TimeClose string `json:"time_close"` | ||
PriceOpen float64 `json:"price_open"` | ||
PriceHigh float64 `json:"price_high"` | ||
PriceLow float64 `json:"price_low"` | ||
PriceClose float64 `json:"price_close"` | ||
VolumeTraded float64 `json:"volume_traded"` | ||
TradesCount int `json:"trades_count"` | ||
} |