-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (152 loc) · 5.29 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"flag"
"fmt"
"log"
"time"
"os/exec"
display "github.com/gaspardpeduzzi/spring_block/display"
server "github.com/gaspardpeduzzi/spring_block/server"
graph "github.com/gaspardpeduzzi/spring_block/graph"
"reflect"
)
func main() {
var addr = flag.String("addr", "s1.ripple.com:51233", "http service address")
var verb = flag.Bool("verb", false, "Display more information")
var analysis = flag.Bool("analysis",false, "Analyse statistics")
flag.Parse()
display.VERBOSE = *verb
display.ANALYSIS = *analysis
display.Init()
c := make(chan int)
liquidOptimizer := NewOptimizer(*addr, c)
go liquidOptimizer.ConstructTxGraph()
go server.LaunchServer()
// Search for arbitrage opportunities and store them
for {
display.DisplayVerbose("waiting for next block...")
<-c
//update server data
server.AccountsNumber = len(liquidOptimizer.Graph.Clients)
allOffers, cycle := liquidOptimizer.Graph.GetProfitableOffers()
server.AccountsNumber = len(liquidOptimizer.Graph.Clients)
//Create array of issuers and clients
keys := reflect.ValueOf(liquidOptimizer.Graph.Issuers).MapKeys()
for i := 0; i < len(keys); i++ {
server.Issuers = append(server.Issuers, keys[i].String())
}
keys = reflect.ValueOf(liquidOptimizer.Graph.Clients).MapKeys()
for i := 0; i < len(keys); i++ {
server.Clients = append(server.Clients, keys[i].String())
}
keys = reflect.ValueOf(liquidOptimizer.Graph.AccountLedger).MapKeys()
for i := 0; i < len(keys); i++ {
server.AccountOrders[keys[i].String()] = len(liquidOptimizer.Graph.AccountLedger[keys[i].String()])
}
server.LatestTx := liquidOptimizer.Graph.
//Latest Opportunity
if allOffers != nil {
Print("====================================================================================")
Print(time.Now().String())
Print(fmt.Sprintf("Found profitable cycle: %s", cycle))
hello := make([]*server.OfferSummary, 0)
profit := 1.0
for i, offer := range allOffers {
Print(fmt.Sprintf("%s -> %s, %e OfferCreate Hash: %s, Volume: %f", cycle[i], cycle[(i+1)%len(cycle)], offer.Rate, offer.TxHash, offer.Quantity))
profit = profit * offer.Rate
summary := &server.OfferSummary{
From: offer.CreatorWillPay,
To: offer.CreatorWillGet,
Rate: offer.Rate,
Hash: offer.TxHash,
Volume: offer.Quantity,
}
hello = append(hello, summary)
}
Print(fmt.Sprintf("Profit: %f", profit))
Submit_Transaction(allOffers)
save(allOffers)
server.ArbitrageOffersDB = append(server.ArbitrageOffersDB, &server.ArbitrageOpportunities{Pair: cycle, Offers: hello})
//Latest Opportunity
latest := server.ArbitrageOffersDB[len(server.ArbitrageOffersDB)-1]
var product float64 = 1.0
for i, offer := range latest.Offers {
sent := product
product = product*offer.Rate
opp := &server.Opportunity{
Step: i,
Sent: fmt.Sprintf("%f", sent) + " "+ cycle[i] ,
Received: fmt.Sprintf("%f", product) + " " +cycle[(i+1)%len(cycle)],
Rate: offer.Rate,
Hash: offer.Hash,
}
server.LatestOpportunity = append(server.LatestOpportunity, opp)
}
//Recent Opportunities
recents := server.ArbitrageOffersDB
if len(server.ArbitrageOffersDB) > 10 {
recents = server.ArbitrageOffersDB[len(server.ArbitrageOffersDB)-11 : len(server.ArbitrageOffersDB)-1]
}
for _, offers := range recents {
cycleSize := 1
pairs := ""
volume := offers.Offers[0].Volume
var product float64 = 1.0
for _, offer := range offers.Offers {
product = product*offer.Rate
pairs += offer.From
cycleSize += 1
if volume > offer.Volume {
volume = offer.Volume
}
}
opp := &server.OpportunityInfo{
Pairs: pairs,
CycleSize: cycleSize,
Volume: volume,
Profit: product,
}
server.RecentOpportunities = append(server.RecentOpportunities, opp)
}
fmt.Println("====================================================================================")
}
elapsed := time.Since(startingTime)
log.Printf("Took %s to parse and analyse opportunities ", elapsed)
}
}
func Submit_Transaction(cycle []graph.Offer) {
maxQuantity_tmp := cycle[0].Quantity
for i, offer := range cycle[:1] {
if maxQuantity_tmp / offer.Rate > cycle[i+1].Quantity {
maxQuantity_tmp = cycle[i+1].Quantity
} else {
maxQuantity_tmp = maxQuantity_tmp / offer.Rate
}
}
goal := maxQuantity_tmp / cycle[len(cycle)-1].Rate
if goal < 1000000 {
Print(fmt.Sprintf("Max Quantity is too small: %v", goal))
return
}
args := fmt.Sprintf("%s %v %v", cycle[0].CreatorWillPay, goal, maxQuantity_tmp)
for i, offer := range cycle[1:] {
args = fmt.Sprintf("%s %s %s", args, offer.CreatorWillPay, cycle[i].Issuer)
}
args = fmt.Sprintf("%s %s", args, "> output")
Print(args)
out, err := exec.Command("./submit.sh", args).Output()
log.Println("submit out", string(out), err)
}
func Print(message string) {
log.Println(message)
out, err := exec.Command("./append2.sh", message).Output()
log.Println("append2 out", string(out), err)
}
func save(cycle []graph.Offer) {
res := fmt.Sprintf(time.Now().Format("2006-01-02 15:04:05"))
for _, offer := range cycle {
res = fmt.Sprintf("%s\n%s", res, offer.ToString())
}
out, err := exec.Command("./append.sh", res).Output()
log.Println("append out", string(out), err)
}