added GetCoinValue function to coinGeckoAPI
This commit is contained in:
parent
bdad511738
commit
21ba0458e1
4
cake.go
4
cake.go
@ -218,7 +218,8 @@ func monthlyRewardOverview(lines []line) rewards {
|
||||
referral := make(map[time.Month]float64)
|
||||
airdrop := make(map[time.Month]float64)
|
||||
swapped := make(map[time.Month]float64)
|
||||
|
||||
// coinValue, err := GetCoinValue("bitcoin", "eur")
|
||||
// if err == nil {
|
||||
// loop through all lines
|
||||
for i := range lines {
|
||||
// filter out operations
|
||||
@ -238,6 +239,7 @@ func monthlyRewardOverview(lines []line) rewards {
|
||||
case "Swapped in":
|
||||
swapped[lines[i].Date.Month()] += lines[i].Amount
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
// get precision for specific coin
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
@ -17,10 +18,8 @@ func CheckStatusCoinGecko() bool {
|
||||
url := "https://api.coingecko.com/api/v3/ping"
|
||||
response, err := http.Get(url)
|
||||
if err == nil {
|
||||
// API is available, let's read the response
|
||||
data, _ := ioutil.ReadAll(response.Body)
|
||||
var status statusResponse
|
||||
json.Unmarshal(data, &status)
|
||||
readBody(response, &status)
|
||||
if status.GeckoSays == "(V3) To the Moon!" {
|
||||
// API is working as expected
|
||||
return true
|
||||
@ -29,6 +28,28 @@ func CheckStatusCoinGecko() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func GetCoinInfo() {
|
||||
|
||||
// GetCoinValue needs a comment
|
||||
func GetCoinValue(coinID string, fiat string) (float64, error) {
|
||||
// check if API is up
|
||||
if CheckStatusCoinGecko() == true {
|
||||
// https://api.coingecko.com/api/v3/simple/price?ids=defichain&vs_currencies=eur
|
||||
url := "https://api.coingecko.com/api/v3/simple/price?ids=" + coinID + "&vs_currencies=" + fiat
|
||||
response, err := http.Get(url)
|
||||
if err == nil {
|
||||
// there's no error, we got a response
|
||||
var r map[string]interface{}
|
||||
readBody(response, &r)
|
||||
// as the response will vary on coin and fiat we need to use interface{} here
|
||||
value := r[coinID].(map[string]interface{})
|
||||
return value[fiat].(float64), nil
|
||||
}
|
||||
return 0, fmt.Errorf("There was a problem with the response from Coingecko, Error is: %s", err.Error())
|
||||
}
|
||||
return 0, fmt.Errorf("The API of Coingecko seems to be down")
|
||||
}
|
||||
|
||||
func readBody(h *http.Response, i interface{}) {
|
||||
// API is available, let's read the response
|
||||
data, _ := ioutil.ReadAll(h.Body)
|
||||
json.Unmarshal(data, i)
|
||||
}
|
||||
|
9
go.mod
9
go.mod
@ -2,11 +2,4 @@ module git.nils.zone/nils/goCake
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
git.nils.zone/nils/prettify v0.0.4
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
||||
github.com/stretchr/testify v1.6.1
|
||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
|
||||
)
|
||||
require github.com/lucasb-eyer/go-colorful v1.0.3
|
||||
|
Loading…
Reference in New Issue
Block a user