Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor according to DDD specs #11

Merged
merged 71 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 66 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
f07ec2b
initial domain implementation
louisinger Nov 27, 2020
d4df49d
feed domain
louisinger Nov 30, 2020
4d591d3
go mod / sum
louisinger Nov 30, 2020
d218afc
ports layer kraken web socket
louisinger Nov 30, 2020
00face7
reset socketConn in KrakenWebSocket impl
louisinger Nov 30, 2020
64e718c
Feed service for krakenWebSocket
louisinger Nov 30, 2020
4433961
export TicketWithPrice member in ports package
louisinger Nov 30, 2020
84ce449
start feeder_service
louisinger Nov 30, 2020
d679ee7
tdexDaemonPriceUpdater in ports package
louisinger Nov 30, 2020
34053e9
add updater_service.go
louisinger Nov 30, 2020
cf2547b
change tickers to subcribe param for factory function
louisinger Dec 1, 2020
4195cc6
feeder service base implementation
louisinger Dec 1, 2020
791e8c9
base implementation of config_service
louisinger Dec 1, 2020
45cff3d
delete old files
louisinger Dec 1, 2020
46245fa
base implementation main
louisinger Dec 1, 2020
ab92856
main test
louisinger Dec 1, 2020
32f6d12
handle close error
louisinger Dec 1, 2020
9987292
improve interupt
louisinger Dec 1, 2020
e92f820
change logger in krakenWebSocket.go
louisinger Dec 1, 2020
da13161
feeder domain test
louisinger Dec 1, 2020
04c41f1
fix message checks in krakenWebSocket.go
louisinger Dec 1, 2020
06e4657
krakenWebSocket tests
louisinger Dec 1, 2020
5e96109
create the chan in constructor function
louisinger Dec 1, 2020
f47b44b
stop the feeder in feeder test
louisinger Dec 1, 2020
6320df6
add sleep time between two socket read
louisinger Dec 1, 2020
2817487
switch to quitChan pattern in FeedService.go
louisinger Dec 1, 2020
02f314a
feed service test
louisinger Dec 1, 2020
08568ec
change nigiri endpoint explorer address
louisinger Dec 1, 2020
cb92c93
main_test pass
louisinger Dec 2, 2020
b27824f
cleanup
louisinger Dec 2, 2020
1035fc3
add mutex in tdexFeeder for isRunning function
louisinger Dec 2, 2020
d5c7705
delete test file
louisinger Dec 2, 2020
be0a97c
add integration test + shortest in makefile
louisinger Dec 2, 2020
1897e70
add some info logs
louisinger Dec 2, 2020
f100974
add default config to gitignore
louisinger Dec 2, 2020
782e851
add diagram
louisinger Dec 2, 2020
751a14c
Update README.md
louisinger Dec 2, 2020
5285a08
Update README.md
louisinger Dec 2, 2020
7ff3e6b
remove unused uuid
louisinger Dec 3, 2020
dd44619
remove uuid package
louisinger Dec 3, 2020
55f0c35
remove t.Run statements in tests
louisinger Dec 3, 2020
cf89ad8
Merge branch 'refactoring' of github.com:louisinger/tdex-feeder into …
louisinger Dec 3, 2020
e3f4200
remove t.Run in main_test.go
louisinger Dec 3, 2020
0464e73
Add some comments
louisinger Dec 3, 2020
e131b87
add go kraken
louisinger Dec 3, 2020
48265e7
use go kraken for web socket implementation
louisinger Dec 3, 2020
68b78d7
change env var name
louisinger Dec 3, 2020
a914ef8
test XBT/USDT ticker
louisinger Dec 3, 2020
f6abd11
test XBT/USDT in feed_service
louisinger Dec 3, 2020
6cec401
formatting + cleaning
louisinger Dec 4, 2020
9422256
add interval manament to updater_service
louisinger Dec 4, 2020
123a32a
add interval config in config_service
louisinger Dec 4, 2020
61a9b23
create feeder from config
louisinger Dec 4, 2020
caafd76
formatting + cleaning
louisinger Dec 4, 2020
956eaea
better error handling in config_service.go
louisinger Dec 7, 2020
5793aaf
ErrInvalidAssetHash implements error interface
louisinger Dec 7, 2020
e6eeca3
add go docs + clean some code
louisinger Dec 7, 2020
3c03cc5
remove gorilla/websocket package
louisinger Dec 7, 2020
70a2541
new config.example file
louisinger Dec 7, 2020
7f330f8
update README
louisinger Dec 7, 2020
a91c46c
Update dockerfile
louisinger Dec 7, 2020
fbbc6ea
replace baseprice by quoteprice in priceupdater
louisinger Dec 10, 2020
2a66c3e
camel case to underscore filenames
louisinger Dec 10, 2020
0af52b9
notify interupt channel if SIGTERM / SIGINT occur
louisinger Dec 10, 2020
b2e2173
add viper
louisinger Dec 10, 2020
78e02e4
handle default config file for integration test
louisinger Dec 10, 2020
f7863ad
add viper env vars in Makefile
louisinger Dec 10, 2020
4dae3c0
move tdex_feeder from domain layer to application one
louisinger Dec 10, 2020
b2ce17e
add logs
louisinger Dec 10, 2020
062e317
format test config
louisinger Dec 10, 2020
dbc5672
Update internal/application/tdex_feeder.go
tiero Dec 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
# Dependency directories (remove the comment below to include it)
# vendor/
build/

cmd/feederd/tdexd
cmd/feederd/config.json

config.json
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN go mod download

COPY . .

RUN go build -o feederd-linux cmd/feederd/main.go
RUN GOOS=linux GOARCH=amd64 go build -o feederd-linux cmd/feederd/main.go

WORKDIR /build

Expand All @@ -20,4 +20,4 @@ RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates

COPY --from=builder /build/ /

CMD ["/feederd-linux","-debug","-conf=./data/config.json"]
CMD ["/feederd-linux"]
tiero marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ vet:
@echo "Vet..."
@go vet ./...

louisinger marked this conversation as resolved.
Show resolved Hide resolved

## test: runs go unit test with default values
test: fmt
chmod u+x ./scripts/test
./scripts/test
test: fmt shorttest

shorttest:
@echo "Testing..."
go test -v -count=1 -race -short ./...

integrationtest:
go test -v -count=1 ./cmd/feederd
35 changes: 14 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# tdex-feeder

Feeder allows to connect an external price feed to the TDex Daemon to determine the current market price
Feeder allows to connect several price feeds to TDex Daemon(s) in order to automatically update the markets prices.

## Overview

tdex-feeder connects to exchanges and retrieves market prices in order to consume the gRPC
interface exposed from tdex-deamon `UpdateMarketPrice`.

![tdex-schema](./tdexfeeder.png)

## ⬇️ Run Standalone

### Install
Expand All @@ -24,11 +26,8 @@ interface exposed from tdex-deamon `UpdateMarketPrice`.
# Run with default config and default flags.
$ feederd

# Run with debug mode on.
$ feederd -debug

# Run with debug mode and different config path.
$ feederd -debug -conf=./config.json
$ FEEDER_CONFIG_PATH=./config.json feederd
```

## 🖥 Local Development
Expand All @@ -43,17 +42,19 @@ Build and use `feederd` with docker.

At the root of the repository
```
docker build -t tdex-feederd .
docker build --pull --rm -f 'Dockerfile' -t feederd:latest .
```

#### Run the daemon

Create a [config.json](#config-file) file
and run the following command in the same folder:
```
docker run -it -d --net=host -v $PWD/config.json:/data/config.json tdex-feederd
docker run -it --name feederd -v $HOME/config.json:/config.json --network="host" feederd
```
`--net=host` in case you're running tdex-deamon locally
the `$HOME/config.json` is the path to the feederd configuration file.

> `--net=host` in case you're running tdex-deamon locally

### Build it yourself

Expand All @@ -71,13 +72,6 @@ Builds feeder as static binary and runs the project with default configuration.

`make run-linux`

##### Flags

```
-conf: Configuration File Path. Default: "./config.json"
-debug: Log Debug Informations Default: false
```

##### Config file

Rename the file `./config.example.json` into `./config.json`
Expand All @@ -86,11 +80,10 @@ connects to kraken socket and to a local instance of tdex-deamon.

```
daemon_endpoint: String with the address and port of gRPC host. Required.
daemon_macaroon: String with the daemon_macaroon necessary for authentication.
kraken_ws_endpoint: String with the address and port of kraken socket. Required.
markets: Json List with necessary markets informations. Required.
base_asset: String of the Hash of the base asset for gRPC request. Required.
quote_asset: String of the Hash of the quote asset for gRPC request. Required.
kraken_ticker: String with the ticker we want kraken to provide informations on. Required.
interval: Int with the time in secods between gRPC requests. Required.
```
base_asset: String of the Hash of the base asset for gRPC request. Required.
quote_asset: String of the Hash of the quote asset for gRPC request. Required.
kraken_ticker: String with the ticker we want kraken to provide informations on. Required.
interval: the minimum time in milliseconds between two updateMarketPrice requests. Required.
```
5 changes: 0 additions & 5 deletions cmd/feeder/main.go

This file was deleted.

1 change: 1 addition & 0 deletions cmd/feederd/config.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"daemon_endpoint":"127.0.0.1:9000","kraken_ws_endpoint":"ws.kraken.com","markets":[{"base_asset":"5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225","quote_asset":"bffce3908a595436b6ab08f916fea2c9fc6a702f46b268ca354205d127f60c48","kraken_ticker":"LTC/USDT","interval":500}]}
louisinger marked this conversation as resolved.
Show resolved Hide resolved
123 changes: 36 additions & 87 deletions cmd/feederd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,114 +4,63 @@
package main

import (
"flag"
"encoding/json"
"io/ioutil"
"os"
"os/signal"
"time"
"syscall"

log "github.com/sirupsen/logrus"
"google.golang.org/grpc"

"github.com/gorilla/websocket"
"github.com/tdex-network/tdex-feeder/config"
"github.com/tdex-network/tdex-feeder/pkg/conn"
"github.com/tdex-network/tdex-feeder/pkg/marketinfo"

pboperator "github.com/tdex-network/tdex-protobuf/generated/go/operator"
)

const (
defaultConfigPath = "./config.json"
"github.com/tdex-network/tdex-feeder/internal/adapters"
"github.com/tdex-network/tdex-feeder/internal/application"
)

func main() {
interrupt, cSocket, marketsInfos, conngRPC := setup()
infiniteLoops(interrupt, cSocket, marketsInfos, conngRPC)
}

func setup() (chan os.Signal, *websocket.Conn, []marketinfo.MarketInfo, *grpc.ClientConn) {
conf := checkFlags()

// Interrupt Notification.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

// Dials the connection the the Socket.
cSocket, err := conn.ConnectToSocket(conf.KrakenWsEndpoint)
if err != nil {
log.Fatal("Socket Connection Error: ", err)
}
marketsInfos := loadMarkets(conf, cSocket)
if len(marketsInfos) == 0 {
log.Warn("list of market to feed is empty")
}
// retrieve feeder service from config file
feeder := configFileToFeederService(config.GetConfigPath())

// Set up the connection to the gRPC server.
conngRPC, err := conn.ConnectTogRPC(conf.DaemonEndpoint)
log.Info("Start the feeder")
go func() {
err := feeder.Start()
if err != nil {
log.Fatal(err)
}
}()

// check for interupt
louisinger marked this conversation as resolved.
Show resolved Hide resolved
<-interrupt
log.Info("Shutting down the feeder")
err := feeder.Stop()
log.Info("Feeder service stopped")
if err != nil {
log.Fatal("gRPC Connection Error: ", err)
log.Fatal(err)
}
return interrupt, cSocket, marketsInfos, conngRPC
os.Exit(0)
}

// Checks for command line flags for Config Path and Debug mode.
// Loads flags as required.
func checkFlags() config.Config {
confFlag := flag.String("conf", defaultConfigPath, "Configuration File Path")
debugFlag := flag.Bool("debug", false, "Log Debug Informations")
flag.Parse()
if *debugFlag == true {
log.SetLevel(log.DebugLevel)
}
// Loads Config File.
conf, err := config.LoadConfig(*confFlag)
func configFileToFeederService(configFilePath string) application.FeederService {
jsonFile, err := os.Open(configFilePath)
if err != nil {
log.Fatal(err)
}
return conf
}
defer jsonFile.Close()

// Loads Config Markets infos into Data Structure and Subscribes to
// Messages from this Markets.
func loadMarkets(conf config.Config, cSocket *websocket.Conn) []marketinfo.MarketInfo {
numberOfMarkets := len(conf.Markets)
marketsInfos := make([]marketinfo.MarketInfo, numberOfMarkets)
for i, marketConfig := range conf.Markets {
marketsInfos[i] = marketinfo.InitialMarketInfo(marketConfig)
m := conn.CreateSubscribeToMarketMessage(marketConfig.KrakenTicker)
err := conn.SendRequestMessage(cSocket, m)
if err != nil {
log.Fatal("Couldn't send request message: ", err)
}
configBytes, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
return marketsInfos
}

func infiniteLoops(interrupt chan os.Signal, cSocket *websocket.Conn, marketsInfos []marketinfo.MarketInfo, conngRPC *grpc.ClientConn) {
defer cSocket.Close()
defer conngRPC.Close()
clientgRPC := pboperator.NewOperatorClient(conngRPC)
done := make(chan string)
// Handles Messages from subscriptions. Will periodically call the
// gRPC UpdateMarketPrice with the price info from the messages.
go conn.HandleMessages(done, cSocket, marketsInfos, clientgRPC)
checkInterrupt(interrupt, cSocket, done)
}

// Loop to keep cycle alive. Waits Interrupt to close the connection.
func checkInterrupt(interrupt chan os.Signal, cSocket *websocket.Conn, done chan string) {
for {
for range interrupt {
log.Println("Shutting down Feeder")
err := cSocket.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Fatal("write close:", err)
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
config := &adapters.Config{}
err = json.Unmarshal(configBytes, config)
if err != nil {
log.Fatal(err)
}

feeder := config.ToFeederService()
return feeder
}
Loading