Update coingecko for formatting, sorting by volume
This commit is contained in:
parent
06347dfcd6
commit
a996bff772
1 changed files with 53 additions and 19 deletions
|
@ -26,13 +26,35 @@ type CoinList struct {
|
|||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type MarketChart struct {
|
||||
Prices [][]float32 `json:"prices"`
|
||||
MarketCaps [][]float32 `json:"market_caps"`
|
||||
TotalVolumes [][]float32 `json:"total_volumes"`
|
||||
type Market struct {
|
||||
Id string `json:"id"`
|
||||
Symbol string `json:"symbol"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
CurrentPrice float64 `json:"current_price"`
|
||||
MarketCap float64 `json:"market_cap"`
|
||||
MarketCapRank int16 `json:"market_cap_rank"`
|
||||
FullyDilutedValuation string `json:"fully_diluted_valuation"`
|
||||
TotalVolume float64 `json:"total_volume"`
|
||||
High24 float64 `json:"high_24h"`
|
||||
Low24 float64 `json:"low_24h"`
|
||||
PriceChange24h float64 `json:"price_change_24h"`
|
||||
PriceChangePercentage24h float64 `json:"price_change_percentage_24h"`
|
||||
MarketCapChange24h float64 `json:"market_cap_change_24h"`
|
||||
MarketCapChangePercentage24h float64 `json:"market_cap_change_percentage_24h"`
|
||||
CirculatingSupply float64 `json:"circulating_supply"`
|
||||
TotalSupply float64 `json:"total_supply"`
|
||||
MaxSupply float64 `json:"max_supply"`
|
||||
ATH float64 `json:"ath"`
|
||||
ATHChangePercentage float64 `json:"ath_change_percentage"`
|
||||
ATHDate string `json:"ath_date"`
|
||||
ATL float64 `json:"atl"`
|
||||
ATLChangePercentage float64 `json:"atl_change_percentage"`
|
||||
ATLDate string `json:"atl_date"`
|
||||
LastUpdated string `json:"last_updated"`
|
||||
}
|
||||
|
||||
func getId(query string) string {
|
||||
func getIds(query string) []string {
|
||||
req, _ := http.NewRequest("GET", "https://api.coingecko.com/api/v3/coins/list", nil)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
client := &http.Client{}
|
||||
|
@ -41,31 +63,43 @@ func getId(query string) string {
|
|||
|
||||
var coinList *[]CoinList
|
||||
json.NewDecoder(resp.Body).Decode(&coinList)
|
||||
var returnList []string
|
||||
for _, coin := range *coinList {
|
||||
// get the coingecko id for the requested coin
|
||||
// get the coingecko ids for the requested coin
|
||||
if coin.Symbol == query {
|
||||
return coin.Id
|
||||
returnList = append(returnList, coin.Id)
|
||||
}
|
||||
}
|
||||
if len(returnList) > 0 {
|
||||
return returnList
|
||||
} else {
|
||||
// fallback to bitcoin
|
||||
return "bitcoin"
|
||||
returnList = append(returnList, "bitcoin")
|
||||
return returnList
|
||||
}
|
||||
}
|
||||
|
||||
func getData(query string) string {
|
||||
url := fmt.Sprintf("https://api.coingecko.com/api/v3/coins/%s/market_chart?vs_currency=usd&days=1", query)
|
||||
func getData(query []string) string {
|
||||
symbol := strings.Join(query, ",")
|
||||
url := fmt.Sprintf("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=%s&order=market_cap_desc&per_page=1&page=1&sparkline=false", symbol)
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
client := &http.Client{}
|
||||
resp, _ := client.Do(req)
|
||||
defer resp.Body.Close()
|
||||
|
||||
var coin *MarketChart
|
||||
var coin []*Market
|
||||
json.NewDecoder(resp.Body).Decode(&coin)
|
||||
|
||||
latestPrice := coin.Prices[len(coin.Prices)-1][1]
|
||||
marketCap := coin.MarketCaps[len(coin.MarketCaps)-1][1]
|
||||
totalVol := coin.TotalVolumes[len(coin.TotalVolumes)-1][1]
|
||||
return fmt.Sprintf("%s -- Price: $%f, Market Cap: $%.0f, 24hr Volume: $%.0f", query, latestPrice, marketCap, totalVol)
|
||||
c := coin[0]
|
||||
const output = `%s (%s)
|
||||
Current Price: $%f
|
||||
Market Ca: $%.0f
|
||||
24 Hour Volume: $%.0f
|
||||
24 Hour High: $%f
|
||||
24 Hour Low: $%f
|
||||
All Time High: $%f
|
||||
All Time Low: $%f`
|
||||
return fmt.Sprintf(output, c.Id, c.Symbol, c.CurrentPrice, c.MarketCap, c.TotalVolume, c.High24, c.Low24, c.ATH, c.ATL)
|
||||
}
|
||||
|
||||
// Commands supported:
|
||||
|
@ -76,8 +110,8 @@ func (e *Service) Commands(cli types.MatrixClient) []types.Command {
|
|||
{
|
||||
Path: []string{"cc"},
|
||||
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
|
||||
coinId := getId(strings.Join(args, ""))
|
||||
output := getData(coinId)
|
||||
coinIds := getIds(strings.Join(args, ""))
|
||||
output := getData(coinIds)
|
||||
return &mevt.MessageEventContent{
|
||||
MsgType: mevt.MsgNotice,
|
||||
Body: output,
|
||||
|
|
Reference in a new issue