Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit f4b9025

Browse files
committed
use build package in API interfaces + undo glide removals
1 parent 4b3734b commit f4b9025

13 files changed

Lines changed: 136 additions & 37 deletions

api/exchange.go

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package api
22

33
import (
44
"fmt"
5+
"math"
56

7+
"github.com/stellar/go/build"
68
hProtocol "github.com/stellar/go/protocols/horizon"
79
"github.com/stellar/go/txnbuild"
10+
"github.com/stellar/go/xdr"
811
"github.com/stellar/kelp/model"
912
)
1013

@@ -222,11 +225,92 @@ type Balance struct {
222225

223226
// ExchangeShim is the interface we use as a generic API for all crypto exchanges
224227
type ExchangeShim interface {
225-
SubmitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error
226-
SubmitOpsSynch(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error // forced synchronous version of SubmitOps
228+
SubmitOps(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error
229+
SubmitOpsSynch(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error // forced synchronous version of SubmitOps
227230
GetBalanceHack(asset hProtocol.Asset) (*Balance, error)
228231
LoadOffersHack() ([]hProtocol.Offer, error)
229232
Constrainable
230233
OrderbookFetcher
231234
FillTrackable
232235
}
236+
237+
// ConvertOperation2TM is a temporary adapter to support transitioning from the old Go SDK to the new SDK without having to bump the major version
238+
func ConvertOperation2TM(ops []txnbuild.Operation) []build.TransactionMutator {
239+
muts := []build.TransactionMutator{}
240+
for _, o := range ops {
241+
var mob build.ManageOfferBuilder
242+
if mso, ok := o.(*txnbuild.ManageSellOffer); ok {
243+
mob = build.ManageOffer(
244+
false,
245+
build.Amount(mso.Amount),
246+
build.Rate{
247+
Selling: build.Asset{Code: mso.Selling.GetCode(), Issuer: mso.Selling.GetIssuer(), Native: mso.Selling.IsNative()},
248+
Buying: build.Asset{Code: mso.Buying.GetCode(), Issuer: mso.Buying.GetIssuer(), Native: mso.Buying.IsNative()},
249+
Price: build.Price(mso.Price),
250+
},
251+
build.OfferID(mso.OfferID),
252+
)
253+
if mso.SourceAccount != nil {
254+
mob.Mutate(build.SourceAccount{AddressOrSeed: mso.SourceAccount.GetAccountID()})
255+
}
256+
} else {
257+
panic(fmt.Sprintf("could not convert txnbuild.Operation to build.TransactionMutator: %v\n", o))
258+
}
259+
muts = append(muts, mob)
260+
}
261+
return muts
262+
}
263+
264+
// ConvertTM2Operation is a temporary adapter to support transitioning from the old Go SDK to the new SDK without having to bump the major version
265+
func ConvertTM2Operation(muts []build.TransactionMutator) []txnbuild.Operation {
266+
ops := []txnbuild.Operation{}
267+
for _, m := range muts {
268+
var mso *txnbuild.ManageSellOffer
269+
if mob, ok := m.(build.ManageOfferBuilder); ok {
270+
mso = convertMOB2MSO(mob)
271+
} else if mob, ok := m.(*build.ManageOfferBuilder); ok {
272+
mso = convertMOB2MSO(*mob)
273+
} else {
274+
panic(fmt.Sprintf("could not convert build.TransactionMutator to txnbuild.Operation: %v (type=%T)\n", m, m))
275+
}
276+
ops = append(ops, mso)
277+
}
278+
return ops
279+
}
280+
281+
func convertMOB2MSO(mob build.ManageOfferBuilder) *txnbuild.ManageSellOffer {
282+
mso := &txnbuild.ManageSellOffer{
283+
Amount: fmt.Sprintf("%.7f", float64(mob.MO.Amount)/math.Pow(10, 7)),
284+
OfferID: int64(mob.MO.OfferId),
285+
Price: fmt.Sprintf("%.7f", float64(mob.MO.Price.N)/float64(mob.MO.Price.D)),
286+
}
287+
if mob.O.SourceAccount != nil {
288+
mso.SourceAccount = &txnbuild.SimpleAccount{
289+
AccountID: mob.O.SourceAccount.Address(),
290+
}
291+
}
292+
293+
if mob.MO.Buying.Type == xdr.AssetTypeAssetTypeNative {
294+
mso.Buying = txnbuild.NativeAsset{}
295+
} else {
296+
var tipe, code, issuer string
297+
mob.MO.Buying.MustExtract(&tipe, &code, &issuer)
298+
mso.Buying = txnbuild.CreditAsset{
299+
Code: code,
300+
Issuer: issuer,
301+
}
302+
}
303+
304+
if mob.MO.Selling.Type == xdr.AssetTypeAssetTypeNative {
305+
mso.Selling = txnbuild.NativeAsset{}
306+
} else {
307+
var tipe, code, issuer string
308+
mob.MO.Selling.MustExtract(&tipe, &code, &issuer)
309+
mso.Selling = txnbuild.CreditAsset{
310+
Code: code,
311+
Issuer: issuer,
312+
}
313+
}
314+
315+
return mso
316+
}

api/strategy.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package api
22

33
import (
4+
"github.com/stellar/go/build"
45
hProtocol "github.com/stellar/go/protocols/horizon"
5-
"github.com/stellar/go/txnbuild"
66
"github.com/stellar/kelp/model"
77
)
88

99
// Strategy represents some logic for a bot to follow while doing market making
1010
type Strategy interface {
11-
PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer, []hProtocol.Offer)
11+
PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer, []hProtocol.Offer)
1212
PreUpdate(maxAssetA float64, maxAssetB float64, trustA float64, trustB float64) error
13-
UpdateWithOps(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, error)
13+
UpdateWithOps(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, error)
1414
PostUpdate() error
1515
GetFillHandlers() ([]FillHandler, error)
1616
}
1717

1818
// SideStrategy represents a strategy on a single side of the orderbook
1919
type SideStrategy interface {
20-
PruneExistingOffers(offers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer)
20+
PruneExistingOffers(offers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer)
2121
PreUpdate(maxAssetA float64, maxAssetB float64, trustA float64, trustB float64) error
22-
UpdateWithOps(offers []hProtocol.Offer) (ops []txnbuild.Operation, newTopOffer *model.Number, e error)
22+
UpdateWithOps(offers []hProtocol.Offer) (ops []build.TransactionMutator, newTopOffer *model.Number, e error)
2323
PostUpdate() error
2424
GetFillHandlers() ([]FillHandler, error)
2525
}

cmd/trade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ func deleteAllOffersAndExit(
697697
l.Infof("created %d operations to delete offers\n", len(dOps))
698698

699699
if len(dOps) > 0 {
700-
e := exchangeShim.SubmitOpsSynch(dOps, func(hash string, e error) {
700+
e := exchangeShim.SubmitOpsSynch(api.ConvertOperation2TM(dOps), func(hash string, e error) {
701701
if e != nil {
702702
logger.Fatal(l, e)
703703
return

glide.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import:
1111
- package: github.com/stellar/go
1212
version: a599ed95b928a7bdcee21cee4999efd05e43c2df
1313
subpackages:
14+
- build
1415
- clients/horizonclient
1516
- support/config
1617
- support/errors

plugins/batchedExchange.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"math/rand"
1212

13+
"github.com/stellar/go/build"
1314
hProtocol "github.com/stellar/go/protocols/horizon"
1415
"github.com/stellar/go/txnbuild"
1516
"github.com/stellar/kelp/api"
@@ -174,12 +175,14 @@ func (b BatchedExchange) GetLatestTradeCursor() (interface{}, error) {
174175
}
175176

176177
// SubmitOpsSynch is the forced synchronous version of SubmitOps below (same for batchedExchange)
177-
func (b BatchedExchange) SubmitOpsSynch(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error {
178+
func (b BatchedExchange) SubmitOpsSynch(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error {
178179
return b.SubmitOps(ops, asyncCallback)
179180
}
180181

181182
// SubmitOps performs any finalization or submission step needed by the exchange
182-
func (b BatchedExchange) SubmitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error {
183+
func (b BatchedExchange) SubmitOps(opsOld []build.TransactionMutator, asyncCallback func(hash string, e error)) error {
184+
ops := api.ConvertTM2Operation(opsOld)
185+
183186
var e error
184187
b.commands, e = b.Ops2Commands(ops, b.baseAsset, b.quoteAsset)
185188
if e != nil {

plugins/composeStrategy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"github.com/stellar/kelp/api"
77
"github.com/stellar/kelp/model"
88

9+
"github.com/stellar/go/build"
910
hProtocol "github.com/stellar/go/protocols/horizon"
1011
"github.com/stellar/go/support/errors"
11-
"github.com/stellar/go/txnbuild"
1212
"github.com/stellar/kelp/support/utils"
1313
)
1414

@@ -39,7 +39,7 @@ func makeComposeStrategy(
3939
}
4040

4141
// PruneExistingOffers impl
42-
func (s *composeStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer, []hProtocol.Offer) {
42+
func (s *composeStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer, []hProtocol.Offer) {
4343
pruneOps1, newBuyingAOffers := s.buyStrat.PruneExistingOffers(buyingAOffers)
4444
pruneOps2, newSellingAOffers := s.sellStrat.PruneExistingOffers(sellingAOffers)
4545
pruneOps1 = append(pruneOps1, pruneOps2...)
@@ -71,15 +71,15 @@ func (s *composeStrategy) PreUpdate(maxAssetBase float64, maxAssetQuote float64,
7171
func (s *composeStrategy) UpdateWithOps(
7272
buyingAOffers []hProtocol.Offer,
7373
sellingAOffers []hProtocol.Offer,
74-
) ([]txnbuild.Operation, error) {
74+
) ([]build.TransactionMutator, error) {
7575
// buy side, flip newTopBuyPrice because it will be inverted from this parent strategy's context of base/quote
7676
buyOps, newTopBuyPriceInverted, e1 := s.buyStrat.UpdateWithOps(buyingAOffers)
7777
newTopBuyPrice := model.InvertNumber(newTopBuyPriceInverted)
7878
// sell side
7979
sellOps, _, e2 := s.sellStrat.UpdateWithOps(sellingAOffers)
8080

8181
// check for errors
82-
ops := []txnbuild.Operation{}
82+
ops := []build.TransactionMutator{}
8383
if e1 != nil && e2 != nil {
8484
return ops, fmt.Errorf("errors on both sides: buying (= %s) and selling (= %s)", e1, e2)
8585
} else if e1 != nil {

plugins/deleteSideStrategy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package plugins
33
import (
44
"log"
55

6+
"github.com/stellar/go/build"
67
hProtocol "github.com/stellar/go/protocols/horizon"
78
"github.com/stellar/go/txnbuild"
89
"github.com/stellar/kelp/api"
@@ -33,14 +34,14 @@ func makeDeleteSideStrategy(
3334
}
3435

3536
// PruneExistingOffers impl
36-
func (s *deleteSideStrategy) PruneExistingOffers(offers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer) {
37+
func (s *deleteSideStrategy) PruneExistingOffers(offers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer) {
3738
log.Printf("deleteSideStrategy: deleting %d offers\n", len(offers))
3839
pruneOps := []txnbuild.Operation{}
3940
for i := 0; i < len(offers); i++ {
4041
pOp := s.sdex.DeleteOffer(offers[i])
4142
pruneOps = append(pruneOps, &pOp)
4243
}
43-
return pruneOps, []hProtocol.Offer{}
44+
return api.ConvertOperation2TM(pruneOps), []hProtocol.Offer{}
4445
}
4546

4647
// PreUpdate impl
@@ -49,8 +50,8 @@ func (s *deleteSideStrategy) PreUpdate(maxAssetBase float64, maxAssetQuote float
4950
}
5051

5152
// UpdateWithOps impl
52-
func (s *deleteSideStrategy) UpdateWithOps(offers []hProtocol.Offer) (ops []txnbuild.Operation, newTopOffer *model.Number, e error) {
53-
return []txnbuild.Operation{}, nil, nil
53+
func (s *deleteSideStrategy) UpdateWithOps(offers []hProtocol.Offer) (ops []build.TransactionMutator, newTopOffer *model.Number, e error) {
54+
return []build.TransactionMutator{}, nil, nil
5455
}
5556

5657
// PostUpdate impl

plugins/mirrorStrategy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"sync"
77

8+
"github.com/stellar/go/build"
89
hProtocol "github.com/stellar/go/protocols/horizon"
910
"github.com/stellar/go/txnbuild"
1011
"github.com/stellar/kelp/api"
@@ -192,8 +193,8 @@ func makeMirrorStrategy(sdex *SDEX, ieif *IEIF, pair *model.TradingPair, baseAss
192193
}
193194

194195
// PruneExistingOffers deletes any extra offers
195-
func (s *mirrorStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer, []hProtocol.Offer) {
196-
return []txnbuild.Operation{}, buyingAOffers, sellingAOffers
196+
func (s *mirrorStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer, []hProtocol.Offer) {
197+
return []build.TransactionMutator{}, buyingAOffers, sellingAOffers
197198
}
198199

199200
// PreUpdate changes the strategy's state in prepration for the update
@@ -230,7 +231,7 @@ func (s *mirrorStrategy) recordBalances() error {
230231
func (s *mirrorStrategy) UpdateWithOps(
231232
buyingAOffers []hProtocol.Offer,
232233
sellingAOffers []hProtocol.Offer,
233-
) ([]txnbuild.Operation, error) {
234+
) ([]build.TransactionMutator, error) {
234235
ob, e := s.exchange.GetOrderBook(s.backingPair, s.orderbookDepth)
235236
if e != nil {
236237
return nil, e
@@ -295,7 +296,7 @@ func (s *mirrorStrategy) UpdateWithOps(
295296
ops = append(ops, sellOps...)
296297
}
297298

298-
return ops, nil
299+
return api.ConvertOperation2TM(ops), nil
299300
}
300301

301302
func (s *mirrorStrategy) updateLevels(

plugins/sdex.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/nikhilsaraf/go-tools/multithreading"
1414
"github.com/pkg/errors"
15+
"github.com/stellar/go/build"
1516
"github.com/stellar/go/clients/horizonclient"
1617
hProtocol "github.com/stellar/go/protocols/horizon"
1718
"github.com/stellar/go/txnbuild"
@@ -358,17 +359,19 @@ func (sdex *SDEX) createModifySellOffer(offer *hProtocol.Offer, selling hProtoco
358359
}
359360

360361
// SubmitOpsSynch is the forced synchronous version of SubmitOps below
361-
func (sdex *SDEX) SubmitOpsSynch(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error {
362+
func (sdex *SDEX) SubmitOpsSynch(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error {
362363
return sdex.submitOps(ops, asyncCallback, false)
363364
}
364365

365366
// SubmitOps submits the passed in operations to the network asynchronously in a single transaction
366-
func (sdex *SDEX) SubmitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error {
367+
func (sdex *SDEX) SubmitOps(ops []build.TransactionMutator, asyncCallback func(hash string, e error)) error {
367368
return sdex.submitOps(ops, asyncCallback, true)
368369
}
369370

370371
// submitOps submits the passed in operations to the network in a single transaction. Asynchronous or not based on flag.
371-
func (sdex *SDEX) submitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error), asyncMode bool) error {
372+
func (sdex *SDEX) submitOps(opsOld []build.TransactionMutator, asyncCallback func(hash string, e error), asyncMode bool) error {
373+
ops := api.ConvertTM2Operation(opsOld)
374+
372375
sdex.incrementSeqNum()
373376
tx := txnbuild.Transaction{
374377
// sequence number is decremented here because Transaction.Build auto increments sequence number

0 commit comments

Comments
 (0)