goCake/rewards.go

268 lines
6.5 KiB
Go
Raw Normal View History

2020-11-08 16:00:33 +00:00
package main
import (
"errors"
2020-11-15 19:46:32 +00:00
"fmt"
2020-11-08 16:00:33 +00:00
"io/ioutil"
2020-11-15 19:46:32 +00:00
"log"
2020-11-08 16:00:33 +00:00
"net/http"
"strconv"
"strings"
"time"
2020-11-15 19:46:32 +00:00
"github.com/lucasb-eyer/go-colorful"
2020-11-08 16:00:33 +00:00
)
type line struct {
Date time.Time
Operation string
Cryptocurrency string
Amount float64
TransactionID string
WithdrawalAddress string
Reference string
}
2020-11-15 19:46:32 +00:00
type rewards struct {
Staking map[time.Month]string
Confectionery map[time.Month]string
LapisDFI map[time.Month]string
Lapis map[time.Month]string
Referral map[time.Month]string
}
2020-11-08 16:09:58 +00:00
// Rewards needs a comment
func Rewards(w http.ResponseWriter, r *http.Request) {
2020-11-08 16:00:33 +00:00
type data struct {
Title string
Uploaded bool
Success bool
2020-11-15 19:46:32 +00:00
Rewards rewards
2020-11-08 16:00:33 +00:00
CumulativeRewards map[time.Month]string
Color string
2020-11-15 19:46:32 +00:00
ColorAlt string
ColorAltAlt string
2020-11-08 16:00:33 +00:00
Currency string
2020-11-15 19:46:32 +00:00
ErrorText string
Lending bool
2020-11-08 16:00:33 +00:00
}
// set common attributes
d := data{
Title: "Monthly Rewards",
}
// check the method used to access this site (GET/POST)
switch r.Method {
case http.MethodGet:
// display upload site
d.Uploaded = false
d.Success = false
render(w, "rewards.html", d)
case http.MethodPost:
// upload the file that was posted here
var success bool = false
2020-11-15 19:46:32 +00:00
var e string
2020-11-08 16:00:33 +00:00
lines, err := uploadFile(w, r)
if err == nil {
success = true
2020-11-15 19:46:32 +00:00
} else {
e = err.Error()
2020-11-08 16:00:33 +00:00
}
// prepare data for usage
var color string
var currency string
2020-11-15 19:46:32 +00:00
var rewards rewards
2020-11-08 16:00:33 +00:00
if success == true {
currency = lines[1].Cryptocurrency
2020-11-15 19:46:32 +00:00
color, _, d.Lending = getCurrencyOpts(currency)
2020-11-08 16:00:33 +00:00
rewards = monthlyRewardOverview(lines)
}
// prettify.Print(rewards)
d.Uploaded = true
d.Success = success
2020-11-15 19:46:32 +00:00
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
2020-11-08 16:00:33 +00:00
d.Color = color
2020-11-15 19:46:32 +00:00
d.ColorAlt = getOtherColors(color, 2)
d.ColorAltAlt = getOtherColors(color, 4)
2020-11-08 16:00:33 +00:00
d.Currency = currency
2020-11-15 19:46:32 +00:00
d.ErrorText = e
2020-11-08 16:00:33 +00:00
render(w, "rewards.html", d)
}
// create a new line instance
}
2020-11-15 19:46:32 +00:00
func getOtherColors(color string, factor float64) string {
col, err := colorful.Hex(color)
if err != nil {
log.Fatal(err)
}
// 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()
}
2020-11-08 16:00:33 +00:00
func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) {
// Maximum upload of 10 MB files
r.ParseMultipartForm(10 << 20)
// Get handler for filename, size and headers
file, handler, err := r.FormFile("csvFile")
if err != nil {
2020-11-15 19:46:32 +00:00
return nil, errors.New("please upload a .csv file")
2020-11-08 16:00:33 +00:00
}
defer file.Close()
// check if we got a csv file
if handler.Header["Content-Type"][0] == "text/csv" {
// accept this gift and read content of file
fileContents, err := ioutil.ReadAll(file)
if err != nil {
2020-11-15 19:46:32 +00:00
fmt.Println("file was uploaded but can't be read! 1", err)
2020-11-08 16:00:33 +00:00
return nil, err
}
// read uploaded file
2020-11-15 19:46:32 +00:00
lines, err := readUploadedFile(fileContents)
if err != nil {
return nil, fmt.Errorf("the .csv file could not be read, error was: %s", err)
}
2020-11-08 16:00:33 +00:00
return lines, err
}
return nil, errors.New("please only upload .csv files")
}
2020-11-15 19:46:32 +00:00
func getCurrencyOpts(currency string) (color string, precision int, lending bool) {
2020-11-08 16:00:33 +00:00
// set default precision
precision = 8
2020-11-15 19:46:32 +00:00
lending = false
2020-11-08 16:00:33 +00:00
// set other options according to the coin
switch currency {
case "BTC": // Bitcoin
color = "#F7931A"
2020-11-15 19:46:32 +00:00
lending = true
2020-11-08 16:00:33 +00:00
case "DASH":
color = "#008de4"
case "DFI": // DeFiChain
color = "#fd0bb0"
case "ETH": // Ethereum
color = "#627eea"
2020-11-15 19:46:32 +00:00
lending = true
2020-11-08 16:00:33 +00:00
precision = 18
case "PIVX":
color = "#5e4778"
precision = 9
case "XZC": // Zcoin
color = "#24b852"
case "USDT": // Tether
color = "#26a17b"
2020-11-15 19:46:32 +00:00
lending = true
2020-11-08 16:00:33 +00:00
precision = 6
}
2020-11-15 19:46:32 +00:00
return color, precision, lending
2020-11-08 16:00:33 +00:00
}
2020-11-15 19:46:32 +00:00
func monthlyRewardOverview(lines []line) rewards {
2020-11-08 16:00:33 +00:00
// create a map to hold all months' sums
2020-11-15 19:46:32 +00:00
staking := make(map[time.Month]float64)
confectionery := make(map[time.Month]float64)
lapisDFI := make(map[time.Month]float64)
lapis := make(map[time.Month]float64)
referral := make(map[time.Month]float64)
2020-11-08 16:00:33 +00:00
// loop through all lines
for i := range lines {
// filter out operations
switch lines[i].Operation {
2020-11-15 19:46:32 +00:00
case "Staking reward":
staking[lines[i].Date.Month()] += lines[i].Amount
case "Confectionery Lapis DFI Bonus":
confectionery[lines[i].Date.Month()] += lines[i].Amount
case "Lapis DFI Bonus":
lapisDFI[lines[i].Date.Month()] += lines[i].Amount
case "Lapis reward":
lapis[lines[i].Date.Month()] += lines[i].Amount
case "Referral reward":
referral[lines[i].Date.Month()] += lines[i].Amount
2020-11-08 16:00:33 +00:00
}
}
// get precision for specific coin
2020-11-15 19:46:32 +00:00
_, precision, _ := getCurrencyOpts(lines[0].Cryptocurrency)
2020-11-08 16:00:33 +00:00
// fill empty data with zeroes
2020-11-15 19:46:32 +00:00
r := rewards{
Staking: fillSums(staking, precision),
Confectionery: fillSums(confectionery, precision),
LapisDFI: fillSums(lapisDFI, precision),
Lapis: fillSums(lapis, precision),
Referral: fillSums(referral, precision),
}
2020-11-08 16:00:33 +00:00
2020-11-15 19:46:32 +00:00
return r
2020-11-08 16:00:33 +00:00
}
func fillSums(s map[time.Month]float64, precision int) map[time.Month]string {
// maps are always referenced by pointers
// create empty map
sumsString := make(map[time.Month]string)
// loop through month
for i := 1; i < 13; i++ {
// check what month are missing in array
if _, ok := s[time.Month(i)]; !ok {
s[time.Month(i)] = 0
}
// format to strings
sumsString[time.Month(i)] = strconv.FormatFloat(s[time.Month(i)], 'g', precision, 64)
}
return sumsString
}
2020-11-15 19:46:32 +00:00
func readUploadedFile(fileContents []byte) ([]line, error) {
2020-11-08 16:00:33 +00:00
var lines []line
csvLines := strings.Split(string(fileContents), "\n")
// loop through all lines (except headers)
for _, csvLine := range csvLines[1:] {
2020-11-15 19:46:32 +00:00
if csvLine != "" {
// split arguments by comma
csvArgs := strings.Split(csvLine, ",")
2020-11-08 16:00:33 +00:00
2020-11-15 19:46:32 +00:00
// fix stupid " in lines
for i := range csvArgs {
csvArgs[i] = strings.ReplaceAll(csvArgs[i], `"`, "")
}
2020-11-08 16:00:33 +00:00
2020-11-15 19:46:32 +00:00
// create output variable
var l line
2020-11-08 16:00:33 +00:00
2020-11-15 19:46:32 +00:00
// assign values from CSV file to output variable
var err error
l.Date, err = time.Parse(time.RFC3339, csvArgs[0])
if err != nil {
return nil, err
}
l.Operation = csvArgs[1]
l.Cryptocurrency = csvArgs[2]
l.Amount, _ = strconv.ParseFloat(csvArgs[3], 64)
l.TransactionID = csvArgs[4]
l.WithdrawalAddress = csvArgs[5]
l.Reference = csvArgs[6]
lines = append(lines, l)
2020-11-08 16:00:33 +00:00
}
}
2020-11-15 19:46:32 +00:00
return lines, nil
2020-11-08 16:00:33 +00:00
}