working on integration from coingecko API
This commit is contained in:
parent
2d97ec49a4
commit
bdad511738
@ -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
|
||||
}
|
34
coinGeckoAPI.go
Normal file
34
coinGeckoAPI.go
Normal 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() {
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
5
go.mod
5
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
|
||||
)
|
||||
|
22
main.go
22
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{}) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
{{template "header" .}}
|
||||
<div class="mainbody">
|
||||
<p>Willkommen auf einer kleinen Testseite</p>
|
||||
<p>Hier ist ja doch noch ein wenig was Platz...</p>
|
||||
<p>If you want to support me you can use this referral <a href="https://pool.cakedefi.com/#/?ref=700671">link</a></p>
|
||||
</div>
|
||||
{{template "footer" .}}
|
||||
|
@ -22,7 +22,7 @@
|
||||
<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 "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 }}
|
||||
</ul>
|
||||
</nav>
|
||||
@ -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);
|
||||
</script>
|
||||
@ -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}}
|
||||
|
||||
|
@ -4,21 +4,19 @@
|
||||
<!-- not successful -->
|
||||
<p>If you upload your CSV file from Cake here, we will display some graphs here</p>
|
||||
<div class="indent">
|
||||
<p>
|
||||
<form enctype="multipart/form-data" action="/rewards" method="post">
|
||||
<input type="file" name="csvFile" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</p>
|
||||
<form enctype="multipart/form-data" action="/rewards" method="post">
|
||||
<input type="file" name="csvFile" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</div>
|
||||
{{if .Uploaded}}
|
||||
<!-- Uploaded but not successful -->
|
||||
{{if ne .ErrorText ""}}
|
||||
<div class="err">
|
||||
{{if ne .ErrorText ""}}
|
||||
<p>{{.ErrorText}}</p>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{else}}
|
||||
<!-- successful -->
|
||||
{{template "barGraph" .}}
|
||||
|
Loading…
Reference in New Issue
Block a user