Skip to content

Commit 7b91748

Browse files
committed
start working on crypto-reader project
1 parent 1f0acfe commit 7b91748

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed

mutexes/log-reader/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testdata/
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"math/rand"
7+
"time"
8+
)
9+
10+
const (
11+
buyOperation = "BUY"
12+
sellOperation = "SELL"
13+
convertOperation = "CONVERT"
14+
withdrawOperation = "WITHDRAW"
15+
dateFormat = "01/02/2006 15:04:05 -0700"
16+
)
17+
18+
func main() {
19+
transactionIntervalFlag := flag.Duration("transaction-interval", time.Minute, "interval between each transaction")
20+
rotationIntervalFlag := flag.Duration("rotation-interval", time.Hour, "interval between each transaction file")
21+
22+
flag.Parse()
23+
24+
addresses := []string{
25+
"0xa42c9E5B5d936309D6B4Ca323B0dD5739643D2Dd",
26+
"0x7F1C681EF8aD3E695b8dd18C9aD99Ad3A1469CEb",
27+
"0xD534d113C3CdDFB34bC9D78d85caE4433E6B6326",
28+
"0x3ddda9438c70f06ce31Bb364788b47EF113e06F9",
29+
"0x1312395388f9f8F0AF11bfc50Bae8284962732b1",
30+
"0x980Bc04e435C5E948B1f70a69cD377783500757b",
31+
"0x120aE479935B4dB6e8bAea92Ac82Efed60165777",
32+
"0xFfEC835E4fEF2038F8CBC1170fD5d3bf3122bCd5",
33+
"0x72C3996FC71f485D95C705aE8A167380e4a891af",
34+
"0x2e23acC09912b6327766179E5F861679D50b5a9b",
35+
"0x07bb6FBE0e76492FeA01f740D01Ec796e5468968",
36+
"0x1C28aA9E5Bd21c62153Dae1AD19F6cc9305C15c1",
37+
"0xf56167Fa1CD74FD6d761E015758a3CE6BE4466F5",
38+
"0xd1ABA973674601DD10FEF7Abb239E4e975E26a44",
39+
"0x4bA6b63527B81B82d6b5eDf75E960e071FA21937",
40+
"0xc68c701B5904fB27Ec72Cc8ff062530a0ffd2015",
41+
"0xeeaFf5e4B8B488303A9F1db36edbB9d73b38dFcf",
42+
"0x3a623858c4e9E8649D9Fbb01e7aE3248d12D2b3E",
43+
"0x00B2cf90D4aDD5023A0e2CF29516fE72E3A02e2c",
44+
"0xf9Fb58eB4871590764987ac1b1244b3AE4135626",
45+
}
46+
cryptoCoins := []string{"BTC", "ETH", "USDT", "BUSD", "SOL", "DOT", "LUNA"}
47+
fiatCoins := []string{"USD", "EUR", "MDL"}
48+
operations := []string{buyOperation, sellOperation, convertOperation, withdrawOperation}
49+
//maxAmounts := map[string]float64{
50+
//}
51+
buyFee := 2.0
52+
withdrawFee := 15
53+
54+
now, then := time.Now().UTC(), time.Now().UTC()
55+
for {
56+
if now.Sub(then) > *rotationIntervalFlag {
57+
return
58+
}
59+
rand.Seed(time.Now().UnixNano())
60+
addressIndex := rand.Intn(len(addresses))
61+
address := addresses[addressIndex]
62+
cryptoCoinIndex := rand.Intn(len(cryptoCoins))
63+
cryptoCoin := cryptoCoins[cryptoCoinIndex]
64+
fiatCoinIndex := rand.Intn(len(fiatCoins))
65+
fiatCoin := fiatCoins[fiatCoinIndex]
66+
operationIndex := rand.Intn(len(operations))
67+
operation := operations[operationIndex]
68+
date := now.Format(dateFormat)
69+
70+
// make sure in and out coins are different otherwise skip iteration
71+
72+
line := ""
73+
switch operation {
74+
case buyOperation:
75+
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%% %s", address, operation, cryptoCoin, 1, fiatCoin, 123, buyFee, date)
76+
case sellOperation:
77+
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%% %s", address, operation, cryptoCoin, 1, fiatCoin, 123, 0, date)
78+
case convertOperation:
79+
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%% %s", address, operation, cryptoCoin, 1, cryptoCoin, 123, 0, date)
80+
case withdrawOperation:
81+
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%s %s", address, operation, cryptoCoin, 1, cryptoCoin, 123, withdrawFee, fiatCoin, date)
82+
}
83+
84+
fmt.Println(line)
85+
now = now.Add(*transactionIntervalFlag)
86+
}
87+
}

mutexes/log-reader/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module githubc.com/steevehook/log-reader
2+
3+
go 1.17
4+
5+
require (
6+
github.com/btcsuite/btcd v0.20.1-beta // indirect
7+
github.com/ethereum/go-ethereum v1.10.16 // indirect
8+
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
9+
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
10+
)

mutexes/log-reader/logging/file.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package logging
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package logging
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"sort"
10+
)
11+
12+
// ReaderConfig represents the configuration to start the log reader
13+
type ReaderConfig struct {
14+
Directory string
15+
Query string
16+
Limit int
17+
}
18+
19+
// NewReader creates a new instance of log reader
20+
func NewReader(cfg ReaderConfig) (*LogReader, error) {
21+
filesInfo, err := ioutil.ReadDir(cfg.Directory)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
info := make([]os.FileInfo, 0, len(filesInfo))
27+
for _, fi := range filesInfo {
28+
if fi.IsDir() {
29+
continue
30+
}
31+
32+
info = append(info, fi)
33+
}
34+
sort.Slice(filesInfo, func(i, j int) bool {
35+
return filesInfo[i].ModTime().Sub(filesInfo[j].ModTime()) < 0
36+
})
37+
38+
lr := &LogReader{
39+
cfg: cfg,
40+
filesInfo: info,
41+
}
42+
return lr, nil
43+
}
44+
45+
// LogReader represents the application log reader type
46+
// responsible for reading logs from a given directory
47+
// that were written in the last N minutes
48+
type LogReader struct {
49+
cfg ReaderConfig
50+
filesInfo []os.FileInfo
51+
}
52+
53+
// Read reads the log files using the given LogReader configuration
54+
// and stores it inside a local bytes buffer to be displayed later
55+
func (r *LogReader) Read(ctx context.Context, w io.Writer) error {
56+
select {
57+
case <-ctx.Done():
58+
return nil
59+
default:
60+
return r.read(w)
61+
}
62+
}
63+
64+
func (r *LogReader) read(w io.Writer) error {
65+
return nil
66+
}
67+
68+
func (r *LogReader) stream(file io.ReadCloser) chan string {
69+
out := make(chan string)
70+
71+
go func() {
72+
defer func() { _ = file.Close() }()
73+
scanner := bufio.NewScanner(file)
74+
for scanner.Scan() {
75+
out <- scanner.Text()
76+
}
77+
78+
close(out)
79+
}()
80+
81+
return out
82+
}

mutexes/log-reader/main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
12+
"githubc.com/steevehook/log-reader/logging"
13+
)
14+
15+
func main() {
16+
quit := make(chan os.Signal, 1)
17+
directoryFlag := flag.String("d", ".", "the directory where all the logs are stored")
18+
queryFlag := flag.String("q", "", "the query string to look for in the log files")
19+
limitFlag := flag.Int("n", 100, "the maximum number of logs to display")
20+
21+
flag.Parse()
22+
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
23+
24+
ctx, cancel := context.WithCancel(context.Background())
25+
cfg := logging.ReaderConfig{
26+
Directory: *directoryFlag,
27+
Query: *queryFlag,
28+
Limit: *limitFlag,
29+
}
30+
fmt.Println(cfg)
31+
logReader, err := logging.NewReader(cfg)
32+
if err != nil {
33+
log.Fatalf("could not create log reader: %v", err)
34+
}
35+
36+
go func() {
37+
err := logReader.Read(ctx, os.Stdout)
38+
if err != nil {
39+
log.Fatalf("could not read logs: %v", err)
40+
}
41+
42+
quit <- os.Interrupt
43+
}()
44+
45+
<-quit
46+
cancel()
47+
}

0 commit comments

Comments
 (0)