Skip to content

Instantly share code, notes, and snippets.

@kaplanmaxe
Created July 21, 2018 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaplanmaxe/7c4da9fa63ccd285ae89bba59db85cd3 to your computer and use it in GitHub Desktop.
Save kaplanmaxe/7c4da9fa63ccd285ae89bba59db85cd3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
const (
d = 0.00061
r0 = 6000000000000
exchangeRate = 0.000185
day = 435
)
// User enum
const (
emma = "Emma"
ryan = "Ryan"
kaitlyn = "Kaitlyn"
ethan = "Ethan"
madison = "Madison"
daniel = "Daniel"
)
// Service enum
const (
fashion = "Fashion"
music = "Music"
sports = "Sports"
)
var balances = map[string]int{
"Emma": 610,
"Ryan": 190,
"Kaitlyn": 10000,
"Ethan": 200,
"Madison": 250,
"Daniel": 600,
}
type tx struct {
Sender string
Receiver string
Service string
Amount int
}
var transactions = []tx{
{
Sender: emma,
Receiver: ethan,
Service: fashion,
Amount: 100,
},
{
Sender: emma,
Receiver: ryan,
Service: music,
Amount: 50,
},
{
Sender: ryan,
Receiver: emma,
Service: music,
Amount: 10,
},
{
Sender: ryan,
Receiver: emma,
Service: sports,
Amount: 30,
},
}
type txAmount struct {
Fashion int
Music int
Sports int
}
var volume = map[string]map[string]int{
"Emma": {
"Fashion": 0,
"Music": 0,
"Sports": 0,
},
"Ryan": {
"Fashion": 0,
"Music": 0,
"Sports": 0,
},
"Kaitlyn": {
"Fashion": 0,
"Music": 0,
"Sports": 0,
},
"Ethan": {
"Fashion": 0,
"Music": 0,
"Sports": 0,
},
"Madison": {
"Fashion": 0,
"Music": 0,
"Sports": 0,
},
"Daniel": {
"Fashion": 0,
"Music": 0,
"Sports": 0,
},
}
var votes = map[string]string{}
func createTransaction(tx tx) {
balances[tx.Sender] -= tx.Amount
balances[tx.Receiver] += tx.Amount
volume[tx.Sender][tx.Service] += tx.Amount
volume[tx.Receiver][tx.Service] += tx.Amount
}
func calculateReserve(day float64) float64 {
return r0 * math.Pow(1-d, day)
}
func calculateVestedAmount(day float64) float64 {
return calculateReserve(day) * d
}
func calculateTotalReward(day int) float64 {
return calculateVestedAmount(float64(day)) * 0.75
}
func createTransactions() {
for _, tx := range transactions {
createTransaction(tx)
}
}
func calculateVotes() {
for user, voteTotal := range volume {
for service, total := range voteTotal {
if total == 0 {
continue
}
if total >= volume[user][votes[user]] {
votes[user] = service
}
}
}
}
func calculateTSE() int {
var total int
for user := range votes {
total += balances[user]
}
return total
}
func calculateServiceSum(service string) int {
var total int
for user := range votes {
if votes[user] == service {
total += balances[user]
}
}
return total
}
func calculateServicePercentage(service string) float64 {
return float64(calculateServiceSum(service)) / float64(calculateTSE())
}
func calculateServiceReward(service string, day int) float64 {
return float64(calculateServicePercentage(service)) * calculateTotalReward(day)
}
func toUSD(kin float64) float64 {
return kin * exchangeRate
}
func main() {
createTransactions()
calculateVotes()
fmt.Println("Fashion: ", calculateServiceReward("Fashion", day), " $", toUSD(calculateServiceReward("Fashion", day)))
fmt.Println("Music: ", calculateServiceReward("Music", day), " $", toUSD(calculateServiceReward("Music", day)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment