-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
100 lines (83 loc) · 2.14 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
package main
import (
"flag"
"log"
"os"
"time"
"code.vegaprotocol.io/oracles-relay/openoracle"
"github.com/pelletier/go-toml"
"nebula.exchange/oracle-price-pusher/prices"
"nebula.exchange/oracle-price-pusher/twelvedata"
)
type Config struct {
NodeAddr string `toml:"node_addr"`
WalletMnemonic string `toml:"wallet_mnemonic"`
EthereumPrivateKey string `toml:"ethereum_private_key"`
Symbol string `toml:"symbol"`
UpdateFrequency time.Duration `toml:"update_frequency"`
TwelveDataApiKey string `toml:"twelvedata_apikey"`
MicCode *string `toml:"mic_code"`
}
var flags = struct {
Config string
}{}
func init() {
flag.StringVar(&flags.Config, "config", "config.toml", "The configuration of the oracle price pusher")
}
func main() {
flag.Parse()
// load our configuration
config, err := loadConfig(flags.Config)
if err != nil {
log.Fatalf("unable to read configuration: %v", err)
}
if len(config.Symbol) <= 0 {
log.Fatalf("missing symbol in config")
}
if config.UpdateFrequency == 0 || config.UpdateFrequency <= 5*time.Second {
log.Fatalf("update frequency is too low")
}
p, err := prices.New(
config.WalletMnemonic,
config.EthereumPrivateKey,
config.NodeAddr,
)
if err != nil {
log.Fatalf("could not instanties prices: %v", err)
}
ticker := time.NewTicker(config.UpdateFrequency)
for range ticker.C {
log.Printf("updating prices")
price, err := twelvedata.Pull(config.Symbol, config.TwelveDataApiKey, config.MicCode)
if err != nil {
log.Printf("couldn't get prices: %v", err)
continue
}
log.Printf("new price: %v", price)
ts := uint64(time.Now().Unix())
err = p.Send(
ts,
[]openoracle.OraclePrice{
{
Asset: config.Symbol,
Price: price,
Timestamp: ts,
},
},
)
if err != nil {
log.Printf("could not send prices to the network: %v", err)
}
}
}
func loadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
config := Config{}
if err := toml.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}