-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor * Handle connection drops and reconnect * Increase verbosity * Fix config file path * Fix readme * Add interval * Write and read feeds by ticker * Rename daemon to client * Add comment to explain kraken feeder code
- Loading branch information
Showing
37 changed files
with
1,472 additions
and
1,474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,85 @@ | ||
// Copyright (c) 2020 The VulpemVentures developers | ||
|
||
// Feeder allows to connect an external price feed to the TDex Daemon to determine the current market price. | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/tdex-network/tdex-feeder/config" | ||
"github.com/tdex-network/tdex-feeder/internal/adapters" | ||
"github.com/tdex-network/tdex-feeder/internal/application" | ||
) | ||
|
||
func main() { | ||
// Interrupt Notification. | ||
interrupt := make(chan os.Signal, 1) | ||
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
// retrieve feeder service from config file | ||
feeder := configFileToFeederService(config.GetConfigPath()) | ||
"github.com/tdex-network/tdex-feeder/internal/config" | ||
"github.com/tdex-network/tdex-feeder/internal/core/application" | ||
grpcclient "github.com/tdex-network/tdex-feeder/internal/core/infrastructure/client/grpc" | ||
krakenfeeder "github.com/tdex-network/tdex-feeder/internal/core/infrastructure/feeder/kraken" | ||
"github.com/tdex-network/tdex-feeder/internal/core/ports" | ||
) | ||
|
||
log.Info("Start the feeder...") | ||
go func() { | ||
err := feeder.Start() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
type indexedPriceFeeders map[string]func(args ...interface{}) (ports.PriceFeeder, error) | ||
|
||
// check for interupt | ||
<-interrupt | ||
log.Info("Shutting down the feeder...") | ||
err := feeder.Stop() | ||
log.Info("Feeder service stopped") | ||
if err != nil { | ||
log.Fatal(err) | ||
func (i indexedPriceFeeders) supported() []string { | ||
keys := make([]string, 0, len(i)) | ||
for k := range i { | ||
keys = append(keys, k) | ||
} | ||
os.Exit(0) | ||
return keys | ||
} | ||
|
||
func configFileToFeederService(configFilePath string) application.FeederService { | ||
jsonFile, err := os.Open(configFilePath) | ||
if err != nil { | ||
log.Fatal(err) | ||
var ( | ||
priceFeeders = indexedPriceFeeders{ | ||
"kraken": krakenfeeder.NewKrakenPriceFeeder, | ||
} | ||
defer jsonFile.Close() | ||
) | ||
|
||
configBytes, err := ioutil.ReadAll(jsonFile) | ||
func main() { | ||
cfg, err := config.NewConfigFromFile() | ||
if err != nil { | ||
log.Fatal(err) | ||
log.WithError(err).Fatalf( | ||
"error while reading config from file %s", config.GetConfigPath(), | ||
) | ||
} | ||
|
||
config := &adapters.Config{} | ||
err = json.Unmarshal(configBytes, config) | ||
if err != nil { | ||
log.Fatal(err) | ||
priceFeederFactory, ok := priceFeeders[cfg.PriceFeeder] | ||
if !ok { | ||
log.Fatalf( | ||
"price feeder must be one of: '%s'", priceFeeders.supported(), | ||
) | ||
} | ||
|
||
feeder, err := config.ToFeederService() | ||
priceFeeder, err := priceFeederFactory(cfg.Interval, cfg.PortableMarkets()) | ||
if err != nil { | ||
log.Fatal(err) | ||
log.WithError(err).Fatal("error while initializing price feeder") | ||
} | ||
|
||
indexedTargets := make(application.IndexedTargetsByMarket) | ||
for _, mkt := range cfg.Markets { | ||
targets := make(map[string]ports.TdexClient) | ||
for _, t := range mkt.CTargets { | ||
target, err := grpcclient.NewGRPCClient(t.RPCAddress, t.MacaroonsPath, t.TLSCertPath) | ||
if err != nil { | ||
log.WithError(err).Fatalf( | ||
"error while connecting with target %s", t.RPCAddress, | ||
) | ||
} | ||
targets[target.RPCAddress()] = target | ||
} | ||
mktKey := ports.MarketKey(mkt) | ||
indexedTargets[mktKey] = targets | ||
} | ||
|
||
return feeder | ||
app := application.NewService(priceFeeder, indexedTargets) | ||
|
||
defer app.Stop() | ||
|
||
log.Info("starting service") | ||
go func() { | ||
if err := app.Start(); err != nil { | ||
log.WithError(err).Fatal("service exited with error") | ||
} | ||
}() | ||
|
||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT) | ||
<-sigChan | ||
|
||
log.Info("shutting down") | ||
} |
Oops, something went wrong.