forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange.go
232 lines (190 loc) · 7.47 KB
/
exchange.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
package api
import (
"fmt"
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/kelp/model"
)
// ExchangeAPIKey specifies API credentials for an exchange
type ExchangeAPIKey struct {
Key string
Secret string
}
// ExchangeParam specifies an additional parameter to be sent when initializing the exchange
type ExchangeParam struct {
Param string
Value string
}
// ExchangeHeader specifies additional HTTP headers
type ExchangeHeader struct {
Header string
Value string
}
// Account allows you to access key account functions
type Account interface {
GetAccountBalances(assetList []interface{}) (map[interface{}]model.Number, error)
}
// Ticker encapsulates all the data for a given Trading Pair
type Ticker struct {
AskPrice *model.Number
BidPrice *model.Number
}
// TradesResult is the result of a GetTrades call
type TradesResult struct {
Cursor interface{}
Trades []model.Trade
}
// TradeHistoryResult is the result of a GetTradeHistory call
// this should be the same object as TradesResult but it's a separate object for backwards compatibility
type TradeHistoryResult struct {
Cursor interface{}
Trades []model.Trade
}
// TickerAPI is the interface we use as a generic API for getting ticker data from any crypto exchange
type TickerAPI interface {
GetTickerPrice(pairs []model.TradingPair) (map[model.TradingPair]Ticker, error)
}
// FillTracker knows how to track fills against open orders
type FillTracker interface {
GetPair() (pair *model.TradingPair)
// TrackFills should be executed in a new thread
TrackFills() error
RegisterHandler(handler FillHandler)
NumHandlers() uint8
}
// FillHandler is invoked by the FillTracker (once registered) anytime an order is filled
type FillHandler interface {
HandleFill(trade model.Trade) error
}
// TradeFetcher is the common method between FillTrackable and exchange
// temporarily extracted out from TradeAPI so SDEX has the flexibility to only implement this rather than exchange and FillTrackable
type TradeFetcher interface {
GetTradeHistory(pair model.TradingPair, maybeCursorStart interface{}, maybeCursorEnd interface{}) (*TradeHistoryResult, error)
}
// FillTrackable enables any implementing exchange to support fill tracking
type FillTrackable interface {
TradeFetcher
GetLatestTradeCursor() (interface{}, error)
}
// Constrainable extracts out the method that SDEX can implement for now
type Constrainable interface {
// return nil if the constraint does not exist for the exchange
GetOrderConstraints(pair *model.TradingPair) *model.OrderConstraints
OverrideOrderConstraints(pair *model.TradingPair, override *model.OrderConstraintsOverride)
}
// OrderbookFetcher extracts out the method that should go into ExchangeShim for now
type OrderbookFetcher interface {
GetOrderBook(pair *model.TradingPair, maxCount int32) (*model.OrderBook, error)
}
// TradeAPI is the interface we use as a generic API for trading on any crypto exchange
type TradeAPI interface {
GetAssetConverter() *model.AssetConverter
Constrainable
OrderbookFetcher
GetTrades(pair *model.TradingPair, maybeCursor interface{}) (*TradesResult, error)
FillTrackable
GetOpenOrders(pairs []*model.TradingPair) (map[model.TradingPair][]model.OpenOrder, error)
AddOrder(order *model.Order) (*model.TransactionID, error)
CancelOrder(txID *model.TransactionID, pair model.TradingPair) (model.CancelOrderResult, error)
}
// PrepareDepositResult is the result of a PrepareDeposit call
type PrepareDepositResult struct {
Fee *model.Number // fee that will be deducted from your deposit, i.e. amount available is depositAmount - fee
Address string // address you should send the funds to
ExpireTs int64 // expire time as a unix timestamp, 0 if it does not expire
}
// DepositAPI is defined by anything where you can deposit funds.
type DepositAPI interface {
/*
Input:
asset - asset you want to deposit
amount - amount you want to deposit
Output:
PrepareDepositResult - contains the deposit instructions
error - ErrDepositAmountAboveLimit, ErrTooManyDepositAddresses, or any other error
*/
PrepareDeposit(asset model.Asset, amount *model.Number) (*PrepareDepositResult, error)
}
// ErrDepositAmountAboveLimit error type
type ErrDepositAmountAboveLimit error
// MakeErrDepositAmountAboveLimit is a factory method
func MakeErrDepositAmountAboveLimit(amount *model.Number, limit *model.Number) ErrDepositAmountAboveLimit {
return fmt.Errorf("deposit amount (%s) is greater than limit (%s)", amount.AsString(), limit.AsString())
}
// ErrTooManyDepositAddresses error type
type ErrTooManyDepositAddresses error
// MakeErrTooManyDepositAddresses is a factory method
func MakeErrTooManyDepositAddresses() ErrTooManyDepositAddresses {
return fmt.Errorf("too many deposit addresses, try reusing one of them")
}
// WithdrawInfo is the result of a GetWithdrawInfo call
type WithdrawInfo struct {
AmountToReceive *model.Number // amount that you will receive after any fees is taken (excludes fees charged on the deposit side)
}
// WithdrawFunds is the result of a WithdrawFunds call
type WithdrawFunds struct {
WithdrawalID string
}
// WithdrawAPI is defined by anything where you can withdraw funds.
type WithdrawAPI interface {
/*
Input:
asset - asset you want to withdraw
amountToWithdraw - amount you want deducted from your account
address - address you want to withdraw to
Output:
WithdrawInfo - details on how to perform the withdrawal
error - ErrWithdrawAmountAboveLimit, ErrWithdrawAmountInvalid, or any other error
*/
GetWithdrawInfo(asset model.Asset, amountToWithdraw *model.Number, address string) (*WithdrawInfo, error)
/*
Input:
asset - asset you want to withdraw
amountToWithdraw - amount you want deducted from your account (fees will be deducted from here, use GetWithdrawInfo for fee estimate)
address - address you want to withdraw to
Output:
WithdrawFunds - result of the withdrawal
error - any error
*/
WithdrawFunds(
asset model.Asset,
amountToWithdraw *model.Number,
address string,
) (*WithdrawFunds, error)
}
// ErrWithdrawAmountAboveLimit error type
type ErrWithdrawAmountAboveLimit error
// MakeErrWithdrawAmountAboveLimit is a factory method
func MakeErrWithdrawAmountAboveLimit(amount *model.Number, limit *model.Number) ErrWithdrawAmountAboveLimit {
return fmt.Errorf("withdraw amount (%s) is greater than limit (%s)", amount.AsString(), limit.AsString())
}
// ErrWithdrawAmountInvalid error type
type ErrWithdrawAmountInvalid error
// MakeErrWithdrawAmountInvalid is a factory method
func MakeErrWithdrawAmountInvalid(amountToWithdraw *model.Number, fee *model.Number) ErrWithdrawAmountInvalid {
return fmt.Errorf("amountToWithdraw is invalid: %s, fee: %s", amountToWithdraw.AsString(), fee.AsString())
}
// Exchange is the interface we use as a generic API for all crypto exchanges
type Exchange interface {
Account
TickerAPI
TradeAPI
DepositAPI
WithdrawAPI
}
// Balance repesents various aspects of an asset's balance
type Balance struct {
Balance float64
Trust float64
Reserve float64
}
// ExchangeShim is the interface we use as a generic API for all crypto exchanges
type ExchangeShim interface {
SubmitOps(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error
SubmitOpsSynch(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error // forced synchronous version of SubmitOps
GetBalanceHack(asset horizon.Asset) (*Balance, error)
LoadOffersHack() ([]horizon.Offer, error)
Constrainable
OrderbookFetcher
FillTrackable
}