forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_book.go
139 lines (122 loc) · 3.54 KB
/
order_book.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
package simplepath
import (
"errors"
"fmt"
"math"
sq "github.com/Masterminds/squirrel"
"github.com/stellar/go/price"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/xdr"
)
// ErrNotEnough represents an error that occurs when pricing a trade on an
// orderbook. This error occurs when the orderbook cannot fulfill the
// requested amount.
var ErrNotEnough = errors.New("not enough depth")
// orderbook represents a one-way orderbook that is selling you a specific asset (ob.Selling)
type orderBook struct {
Selling xdr.Asset // the offers are selling this asset
Buying xdr.Asset // the offers are buying this asset
Q *core.Q
}
// CostToConsumeLiquidity returns the buyingAmount (ob.Buying) needed to consume the sellingAmount (ob.Selling)
func (ob *orderBook) CostToConsumeLiquidity(sellingAmount xdr.Int64) (xdr.Int64, error) {
// load orderbook from core's db
sql, e := ob.query()
if e != nil {
return 0, e
}
rows, e := ob.Q.Query(sql)
if e != nil {
return 0, e
}
defer rows.Close()
// remaining is the units of ob.Selling that we want to consume
remaining := int64(sellingAmount)
var buyingAmount int64
for rows.Next() {
// load data from the row
var offerAmount, pricen, priced, offerid int64
e = rows.Scan(&offerAmount, &pricen, &priced, &offerid)
if e != nil {
return 0, e
}
buyingUnitsExtracted, sellingUnitsExtracted, e := price.ConvertToBuyingUnits(offerAmount, remaining, pricen, priced)
if e != nil {
return 0, e
}
// overflow check
if willAddOverflow(buyingAmount, buyingUnitsExtracted) {
return xdr.Int64(0), fmt.Errorf("adding these two values will cause an integer overflow: %d, %d", buyingAmount, buyingUnitsExtracted)
}
buyingAmount += buyingUnitsExtracted
remaining -= sellingUnitsExtracted
// check if we got all the units we wanted
if remaining <= 0 {
return xdr.Int64(buyingAmount), nil
}
}
return 0, ErrNotEnough
}
func willAddOverflow(a int64, b int64) bool {
return a > math.MaxInt64-b
}
func (ob *orderBook) query() (sq.SelectBuilder, error) {
schemaVersion, err := ob.Q.SchemaVersion()
if err != nil {
return sq.SelectBuilder{}, err
}
if schemaVersion < 9 {
return ob.querySchema8()
} else {
return ob.querySchema9()
}
}
func (ob *orderBook) querySchema8() (sq.SelectBuilder, error) {
var (
// selling/buying types
st, bt xdr.AssetType
// selling/buying codes
sc, bc string
// selling/buying issuers
si, bi string
)
e := ob.Selling.Extract(&st, &sc, &si)
if e != nil {
return sq.SelectBuilder{}, e
}
e = ob.Buying.Extract(&bt, &bc, &bi)
if e != nil {
return sq.SelectBuilder{}, e
}
sql := sq.
Select("amount", "pricen", "priced", "offerid").
From("offers").
Where(sq.Eq{
"sellingassettype": st,
"COALESCE(sellingassetcode, '')": sc,
"COALESCE(sellingissuer, '')": si}).
Where(sq.Eq{
"buyingassettype": bt,
"COALESCE(buyingassetcode, '')": bc,
"COALESCE(buyingissuer, '')": bi}).
OrderBy("price ASC")
return sql, nil
}
func (ob *orderBook) querySchema9() (sq.SelectBuilder, error) {
var sellingXDRString, buyingXDRString string
sellingXDRString, err := xdr.MarshalBase64(ob.Selling)
if err != nil {
return sq.SelectBuilder{}, err
}
buyingXDRString, err = xdr.MarshalBase64(ob.Buying)
if err != nil {
return sq.SelectBuilder{}, err
}
sql := sq.
Select("amount", "pricen", "priced", "offerid").
From("offers").
Where(sq.Eq{"sellingasset": sellingXDRString}).
Where(sq.Eq{"buyingasset": buyingXDRString}).
OrderBy("price ASC")
return sql, nil
}