forked from joinhive-ai/livepeer-usd-pricer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (105 loc) · 3.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"flag"
"fmt"
"livepool/usd-pricing/feeder"
gecko "livepool/usd-pricing/feeds/coingecko"
"livepool/usd-pricing/pricer"
"math/big"
"os"
"os/signal"
"strings"
"time"
"github.com/golang/glog"
)
const httpPort = "7935"
func defaultAddr(addr, defaultHost, defaultPort string) string {
if addr == "" {
return defaultHost + ":" + defaultPort
}
if addr[0] == ':' {
return defaultHost + addr
}
// not IPv6 safe
if !strings.Contains(addr, ":") {
return addr + ":" + defaultPort
}
return addr
}
func startFeed(feed string) feeder.Feed {
switch feed {
case "coingecko":
return gecko.NewGecko()
default:
return nil
}
}
func main() {
flag.Set("logtostderr", "true")
vFlag := flag.Lookup("v")
//We preserve this flag before resetting all the flags. Not a scalable approach, but it'll do for now. More discussions here - https://github.com/livepeer/go-livepeer/pull/617
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
node := flag.String("node", "", "CLI webserver address of the Livepeer node")
price := flag.String("price", "0.0002", "Price per MILLION PIXELS as a float string")
feed := flag.String("feed", "coingecko", "the pricing feed(s) to use")
minUpdateDelta := flag.String("priceDelta", "0.05", "percentage as a floatstring of price delta required to update pixel pricing on the node")
pollingInterval := flag.Duration("pollingInterval", 1*time.Hour, "polling interval to fetch ethereum price")
verbosity := flag.String("v", "4", "Log verbosity. {4|5|6}")
flag.Parse()
vFlag.Value.Set(*verbosity)
fmt.Println("** Welcome to the Pricer **")
fmt.Println("This service changes your node's pricing based on the current ETHUSD exchange rate")
fmt.Printf("You will currently charge $ %v per million pixels **\n", *price)
fmt.Println("")
nodeAddr := defaultAddr(*node, "127.0.0.1", httpPort)
if !strings.HasPrefix(nodeAddr, "http") {
nodeAddr = "http://" + nodeAddr
}
priceRatMillion, ok := new(big.Rat).SetString(*price)
if !ok {
glog.Errorf("provided price is not a valid float string: %v", price)
return
}
priceRat := priceRatMillion.Mul(priceRatMillion, big.NewRat(1, 1000000))
delta, ok := new(big.Rat).SetString(*minUpdateDelta)
if !ok {
glog.Errorf("provided price delta is not a valid float string: %v", *minUpdateDelta)
}
var feeds []feeder.Feed
if len(*feed) > 0 {
for _, feed := range strings.Split(*feed, ",") {
feed = strings.TrimSpace(feed)
feedClient := startFeed(feed)
if feedClient == nil {
glog.Errorf("provided feed '%v' is not valid", feed)
continue
}
feeds = append(feeds, feedClient)
}
}
if len(feeds) == 0 {
glog.Errorf("No feeds to fetch price from")
return
}
feeder, err := feeder.NewFeeder(nodeAddr, feeds)
if err != nil {
glog.Errorf("Unable to start feeder: %v", err)
return
}
pricer := pricer.NewPricer(feeder, priceRat, delta, *pollingInterval)
errCh := make(chan error)
go func() {
glog.Info("Starting Pricer")
errCh <- pricer.Start()
}()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
select {
case <-c:
case err := <-errCh:
if err != nil {
glog.Error(err)
}
return
}
}