Skip to content

Commit ff56a8e

Browse files
committed
add crypto transactions generator
1 parent 7b91748 commit ff56a8e

File tree

3 files changed

+88
-23
lines changed

3 files changed

+88
-23
lines changed

mutexes/log-reader/cmd/generator/main.go

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package main
22

33
import (
4+
"bytes"
45
"flag"
56
"fmt"
7+
"log"
68
"math/rand"
9+
"os"
10+
"path"
711
"time"
812
)
913

@@ -13,14 +17,57 @@ const (
1317
convertOperation = "CONVERT"
1418
withdrawOperation = "WITHDRAW"
1519
dateFormat = "01/02/2006 15:04:05 -0700"
20+
dateFileFormat = "01-02-2006-15:04:05"
1621
)
1722

23+
type config struct {
24+
rotationInterval time.Duration
25+
transactionInterval time.Duration
26+
transactionTotalLifetime time.Duration
27+
}
28+
1829
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")
30+
directoryFlag := flag.String("dir", "testdata", "the directory to store the transaction files in")
31+
intervalFlag := flag.Duration("interval", time.Minute, "interval between each transaction")
32+
rotationFlag := flag.Duration("rotation", time.Hour, "rotation interval between each transaction file")
33+
totalFlag := flag.Duration("total", time.Hour*10, "total lifetime of all transactions")
2134

2235
flag.Parse()
2336

37+
err := os.MkdirAll(*directoryFlag, 0777)
38+
if err != nil {
39+
log.Fatalf("could not craete directory: %v", err)
40+
}
41+
42+
then := time.Now().UTC()
43+
cfg := config{
44+
rotationInterval: *rotationFlag,
45+
transactionInterval: *intervalFlag,
46+
transactionTotalLifetime: *totalFlag,
47+
}
48+
49+
now := time.Now().UTC()
50+
for {
51+
if now.Sub(then) > cfg.transactionTotalLifetime {
52+
return
53+
}
54+
55+
bs := generateTransactions(cfg, now)
56+
filename := fmt.Sprintf("transaction-%s", now.Format(dateFileFormat))
57+
file, err := os.Create(path.Join(*directoryFlag, filename))
58+
if err != nil {
59+
log.Fatalf("could not create file: %v", err)
60+
}
61+
_, err = file.Write(bs)
62+
if err != nil {
63+
log.Fatalf("could not write to file: %v", err)
64+
}
65+
66+
now = now.Add(cfg.rotationInterval+cfg.transactionInterval)
67+
}
68+
}
69+
70+
func generateTransactions(cfg config, now time.Time) []byte {
2471
addresses := []string{
2572
"0xa42c9E5B5d936309D6B4Ca323B0dD5739643D2Dd",
2673
"0x7F1C681EF8aD3E695b8dd18C9aD99Ad3A1469CEb",
@@ -44,44 +91,69 @@ func main() {
4491
"0xf9Fb58eB4871590764987ac1b1244b3AE4135626",
4592
}
4693
cryptoCoins := []string{"BTC", "ETH", "USDT", "BUSD", "SOL", "DOT", "LUNA"}
47-
fiatCoins := []string{"USD", "EUR", "MDL"}
94+
fiatCoins := []string{"USD", "EUR", "GBP"}
4895
operations := []string{buyOperation, sellOperation, convertOperation, withdrawOperation}
49-
//maxAmounts := map[string]float64{
50-
//}
96+
maxAmounts := map[string]float64{
97+
"BTC": 2,
98+
"ETH": 20,
99+
"USDT": 5000,
100+
"BUSD": 5000,
101+
"SOL": 50,
102+
"DOT": 100,
103+
"LUNA": 80,
104+
}
105+
prices := map[string]float64{
106+
"BTC": 41000,
107+
"ETH": 2700,
108+
"USDT": 0.9999,
109+
"BUSD": 0.9999,
110+
"SOL": 90,
111+
"DOT": 18,
112+
"LUNA": 90,
113+
}
51114
buyFee := 2.0
115+
sellFee := 3.0
52116
withdrawFee := 15
53117

54-
now, then := time.Now().UTC(), time.Now().UTC()
118+
buf := &bytes.Buffer{}
119+
then := now
55120
for {
56-
if now.Sub(then) > *rotationIntervalFlag {
57-
return
121+
if now.Sub(then) > cfg.rotationInterval {
122+
break
58123
}
59124
rand.Seed(time.Now().UnixNano())
60125
addressIndex := rand.Intn(len(addresses))
61126
address := addresses[addressIndex]
62127
cryptoCoinIndex := rand.Intn(len(cryptoCoins))
128+
cryptoCoinIndexAlt := rand.Intn(len(cryptoCoins))
63129
cryptoCoin := cryptoCoins[cryptoCoinIndex]
130+
cryptoCoinAlt := cryptoCoins[cryptoCoinIndexAlt]
64131
fiatCoinIndex := rand.Intn(len(fiatCoins))
65132
fiatCoin := fiatCoins[fiatCoinIndex]
66133
operationIndex := rand.Intn(len(operations))
67134
operation := operations[operationIndex]
135+
amount := rand.Float64() * maxAmounts[cryptoCoin]
136+
price := (prices[cryptoCoin]*rand.Float64() + prices[cryptoCoin]) / 2
68137
date := now.Format(dateFormat)
69138

70-
// make sure in and out coins are different otherwise skip iteration
71-
72139
line := ""
73140
switch operation {
74141
case buyOperation:
75-
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%% %s", address, operation, cryptoCoin, 1, fiatCoin, 123, buyFee, date)
142+
line = fmt.Sprintf("%s %s %s/%s:%.2f %s:%.2f %v%%(%.2f %s) %s", address, operation, cryptoCoin, fiatCoin, price, fiatCoin, amount, buyFee, amount*buyFee/100, fiatCoin, date)
76143
case sellOperation:
77-
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%% %s", address, operation, cryptoCoin, 1, fiatCoin, 123, 0, date)
144+
line = fmt.Sprintf("%s %s %s/%s:%.2f %s:%.2f %v%%(%.2f %s) %s", address, operation, cryptoCoin, fiatCoin, price, fiatCoin, amount, sellFee, amount*sellFee/100, fiatCoin, date)
78145
case convertOperation:
79-
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%% %s", address, operation, cryptoCoin, 1, cryptoCoin, 123, 0, date)
146+
if cryptoCoin == cryptoCoinAlt {
147+
continue
148+
}
149+
line = fmt.Sprintf("%s %s %s/%s:%.2f %s:%.2f %v%% %s", address, operation, cryptoCoin, cryptoCoinAlt, price, cryptoCoinAlt, price*amount, 0, date)
80150
case withdrawOperation:
81-
line = fmt.Sprintf("%s %s %s:%v %s:%v %v%s %s", address, operation, cryptoCoin, 1, cryptoCoin, 123, withdrawFee, fiatCoin, date)
151+
line = fmt.Sprintf("%s %s %s/%s:%.2f %s:%.2f %v%s %s", address, operation, cryptoCoin, fiatCoin, price, fiatCoin, amount*price, withdrawFee, fiatCoin, date)
82152
}
83153

84-
fmt.Println(line)
85-
now = now.Add(*transactionIntervalFlag)
154+
buf.WriteString(fmt.Sprintf("%s\n", line))
155+
now = now.Add(cfg.transactionInterval)
86156
}
157+
158+
return buf.Bytes()
87159
}

mutexes/log-reader/go.mod

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
module githubc.com/steevehook/log-reader
22

33
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/go.sum

Whitespace-only changes.

0 commit comments

Comments
 (0)