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

Commit

Permalink
Use horizon/protocol types (#202)
Browse files Browse the repository at this point in the history
* change horizon types

* use hprotocol
  • Loading branch information
poliha authored and nikhilsaraf committed Jul 25, 2019
1 parent b01b3ba commit 4af564d
Show file tree
Hide file tree
Showing 29 changed files with 230 additions and 223 deletions.
6 changes: 3 additions & 3 deletions api/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

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

Expand Down Expand Up @@ -224,8 +224,8 @@ type Balance struct {
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)
GetBalanceHack(asset hProtocol.Asset) (*Balance, error)
LoadOffersHack() ([]hProtocol.Offer, error)
Constrainable
OrderbookFetcher
FillTrackable
Expand Down
10 changes: 5 additions & 5 deletions api/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package api

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

// Strategy represents some logic for a bot to follow while doing market making
type Strategy interface {
PruneExistingOffers(buyingAOffers []horizon.Offer, sellingAOffers []horizon.Offer) ([]build.TransactionMutator, []horizon.Offer, []horizon.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 []horizon.Offer, sellingAOffers []horizon.Offer) ([]build.TransactionMutator, 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 []horizon.Offer) ([]build.TransactionMutator, []horizon.Offer)
PruneExistingOffers(offers []hProtocol.Offer) ([]build.TransactionMutator, []hProtocol.Offer)
PreUpdate(maxAssetA float64, maxAssetB float64, trustA float64, trustB float64) error
UpdateWithOps(offers []horizon.Offer) (ops []build.TransactionMutator, newTopOffer *model.Number, e error)
UpdateWithOps(offers []hProtocol.Offer) (ops []build.TransactionMutator, newTopOffer *model.Number, e error)
PostUpdate() error
GetFillHandlers() ([]FillHandler, error)
}
3 changes: 2 additions & 1 deletion cmd/terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/nikhilsaraf/go-tools/multithreading"
"github.com/spf13/cobra"
"github.com/stellar/go/clients/horizon"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/config"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/plugins"
Expand Down Expand Up @@ -57,7 +58,7 @@ func init() {
-1, // not needed here
false,
nil, // not needed here
map[model.Asset]horizon.Asset{},
map[model.Asset]hProtocol.Asset{},
plugins.SdexFixedFeeFn(0),
)
terminator := terminator.MakeTerminator(client, sdex, *configFile.TradingAccount, configFile.TickIntervalSeconds, configFile.AllowInactiveMinutes)
Expand Down
7 changes: 4 additions & 3 deletions cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/clients/horizonclient"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/config"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
Expand Down Expand Up @@ -258,7 +259,7 @@ func makeExchangeShimSdex(
}
}

sdexAssetMap := map[model.Asset]horizon.Asset{
sdexAssetMap := map[model.Asset]hProtocol.Asset{
tradingPair.Base: botConfig.AssetBase(),
tradingPair.Quote: botConfig.AssetQuote(),
}
Expand Down Expand Up @@ -294,8 +295,8 @@ func makeStrategy(
client *horizon.Client,
sdex *plugins.SDEX,
exchangeShim api.ExchangeShim,
assetBase horizon.Asset,
assetQuote horizon.Asset,
assetBase hProtocol.Asset,
assetQuote hProtocol.Asset,
ieif *plugins.IEIF,
tradingPair *model.TradingPair,
options inputs,
Expand Down
3 changes: 2 additions & 1 deletion gui/backend/autogenerate_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/kelp/gui/model2"
"github.com/stellar/kelp/plugins"
"github.com/stellar/kelp/support/kelpos"
Expand Down Expand Up @@ -138,7 +139,7 @@ func (s *APIServer) setupAccount(address string, signer string, botName string)
return nil
}

func (s *APIServer) checkFundAccount(address string, botName string) (*horizon.Account, error) {
func (s *APIServer) checkFundAccount(address string, botName string) (*hProtocol.Account, error) {
account, e := horizonclient.DefaultTestNetClient.AccountDetail(horizonclient.AccountRequest{AccountID: address})
if e == nil {
log.Printf("account already exists %s for bot '%s', no need to fund via friendbot\n", address, botName)
Expand Down
7 changes: 4 additions & 3 deletions gui/backend/upsert_bot_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/keypair"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/strkey"
"github.com/stellar/kelp/gui/model2"
"github.com/stellar/kelp/plugins"
Expand Down Expand Up @@ -184,7 +185,7 @@ func (s *APIServer) reinitBotCheck(req upsertBotConfigRequest) {
}

// add trustline for trader account if needed
assets := []horizon.Asset{
assets := []hProtocol.Asset{
req.TraderConfig.AssetBase(),
req.TraderConfig.AssetQuote(),
}
Expand Down Expand Up @@ -217,7 +218,7 @@ func (s *APIServer) reinitBotCheck(req upsertBotConfigRequest) {
}()
}

func (s *APIServer) checkAddTrustline(account horizon.Account, kp keypair.KP, traderSeed string, botName string, isTestnet bool, assets []horizon.Asset) error {
func (s *APIServer) checkAddTrustline(account hProtocol.Account, kp keypair.KP, traderSeed string, botName string, isTestnet bool, assets []hProtocol.Asset) error {
network := build.PublicNetwork
client := horizon.DefaultPublicNetClient
if isTestnet {
Expand All @@ -226,7 +227,7 @@ func (s *APIServer) checkAddTrustline(account horizon.Account, kp keypair.KP, tr
}

// find trustlines to be added
trustlines := []horizon.Asset{}
trustlines := []hProtocol.Asset{}
for _, a := range assets {
if a.Type == "native" {
log.Printf("not adding a trustline for the native asset\n")
Expand Down
4 changes: 2 additions & 2 deletions model/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"

"github.com/stellar/go/clients/horizon"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/kelp/support/utils"
)

Expand Down Expand Up @@ -173,7 +173,7 @@ var KrakenAssetConverterOpenOrders = makeAssetConverter(map[Asset]string{
})

// FromHorizonAsset is a factory method
func FromHorizonAsset(hAsset horizon.Asset) Asset {
func FromHorizonAsset(hAsset hProtocol.Asset) Asset {
if hAsset.Type == utils.Native {
return XLM
}
Expand Down
4 changes: 2 additions & 2 deletions model/botKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"strings"

"github.com/stellar/go/clients/horizon"
hProtocol "github.com/stellar/go/protocols/horizon"
)

const botDataKeyPrefix = "b/"
Expand All @@ -30,7 +30,7 @@ func (b BotKey) String() string {
}

// MakeSortedBotKey makes a BotKey by sorting the passed in assets
func MakeSortedBotKey(assetA horizon.Asset, assetB horizon.Asset) *BotKey {
func MakeSortedBotKey(assetA hProtocol.Asset, assetB hProtocol.Asset) *BotKey {
var assetBaseCode, assetBaseIssuer, assetQuoteCode, assetQuoteIssuer string
if assetA.Type == native && assetB.Type == native {
log.Fatal("invalid asset types, both cannot be native")
Expand Down
6 changes: 3 additions & 3 deletions plugins/balancedStrategy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package plugins

import (
"github.com/stellar/go/clients/horizon"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/support/utils"
Expand Down Expand Up @@ -34,8 +34,8 @@ func makeBalancedStrategy(
sdex *SDEX,
pair *model.TradingPair,
ieif *IEIF,
assetBase *horizon.Asset,
assetQuote *horizon.Asset,
assetBase *hProtocol.Asset,
assetQuote *hProtocol.Asset,
config *balancedConfig,
) api.Strategy {
orderConstraints := sdex.GetOrderConstraints(pair)
Expand Down
41 changes: 20 additions & 21 deletions plugins/batchedExchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math/rand"

"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/xdr"
"github.com/stellar/kelp/api"
Expand All @@ -26,8 +25,8 @@ type BatchedExchange struct {
commands []Command
inner api.Exchange
simMode bool
baseAsset horizon.Asset
quoteAsset horizon.Asset
baseAsset hProtocol.Asset
quoteAsset hProtocol.Asset
tradingAccount string
orderID2OfferID map[string]int64
offerID2OrderID map[int64]string
Expand All @@ -39,8 +38,8 @@ var _ api.ExchangeShim = BatchedExchange{}
func MakeBatchedExchange(
inner api.Exchange,
simMode bool,
baseAsset horizon.Asset,
quoteAsset horizon.Asset,
baseAsset hProtocol.Asset,
quoteAsset hProtocol.Asset,
tradingAccount string,
) *BatchedExchange {
return &BatchedExchange{
Expand Down Expand Up @@ -109,7 +108,7 @@ func MakeCommandCancel(openOrder *model.OpenOrder) Command {
}

// GetBalanceHack impl
func (b BatchedExchange) GetBalanceHack(asset horizon.Asset) (*api.Balance, error) {
func (b BatchedExchange) GetBalanceHack(asset hProtocol.Asset) (*api.Balance, error) {
modelAsset := model.FromHorizonAsset(asset)
balances, e := b.GetAccountBalances([]interface{}{modelAsset})
if e != nil {
Expand All @@ -127,7 +126,7 @@ func (b BatchedExchange) GetBalanceHack(asset horizon.Asset) (*api.Balance, erro
}

// LoadOffersHack impl
func (b BatchedExchange) LoadOffersHack() ([]horizon.Offer, error) {
func (b BatchedExchange) LoadOffersHack() ([]hProtocol.Offer, error) {
pair := &model.TradingPair{
Base: model.FromHorizonAsset(b.baseAsset),
Quote: model.FromHorizonAsset(b.quoteAsset),
Expand All @@ -137,9 +136,9 @@ func (b BatchedExchange) LoadOffersHack() ([]horizon.Offer, error) {
return nil, fmt.Errorf("error fetching open orders in LoadOffersHack: %s", e)
}

offers := []horizon.Offer{}
offers := []hProtocol.Offer{}
for i, v := range openOrders {
var offers1 []horizon.Offer
var offers1 []hProtocol.Offer
offers1, e = b.OpenOrders2Offers(v, b.baseAsset, b.quoteAsset, b.tradingAccount)
if e != nil {
return nil, fmt.Errorf("error converting open orders to offers in iteration %v in LoadOffersHack: %s", i, e)
Expand Down Expand Up @@ -298,7 +297,7 @@ func (b BatchedExchange) genUniqueID() int64 {
}

// OpenOrders2Offers converts...
func (b BatchedExchange) OpenOrders2Offers(orders []model.OpenOrder, baseAsset horizon.Asset, quoteAsset horizon.Asset, tradingAccount string) ([]hProtocol.Offer, error) {
func (b BatchedExchange) OpenOrders2Offers(orders []model.OpenOrder, baseAsset hProtocol.Asset, quoteAsset hProtocol.Asset, tradingAccount string) ([]hProtocol.Offer, error) {
offers := []hProtocol.Offer{}
for _, order := range orders {
sellingAsset := baseAsset
Expand All @@ -316,7 +315,7 @@ func (b BatchedExchange) OpenOrders2Offers(orders []model.OpenOrder, baseAsset h
amount = fmt.Sprintf("%.8f", order.Volume.AsFloat()*order.Price.AsFloat())
invertedPrice := model.InvertNumber(order.Price)
// invert price ratio here instead of using convert2Price again since it has an overflow for XLM/BTC
price = horizon.Price{
price = hProtocol.Price{
N: price.D,
D: price.N,
}
Expand Down Expand Up @@ -353,18 +352,18 @@ func (b BatchedExchange) OpenOrders2Offers(orders []model.OpenOrder, baseAsset h
return offers, nil
}

func convert2Price(number *model.Number) (horizon.Price, error) {
func convert2Price(number *model.Number) (hProtocol.Price, error) {
n, d, e := number.AsRatio()
if e != nil {
return horizon.Price{}, fmt.Errorf("unable to convert2Price: %s", e)
return hProtocol.Price{}, fmt.Errorf("unable to convert2Price: %s", e)
}
return horizon.Price{
return hProtocol.Price{
N: n,
D: d,
}, nil
}

func assetsEqual(hAsset horizon.Asset, xAsset xdr.Asset) (bool, error) {
func assetsEqual(hAsset hProtocol.Asset, xAsset xdr.Asset) (bool, error) {
if xAsset.Type == xdr.AssetTypeAssetTypeNative {
return hAsset.Type == utils.Native, nil
} else if hAsset.Type == utils.Native {
Expand All @@ -380,7 +379,7 @@ func assetsEqual(hAsset horizon.Asset, xAsset xdr.Asset) (bool, error) {
}

// manageOffer2Order converts a manage offer operation to a model.Order
func manageOffer2Order(mob *build.ManageOfferBuilder, baseAsset horizon.Asset, quoteAsset horizon.Asset, orderConstraints *model.OrderConstraints) (*model.Order, error) {
func manageOffer2Order(mob *build.ManageOfferBuilder, baseAsset hProtocol.Asset, quoteAsset hProtocol.Asset, orderConstraints *model.OrderConstraints) (*model.Order, error) {
orderAction := model.OrderActionSell
price := model.NumberFromFloat(float64(mob.MO.Price.N)/float64(mob.MO.Price.D), largePrecision)
volume := model.NumberFromFloat(float64(mob.MO.Amount)/math.Pow(10, 7), largePrecision)
Expand Down Expand Up @@ -423,7 +422,7 @@ func order2OpenOrder(order *model.Order, txID *model.TransactionID) *model.OpenO
}

// Ops2Commands converts...
func (b BatchedExchange) Ops2Commands(ops []build.TransactionMutator, baseAsset horizon.Asset, quoteAsset horizon.Asset) ([]Command, error) {
func (b BatchedExchange) Ops2Commands(ops []build.TransactionMutator, baseAsset hProtocol.Asset, quoteAsset hProtocol.Asset) ([]Command, error) {
pair := &model.TradingPair{
Base: model.FromHorizonAsset(baseAsset),
Quote: model.FromHorizonAsset(quoteAsset),
Expand All @@ -434,8 +433,8 @@ func (b BatchedExchange) Ops2Commands(ops []build.TransactionMutator, baseAsset
// Ops2CommandsHack converts...
func Ops2CommandsHack(
ops []build.TransactionMutator,
baseAsset horizon.Asset,
quoteAsset horizon.Asset,
baseAsset hProtocol.Asset,
quoteAsset hProtocol.Asset,
offerID2OrderID map[int64]string, // if map is nil then we ignore ID errors
orderConstraints *model.OrderConstraints,
) ([]Command, error) {
Expand Down Expand Up @@ -464,8 +463,8 @@ func Ops2CommandsHack(
// op2CommandsHack converts one op to possibly many Commands
func op2CommandsHack(
manageOffer *build.ManageOfferBuilder,
baseAsset horizon.Asset,
quoteAsset horizon.Asset,
baseAsset hProtocol.Asset,
quoteAsset hProtocol.Asset,
offerID2OrderID map[int64]string, // if map is nil then we ignore ID errors
orderConstraints *model.OrderConstraints,
) ([]Command, error) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/buysellStrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package plugins
import (
"fmt"

"github.com/stellar/go/clients/horizon"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/support/utils"
Expand Down Expand Up @@ -63,8 +63,8 @@ func makeBuySellStrategy(
sdex *SDEX,
pair *model.TradingPair,
ieif *IEIF,
assetBase *horizon.Asset,
assetQuote *horizon.Asset,
assetBase *hProtocol.Asset,
assetQuote *hProtocol.Asset,
config *BuySellConfig,
) (api.Strategy, error) {
offsetSell := rateOffset{
Expand Down
Loading

0 comments on commit 4af564d

Please sign in to comment.