-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathoffers.go
410 lines (342 loc) · 12 KB
/
offers.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
package microstellar
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
)
// OfferType tells ManagedOffer what operation to perform
type OfferType int
// The available offer types.
const (
OfferCreate = OfferType(0)
OfferCreatePassive = OfferType(1)
OfferUpdate = OfferType(2)
OfferDelete = OfferType(3)
)
// OfferParams specify the parameters
type OfferParams struct {
// Create, update, or delete.
OfferType OfferType
// The asset that's being sold on the DEX.
SellAsset *Asset
// The asset that you want to buy on the DEX.
BuyAsset *Asset
// How much you're willing to pay (in BuyAsset units) per unit of SellAsset.
Price string
// How many units of SellAsset are you selling?
SellAmount string
// Existing offer ID (for Update and Delete)
OfferID string
}
// Offer is an offer on the DEX.
type Offer horizon.Offer
func newOfferFromHorizon(offer horizon.Offer) Offer {
return Offer(offer)
}
// horizonAsset is an asset returned by the horizon server.
type horizonAsset struct {
Code string `json:"asset_code"`
Issuer string `json:"asset_issuer"`
Type string `json:"asset_type"`
}
// Link is typically embedded in a horizon response
type horizonLink struct {
Href string `json:"href"`
Templated bool `json:"templated,omitempty"`
}
// horizonPath is a payment path returned by a horizon server
type horizonPath struct {
DestAmount string `json:"destination_amount"`
DestAssetCode string `json:"destination_asset_code"`
DestAssetIssuer string `json:"destination_asset_issuer"`
DestAssetType string `json:"destination_asset_type"`
SourceAmount string `json:"source_amount"`
SourceAssetCode string `json:"source_asset_code"`
SourceAssetIssuer string `json:"source_asset_issuer"`
SourceAssetType string `json:"source_asset_type"`
Path []horizonAsset `json:"path"`
}
// PathResponse is what the horizon server returns
type horizonPathResponse struct {
Links struct {
Self horizonLink `json:"self"`
Next horizonLink `json:"next"`
Prev horizonLink `json:"prev"`
} `json:"_links"`
Embedded struct {
Records []horizonPath `json:"records"`
} `json:"_embedded"`
}
// Path is a microstellar payment path
type Path struct {
DestAsset *Asset
DestAmount string
SourceAsset *Asset
SourceAmount string
Hops []*Asset
}
// LoadOffers returns all existing trade offers made by address.
func (ms *MicroStellar) LoadOffers(address string, options ...*Options) ([]Offer, error) {
if err := ValidAddress(address); err != nil {
return nil, ms.errorf("invalid address: %s", address)
}
opt := mergeOptions(options)
params := []interface{}{}
if opt.hasLimit {
params = append(params, horizon.Limit(opt.limit))
}
if opt.hasCursor {
params = append(params, horizon.Cursor(opt.cursor))
}
if opt.sortDescending {
params = append(params, horizon.Order("desc"))
} else {
params = append(params, horizon.Order("asc"))
}
debugf("LoadOffers", "loading offers for %s, with params +%v", address, params)
if ms.fake {
return []Offer{}, ms.success()
}
tx := ms.getTx()
horizonOffers, err := tx.GetClient().LoadAccountOffers(address, params...)
if err != nil {
return nil, ms.wrapf(err, "can't load offers")
}
results := make([]Offer, len(horizonOffers.Embedded.Records))
for i, o := range horizonOffers.Embedded.Records {
results[i] = Offer(o)
}
return results, ms.success()
}
// ManageOffer lets you trade on the DEX. See the Create/Update/DeleteOffer methods below
// to see how this is used.
func (ms *MicroStellar) ManageOffer(sourceSeed string, params *OfferParams, options ...*Options) error {
if !ValidAddressOrSeed(sourceSeed) {
return ms.errorf("invalid source address or seed: %s", sourceSeed)
}
if err := params.BuyAsset.Validate(); err != nil {
return ms.wrapf(err, "ManageOffer")
}
if err := params.SellAsset.Validate(); err != nil {
return ms.wrapf(err, "ManageOffer")
}
rate := build.Rate{
Selling: params.SellAsset.ToStellarAsset(),
Buying: params.BuyAsset.ToStellarAsset(),
Price: build.Price(params.Price),
}
var offerID uint64
if params.OfferID != "" {
var err error
if offerID, err = strconv.ParseUint(params.OfferID, 10, 64); err != nil {
return ms.wrapf(err, "ManageOffer: bad OfferID: %v", params.OfferID)
}
}
var builder build.ManageOfferBuilder
switch params.OfferType {
case OfferCreate:
amount := build.Amount(params.SellAmount)
builder = build.CreateOffer(rate, amount)
case OfferCreatePassive:
amount := build.Amount(params.SellAmount)
builder = build.CreatePassiveOffer(rate, amount)
case OfferUpdate:
amount := build.Amount(params.SellAmount)
builder = build.UpdateOffer(rate, amount, build.OfferID(offerID))
case OfferDelete:
builder = build.DeleteOffer(rate, build.OfferID(offerID))
default:
return ms.errorf("ManageOffer: bad OfferType: %v", params.OfferType)
}
tx := ms.getTx()
if len(options) > 0 {
tx.SetOptions(options[0])
}
tx.Build(sourceAccount(sourceSeed), builder)
return ms.signAndSubmit(tx, sourceSeed)
}
// CreateOffer creates an offer to trade sellAmount of sellAsset held by sourceSeed for buyAsset at
// price (which buy_unit-over-sell_unit.) The offer is made on Stellar's decentralized exchange (DEX.)
//
// You can use add Opts().MakePassive() to make this a passive offer.
func (ms *MicroStellar) CreateOffer(sourceSeed string, sellAsset *Asset, buyAsset *Asset, price string, sellAmount string, options ...*Options) error {
offerType := OfferCreate
if len(options) > 0 {
if options[0].passiveOffer {
offerType = OfferCreatePassive
}
}
return ms.ManageOffer(sourceSeed, &OfferParams{
OfferType: offerType,
SellAsset: sellAsset,
SellAmount: sellAmount,
BuyAsset: buyAsset,
Price: price,
}, options...)
}
// UpdateOffer updates the existing offer with ID offerID on the DEX.
func (ms *MicroStellar) UpdateOffer(sourceSeed string, offerID string, sellAsset *Asset, buyAsset *Asset, price string, sellAmount string, options ...*Options) error {
return ms.ManageOffer(sourceSeed, &OfferParams{
OfferType: OfferUpdate,
SellAsset: sellAsset,
SellAmount: sellAmount,
BuyAsset: buyAsset,
Price: price,
OfferID: offerID,
}, options...)
}
// DeleteOffer deletes the specified parameters (assets, price, ID) on the DEX.
func (ms *MicroStellar) DeleteOffer(sourceSeed string, offerID string, sellAsset *Asset, buyAsset *Asset, price string, options ...*Options) error {
return ms.ManageOffer(sourceSeed, &OfferParams{
OfferType: OfferDelete,
SellAsset: sellAsset,
BuyAsset: buyAsset,
Price: price,
OfferID: offerID,
}, options...)
}
// FindPaths finds payment paths between source and dest assets. Use Options.WithAsset
// to filter the results by source asset and max spend.
func (ms *MicroStellar) FindPaths(sourceAddress string, destAddress string, destAsset *Asset, destAmount string, options ...*Options) ([]Path, error) {
tx := ms.getTx()
client := tx.GetClient()
baseURL := strings.TrimRight(client.URL, "/") + "/paths"
query := url.Values{}
query.Add("source_account", sourceAddress)
query.Add("destination_account", destAddress)
query.Add("destination_asset_type", string(destAsset.Type))
query.Add("destination_asset_code", destAsset.Code)
query.Add("destination_asset_issuer", destAsset.Issuer)
query.Add("destination_amount", destAmount)
endpoint := fmt.Sprintf(baseURL+"?%s", query.Encode())
if _, err := url.Parse(endpoint); err != nil {
return nil, ms.errorf("endpoint parse error: %v", err)
}
debugf("FindPaths", "querying endpoint: %s", endpoint)
resp, err := client.HTTP.Get(endpoint)
if err != nil {
return nil, ms.errorf("failed to query server: %v", err)
}
var pathResponse horizonPathResponse
bytes, _ := ioutil.ReadAll(resp.Body)
body := string(bytes)
debugf("FindPaths", "Got Body: %+v", body)
err = json.Unmarshal(bytes, &pathResponse)
if err != nil {
return nil, ms.errorf("error unmarshalling response: %v", err)
}
opts := mergeOptions(options)
returnPath := []Path{}
for _, path := range pathResponse.Embedded.Records {
sourceAsset := NewAsset(path.SourceAssetCode, path.SourceAssetIssuer, AssetType(path.SourceAssetType))
if opts.sendAsset != nil && !opts.sendAsset.Equals(*sourceAsset) {
// Filtered
continue
}
if opts.maxAmount != "" {
maxAmount, err := ParseAmount(opts.maxAmount)
if err != nil {
return nil, ms.errorf("error parsing maxAmount: %s: %v", opts.maxAmount, err)
}
pathAmount, err := ParseAmount(path.SourceAmount)
if err != nil {
return nil, ms.errorf("error parsing path.source_amount: %s: %v", path.SourceAmount, err)
}
if pathAmount > maxAmount {
// Too expensive, skip
continue
}
}
debugf("FindPaths", "cost: %s path source: %s(%s) %s", path.SourceAmount, sourceAsset.Code, sourceAsset.Type, sourceAsset.Issuer)
hops := []*Asset{}
for _, hop := range path.Path {
debugf("FindPaths", "hop: %s(%s) %s", hop.Code, hop.Type, hop.Issuer)
hops = append(hops, NewAsset(hop.Code, hop.Issuer, AssetType(hop.Type)))
}
returnPath = append(returnPath,
Path{
SourceAsset: sourceAsset,
SourceAmount: path.SourceAmount,
DestAsset: NewAsset(path.DestAssetCode, path.DestAssetIssuer, AssetType(path.DestAssetType)),
DestAmount: path.DestAmount,
Hops: hops,
})
}
return returnPath, ms.success()
}
// HorizonOrderBook represents an a horzon order_book response.
type horizonOrderBook struct {
Bids []struct {
Price string `json:"price"`
Amount string `json:"amount"`
} `json:"bids"`
Asks []struct {
Price string `json:"price"`
Amount string `json:"amount"`
} `json:"asks"`
Base horizonAsset `json:"base"`
Counter horizonAsset `json:"counter"`
}
// BidAsk represents a price and amount for a specific Bid or Ask.
type BidAsk struct {
Price string `json:"price"`
Amount string `json:"amount"`
}
// OrderBook is returned by LoadOrderBook.
type OrderBook struct {
Asks []BidAsk `json:"asks"`
Bids []BidAsk `json:"bids"`
Base *Asset `json:"base"`
Counter *Asset `json:"counter"`
}
// LoadOrderBook returns the current orderbook for all trades between sellAsset and buyAsset. Use
// Opts().WithLimit(limit) to limit the number of entries returned.
func (ms *MicroStellar) LoadOrderBook(sellAsset *Asset, buyAsset *Asset, options ...*Options) (*OrderBook, error) {
tx := ms.getTx()
client := tx.GetClient()
baseURL := strings.TrimRight(client.URL, "/") + "/order_book"
opts := mergeOptions(options)
query := url.Values{}
query.Add("selling_asset_type", string(sellAsset.Type))
query.Add("selling_asset_issuer", sellAsset.Issuer)
query.Add("selling_asset_code", sellAsset.Code)
query.Add("buying_asset_type", string(buyAsset.Type))
query.Add("buying_asset_issuer", buyAsset.Issuer)
query.Add("buying_asset_code", buyAsset.Code)
if opts.hasLimit {
query.Add("limit", fmt.Sprintf("%d", opts.limit))
}
endpoint := fmt.Sprintf(baseURL+"?%s", query.Encode())
if _, err := url.Parse(endpoint); err != nil {
return nil, ms.errorf("endpoint parse error: %v", err)
}
debugf("LoadOrderBook", "querying endpoint: %s", endpoint)
resp, err := client.HTTP.Get(endpoint)
if err != nil {
return nil, ms.errorf("failed to query server: %v", err)
}
var orderBook horizonOrderBook
bytes, _ := ioutil.ReadAll(resp.Body)
body := string(bytes)
debugf("LoadOrderBook", "Got Body: %+v", body)
err = json.Unmarshal(bytes, &orderBook)
if err != nil {
return nil, ms.errorf("error unmarshalling response: %v", err)
}
returnOrderBook := OrderBook{
Base: NewAsset(orderBook.Base.Code, orderBook.Base.Issuer, AssetType(orderBook.Base.Type)),
Counter: NewAsset(orderBook.Counter.Code, orderBook.Counter.Issuer, AssetType(orderBook.Counter.Type)),
}
for _, ask := range orderBook.Asks {
returnOrderBook.Asks = append(returnOrderBook.Asks, BidAsk{Price: ask.Price, Amount: ask.Amount})
}
for _, bid := range orderBook.Bids {
returnOrderBook.Bids = append(returnOrderBook.Bids, BidAsk{Price: bid.Price, Amount: bid.Amount})
}
return &returnOrderBook, ms.success()
}