diff --git a/rewards.go b/cake.go similarity index 76% rename from rewards.go rename to cake.go index c6a11ee..ecca2dd 100644 --- a/rewards.go +++ b/cake.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "log" + "math" "net/http" "runtime" "strconv" @@ -34,17 +35,15 @@ type rewards struct { Swapped map[time.Month]string } -// Rewards needs a comment -func Rewards(w http.ResponseWriter, r *http.Request) { +// CakeRewards needs a comment +func CakeRewards(w http.ResponseWriter, r *http.Request) { type data struct { Title string Uploaded bool Success bool Rewards rewards CumulativeRewards map[time.Month]string - Color string - ColorAlt string - ColorAltAlt string + Colors []string Currency string ErrorText string Lending bool @@ -52,7 +51,7 @@ func Rewards(w http.ResponseWriter, r *http.Request) { // set common attributes d := data{ - Title: "Monthly Rewards", + Title: "Pool by Cake Rewards", } // check the method used to access this site (GET/POST) @@ -67,44 +66,70 @@ func Rewards(w http.ResponseWriter, r *http.Request) { case http.MethodPost: // upload the file that was posted here var success bool = false - var e string lines, err := uploadFile(w, r) if err == nil { success = true } else { - e = err.Error() + // set ErrorText + d.ErrorText = err.Error() } // prepare data for usage var color string var currency string + var precision int var rewards rewards if success == true { currency = lines[1].Cryptocurrency - color, _, d.Lending = getCurrencyOpts(currency) + color, precision, d.Lending = getCurrencyOpts(currency) rewards = monthlyRewardOverview(lines) + d.CumulativeRewards = getCumulative(rewards, precision) + d.Success = success + d.Rewards.Staking = rewards.Staking + d.Rewards.Confectionery = rewards.Confectionery + d.Rewards.Lapis = rewards.Lapis + d.Rewards.LapisDFI = rewards.LapisDFI + d.Rewards.Referral = rewards.Referral + d.Rewards.Airdrop = rewards.Airdrop + d.Rewards.Swapped = rewards.Swapped + d.Colors = getOtherColors(color, 8) + d.Currency = currency } + d.Uploaded = true // prettify.Print(rewards) - d.Uploaded = true - d.Success = success - d.Rewards.Staking = rewards.Staking - d.Rewards.Confectionery = rewards.Confectionery - d.Rewards.Lapis = rewards.Lapis - d.Rewards.LapisDFI = rewards.LapisDFI - d.Rewards.Referral = rewards.Referral - d.Color = color - d.ColorAlt = getOtherColors(color, 2) - d.ColorAltAlt = getOtherColors(color, 4) - d.Currency = currency - d.ErrorText = e render(w, "rewards.html", d) } // create a new line instance } -func getOtherColors(color string, factor float64) string { +func getCumulative(r rewards, precision int) map[time.Month]string { + // create map to calculate cumulative rewards + res := make(map[time.Month]string) + + for i := 1; i < 13; i++ { + month := time.Month(i) + // parse all fields + airdrop, err := strconv.ParseFloat(r.Airdrop[month], 64) + confectionery, err := strconv.ParseFloat(r.Confectionery[month], 64) + lapis, err := strconv.ParseFloat(r.Lapis[month], 64) + lapisDFI, err := strconv.ParseFloat(r.LapisDFI[month], 64) + referral, err := strconv.ParseFloat(r.Referral[month], 64) + staking, err := strconv.ParseFloat(r.Staking[month], 64) + swapped, err := strconv.ParseFloat(r.Swapped[month], 64) + + // add all fields up and format result as string + res[month] = strconv.FormatFloat(airdrop+confectionery+lapis+lapisDFI+referral+staking+swapped, 'g', precision, 64) + if err != nil { + panic(err) + } + } + // return result + return res +} + +func getOtherColors(color string, n int) []string { col, err := colorful.Hex(color) if err != nil { log.Fatal(err) @@ -112,7 +137,12 @@ func getOtherColors(color string, factor float64) string { // get r,g,b from color r, g, b := col.LinearRgb() // return color reduced in "brightness" by factor of f - return colorful.LinearRgb(r/factor, g/factor, b/factor).Hex() + var c []string + for i := 1; i < int(math.Pow(2, float64(n))); i *= 2 { + c = append(c, colorful.LinearRgb(r/float64(i), g/float64(i), b/float64(i)).Hex()) + } + + return c } func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) { @@ -219,6 +249,8 @@ func monthlyRewardOverview(lines []line) rewards { LapisDFI: fillSums(lapisDFI, precision), Lapis: fillSums(lapis, precision), Referral: fillSums(referral, precision), + Airdrop: fillSums(airdrop, precision), + Swapped: fillSums(swapped, precision), } return r } diff --git a/coinGeckoAPI.go b/coinGeckoAPI.go new file mode 100644 index 0000000..7457c96 --- /dev/null +++ b/coinGeckoAPI.go @@ -0,0 +1,34 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +// CheckStatusCoinGecko needs a comment +func CheckStatusCoinGecko() bool { + // needed answer for Coingecko API + type statusResponse struct { + GeckoSays string `json:"gecko_says"` + } + + // see if API is available + 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) + if status.GeckoSays == "(V3) To the Moon!" { + // API is working as expected + return true + } + } + return false +} + +func GetCoinInfo() { + +} diff --git a/css/main.css b/css/main.css index bc1b02f..5815bb5 100644 --- a/css/main.css +++ b/css/main.css @@ -10,14 +10,14 @@ body { } h1 { - color: #17375e; + color: #29755b; margin-left: 20px; margin-top: 20px; margin-bottom: 10px } p { - color:#292929; + margin-top: 20px; margin-left: 20px; margin-bottom: 20px; } diff --git a/go.mod b/go.mod index 9d21502..83a9f8b 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,10 @@ 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/kr/pretty v0.1.0 // 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-20180628173108-788fd7840127 // indirect + gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect ) diff --git a/main.go b/main.go index 3b81a8b..3981f89 100644 --- a/main.go +++ b/main.go @@ -3,32 +3,16 @@ package main import ( "html/template" "net/http" - "os" ) func main() { - // this code below makes a file server and serves everything as a file - // fs := http.FileServer(http.Dir("")) - // http.Handle("/", fs) - - // serve everything in the css folder, the img folder and mp3 folder as a file http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css")))) // when navigating to /home it should serve the home page http.HandleFunc("/", Home) - http.HandleFunc("/rewards", Rewards) - http.HandleFunc("/table", Rewards) - http.ListenAndServe(getPort(), nil) -} - -// Detect $PORT and if present uses it for listen and serve else defaults to :8080 -// This is so that app can run on Heroku -func getPort() string { - p := os.Getenv("PORT") - if p != "" { - return ":" + p - } - return ":8080" + http.HandleFunc("/rewards", CakeRewards) + http.HandleFunc("/table", CakeRewards) + http.ListenAndServe(":8080", nil) } func render(w http.ResponseWriter, tmpl string, data interface{}) { diff --git a/templates/home.html b/templates/home.html index 1359481..9edffec 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,6 +1,5 @@ {{template "header" .}}
-

Willkommen auf einer kleinen Testseite

-

Hier ist ja doch noch ein wenig was Platz...

+

If you want to support me you can use this referral link

{{template "footer" .}} diff --git a/templates/includes.html b/templates/includes.html index 23514f2..7ec874f 100644 --- a/templates/includes.html +++ b/templates/includes.html @@ -22,7 +22,7 @@
  • Home
  • -
  • Monthly Rewards
  • +
  • Pool by Cake Rewards
  • {{ end }} @@ -51,7 +51,7 @@ xAxis: {{ template "xAxis" . }} yAxis: [{}], series: [{{template "series" . }}], - color: ["{{.Color}}","{{.ColorAlt}}","{{.ColorAltAlt}}"], + color: [{{ range $color := .Colors }}"{{ $color }}",{{ end }}"gray"], }; rewardChart.setOption(rewardOptions); @@ -70,6 +70,9 @@ {"name":"Referral reward","type":"bar","stack":"stackA","waveAnimation":false,"data":[ {{ range $month, $amount := .Rewards.Referral}}{"value": "{{ $amount }}" },{{ end }} ]}, +{"name":"Cumulative","type":"bar","stack":"stackB","waveAnimation":false,"data":[ +{{ range $month, $amount := .CumulativeRewards}}{"value": "{{ $amount }}" },{{ end }} +]}, {{else}} // Stakeable coins {"name":"Staking Rewards","type":"bar","stack":"stackA","waveAnimation":false,"data":[ @@ -81,6 +84,12 @@ {"name":"Confectionery Lapis DFI Bonus","type":"bar","stack":"stackA","waveAnimation":false,"data":[ {{ range $month, $amount := .Rewards.Confectionery}}{"value": "{{ $amount }}" },{{ end }} ]}, +{"name":"Airdrop","type":"bar","stack":"stackA","waveAnimation":false,"data":[ +{{ range $month, $amount := .Rewards.Airdrop}}{"value": "{{ $amount }}" },{{ end }} +]}, +{"name":"Cumulative","type":"bar","stack":"stackB","waveAnimation":false,"data":[ +{{ range $month, $amount := .CumulativeRewards}}{"value": "{{ $amount }}" },{{ end }} +]}, {{end}} {{end}} diff --git a/templates/rewards.html b/templates/rewards.html index d63fb3b..0bda38c 100644 --- a/templates/rewards.html +++ b/templates/rewards.html @@ -4,21 +4,19 @@

    If you upload your CSV file from Cake here, we will display some graphs here

    -

    -

    - - -
    -

    +
    + + +
    {{if .Uploaded}} + {{if ne .ErrorText ""}}
    - {{if ne .ErrorText ""}}

    {{.ErrorText}}

    - {{end}}
    {{end}} + {{end}} {{else}} {{template "barGraph" .}}