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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
package core
import (
"context"
"eve_haul_go/internal/esi"
"fmt"
"log"
"sync"
"time"
)
const (
PrimaryCandidateLimit = 30000
PrimaryTypeLimit = 250
PrimaryStationPairLimit = 40
FallbackCandidateLimit = 80000
FallbackTypeLimit = 600
FallbackStationPairLimit = 80
WalletPollInterval = 60 * time.Second
TradeConfirmationTTL = 6 * time.Hour
)
type TrackedTradeConfirmation struct {
Trade Trade
CreatedAt time.Time
ExpiresAt time.Time
BuyRemaining int
SellRemaining int
IsBuy bool
IsSell bool
}
func NewTrackedTradeConfirmation(trade Trade, createdAt time.Time) *TrackedTradeConfirmation {
return &TrackedTradeConfirmation{
Trade: trade,
CreatedAt: createdAt,
ExpiresAt: createdAt.Add(TradeConfirmationTTL),
BuyRemaining: trade.Quantity,
SellRemaining: trade.Quantity,
IsBuy: trade.Quantity > 0,
IsSell: trade.Quantity > 0,
}
}
func (t *TrackedTradeConfirmation) IsComplete() bool {
return t.BuyRemaining <= 0 && t.SellRemaining <= 0
}
func (t *TrackedTradeConfirmation) IsExpired(now time.Time) bool {
return now.After(t.ExpiresAt) || now.Equal(t.ExpiresAt)
}
type Manager struct {
mu sync.Mutex
Ship Ship
Graph *Graph
Market *Market
Route []RouteStep
RouteInfo *RouteInfo
RoutePath []int
RoutePathOffsets []int
ActiveRouteTrades []Trade
ActiveConfirmations []*TrackedTradeConfirmation
PendingConfirmations []*TrackedTradeConfirmation
UpdatingOrders bool
Log []string
Client *esi.EsiClient
LastWalletPollAt time.Time
LastSeenWalletTxID int
WalletScopeErr bool
}
func NewManager(client *esi.EsiClient, graph *Graph) *Manager {
return &Manager{
Ship: *graph.Ship,
Graph: graph,
Market: _newMarketIgnore(nil, client),
Client: client,
Log: []string{"Manager initialized."},
Route: []RouteStep{},
RouteInfo: &RouteInfo{},
}
}
func (m *Manager) CreateRoute(ctx context.Context) error {
m.mu.Lock()
defer m.mu.Unlock()
m.Route = []RouteStep{}
m.RouteInfo = &RouteInfo{}
m.ActiveRouteTrades = []Trade{}
m.ActiveConfirmations = []*TrackedTradeConfirmation{}
m.Log = append([]string{"Creating new route..."}, m.Log...)
log.Printf("Starting primary pass...")
m.Log = append([]string{"Starting primary pass..."}, m.Log...)
shortlist := ShortlistConfig{
CandidateLimit: PrimaryCandidateLimit,
TypeLimit: PrimaryTypeLimit,
StationPairLimit: PrimaryStationPairLimit,
RankMode: "and",
}
matches, err := Arbitrage(m.Market, int(m.Ship.Cargo), m.Ship.TaxRate, m.Ship.ProfitThreshold, m.Ship.MaxCapital, &shortlist)
if err != nil {
return err
}
if len(matches) == 0 {
m.Log = append([]string{"Primary pass found no matches, starting fallback pass..."}, m.Log...)
log.Printf("Starting fallback pass...")
shortlist = ShortlistConfig{
CandidateLimit: FallbackCandidateLimit,
TypeLimit: FallbackTypeLimit,
StationPairLimit: FallbackStationPairLimit,
RankMode: "and",
}
matches, err = Arbitrage(m.Market, int(m.Ship.Cargo), m.Ship.TaxRate, m.Ship.ProfitThreshold, m.Ship.MaxCapital, &shortlist)
if err != nil {
return err
}
}
if len(matches) == 0 {
m.Log = append([]string{"No profitable trades found."}, m.Log...)
return nil
}
log.Printf("Pathfinder evaluating matches...")
filteredMatches := Pathfinder(matches, m.Graph, m.Ship.Location)
if len(filteredMatches) == 0 {
m.Log = append([]string{"No paths found for trades."}, m.Log...)
return nil
}
legMetrics := BuildTradeLegMetrics(m.Graph, m.Ship.Location, filteredMatches)
log.Printf("Route Optimizer evaluating %d matches...", len(filteredMatches))
plan := RouteOptimizer(filteredMatches, m.Graph, m.Ship, legMetrics)
if plan == nil || len(plan.Route) == 0 {
m.Log = append([]string{"Optimal route generation failed."}, m.Log...)
return nil
}
m.Route = plan.Route
m.RouteInfo = plan.Info
m.RoutePath = plan.Path
m.RoutePathOffsets = plan.RawOffsets
m.ActiveRouteTrades = plan.OptimizedTrades
now := time.Now()
for _, t := range m.ActiveRouteTrades {
m.ActiveConfirmations = append(m.ActiveConfirmations, NewTrackedTradeConfirmation(t, now))
}
m.Log = append([]string{fmt.Sprintf("Route created with %d steps.", len(m.Route))}, m.Log...)
return nil
}
func (m *Manager) ReconcileTradeConfirmations(ctx context.Context) {
now := time.Now()
m.mu.Lock()
if now.Sub(m.LastWalletPollAt) < WalletPollInterval {
m.mu.Unlock()
return
}
m.mu.Unlock()
transactions, err := m.Client.GetWalletTransactions(nil)
if err != nil {
m.mu.Lock()
if !m.WalletScopeErr {
m.Log = append([]string{fmt.Sprintf("Wallet Error: %v", err)}, m.Log...)
m.WalletScopeErr = true
}
m.mu.Unlock()
return
}
m.mu.Lock()
m.LastWalletPollAt = now
orderAdjustments := make(map[int]int)
for i := len(transactions) - 1; i >= 0; i-- {
tx := transactions[i]
if tx.TransactionID <= m.LastSeenWalletTxID {
continue
}
m.LastSeenWalletTxID = tx.TransactionID
m.applyWalletTransaction(tx, tx.Date, orderAdjustments)
}
if len(orderAdjustments) > 0 {
err = m.Market.ApplyOrderVolumeAdjustments(orderAdjustments)
if err != nil {
log.Printf("Error applying volume adjustments: %v", err)
} else {
m.Log = append([]string{fmt.Sprintf("Applied wallet transactions. Updated %d orders.", len(orderAdjustments))}, m.Log...)
}
}
m.pruneTradeConfirmations(now)
m.mu.Unlock()
}
func (m *Manager) applyWalletTransaction(tx esi.WalletTransaction, txDate time.Time, adjustments map[int]int) {
for _, conf := range m.PendingConfirmations {
m.matchAndDeduct(conf, tx, txDate, adjustments)
}
for _, conf := range m.ActiveConfirmations {
m.matchAndDeduct(conf, tx, txDate, adjustments)
}
}
func (m *Manager) matchAndDeduct(conf *TrackedTradeConfirmation, tx esi.WalletTransaction, txDate time.Time, adjustments map[int]int) {
if conf.IsComplete() {
return
}
if conf.Trade.TypeID != int(tx.TypeID) {
return
}
tolPriceCheck := func(expected, actual float64) bool {
diff := expected - actual
if diff < 0 {
diff = -diff
}
allowed := expected * 0.002
if 0.01 > allowed {
allowed = 0.01
}
return diff <= allowed
}
if tx.IsBuy && conf.BuyRemaining > 0 && tx.LocationID == conf.Trade.FromStation && tolPriceCheck(conf.Trade.FromPrice, tx.UnitPrice) {
deduct := int(tx.Quantity)
if deduct > conf.BuyRemaining {
deduct = conf.BuyRemaining
}
conf.BuyRemaining -= deduct
adjustments[conf.Trade.FromOrderID] += deduct
}
if !tx.IsBuy && conf.SellRemaining > 0 && tx.LocationID == conf.Trade.ToStation && tolPriceCheck(conf.Trade.ToPrice, tx.UnitPrice) {
deduct := int(tx.Quantity)
if deduct > conf.SellRemaining {
deduct = conf.SellRemaining
}
conf.SellRemaining -= deduct
adjustments[conf.Trade.ToOrderID] += deduct
}
}
func (m *Manager) pruneTradeConfirmations(now time.Time) {
var active []*TrackedTradeConfirmation
for _, c := range m.ActiveConfirmations {
if c.IsComplete() {
continue
}
if c.IsExpired(now) {
m.Log = append([]string{fmt.Sprintf("WARN: Expired active trade: %d", c.Trade.TypeID)}, m.Log...)
continue
}
active = append(active, c)
}
m.ActiveConfirmations = active
var pending []*TrackedTradeConfirmation
for _, c := range m.PendingConfirmations {
if c.IsComplete() {
continue
}
if c.IsExpired(now) {
m.Log = append([]string{fmt.Sprintf("WARN: Expired pending trade: %d", c.Trade.TypeID)}, m.Log...)
continue
}
pending = append(pending, c)
}
m.PendingConfirmations = pending
}
func (m *Manager) Update(ctx context.Context) error {
m.ReconcileTradeConfirmations(ctx)
loc, err := m.Client.GetLocation()
if err != nil {
return err
}
m.mu.Lock()
if loc.StationID != 0 {
m.Ship.Location = int(loc.StationID)
} else {
m.Ship.Location = int(loc.SolarSystemID)
}
m.mu.Unlock()
m.mu.Lock()
if !m.UpdatingOrders {
m.UpdatingOrders = true
m.mu.Unlock()
go func() {
defer func() {
m.mu.Lock()
m.UpdatingOrders = false
m.mu.Unlock()
}()
m.Market.UpdateOrders()
}()
} else {
m.mu.Unlock()
}
return nil
}
func _newMarketIgnore(items map[int]*Item, esiClient *esi.EsiClient) *Market {
m, _ := NewMarket(items, esiClient)
return m
}
func (m *Manager) SetWaypoints() error {
m.mu.Lock()
defer m.mu.Unlock()
if len(m.RoutePath) == 0 {
return fmt.Errorf("no route path available")
}
_, err := m.Client.SetWaypoints(m.RoutePath)
return err
}
|