forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdex.go
815 lines (713 loc) · 27.2 KB
/
sdex.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
package plugins
import (
"fmt"
"log"
"math"
"reflect"
"strconv"
"strings"
"github.com/interstellar/kelp/api"
"github.com/interstellar/kelp/model"
"github.com/interstellar/kelp/support/utils"
"github.com/nikhilsaraf/go-tools/multithreading"
"github.com/pkg/errors"
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
)
const baseReserve = 0.5
const baseFee = 0.0000100
const maxLumenTrust = math.MaxFloat64
const maxPageLimit = 200
var sdexOrderConstraints = model.MakeOrderConstraints(7, 7, 0.0000001)
// TODO we need a reasonable value for the resolution here (currently arbitrary 300000 from a test in horizon)
const fetchTradesResolution = 300000
// SDEX helps with building and submitting transactions to the Stellar network
type SDEX struct {
API *horizon.Client
SourceAccount string
TradingAccount string
SourceSeed string
TradingSeed string
Network build.Network
threadTracker *multithreading.ThreadTracker
operationalBuffer float64
operationalBufferNonNativePct float64
simMode bool
pair *model.TradingPair
assetMap map[model.Asset]horizon.Asset // this is needed until we fully address putting SDEX behind the Exchange interface
// uninitialized
seqNum uint64
reloadSeqNum bool
// explicitly calculate liabilities here for now, we can switch over to using the values from Horizon once the protocol change has taken effect
cachedLiabilities map[horizon.Asset]Liabilities
// TODO 2 streamline requests instead of caching
// cache balances to avoid redundant requests
cachedBalances map[horizon.Asset]Balance
}
// enforce SDEX implements api.Constrainable
var _ api.Constrainable = &SDEX{}
// Liabilities represents the "committed" units of an asset on both the buy and sell sides
type Liabilities struct {
Buying float64 // affects how much more can be bought
Selling float64 // affects how much more can be sold
}
// Balance repesents an asset's balance response from the assetBalance method below
type Balance struct {
Balance float64
Trust float64
Reserve float64
}
// MakeSDEX is a factory method for SDEX
func MakeSDEX(
api *horizon.Client,
sourceSeed string,
tradingSeed string,
sourceAccount string,
tradingAccount string,
network build.Network,
threadTracker *multithreading.ThreadTracker,
operationalBuffer float64,
operationalBufferNonNativePct float64,
simMode bool,
pair *model.TradingPair,
assetMap map[model.Asset]horizon.Asset,
) *SDEX {
sdex := &SDEX{
API: api,
SourceSeed: sourceSeed,
TradingSeed: tradingSeed,
SourceAccount: sourceAccount,
TradingAccount: tradingAccount,
Network: network,
threadTracker: threadTracker,
operationalBuffer: operationalBuffer,
operationalBufferNonNativePct: operationalBufferNonNativePct,
simMode: simMode,
pair: pair,
assetMap: assetMap,
}
log.Printf("Using network passphrase: %s\n", sdex.Network.Passphrase)
if sdex.SourceAccount == "" {
sdex.SourceAccount = sdex.TradingAccount
sdex.SourceSeed = sdex.TradingSeed
log.Println("No Source Account Set")
}
sdex.reloadSeqNum = true
return sdex
}
func (sdex *SDEX) incrementSeqNum() {
if sdex.reloadSeqNum {
log.Println("reloading sequence number")
seqNum, err := sdex.API.SequenceForAccount(sdex.SourceAccount)
if err != nil {
log.Printf("error getting seq num: %s\n", err)
return
}
sdex.seqNum = uint64(seqNum)
sdex.reloadSeqNum = false
}
sdex.seqNum++
}
// GetOrderConstraints impl
func (sdex *SDEX) GetOrderConstraints(pair *model.TradingPair) *model.OrderConstraints {
return sdexOrderConstraints
}
// DeleteAllOffers is a helper that accumulates delete operations for the passed in offers
func (sdex *SDEX) DeleteAllOffers(offers []horizon.Offer) []build.TransactionMutator {
ops := []build.TransactionMutator{}
for _, offer := range offers {
op := sdex.DeleteOffer(offer)
ops = append(ops, &op)
}
return ops
}
// DeleteOffer returns the op that needs to be submitted to the network in order to delete the passed in offer
func (sdex *SDEX) DeleteOffer(offer horizon.Offer) build.ManageOfferBuilder {
rate := build.Rate{
Selling: utils.Asset2Asset(offer.Selling),
Buying: utils.Asset2Asset(offer.Buying),
Price: build.Price(offer.Price),
}
if sdex.SourceAccount == sdex.TradingAccount {
return build.ManageOffer(false, build.Amount("0"), rate, build.OfferID(offer.ID))
}
return build.ManageOffer(false, build.Amount("0"), rate, build.OfferID(offer.ID), build.SourceAccount{AddressOrSeed: sdex.TradingAccount})
}
// ModifyBuyOffer modifies a buy offer
func (sdex *SDEX) ModifyBuyOffer(offer horizon.Offer, price float64, amount float64, incrementalNativeAmountRaw float64) (*build.ManageOfferBuilder, error) {
return sdex.ModifySellOffer(offer, 1/price, amount*price, incrementalNativeAmountRaw)
}
// ModifySellOffer modifies a sell offer
func (sdex *SDEX) ModifySellOffer(offer horizon.Offer, price float64, amount float64, incrementalNativeAmountRaw float64) (*build.ManageOfferBuilder, error) {
return sdex.createModifySellOffer(&offer, offer.Selling, offer.Buying, price, amount, incrementalNativeAmountRaw)
}
// CreateSellOffer creates a sell offer
func (sdex *SDEX) CreateSellOffer(base horizon.Asset, counter horizon.Asset, price float64, amount float64, incrementalNativeAmountRaw float64) (*build.ManageOfferBuilder, error) {
return sdex.createModifySellOffer(nil, base, counter, price, amount, incrementalNativeAmountRaw)
}
// ParseOfferAmount is a convenience method to parse an offer amount
func (sdex *SDEX) ParseOfferAmount(amt string) (float64, error) {
offerAmt, err := strconv.ParseFloat(amt, 64)
if err != nil {
log.Printf("error parsing offer amount: %s\n", err)
return -1, err
}
return offerAmt, nil
}
func (sdex *SDEX) minReserve(subentries int32) float64 {
return float64(2+subentries) * baseReserve
}
// ResetCachedBalances resets the cached balances map
func (sdex *SDEX) ResetCachedBalances() {
sdex.cachedBalances = map[horizon.Asset]Balance{}
}
// assetBalance is a memoized version of _assetBalance
func (sdex *SDEX) assetBalance(asset horizon.Asset) (float64, float64, float64, error) {
if v, ok := sdex.cachedBalances[asset]; ok {
return v.Balance, v.Trust, v.Reserve, nil
}
b, t, r, e := sdex._assetBalance(asset)
if e == nil {
sdex.cachedBalances[asset] = Balance{
Balance: b,
Trust: t,
Reserve: r,
}
}
return b, t, r, e
}
// assetBalance returns asset balance, asset trust limit, reserve balance, error
func (sdex *SDEX) _assetBalance(asset horizon.Asset) (float64, float64, float64, error) {
account, err := sdex.API.LoadAccount(sdex.TradingAccount)
if err != nil {
return -1, -1, -1, fmt.Errorf("error: unable to load account to fetch balance: %s", err)
}
for _, balance := range account.Balances {
if utils.AssetsEqual(balance.Asset, asset) {
b, e := strconv.ParseFloat(balance.Balance, 64)
if e != nil {
return -1, -1, -1, fmt.Errorf("error: cannot parse balance: %s", e)
}
if balance.Asset.Type == utils.Native {
return b, maxLumenTrust, sdex.minReserve(account.SubentryCount) + sdex.operationalBuffer, e
}
t, e := strconv.ParseFloat(balance.Limit, 64)
if e != nil {
return -1, -1, -1, fmt.Errorf("error: cannot parse trust limit: %s", e)
}
return b, t, b * sdex.operationalBufferNonNativePct, nil
}
}
return -1, -1, -1, errors.New("could not find a balance for the asset passed in")
}
// ComputeIncrementalNativeAmountRaw returns the native amount that will be added to liabilities because of fee and min-reserve additions
func (sdex *SDEX) ComputeIncrementalNativeAmountRaw(isNewOffer bool) float64 {
incrementalNativeAmountRaw := 0.0
if sdex.TradingAccount == sdex.SourceAccount {
// at the minimum it will cost us a unit of base fee for this operation
incrementalNativeAmountRaw += baseFee
}
if isNewOffer {
// new offers will increase the min reserve
incrementalNativeAmountRaw += baseReserve
}
return incrementalNativeAmountRaw
}
// createModifySellOffer is the main method that handles the logic of creating or modifying an offer, note that all offers are treated as sell offers in Stellar
func (sdex *SDEX) createModifySellOffer(offer *horizon.Offer, selling horizon.Asset, buying horizon.Asset, price float64, amount float64, incrementalNativeAmountRaw float64) (*build.ManageOfferBuilder, error) {
if price <= 0 {
return nil, fmt.Errorf("error: cannot create or modify offer, invalid price: %.7f", price)
}
if amount <= 0 {
return nil, fmt.Errorf("error: cannot create or modify offer, invalid amount: %.7f", amount)
}
// check liability limits on the asset being sold
incrementalSell := amount
willOversell, e := sdex.willOversell(selling, amount)
if e != nil {
return nil, e
}
if willOversell {
return nil, nil
}
// check trust limits on asset being bought
incrementalBuy := price * amount
willOverbuy, e := sdex.willOverbuy(buying, incrementalBuy)
if e != nil {
return nil, e
}
if willOverbuy {
return nil, nil
}
// explicitly check that we will not oversell XLM because of fee and min reserves
incrementalNativeAmountTotal := incrementalNativeAmountRaw
if selling.Type == utils.Native {
incrementalNativeAmountTotal += incrementalSell
}
willOversellNative, e := sdex.willOversellNative(incrementalNativeAmountTotal)
if e != nil {
return nil, e
}
if willOversellNative {
return nil, nil
}
stringPrice := strconv.FormatFloat(price, 'f', int(sdexOrderConstraints.PricePrecision), 64)
rate := build.Rate{
Selling: utils.Asset2Asset(selling),
Buying: utils.Asset2Asset(buying),
Price: build.Price(stringPrice),
}
mutators := []interface{}{
rate,
build.Amount(strconv.FormatFloat(amount, 'f', int(sdexOrderConstraints.VolumePrecision), 64)),
}
if offer != nil {
mutators = append(mutators, build.OfferID(offer.ID))
}
if sdex.SourceAccount != sdex.TradingAccount {
mutators = append(mutators, build.SourceAccount{AddressOrSeed: sdex.TradingAccount})
}
result := build.ManageOffer(false, mutators...)
return &result, nil
}
// AddLiabilities updates the cached liabilities, units are in their respective assets
func (sdex *SDEX) AddLiabilities(selling horizon.Asset, buying horizon.Asset, incrementalSell float64, incrementalBuy float64, incrementalNativeAmountRaw float64) {
sdex.cachedLiabilities[selling] = Liabilities{
Selling: sdex.cachedLiabilities[selling].Selling + incrementalSell,
Buying: sdex.cachedLiabilities[selling].Buying,
}
sdex.cachedLiabilities[buying] = Liabilities{
Selling: sdex.cachedLiabilities[buying].Selling,
Buying: sdex.cachedLiabilities[buying].Buying + incrementalBuy,
}
sdex.cachedLiabilities[utils.NativeAsset] = Liabilities{
Selling: sdex.cachedLiabilities[utils.NativeAsset].Selling + incrementalNativeAmountRaw,
Buying: sdex.cachedLiabilities[utils.NativeAsset].Buying,
}
}
// willOversellNative returns willOversellNative, error
func (sdex *SDEX) willOversellNative(incrementalNativeAmount float64) (bool, error) {
nativeBal, _, minAccountBal, e := sdex.assetBalance(utils.NativeAsset)
if e != nil {
return false, e
}
nativeLiabilities, e := sdex.assetLiabilities(utils.NativeAsset)
if e != nil {
return false, e
}
willOversellNative := incrementalNativeAmount > (nativeBal - minAccountBal - nativeLiabilities.Selling)
if willOversellNative {
log.Printf("we will oversell the native asset after considering fee and min reserves, incrementalNativeAmount = %.7f, nativeBal = %.7f, minAccountBal = %.7f, nativeLiabilities.Selling = %.7f\n",
incrementalNativeAmount, nativeBal, minAccountBal, nativeLiabilities.Selling)
}
return willOversellNative, nil
}
// willOversell returns willOversell, error
func (sdex *SDEX) willOversell(asset horizon.Asset, amountSelling float64) (bool, error) {
bal, _, minAccountBal, e := sdex.assetBalance(asset)
if e != nil {
return false, e
}
liabilities, e := sdex.assetLiabilities(asset)
if e != nil {
return false, e
}
willOversell := amountSelling > (bal - minAccountBal - liabilities.Selling)
if willOversell {
log.Printf("we will oversell the asset '%s', amountSelling = %.7f, bal = %.7f, minAccountBal = %.7f, liabilities.Selling = %.7f\n",
utils.Asset2String(asset), amountSelling, bal, minAccountBal, liabilities.Selling)
}
return willOversell, nil
}
// willOverbuy returns willOverbuy, error
func (sdex *SDEX) willOverbuy(asset horizon.Asset, amountBuying float64) (bool, error) {
if asset.Type == utils.Native {
// you can never overbuy the native asset
return false, nil
}
_, trust, _, e := sdex.assetBalance(asset)
if e != nil {
return false, e
}
liabilities, e := sdex.assetLiabilities(asset)
if e != nil {
return false, e
}
willOverbuy := amountBuying > (trust - liabilities.Buying)
return willOverbuy, nil
}
// SubmitOps submits the passed in operations to the network asynchronously in a single transaction
func (sdex *SDEX) SubmitOps(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error {
sdex.incrementSeqNum()
muts := []build.TransactionMutator{
build.Sequence{Sequence: sdex.seqNum},
sdex.Network,
build.SourceAccount{AddressOrSeed: sdex.SourceAccount},
}
muts = append(muts, ops...)
tx, e := build.Transaction(muts...)
if e != nil {
return errors.Wrap(e, "SubmitOps error: ")
}
// convert to xdr string
txeB64, e := sdex.sign(tx)
if e != nil {
return e
}
log.Printf("tx XDR: %s\n", txeB64)
// submit
if !sdex.simMode {
log.Println("submitting tx XDR to network (async)")
sdex.threadTracker.TriggerGoroutine(func(inputs []interface{}) {
sdex.submit(txeB64, asyncCallback)
}, nil)
} else {
log.Println("not submitting tx XDR to network in simulation mode, calling asyncCallback with empty hash value")
sdex.invokeAsyncCallback(asyncCallback, "", nil)
}
return nil
}
// CreateBuyOffer creates a buy offer
func (sdex *SDEX) CreateBuyOffer(base horizon.Asset, counter horizon.Asset, price float64, amount float64, incrementalNativeAmountRaw float64) (*build.ManageOfferBuilder, error) {
return sdex.CreateSellOffer(counter, base, 1/price, amount*price, incrementalNativeAmountRaw)
}
func (sdex *SDEX) sign(tx *build.TransactionBuilder) (string, error) {
var txe build.TransactionEnvelopeBuilder
var e error
if sdex.SourceSeed != sdex.TradingSeed {
txe, e = tx.Sign(sdex.SourceSeed, sdex.TradingSeed)
} else {
txe, e = tx.Sign(sdex.SourceSeed)
}
if e != nil {
return "", e
}
return txe.Base64()
}
func (sdex *SDEX) submit(txeB64 string, asyncCallback func(hash string, e error)) {
resp, err := sdex.API.SubmitTransaction(txeB64)
if err != nil {
if herr, ok := errors.Cause(err).(*horizon.Error); ok {
var rcs *horizon.TransactionResultCodes
rcs, err = herr.ResultCodes()
if err != nil {
log.Printf("(async) error: no result codes from horizon: %s\n", err)
sdex.invokeAsyncCallback(asyncCallback, "", err)
return
}
if rcs.TransactionCode == "tx_bad_seq" {
log.Println("(async) error: tx_bad_seq, setting flag to reload seq number")
sdex.reloadSeqNum = true
}
log.Println("(async) error: result code details: tx code =", rcs.TransactionCode, ", opcodes =", rcs.OperationCodes)
} else {
log.Printf("(async) error: tx failed for unknown reason, error message: %s\n", err)
}
sdex.invokeAsyncCallback(asyncCallback, "", err)
return
}
log.Printf("(async) tx confirmation hash: %s\n", resp.Hash)
sdex.invokeAsyncCallback(asyncCallback, resp.Hash, nil)
}
func (sdex *SDEX) invokeAsyncCallback(asyncCallback func(hash string, e error), hash string, e error) {
if asyncCallback == nil {
return
}
sdex.threadTracker.TriggerGoroutine(func(inputs []interface{}) {
asyncCallback(hash, e)
}, nil)
}
func (sdex *SDEX) logLiabilities(asset horizon.Asset, assetStr string) {
l, e := sdex.assetLiabilities(asset)
if e != nil {
log.Printf("could not fetch liability for asset '%s', error = %s\n", assetStr, e)
return
}
bal, trust, minAccountBal, e := sdex.assetBalance(asset)
if e != nil {
log.Printf("cannot fetch balance for asset '%s', error = %s\n", assetStr, e)
return
}
trustString := "math.MaxFloat64"
if trust != maxLumenTrust {
trustString = fmt.Sprintf("%.7f", trust)
}
log.Printf("asset=%s, balance=%.7f, trust=%s, minAccountBal=%.7f, buyingLiabilities=%.7f, sellingLiabilities=%.7f\n",
assetStr, bal, trustString, minAccountBal, l.Buying, l.Selling)
}
// LogAllLiabilities logs the liabilities for the two assets along with the native asset
func (sdex *SDEX) LogAllLiabilities(assetBase horizon.Asset, assetQuote horizon.Asset) {
sdex.logLiabilities(assetBase, "base ")
sdex.logLiabilities(assetQuote, "quote ")
if assetBase != utils.NativeAsset && assetQuote != utils.NativeAsset {
sdex.logLiabilities(utils.NativeAsset, "native")
}
}
// RecomputeAndLogCachedLiabilities clears the cached liabilities and recomputes from the network before logging
func (sdex *SDEX) RecomputeAndLogCachedLiabilities(assetBase horizon.Asset, assetQuote horizon.Asset) {
sdex.cachedLiabilities = map[horizon.Asset]Liabilities{}
// reset cached balances too so we fetch fresh balances
sdex.ResetCachedBalances()
sdex.LogAllLiabilities(assetBase, assetQuote)
}
// ResetCachedLiabilities resets the cache to include only the two assets passed in
func (sdex *SDEX) ResetCachedLiabilities(assetBase horizon.Asset, assetQuote horizon.Asset) error {
// re-compute the liabilities
sdex.cachedLiabilities = map[horizon.Asset]Liabilities{}
baseLiabilities, basePairLiabilities, e := sdex.pairLiabilities(assetBase, assetQuote)
if e != nil {
return e
}
quoteLiabilities, quotePairLiabilities, e := sdex.pairLiabilities(assetQuote, assetBase)
if e != nil {
return e
}
// delete liability amounts related to all offers (filter on only those offers involving **both** assets in case the account is used by multiple bots)
sdex.cachedLiabilities[assetBase] = Liabilities{
Buying: baseLiabilities.Buying - basePairLiabilities.Buying,
Selling: baseLiabilities.Selling - basePairLiabilities.Selling,
}
sdex.cachedLiabilities[assetQuote] = Liabilities{
Buying: quoteLiabilities.Buying - quotePairLiabilities.Buying,
Selling: quoteLiabilities.Selling - quotePairLiabilities.Selling,
}
return nil
}
// AvailableCapacity returns the buying and selling amounts available for a given asset
func (sdex *SDEX) AvailableCapacity(asset horizon.Asset, incrementalNativeAmountRaw float64) (*Liabilities, error) {
l, e := sdex.assetLiabilities(asset)
if e != nil {
return nil, e
}
bal, trust, minAccountBal, e := sdex.assetBalance(asset)
if e != nil {
return nil, e
}
// factor in cost of increase in minReserve and fee when calculating selling capacity of native asset
incrementalSellingLiability := 0.0
if asset == utils.NativeAsset {
incrementalSellingLiability = incrementalNativeAmountRaw
}
return &Liabilities{
Buying: trust - l.Buying,
Selling: bal - minAccountBal - l.Selling - incrementalSellingLiability,
}, nil
}
// assetLiabilities returns the liabilities for the asset
func (sdex *SDEX) assetLiabilities(asset horizon.Asset) (*Liabilities, error) {
if v, ok := sdex.cachedLiabilities[asset]; ok {
return &v, nil
}
assetLiabilities, _, e := sdex._liabilities(asset, asset) // pass in the same asset, we ignore the returned object anyway
return assetLiabilities, e
}
// pairLiabilities returns the liabilities for the asset along with the pairLiabilities
func (sdex *SDEX) pairLiabilities(asset horizon.Asset, otherAsset horizon.Asset) (*Liabilities, *Liabilities, error) {
assetLiabilities, pairLiabilities, e := sdex._liabilities(asset, otherAsset)
return assetLiabilities, pairLiabilities, e
}
// liabilities returns the asset liabilities and pairLiabilities (non-nil only if the other asset is specified)
func (sdex *SDEX) _liabilities(asset horizon.Asset, otherAsset horizon.Asset) (*Liabilities, *Liabilities, error) {
// uses all offers for this trading account to accommodate sharing by other bots
offers, err := utils.LoadAllOffers(sdex.TradingAccount, sdex.API)
if err != nil {
assetString := utils.Asset2String(asset)
log.Printf("error: cannot load offers to compute liabilities for asset (%s): %s\n", assetString, err)
return nil, nil, err
}
// liabilities for the asset
liabilities := Liabilities{}
// liabilities for the asset w.r.t. the trading pair
pairLiabilities := Liabilities{}
for _, offer := range offers {
if offer.Selling == asset {
offerAmt, err := sdex.ParseOfferAmount(offer.Amount)
if err != nil {
return nil, nil, err
}
liabilities.Selling += offerAmt
if offer.Buying == otherAsset {
pairLiabilities.Selling += offerAmt
}
} else if offer.Buying == asset {
offerAmt, err := sdex.ParseOfferAmount(offer.Amount)
if err != nil {
return nil, nil, err
}
offerPrice, err := sdex.ParseOfferAmount(offer.Price)
if err != nil {
return nil, nil, err
}
buyingAmount := offerAmt * offerPrice
liabilities.Buying += buyingAmount
if offer.Selling == otherAsset {
pairLiabilities.Buying += buyingAmount
}
}
}
sdex.cachedLiabilities[asset] = liabilities
return &liabilities, &pairLiabilities, nil
}
func (sdex *SDEX) pair2Assets() (baseAsset horizon.Asset, quoteAsset horizon.Asset, e error) {
var ok bool
baseAsset, ok = sdex.assetMap[sdex.pair.Base]
if !ok {
return horizon.Asset{}, horizon.Asset{}, fmt.Errorf("unexpected error, base asset was not found in sdex.assetMap")
}
quoteAsset, ok = sdex.assetMap[sdex.pair.Quote]
if !ok {
return horizon.Asset{}, horizon.Asset{}, fmt.Errorf("unexpected error, quote asset was not found in sdex.assetMap")
}
return baseAsset, quoteAsset, nil
}
// enforce SDEX implementing api.FillTrackable
var _ api.FillTrackable = &SDEX{}
// GetTradeHistory fetches trades for the trading account bound to this instance of SDEX
func (sdex *SDEX) GetTradeHistory(pair model.TradingPair, maybeCursorStart interface{}, maybeCursorEnd interface{}) (*api.TradeHistoryResult, error) {
if pair != *sdex.pair {
return nil, fmt.Errorf("passed in pair (%s) did not match sdex.pair (%s)", pair.String(), sdex.pair.String())
}
baseAsset, quoteAsset, e := sdex.pair2Assets()
if e != nil {
return nil, fmt.Errorf("error while converting pair to base and quote asset: %s", e)
}
var cursorStart string
if maybeCursorStart != nil {
var ok bool
cursorStart, ok = maybeCursorStart.(string)
if !ok {
return nil, fmt.Errorf("could not convert maybeCursorStart to string, type=%s, maybeCursorStart=%v", reflect.TypeOf(maybeCursorStart), maybeCursorStart)
}
}
var cursorEnd string
if maybeCursorEnd != nil {
var ok bool
cursorEnd, ok = maybeCursorEnd.(string)
if !ok {
return nil, fmt.Errorf("could not convert maybeCursorEnd to string, type=%s, maybeCursorEnd=%v", reflect.TypeOf(maybeCursorEnd), maybeCursorEnd)
}
}
trades := []model.Trade{}
for {
tradesPage, e := sdex.API.LoadTrades(baseAsset, quoteAsset, 0, fetchTradesResolution, horizon.Cursor(cursorStart), horizon.Order(horizon.OrderAsc), horizon.Limit(maxPageLimit))
if e != nil {
if strings.Contains(e.Error(), "Rate limit exceeded") {
// return normally, we will continue loading trades in the next call from where we left off
return &api.TradeHistoryResult{
Cursor: cursorStart,
Trades: trades,
}, nil
}
return nil, fmt.Errorf("error while fetching trades in SDEX (cursor=%s): %s", cursorStart, e)
}
if len(tradesPage.Embedded.Records) == 0 {
return &api.TradeHistoryResult{
Cursor: cursorStart,
Trades: trades,
}, nil
}
updatedResult, hitCursorEnd, e := sdex.tradesPage2TradeHistoryResult(baseAsset, quoteAsset, tradesPage, cursorEnd)
if e != nil {
return nil, fmt.Errorf("error converting tradesPage2TradesResult: %s", e)
}
cursorStart = updatedResult.Cursor.(string)
trades = append(trades, updatedResult.Trades...)
if hitCursorEnd {
return &api.TradeHistoryResult{
Cursor: cursorStart,
Trades: trades,
}, nil
}
}
}
func (sdex *SDEX) getOrderAction(baseAsset horizon.Asset, quoteAsset horizon.Asset, trade horizon.Trade) *model.OrderAction {
if trade.BaseAccount != sdex.TradingAccount && trade.CounterAccount != sdex.TradingAccount {
return nil
}
tradeBaseAsset := utils.Native
if trade.BaseAssetType != utils.Native {
tradeBaseAsset = trade.BaseAssetCode + ":" + trade.BaseAssetIssuer
}
tradeQuoteAsset := utils.Native
if trade.CounterAssetType != utils.Native {
tradeQuoteAsset = trade.CounterAssetCode + ":" + trade.CounterAssetIssuer
}
sdexBaseAsset := utils.Asset2String(baseAsset)
sdexQuoteAsset := utils.Asset2String(quoteAsset)
// compare the base and quote asset on the trade to what we are using as our base and quote
// then compare whether it was the base or the quote that was the seller
actionSell := model.OrderActionSell
actionBuy := model.OrderActionBuy
if sdexBaseAsset == tradeBaseAsset && sdexQuoteAsset == tradeQuoteAsset {
if trade.BaseIsSeller {
return &actionSell
}
return &actionBuy
} else if sdexBaseAsset == tradeQuoteAsset && sdexQuoteAsset == tradeBaseAsset {
if trade.BaseIsSeller {
return &actionBuy
}
return &actionSell
} else {
return nil
}
}
// returns tradeHistoryResult, hitCursorEnd, and any error
func (sdex *SDEX) tradesPage2TradeHistoryResult(baseAsset horizon.Asset, quoteAsset horizon.Asset, tradesPage horizon.TradesPage, cursorEnd string) (*api.TradeHistoryResult, bool, error) {
var cursor string
trades := []model.Trade{}
for _, t := range tradesPage.Embedded.Records {
orderAction := sdex.getOrderAction(baseAsset, quoteAsset, t)
if orderAction == nil {
// we have encountered a trade that is different from the base and quote asset for our trading account
continue
}
vol, e := model.NumberFromString(t.BaseAmount, sdexOrderConstraints.VolumePrecision)
if e != nil {
return nil, false, fmt.Errorf("could not convert baseAmount to model.Number: %s", e)
}
floatPrice := float64(t.Price.N) / float64(t.Price.D)
price := model.NumberFromFloat(floatPrice, sdexOrderConstraints.PricePrecision)
trades = append(trades, model.Trade{
Order: model.Order{
Pair: sdex.pair,
OrderAction: *orderAction,
OrderType: model.OrderTypeLimit,
Price: price,
Volume: vol,
Timestamp: model.MakeTimestampFromTime(t.LedgerCloseTime),
},
TransactionID: model.MakeTransactionID(t.ID),
Cost: price.Multiply(*vol),
Fee: model.NumberFromFloat(baseFee, sdexOrderConstraints.PricePrecision),
})
cursor = t.PT
if cursor == cursorEnd {
return &api.TradeHistoryResult{
Cursor: cursor,
Trades: trades,
}, true, nil
}
}
return &api.TradeHistoryResult{
Cursor: cursor,
Trades: trades,
}, false, nil
}
// GetLatestTradeCursor impl.
func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) {
baseAsset, quoteAsset, e := sdex.pair2Assets()
if e != nil {
return nil, fmt.Errorf("error while convertig pair to base and quote asset: %s", e)
}
tradesPage, e := sdex.API.LoadTrades(baseAsset, quoteAsset, 0, fetchTradesResolution, horizon.Order(horizon.OrderDesc), horizon.Limit(1))
if e != nil {
return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s", e)
}
records := tradesPage.Embedded.Records
if len(records) == 0 {
// we want to use nil as the latest trade cursor if there are no trades
return nil, nil
}
return records[0].PT, nil
}