Skip to content

Commit

Permalink
use build package in API interfaces + undo glide removals
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsaraf committed Sep 3, 2019
1 parent 4b3734b commit f4b9025
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 37 deletions.
88 changes: 86 additions & 2 deletions api/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package api

import (
"fmt"
"math"

"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
"github.com/stellar/kelp/model"
)

Expand Down Expand Up @@ -222,11 +225,92 @@ type Balance struct {

// ExchangeShim is the interface we use as a generic API for all crypto exchanges
type ExchangeShim interface {
SubmitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error
SubmitOpsSynch(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error // forced synchronous version of SubmitOps
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 hProtocol.Asset) (*Balance, error)
LoadOffersHack() ([]hProtocol.Offer, error)
Constrainable
OrderbookFetcher
FillTrackable
}

// ConvertOperation2TM is a temporary adapter to support transitioning from the old Go SDK to the new SDK without having to bump the major version
func ConvertOperation2TM(ops []txnbuild.Operation) []build.TransactionMutator {
muts := []build.TransactionMutator{}
for _, o := range ops {
var mob build.ManageOfferBuilder
if mso, ok := o.(*txnbuild.ManageSellOffer); ok {
mob = build.ManageOffer(
false,
build.Amount(mso.Amount),
build.Rate{
Selling: build.Asset{Code: mso.Selling.GetCode(), Issuer: mso.Selling.GetIssuer(), Native: mso.Selling.IsNative()},
Buying: build.Asset{Code: mso.Buying.GetCode(), Issuer: mso.Buying.GetIssuer(), Native: mso.Buying.IsNative()},
Price: build.Price(mso.Price),
},
build.OfferID(mso.OfferID),
)
if mso.SourceAccount != nil {
mob.Mutate(build.SourceAccount{AddressOrSeed: mso.SourceAccount.GetAccountID()})
}
} else {
panic(fmt.Sprintf("could not convert txnbuild.Operation to build.TransactionMutator: %v\n", o))
}
muts = append(muts, mob)
}
return muts
}

// ConvertTM2Operation is a temporary adapter to support transitioning from the old Go SDK to the new SDK without having to bump the major version
func ConvertTM2Operation(muts []build.TransactionMutator) []txnbuild.Operation {
ops := []txnbuild.Operation{}
for _, m := range muts {
var mso *txnbuild.ManageSellOffer
if mob, ok := m.(build.ManageOfferBuilder); ok {
mso = convertMOB2MSO(mob)
} else if mob, ok := m.(*build.ManageOfferBuilder); ok {
mso = convertMOB2MSO(*mob)
} else {
panic(fmt.Sprintf("could not convert build.TransactionMutator to txnbuild.Operation: %v (type=%T)\n", m, m))
}
ops = append(ops, mso)
}
return ops
}

func convertMOB2MSO(mob build.ManageOfferBuilder) *txnbuild.ManageSellOffer {
mso := &txnbuild.ManageSellOffer{
Amount: fmt.Sprintf("%.7f", float64(mob.MO.Amount)/math.Pow(10, 7)),
OfferID: int64(mob.MO.OfferId),
Price: fmt.Sprintf("%.7f", float64(mob.MO.Price.N)/float64(mob.MO.Price.D)),
}
if mob.O.SourceAccount != nil {
mso.SourceAccount = &txnbuild.SimpleAccount{
AccountID: mob.O.SourceAccount.Address(),
}
}

if mob.MO.Buying.Type == xdr.AssetTypeAssetTypeNative {
mso.Buying = txnbuild.NativeAsset{}
} else {
var tipe, code, issuer string
mob.MO.Buying.MustExtract(&tipe, &code, &issuer)
mso.Buying = txnbuild.CreditAsset{
Code: code,
Issuer: issuer,
}
}

if mob.MO.Selling.Type == xdr.AssetTypeAssetTypeNative {
mso.Selling = txnbuild.NativeAsset{}
} else {
var tipe, code, issuer string
mob.MO.Selling.MustExtract(&tipe, &code, &issuer)
mso.Selling = txnbuild.CreditAsset{
Code: code,
Issuer: issuer,
}
}

return mso
}
10 changes: 5 additions & 5 deletions api/strategy.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package api

import (
"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/model"
)

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

// SideStrategy represents a strategy on a single side of the orderbook
type SideStrategy interface {
PruneExistingOffers(offers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer)
PruneExistingOffers(offers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer)
PreUpdate(maxAssetA float64, maxAssetB float64, trustA float64, trustB float64) error
UpdateWithOps(offers []hProtocol.Offer) (ops []txnbuild.Operation, newTopOffer *model.Number, e error)
UpdateWithOps(offers []hProtocol.Offer) (ops []build.TransactionMutator, newTopOffer *model.Number, e error)
PostUpdate() error
GetFillHandlers() ([]FillHandler, error)
}
2 changes: 1 addition & 1 deletion cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ func deleteAllOffersAndExit(
l.Infof("created %d operations to delete offers\n", len(dOps))

if len(dOps) > 0 {
e := exchangeShim.SubmitOpsSynch(dOps, func(hash string, e error) {
e := exchangeShim.SubmitOpsSynch(api.ConvertOperation2TM(dOps), func(hash string, e error) {
if e != nil {
logger.Fatal(l, e)
return
Expand Down
5 changes: 3 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import:
- package: github.com/stellar/go
version: a599ed95b928a7bdcee21cee4999efd05e43c2df
subpackages:
- build
- clients/horizonclient
- support/config
- support/errors
Expand Down
7 changes: 5 additions & 2 deletions plugins/batchedExchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"math/rand"

"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/api"
Expand Down Expand Up @@ -174,12 +175,14 @@ func (b BatchedExchange) GetLatestTradeCursor() (interface{}, error) {
}

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

// SubmitOps performs any finalization or submission step needed by the exchange
func (b BatchedExchange) SubmitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error)) error {
func (b BatchedExchange) SubmitOps(opsOld []build.TransactionMutator, asyncCallback func(hash string, e error)) error {
ops := api.ConvertTM2Operation(opsOld)

var e error
b.commands, e = b.Ops2Commands(ops, b.baseAsset, b.quoteAsset)
if e != nil {
Expand Down
8 changes: 4 additions & 4 deletions plugins/composeStrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"

"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/support/utils"
)

Expand Down Expand Up @@ -39,7 +39,7 @@ func makeComposeStrategy(
}

// PruneExistingOffers impl
func (s *composeStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer, []hProtocol.Offer) {
func (s *composeStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer, []hProtocol.Offer) {
pruneOps1, newBuyingAOffers := s.buyStrat.PruneExistingOffers(buyingAOffers)
pruneOps2, newSellingAOffers := s.sellStrat.PruneExistingOffers(sellingAOffers)
pruneOps1 = append(pruneOps1, pruneOps2...)
Expand Down Expand Up @@ -71,15 +71,15 @@ func (s *composeStrategy) PreUpdate(maxAssetBase float64, maxAssetQuote float64,
func (s *composeStrategy) UpdateWithOps(
buyingAOffers []hProtocol.Offer,
sellingAOffers []hProtocol.Offer,
) ([]txnbuild.Operation, error) {
) ([]build.TransactionMutator, error) {
// buy side, flip newTopBuyPrice because it will be inverted from this parent strategy's context of base/quote
buyOps, newTopBuyPriceInverted, e1 := s.buyStrat.UpdateWithOps(buyingAOffers)
newTopBuyPrice := model.InvertNumber(newTopBuyPriceInverted)
// sell side
sellOps, _, e2 := s.sellStrat.UpdateWithOps(sellingAOffers)

// check for errors
ops := []txnbuild.Operation{}
ops := []build.TransactionMutator{}
if e1 != nil && e2 != nil {
return ops, fmt.Errorf("errors on both sides: buying (= %s) and selling (= %s)", e1, e2)
} else if e1 != nil {
Expand Down
9 changes: 5 additions & 4 deletions plugins/deleteSideStrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plugins
import (
"log"

"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/api"
Expand Down Expand Up @@ -33,14 +34,14 @@ func makeDeleteSideStrategy(
}

// PruneExistingOffers impl
func (s *deleteSideStrategy) PruneExistingOffers(offers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer) {
func (s *deleteSideStrategy) PruneExistingOffers(offers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer) {
log.Printf("deleteSideStrategy: deleting %d offers\n", len(offers))
pruneOps := []txnbuild.Operation{}
for i := 0; i < len(offers); i++ {
pOp := s.sdex.DeleteOffer(offers[i])
pruneOps = append(pruneOps, &pOp)
}
return pruneOps, []hProtocol.Offer{}
return api.ConvertOperation2TM(pruneOps), []hProtocol.Offer{}
}

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

// UpdateWithOps impl
func (s *deleteSideStrategy) UpdateWithOps(offers []hProtocol.Offer) (ops []txnbuild.Operation, newTopOffer *model.Number, e error) {
return []txnbuild.Operation{}, nil, nil
func (s *deleteSideStrategy) UpdateWithOps(offers []hProtocol.Offer) (ops []build.TransactionMutator, newTopOffer *model.Number, e error) {
return []build.TransactionMutator{}, nil, nil
}

// PostUpdate impl
Expand Down
9 changes: 5 additions & 4 deletions plugins/mirrorStrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"sync"

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

// PruneExistingOffers deletes any extra offers
func (s *mirrorStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer, []hProtocol.Offer) {
return []txnbuild.Operation{}, buyingAOffers, sellingAOffers
func (s *mirrorStrategy) PruneExistingOffers(buyingAOffers []hProtocol.Offer, sellingAOffers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer, []hProtocol.Offer) {
return []build.TransactionMutator{}, buyingAOffers, sellingAOffers
}

// PreUpdate changes the strategy's state in prepration for the update
Expand Down Expand Up @@ -230,7 +231,7 @@ func (s *mirrorStrategy) recordBalances() error {
func (s *mirrorStrategy) UpdateWithOps(
buyingAOffers []hProtocol.Offer,
sellingAOffers []hProtocol.Offer,
) ([]txnbuild.Operation, error) {
) ([]build.TransactionMutator, error) {
ob, e := s.exchange.GetOrderBook(s.backingPair, s.orderbookDepth)
if e != nil {
return nil, e
Expand Down Expand Up @@ -295,7 +296,7 @@ func (s *mirrorStrategy) UpdateWithOps(
ops = append(ops, sellOps...)
}

return ops, nil
return api.ConvertOperation2TM(ops), nil
}

func (s *mirrorStrategy) updateLevels(
Expand Down
9 changes: 6 additions & 3 deletions plugins/sdex.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/nikhilsaraf/go-tools/multithreading"
"github.com/pkg/errors"
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizonclient"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
Expand Down Expand Up @@ -358,17 +359,19 @@ func (sdex *SDEX) createModifySellOffer(offer *hProtocol.Offer, selling hProtoco
}

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

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

// submitOps submits the passed in operations to the network in a single transaction. Asynchronous or not based on flag.
func (sdex *SDEX) submitOps(ops []txnbuild.Operation, asyncCallback func(hash string, e error), asyncMode bool) error {
func (sdex *SDEX) submitOps(opsOld []build.TransactionMutator, asyncCallback func(hash string, e error), asyncMode bool) error {
ops := api.ConvertTM2Operation(opsOld)

sdex.incrementSeqNum()
tx := txnbuild.Transaction{
// sequence number is decremented here because Transaction.Build auto increments sequence number
Expand Down
10 changes: 6 additions & 4 deletions plugins/sellSideStrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"

"github.com/stellar/go/build"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/api"
Expand Down Expand Up @@ -67,7 +68,7 @@ func makeSellSideStrategy(
}

// PruneExistingOffers impl
func (s *sellSideStrategy) PruneExistingOffers(offers []hProtocol.Offer) ([]txnbuild.Operation, []hProtocol.Offer) {
func (s *sellSideStrategy) PruneExistingOffers(offers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer) {
// figure out which offers we want to prune
shouldPrune := computeOffersToPrune(offers, s.desiredLevels)

Expand All @@ -91,7 +92,7 @@ func (s *sellSideStrategy) PruneExistingOffers(offers []hProtocol.Offer) ([]txnb
// base and quote here refers to the bot's base and quote, not the base and quote of the sellSideStrategy
log.Printf("offer | %s | level=%d | curPriceQuote=%.8f | curAmtBase=%.8f | pruning=%v\n", s.action, i+1, curPrice, curAmount, isPruning)
}
return pruneOps, updatedOffers
return api.ConvertOperation2TM(pruneOps), updatedOffers
}

// computeOffersToPrune returns a list of bools representing whether we should prune the offer at that position or not
Expand Down Expand Up @@ -259,7 +260,8 @@ func (s *sellSideStrategy) createPrecedingOffers(
}

// UpdateWithOps impl
func (s *sellSideStrategy) UpdateWithOps(offers []hProtocol.Offer) (ops []txnbuild.Operation, newTopOffer *model.Number, e error) {
func (s *sellSideStrategy) UpdateWithOps(offers []hProtocol.Offer) (opsOld []build.TransactionMutator, newTopOffer *model.Number, e error) {
var ops []txnbuild.Operation
deleteOps := []txnbuild.Operation{}

// first we want to re-create any offers that precede our existing offers and are additions to the existing offers that we have
Expand Down Expand Up @@ -325,7 +327,7 @@ func (s *sellSideStrategy) UpdateWithOps(offers []hProtocol.Offer) (ops []txnbui
// prepend deleteOps because we want to delete offers first so we "free" up our liabilities capacity to place the new/modified offers
ops = append(deleteOps, ops...)

return ops, newTopOffer, nil
return api.ConvertOperation2TM(ops), newTopOffer, nil
}

// PostUpdate impl
Expand Down
Loading

0 comments on commit f4b9025

Please sign in to comment.