working on integration from coingecko API

This commit is contained in:
Nils Stinnesbeck 2020-11-21 15:06:58 +01:00
parent 2d97ec49a4
commit bdad511738
Signed by: nils
GPG Key ID: 86D4882C6C6CA48B
8 changed files with 115 additions and 58 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math"
"net/http" "net/http"
"runtime" "runtime"
"strconv" "strconv"
@ -34,17 +35,15 @@ type rewards struct {
Swapped map[time.Month]string Swapped map[time.Month]string
} }
// Rewards needs a comment // CakeRewards needs a comment
func Rewards(w http.ResponseWriter, r *http.Request) { func CakeRewards(w http.ResponseWriter, r *http.Request) {
type data struct { type data struct {
Title string Title string
Uploaded bool Uploaded bool
Success bool Success bool
Rewards rewards Rewards rewards
CumulativeRewards map[time.Month]string CumulativeRewards map[time.Month]string
Color string Colors []string
ColorAlt string
ColorAltAlt string
Currency string Currency string
ErrorText string ErrorText string
Lending bool Lending bool
@ -52,7 +51,7 @@ func Rewards(w http.ResponseWriter, r *http.Request) {
// set common attributes // set common attributes
d := data{ d := data{
Title: "Monthly Rewards", Title: "Pool by Cake Rewards",
} }
// check the method used to access this site (GET/POST) // 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: case http.MethodPost:
// upload the file that was posted here // upload the file that was posted here
var success bool = false var success bool = false
var e string
lines, err := uploadFile(w, r) lines, err := uploadFile(w, r)
if err == nil { if err == nil {
success = true success = true
} else { } else {
e = err.Error() // set ErrorText
d.ErrorText = err.Error()
} }
// prepare data for usage // prepare data for usage
var color string var color string
var currency string var currency string
var precision int
var rewards rewards var rewards rewards
if success == true { if success == true {
currency = lines[1].Cryptocurrency currency = lines[1].Cryptocurrency
color, _, d.Lending = getCurrencyOpts(currency) color, precision, d.Lending = getCurrencyOpts(currency)
rewards = monthlyRewardOverview(lines) rewards = monthlyRewardOverview(lines)
} d.CumulativeRewards = getCumulative(rewards, precision)
// prettify.Print(rewards)
d.Uploaded = true
d.Success = success d.Success = success
d.Rewards.Staking = rewards.Staking d.Rewards.Staking = rewards.Staking
d.Rewards.Confectionery = rewards.Confectionery d.Rewards.Confectionery = rewards.Confectionery
d.Rewards.Lapis = rewards.Lapis d.Rewards.Lapis = rewards.Lapis
d.Rewards.LapisDFI = rewards.LapisDFI d.Rewards.LapisDFI = rewards.LapisDFI
d.Rewards.Referral = rewards.Referral d.Rewards.Referral = rewards.Referral
d.Color = color d.Rewards.Airdrop = rewards.Airdrop
d.ColorAlt = getOtherColors(color, 2) d.Rewards.Swapped = rewards.Swapped
d.ColorAltAlt = getOtherColors(color, 4) d.Colors = getOtherColors(color, 8)
d.Currency = currency d.Currency = currency
d.ErrorText = e }
d.Uploaded = true
// prettify.Print(rewards)
render(w, "rewards.html", d) render(w, "rewards.html", d)
} }
// create a new line instance // 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) col, err := colorful.Hex(color)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -112,7 +137,12 @@ func getOtherColors(color string, factor float64) string {
// get r,g,b from color // get r,g,b from color
r, g, b := col.LinearRgb() r, g, b := col.LinearRgb()
// return color reduced in "brightness" by factor of f // 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) { func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) {
@ -219,6 +249,8 @@ func monthlyRewardOverview(lines []line) rewards {
LapisDFI: fillSums(lapisDFI, precision), LapisDFI: fillSums(lapisDFI, precision),
Lapis: fillSums(lapis, precision), Lapis: fillSums(lapis, precision),
Referral: fillSums(referral, precision), Referral: fillSums(referral, precision),
Airdrop: fillSums(airdrop, precision),
Swapped: fillSums(swapped, precision),
} }
return r return r
} }

34
coinGeckoAPI.go Normal file
View File

@ -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() {
}

View File

@ -10,14 +10,14 @@ body {
} }
h1 { h1 {
color: #17375e; color: #29755b;
margin-left: 20px; margin-left: 20px;
margin-top: 20px; margin-top: 20px;
margin-bottom: 10px margin-bottom: 10px
} }
p { p {
color:#292929; margin-top: 20px;
margin-left: 20px; margin-left: 20px;
margin-bottom: 20px; margin-bottom: 20px;
} }

5
go.mod
View File

@ -3,9 +3,10 @@ module git.nils.zone/nils/goCake
go 1.15 go 1.15
require ( require (
git.nils.zone/nils/prettify v0.0.4
github.com/davecgh/go-spew v1.1.1 // indirect 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/lucasb-eyer/go-colorful v1.0.3
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/stretchr/testify v1.6.1 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
) )

22
main.go
View File

@ -3,32 +3,16 @@ package main
import ( import (
"html/template" "html/template"
"net/http" "net/http"
"os"
) )
func main() { 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")))) http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
// when navigating to /home it should serve the home page // when navigating to /home it should serve the home page
http.HandleFunc("/", Home) http.HandleFunc("/", Home)
http.HandleFunc("/rewards", Rewards) http.HandleFunc("/rewards", CakeRewards)
http.HandleFunc("/table", Rewards) http.HandleFunc("/table", CakeRewards)
http.ListenAndServe(getPort(), nil) http.ListenAndServe(":8080", 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"
} }
func render(w http.ResponseWriter, tmpl string, data interface{}) { func render(w http.ResponseWriter, tmpl string, data interface{}) {

View File

@ -1,6 +1,5 @@
{{template "header" .}} {{template "header" .}}
<div class="mainbody"> <div class="mainbody">
<p>Willkommen auf einer kleinen Testseite</p> <p>If you want to support me you can use this referral <a href="https://pool.cakedefi.com/#/?ref=700671">link</a></p>
<p>Hier ist ja doch noch ein wenig was Platz...</p>
</div> </div>
{{template "footer" .}} {{template "footer" .}}

View File

@ -22,7 +22,7 @@
<li><a {{ if (eq .Title "Home") }}class="active"{{end}} href="/">Home</a></li> <li><a {{ if (eq .Title "Home") }}class="active"{{end}} href="/">Home</a></li>
<!-- <li><a {{ if (eq .Title "Rewards") }}class="active"{{end}} href="rewards">Monthly Rewards</a></li> --> <!-- <li><a {{ if (eq .Title "Rewards") }}class="active"{{end}} href="rewards">Monthly Rewards</a></li> -->
<!-- <li><a {{ if (eq .Title "Graphs") }}class="active"{{end}} href="graphs">Graphs</a></li> --> <!-- <li><a {{ if (eq .Title "Graphs") }}class="active"{{end}} href="graphs">Graphs</a></li> -->
<li><a {{ if (eq .Title "Monthly Rewards") }}class="active"{{end}} href="rewards">Monthly Rewards</a></li> <li><a {{ if (eq .Title "Pool by Cake Rewards") }}class="active"{{end}} href="rewards">Pool by Cake Rewards</a></li>
{{ end }} {{ end }}
</ul> </ul>
</nav> </nav>
@ -51,7 +51,7 @@
xAxis: {{ template "xAxis" . }} xAxis: {{ template "xAxis" . }}
yAxis: [{}], yAxis: [{}],
series: [{{template "series" . }}], series: [{{template "series" . }}],
color: ["{{.Color}}","{{.ColorAlt}}","{{.ColorAltAlt}}"], color: [{{ range $color := .Colors }}"{{ $color }}",{{ end }}"gray"],
}; };
rewardChart.setOption(rewardOptions); rewardChart.setOption(rewardOptions);
</script> </script>
@ -70,6 +70,9 @@
{"name":"Referral reward","type":"bar","stack":"stackA","waveAnimation":false,"data":[ {"name":"Referral reward","type":"bar","stack":"stackA","waveAnimation":false,"data":[
{{ range $month, $amount := .Rewards.Referral}}{"value": "{{ $amount }}" },{{ end }} {{ 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}} {{else}}
// Stakeable coins // Stakeable coins
{"name":"Staking Rewards","type":"bar","stack":"stackA","waveAnimation":false,"data":[ {"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":[ {"name":"Confectionery Lapis DFI Bonus","type":"bar","stack":"stackA","waveAnimation":false,"data":[
{{ range $month, $amount := .Rewards.Confectionery}}{"value": "{{ $amount }}" },{{ end }} {{ 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}}
{{end}} {{end}}

View File

@ -4,21 +4,19 @@
<!-- not successful --> <!-- not successful -->
<p>If you upload your CSV file from Cake here, we will display some graphs here</p> <p>If you upload your CSV file from Cake here, we will display some graphs here</p>
<div class="indent"> <div class="indent">
<p>
<form enctype="multipart/form-data" action="/rewards" method="post"> <form enctype="multipart/form-data" action="/rewards" method="post">
<input type="file" name="csvFile" /> <input type="file" name="csvFile" />
<input type="submit" value="upload" /> <input type="submit" value="upload" />
</form> </form>
</p>
</div> </div>
{{if .Uploaded}} {{if .Uploaded}}
<!-- Uploaded but not successful --> <!-- Uploaded but not successful -->
<div class="err">
{{if ne .ErrorText ""}} {{if ne .ErrorText ""}}
<div class="err">
<p>{{.ErrorText}}</p> <p>{{.ErrorText}}</p>
{{end}}
</div> </div>
{{end}} {{end}}
{{end}}
{{else}} {{else}}
<!-- successful --> <!-- successful -->
{{template "barGraph" .}} {{template "barGraph" .}}