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"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MarketChart struct {
|
type Market struct {
|
||||||
Prices [][]float32 `json:"prices"`
|
Id string `json:"id"`
|
||||||
MarketCaps [][]float32 `json:"market_caps"`
|
Symbol string `json:"symbol"`
|
||||||
TotalVolumes [][]float32 `json:"total_volumes"`
|
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, _ := http.NewRequest("GET", "https://api.coingecko.com/api/v3/coins/list", nil)
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
@ -41,31 +63,43 @@ func getId(query string) string {
|
||||||
|
|
||||||
var coinList *[]CoinList
|
var coinList *[]CoinList
|
||||||
json.NewDecoder(resp.Body).Decode(&coinList)
|
json.NewDecoder(resp.Body).Decode(&coinList)
|
||||||
|
var returnList []string
|
||||||
for _, coin := range *coinList {
|
for _, coin := range *coinList {
|
||||||
// get the coingecko id for the requested coin
|
// get the coingecko ids for the requested coin
|
||||||
if coin.Symbol == query {
|
if coin.Symbol == query {
|
||||||
return coin.Id
|
returnList = append(returnList, coin.Id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// fallback to bitcoin
|
if len(returnList) > 0 {
|
||||||
return "bitcoin"
|
return returnList
|
||||||
|
} else {
|
||||||
|
// fallback to bitcoin
|
||||||
|
returnList = append(returnList, "bitcoin")
|
||||||
|
return returnList
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getData(query string) string {
|
func getData(query []string) string {
|
||||||
url := fmt.Sprintf("https://api.coingecko.com/api/v3/coins/%s/market_chart?vs_currency=usd&days=1", query)
|
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, _ := http.NewRequest("GET", url, nil)
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, _ := client.Do(req)
|
resp, _ := client.Do(req)
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var coin *MarketChart
|
var coin []*Market
|
||||||
json.NewDecoder(resp.Body).Decode(&coin)
|
json.NewDecoder(resp.Body).Decode(&coin)
|
||||||
|
c := coin[0]
|
||||||
latestPrice := coin.Prices[len(coin.Prices)-1][1]
|
const output = `%s (%s)
|
||||||
marketCap := coin.MarketCaps[len(coin.MarketCaps)-1][1]
|
Current Price: $%f
|
||||||
totalVol := coin.TotalVolumes[len(coin.TotalVolumes)-1][1]
|
Market Ca: $%.0f
|
||||||
return fmt.Sprintf("%s -- Price: $%f, Market Cap: $%.0f, 24hr Volume: $%.0f", query, latestPrice, marketCap, totalVol)
|
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:
|
// Commands supported:
|
||||||
|
@ -76,8 +110,8 @@ func (e *Service) Commands(cli types.MatrixClient) []types.Command {
|
||||||
{
|
{
|
||||||
Path: []string{"cc"},
|
Path: []string{"cc"},
|
||||||
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
|
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
|
||||||
coinId := getId(strings.Join(args, ""))
|
coinIds := getIds(strings.Join(args, ""))
|
||||||
output := getData(coinId)
|
output := getData(coinIds)
|
||||||
return &mevt.MessageEventContent{
|
return &mevt.MessageEventContent{
|
||||||
MsgType: mevt.MsgNotice,
|
MsgType: mevt.MsgNotice,
|
||||||
Body: output,
|
Body: output,
|
||||||
|
|
Reference in a new issue