store selected currency + truncate decimals up to 2

pull/15/head
Miguel Mota 6 years ago
parent eb72c6bff9
commit e623c10dfd

@ -437,6 +437,10 @@ Action|Description
- A: The supported fiat currencies for conversion are `USD`, `EUR`, `GBP`, `CNY`, `HKD`, `JPY`, `KRW`, `NZD`, `CFH`, `MXN`, `AUD`, `IDR`, `RUB`, and `CAD`. The supported crypto currencies for conversion are `BTC` and `ETH`.
- Q: How do I save the selected currency to convert to?
- A: Press <kbd>ctrl</kbd>+<kbd>s</kbd> to save the selected currency to convert to.
- Q: The data isn't refreshing!
- A: The CoinMarketCap API has rate limits, so make sure to keep manual refreshes to a minimum. If you've hit the rate limit then wait about half an hour to be able to fetch the data again. Keep in mind the oinMarketCap updates prices every 5 minutes constant refreshes aren't necessary.

@ -15,6 +15,7 @@ var fileperm = os.FileMode(0644)
type config struct {
Shortcuts map[string]interface{} `toml:"shortcuts"`
Favorites map[string][]interface{} `toml:"favorites"`
Currency interface{} `toml:"currency"`
}
func (ct *Cointop) setupConfig() error {
@ -38,6 +39,10 @@ func (ct *Cointop) setupConfig() error {
if err != nil {
return err
}
err = ct.loadCurrencyFromConfig()
if err != nil {
return err
}
return nil
}
@ -137,9 +142,12 @@ func (ct *Cointop) configToToml() ([]byte, error) {
"symbols": favorites,
}
var currencyIfc interface{} = ct.currencyconversion
var inputs = &config{
Shortcuts: shortcutsIfcs,
Favorites: favoritesIfcs,
Currency: currencyIfc,
}
var b bytes.Buffer
@ -167,3 +175,10 @@ func (ct *Cointop) loadShortcutsFromConfig() error {
}
return nil
}
func (ct *Cointop) loadCurrencyFromConfig() error {
if currency, ok := ct.config.Currency.(string); ok {
ct.currencyconversion = strings.ToUpper(currency)
}
return nil
}

@ -1,7 +1,9 @@
package api
import (
"fmt"
"strconv"
"strings"
"sync"
apitypes "github.com/miguelmota/cointop/pkg/api/types"
@ -17,8 +19,6 @@ func New() *Service {
return &Service{}
}
// https://api.coinmarketcap.com/v1/ticker/?start=0&limit=0
func getLimitedCoinData(convert string, offset int) (map[string]apitypes.Coin, error) {
ret := make(map[string]apitypes.Coin)
max := 100
@ -51,8 +51,9 @@ func getLimitedCoinData(convert string, offset int) (map[string]apitypes.Coin, e
return ret, nil
}
// GetAllCoinData gets all coin data
func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, error) {
// GetAllCoinDataV2 gets all coin data
// V1 is currently better for fetching all the coins at once
func (s *Service) GetAllCoinDataV2(convert string) (map[string]apitypes.Coin, error) {
var wg sync.WaitGroup
ret := make(map[string]apitypes.Coin)
for i := 0; i < 5; i++ {
@ -72,6 +73,37 @@ func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, erro
return ret, nil
}
// GetAllCoinData get all coin data
func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, error) {
ret := make(map[string]apitypes.Coin)
coins, err := cmc.V1Tickers(0, convert)
if err != nil {
return ret, err
}
for _, v := range coins {
priceraw := v.Quotes[convert].Price
pricestr := fmt.Sprintf("%.2f", priceraw)
price, _ := strconv.ParseFloat(pricestr, 64)
ret[v.Symbol] = apitypes.Coin{
ID: strings.ToLower(v.Name),
Name: v.Name,
Symbol: v.Symbol,
Rank: v.Rank,
AvailableSupply: v.AvailableSupply,
TotalSupply: v.TotalSupply,
MarketCapUSD: v.Quotes[convert].MarketCap,
PriceUSD: price,
PercentChange1H: v.PercentChange1H,
PercentChange24H: v.PercentChange24H,
PercentChange7D: v.PercentChange7D,
USD24HVolume: v.Quotes[convert].Volume24H,
PriceBTC: 0,
LastUpdated: strconv.Itoa(v.LastUpdated),
}
}
return ret, nil
}
// GetCoinGraphData gets coin graph data
func (s *Service) GetCoinGraphData(coin string, start int64, end int64) (apitypes.CoinGraph, error) {
ret := apitypes.CoinGraph{}

@ -71,3 +71,23 @@ type MarketGraph struct {
MarketCapByAvailableSupply [][]float64 `json:"market_cap_by_available_supply"`
VolumeUSD [][]float64 `json:"volume_usd"`
}
// V1Ticker struct
type V1Ticker struct {
ID string `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Rank int `json:"rank,string"`
PriceUSD float64 `json:"price_usd,string"`
PriceBTC float64 `json:"price_btc,string"`
USD24HVolume float64 `json:"24h_volume_usd,string"`
MarketCapUSD float64 `json:"market_cap_usd,string"`
AvailableSupply float64 `json:"available_supply,string"`
TotalSupply float64 `json:"total_supply,string"`
PercentChange1H float64 `json:"percent_change_1h,string"`
PercentChange24H float64 `json:"percent_change_24h,string"`
PercentChange7D float64 `json:"percent_change_7d,string"`
LastUpdated int `json:"last_updated,string"`
Quotes map[string]*TickerQuote `json:"quotes"`
}

@ -0,0 +1,63 @@
package coinmarketcap
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/miguelmota/cointop/pkg/cmc/types"
)
// V1Tickers get information about all coins listed in Coin Market Cap
func V1Tickers(limit int, convert string) (map[string]*types.V1Ticker, error) {
var params []string
if limit >= 0 {
params = append(params, fmt.Sprintf("limit=%v", limit))
}
if convert != "" {
params = append(params, fmt.Sprintf("convert=%v", convert))
}
baseURL := "https://api.coinmarketcap.com/v1"
url := fmt.Sprintf("%s/ticker?%s", baseURL, strings.Join(params, "&"))
resp, err := makeReq(url)
var data []*types.V1Ticker
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
var mapstring []map[string]interface{}
err = json.Unmarshal(resp, &mapstring)
if err != nil {
return nil, err
}
// creating map from the array
allCoins := make(map[string]*types.V1Ticker)
for i := 0; i < len(data); i++ {
allCoins[data[i].ID] = data[i]
}
for _, item := range mapstring {
id, _ := item["id"].(string)
priceifc := item[fmt.Sprintf("price_%s", strings.ToLower(convert))]
pricestr, _ := priceifc.(string)
price, _ := strconv.ParseFloat(pricestr, 64)
marketcapifc := item[fmt.Sprintf("market_cap_%s", strings.ToLower(convert))]
marketcapstr, _ := marketcapifc.(string)
marketcap, _ := strconv.ParseFloat(marketcapstr, 64)
volumeifc := item[fmt.Sprintf("24h_volume_%s", strings.ToLower(convert))]
volumestr, _ := volumeifc.(string)
volume, _ := strconv.ParseFloat(volumestr, 64)
quotes := &types.TickerQuote{
Price: price,
Volume24H: volume,
MarketCap: marketcap,
}
allCoins[id].Quotes = map[string]*types.TickerQuote{}
allCoins[id].Quotes[strings.ToUpper(convert)] = quotes
}
return allCoins, nil
}

@ -0,0 +1,14 @@
package coinmarketcap
import "testing"
func TestV1Tickers(t *testing.T) {
coins, err := V1Tickers(10, "EUR")
if err != nil {
t.FailNow()
}
if len(coins) != 10 {
t.FailNow()
}
}
Loading…
Cancel
Save