diff --git a/accounting/pnl/pnl.go b/accounting/pnl/pnl.go index a2a1d6258..aef7e46bc 100644 --- a/accounting/pnl/pnl.go +++ b/accounting/pnl/pnl.go @@ -16,7 +16,8 @@ import ( "strconv" "strings" - "github.com/stellar/go/clients/horizon" + "github.com/stellar/go/clients/horizonclient" + hProtocol "github.com/stellar/go/protocols/horizon" "github.com/stellar/kelp/api" "github.com/stellar/kelp/plugins" ) @@ -51,7 +52,7 @@ func main() { } func getTotalNativeValue(address string, cmcRef string) float64 { - client := horizon.DefaultPublicNetClient + client := horizonclient.DefaultPublicNetClient account := loadAccount(client, address) nativeBal := 0.0 @@ -95,11 +96,12 @@ func getTotalNativeValue(address string, cmcRef string) float64 { return totalNativeValue } -func loadAccount(client *horizon.Client, address string) horizon.Account { - account, e := client.LoadAccount(address) +func loadAccount(client *horizonclient.Client, address string) hProtocol.Account { + acctReq := horizonclient.AccountRequest{AccountID: address} + account, e := client.AccountDetail(acctReq) if e != nil { switch t := e.(type) { - case *horizon.Error: + case *horizonclient.Error: log.Fatal(t.Problem) default: log.Fatal(e) diff --git a/cmd/terminate.go b/cmd/terminate.go index 83be26015..f7ef567cf 100644 --- a/cmd/terminate.go +++ b/cmd/terminate.go @@ -6,7 +6,7 @@ import ( "github.com/nikhilsaraf/go-tools/multithreading" "github.com/spf13/cobra" - "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/model" @@ -38,8 +38,8 @@ func init() { log.Println("Started Terminator for account: ", *configFile.TradingAccount) // --- start initialization of objects ---- - client := &horizon.Client{ - URL: configFile.HorizonURL, + client := &horizonclient.Client{ + HorizonURL: configFile.HorizonURL, HTTP: http.DefaultClient, AppName: "kelp", AppVersion: version, diff --git a/cmd/trade.go b/cmd/trade.go index 61ab0d7bf..0ab95fe16 100644 --- a/cmd/trade.go +++ b/cmd/trade.go @@ -13,7 +13,6 @@ import ( "github.com/nikhilsaraf/go-tools/multithreading" "github.com/spf13/cobra" "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" @@ -195,8 +194,7 @@ func makeExchangeShimSdex( l logger.Logger, botConfig trader.BotConfig, options inputs, - client *horizon.Client, - newClient *horizonclient.Client, + client *horizonclient.Client, ieif *plugins.IEIF, network build.Network, threadTracker *multithreading.ThreadTracker, @@ -263,7 +261,7 @@ func makeExchangeShimSdex( tradingPair.Base: botConfig.AssetBase(), tradingPair.Quote: botConfig.AssetQuote(), } - feeFn := makeFeeFn(l, botConfig, newClient) + feeFn := makeFeeFn(l, botConfig, client) sdex := plugins.MakeSDEX( client, ieif, @@ -292,7 +290,7 @@ func makeStrategy( l logger.Logger, network build.Network, botConfig trader.BotConfig, - client *horizon.Client, + client *horizonclient.Client, sdex *plugins.SDEX, exchangeShim api.ExchangeShim, assetBase hProtocol.Asset, @@ -324,7 +322,7 @@ func makeStrategy( func makeBot( l logger.Logger, botConfig trader.BotConfig, - client *horizon.Client, + client *horizonclient.Client, sdex *plugins.SDEX, exchangeShim api.ExchangeShim, ieif *plugins.IEIF, @@ -397,20 +395,13 @@ func runTradeCmd(options inputs) { Quote: model.Asset(utils.Asset2CodeString(assetQuote)), } - client := &horizon.Client{ - URL: botConfig.HorizonURL, - HTTP: http.DefaultClient, - } - newClient := &horizonclient.Client{ - // TODO horizonclient.Client has a bug in it where it does not use "/" to separate the horizonURL from the fee_stats endpoint - HorizonURL: botConfig.HorizonURL + "/", + client := &horizonclient.Client{ + HorizonURL: botConfig.HorizonURL, HTTP: http.DefaultClient, } if !*options.noHeaders { client.AppName = "kelp" client.AppVersion = version - newClient.AppName = "kelp" - newClient.AppVersion = version p := prefs.Make(prefsFilename) if p.FirstTime() { @@ -439,7 +430,6 @@ func runTradeCmd(options inputs) { botConfig, options, client, - newClient, ieif, network, threadTracker, @@ -560,7 +550,7 @@ func startFillTracking( l logger.Logger, strategy api.Strategy, botConfig trader.BotConfig, - client *horizon.Client, + client *horizonclient.Client, sdex *plugins.SDEX, exchangeShim api.ExchangeShim, tradingPair *model.TradingPair, @@ -607,7 +597,7 @@ func startQueryServer( strategyName string, strategy api.Strategy, botConfig trader.BotConfig, - client *horizon.Client, + client *horizonclient.Client, sdex *plugins.SDEX, exchangeShim api.ExchangeShim, tradingPair *model.TradingPair, @@ -643,14 +633,15 @@ func startQueryServer( }() } -func validateTrustlines(l logger.Logger, client *horizon.Client, botConfig *trader.BotConfig) { +func validateTrustlines(l logger.Logger, client *horizonclient.Client, botConfig *trader.BotConfig) { if !botConfig.IsTradingSdex() { l.Info("no need to validate trustlines because we're not using SDEX as the trading exchange") return } log.Printf("validating trustlines...\n") - account, e := client.LoadAccount(botConfig.TradingAccount()) + acctReq := horizonclient.AccountRequest{AccountID: botConfig.TradingAccount()} + account, e := client.AccountDetail(acctReq) if e != nil { logger.Fatal(l, e) } @@ -679,7 +670,7 @@ func validateTrustlines(l logger.Logger, client *horizon.Client, botConfig *trad func deleteAllOffersAndExit( l logger.Logger, botConfig trader.BotConfig, - client *horizon.Client, + client *horizonclient.Client, sdex *plugins.SDEX, exchangeShim api.ExchangeShim, threadTracker *multithreading.ThreadTracker, diff --git a/gui/backend/get_bot_info.go b/gui/backend/get_bot_info.go index 2bcfb60ca..31ca11dca 100644 --- a/gui/backend/get_bot_info.go +++ b/gui/backend/get_bot_info.go @@ -138,7 +138,7 @@ func (s *APIServer) runGetBotInfoDirect(w http.ResponseWriter, botName string) { } } - offers, e := utils.LoadAllOffers(account.AccountID, horizon.DefaultTestNetClient) + offers, e := utils.LoadAllOffers(account.AccountID, horizonclient.DefaultTestNetClient) if e != nil { s.writeErrorJson(w, fmt.Sprintf("error getting offers for account '%s' for botName '%s': %s\n", botConfig.TradingAccount(), botName, e)) return diff --git a/plugins/priceFeed.go b/plugins/priceFeed.go index ee76382ed..b6ddb216e 100644 --- a/plugins/priceFeed.go +++ b/plugins/priceFeed.go @@ -5,14 +5,14 @@ import ( "strings" "github.com/stellar/go/build" - "github.com/stellar/go/clients/horizon" + "github.com/stellar/go/clients/horizonclient" "github.com/stellar/kelp/api" "github.com/stellar/kelp/model" ) // privateSdexHack is a temporary hack struct for SDEX price feeds pending refactor type privateSdexHack struct { - API *horizon.Client + API *horizonclient.Client Ieif *IEIF Network build.Network } @@ -21,7 +21,7 @@ type privateSdexHack struct { var privateSdexHackVar *privateSdexHack // SetPrivateSdexHack sets the privateSdexHack variable which is temporary until the pending SDEX price feed refactor -func SetPrivateSdexHack(api *horizon.Client, ieif *IEIF, network build.Network) error { +func SetPrivateSdexHack(api *horizonclient.Client, ieif *IEIF, network build.Network) error { if privateSdexHackVar != nil { return fmt.Errorf("privateSdexHack is already set: %+v", privateSdexHackVar) } diff --git a/plugins/sdex.go b/plugins/sdex.go index f66c7d8b5..84fc4a38c 100644 --- a/plugins/sdex.go +++ b/plugins/sdex.go @@ -13,7 +13,7 @@ import ( "github.com/nikhilsaraf/go-tools/multithreading" "github.com/pkg/errors" "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/kelp/api" "github.com/stellar/kelp/model" @@ -33,7 +33,7 @@ const fetchTradesResolution = 300000 // SDEX helps with building and submitting transactions to the Stellar network type SDEX struct { - API *horizon.Client + API *horizonclient.Client SourceAccount string TradingAccount string SourceSeed string @@ -70,7 +70,7 @@ type Balance struct { // MakeSDEX is a factory method for SDEX func MakeSDEX( - api *horizon.Client, + api *horizonclient.Client, ieif *IEIF, exchangeShim api.ExchangeShim, sourceSeed string, @@ -157,7 +157,13 @@ func (sdex *SDEX) GetAssetConverter() *model.AssetConverter { func (sdex *SDEX) incrementSeqNum() { if sdex.reloadSeqNum { log.Println("reloading sequence number") - seqNum, err := sdex.API.SequenceForAccount(sdex.SourceAccount) + acctReq := horizonclient.AccountRequest{AccountID: sdex.SourceAccount} + accountDetail, err := sdex.API.AccountDetail(acctReq) + if err != nil { + log.Printf("error loading account detail: %s\n", err) + return + } + seqNum, err := accountDetail.GetSequenceNumber() if err != nil { log.Printf("error getting seq num: %s\n", err) return @@ -223,7 +229,8 @@ func (sdex *SDEX) minReserve(subentries int32) float64 { // assetBalance returns asset balance, asset trust limit, reserve balance (zero for non-XLM), error func (sdex *SDEX) _assetBalance(asset hProtocol.Asset) (*api.Balance, error) { - account, err := sdex.API.LoadAccount(sdex.TradingAccount) + acctReq := horizonclient.AccountRequest{AccountID: sdex.TradingAccount} + account, err := sdex.API.AccountDetail(acctReq) if err != nil { return nil, fmt.Errorf("error: unable to load account to fetch balance: %s", err) } @@ -433,10 +440,10 @@ func (sdex *SDEX) sign(tx *build.TransactionBuilder) (string, error) { } func (sdex *SDEX) submit(txeB64 string, asyncCallback func(hash string, e error), asyncMode bool) { - resp, err := sdex.API.SubmitTransaction(txeB64) + resp, err := sdex.API.SubmitTransactionXDR(txeB64) if err != nil { - if herr, ok := errors.Cause(err).(*horizon.Error); ok { - var rcs *horizon.TransactionResultCodes + if herr, ok := errors.Cause(err).(*horizonclient.Error); ok { + var rcs *hProtocol.TransactionResultCodes rcs, err = herr.ResultCodes() if err != nil { log.Printf("(async) error: no result codes from horizon: %s\n", err) @@ -530,7 +537,19 @@ func (sdex *SDEX) GetTradeHistory(pair model.TradingPair, maybeCursorStart inter trades := []model.Trade{} for { - tradesPage, e := sdex.API.LoadTrades(baseAsset, quoteAsset, 0, fetchTradesResolution, horizon.Cursor(cursorStart), horizon.Order(horizon.OrderAsc), horizon.Limit(maxPageLimit)) + tradeReq := horizonclient.TradeRequest{ + BaseAssetType: horizonclient.AssetType(baseAsset.Type), + BaseAssetCode: baseAsset.Code, + BaseAssetIssuer: baseAsset.Issuer, + CounterAssetType: horizonclient.AssetType(quoteAsset.Type), + CounterAssetCode: quoteAsset.Code, + CounterAssetIssuer: quoteAsset.Issuer, + Order: horizonclient.OrderAsc, + Cursor: cursorStart, + Limit: uint(maxPageLimit), + } + + tradesPage, e := sdex.API.Trades(tradeReq) if e != nil { if strings.Contains(e.Error(), "Rate limit exceeded") { // return normally, we will continue loading trades in the next call from where we left off @@ -631,7 +650,7 @@ func (sdex *SDEX) getOrderAction(baseAsset hProtocol.Asset, quoteAsset hProtocol } // returns tradeHistoryResult, hitCursorEnd, and any error -func (sdex *SDEX) tradesPage2TradeHistoryResult(baseAsset hProtocol.Asset, quoteAsset hProtocol.Asset, tradesPage horizon.TradesPage, cursorEnd string) (*api.TradeHistoryResult, bool, error) { +func (sdex *SDEX) tradesPage2TradeHistoryResult(baseAsset hProtocol.Asset, quoteAsset hProtocol.Asset, tradesPage hProtocol.TradesPage, cursorEnd string) (*api.TradeHistoryResult, bool, error) { var cursor string trades := []model.Trade{} @@ -688,7 +707,18 @@ func (sdex *SDEX) GetLatestTradeCursor() (interface{}, error) { return nil, fmt.Errorf("error while convertig pair to base and quote asset: %s", e) } - tradesPage, e := sdex.API.LoadTrades(baseAsset, quoteAsset, 0, fetchTradesResolution, horizon.Order(horizon.OrderDesc), horizon.Limit(1)) + tradeReq := horizonclient.TradeRequest{ + BaseAssetType: horizonclient.AssetType(baseAsset.Type), + BaseAssetCode: baseAsset.Code, + BaseAssetIssuer: baseAsset.Issuer, + CounterAssetType: horizonclient.AssetType(quoteAsset.Type), + CounterAssetCode: quoteAsset.Code, + CounterAssetIssuer: quoteAsset.Issuer, + Order: horizonclient.OrderDesc, + Limit: uint(1), + } + + tradesPage, e := sdex.API.Trades(tradeReq) if e != nil { return nil, fmt.Errorf("error while fetching latest trade cursor in SDEX: %s", e) } @@ -713,7 +743,16 @@ func (sdex *SDEX) GetOrderBook(pair *model.TradingPair, maxCount int32) (*model. return nil, fmt.Errorf("cannot get SDEX orderbook: %s", e) } - ob, e := sdex.API.LoadOrderBook(baseAsset, quoteAsset) + obReq := horizonclient.OrderBookRequest{ + SellingAssetType: horizonclient.AssetType(baseAsset.Type), + SellingAssetCode: baseAsset.Code, + SellingAssetIssuer: baseAsset.Issuer, + BuyingAssetType: horizonclient.AssetType(quoteAsset.Type), + BuyingAssetCode: quoteAsset.Code, + BuyingAssetIssuer: quoteAsset.Issuer, + } + + ob, e := sdex.API.OrderBook(obReq) if e != nil { return nil, fmt.Errorf("cannot get SDEX orderbook: %s", e) } diff --git a/plugins/sdexFeed.go b/plugins/sdexFeed.go index 461b06bdf..fd9c3d5e7 100644 --- a/plugins/sdexFeed.go +++ b/plugins/sdexFeed.go @@ -5,7 +5,7 @@ import ( "strings" "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/kelp/api" "github.com/stellar/kelp/model" @@ -44,7 +44,7 @@ func makeSDEXFeed(url string) (*sdexFeed, error) { tradingPair.Quote: *quoteAsset, } - var api *horizon.Client + var api *horizonclient.Client var ieif *IEIF var network build.Network if privateSdexHackVar != nil { @@ -53,7 +53,7 @@ func makeSDEXFeed(url string) (*sdexFeed, error) { network = privateSdexHackVar.Network } else { // use production network by default - api = horizon.DefaultPublicNetClient + api = horizonclient.DefaultPublicNetClient ieif = MakeIEIF(true) network = build.PublicNetwork } diff --git a/query/server.go b/query/server.go index e7b2ad31e..4fb7606da 100644 --- a/query/server.go +++ b/query/server.go @@ -9,7 +9,7 @@ import ( "github.com/stellar/kelp/support/utils" - "github.com/stellar/go/clients/horizon" + "github.com/stellar/go/clients/horizonclient" "github.com/stellar/kelp/api" "github.com/stellar/kelp/model" "github.com/stellar/kelp/plugins" @@ -23,7 +23,7 @@ type Server struct { strategyName string strategy api.Strategy botConfig trader.BotConfig - client *horizon.Client + client *horizonclient.Client sdex *plugins.SDEX exchangeShim api.ExchangeShim tradingPair *model.TradingPair @@ -35,7 +35,7 @@ func MakeServer( strategyName string, strategy api.Strategy, botConfig trader.BotConfig, - client *horizon.Client, + client *horizonclient.Client, sdex *plugins.SDEX, exchangeShim api.ExchangeShim, tradingPair *model.TradingPair, diff --git a/support/utils/functions.go b/support/utils/functions.go index a863f6f10..1b1b7e3ae 100644 --- a/support/utils/functions.go +++ b/support/utils/functions.go @@ -12,7 +12,7 @@ import ( "time" "github.com/stellar/go/build" - "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/go/protocols/horizon/base" @@ -141,16 +141,21 @@ func String2Asset(code string, issuer string) hProtocol.Asset { } // LoadAllOffers loads all the offers for a given account -func LoadAllOffers(account string, api *horizon.Client) ([]hProtocol.Offer, error) { +func LoadAllOffers(account string, api *horizonclient.Client) ([]hProtocol.Offer, error) { // get what orders are outstanding now - offersPage, e := api.LoadAccountOffers(account, horizon.Limit(193)) + offerReq := horizonclient.OfferRequest{ + ForAccount: account, + Limit: uint(193), + } + + offersPage, e := api.Offers(offerReq) if e != nil { return []hProtocol.Offer{}, fmt.Errorf("Can't load offers: %s\n", e) } offersRet := offersPage.Embedded.Records for len(offersPage.Embedded.Records) > 0 { - offersPage, e = api.LoadAccountOffers(account, horizon.Limit(193), horizon.At(offersPage.Links.Next.Href)) + offersPage, e = api.NextOffersPage(offersPage) if e != nil { return []hProtocol.Offer{}, fmt.Errorf("Can't load offers: %s\n", e) } diff --git a/terminator/terminator.go b/terminator/terminator.go index 20397a5bb..15827676e 100644 --- a/terminator/terminator.go +++ b/terminator/terminator.go @@ -8,7 +8,8 @@ import ( "time" "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/kelp/model" "github.com/stellar/kelp/plugins" "github.com/stellar/kelp/support/utils" @@ -18,7 +19,7 @@ const terminatorKey = "term" // Terminator contains the logic to terminate offers type Terminator struct { - api *horizon.Client + api *horizonclient.Client sdex *plugins.SDEX tradingAccount string tickIntervalSeconds int32 @@ -27,7 +28,7 @@ type Terminator struct { // MakeTerminator is a factory method to make a Terminator func MakeTerminator( - api *horizon.Client, + api *horizonclient.Client, sdex *plugins.SDEX, tradingAccount string, tickIntervalSeconds int32, @@ -69,7 +70,8 @@ func (t *Terminator) run() { panic("need to add db-based support, manage-data based support is invalid since we don't write it from trader anymore.") } - account, e := t.api.LoadAccount(t.tradingAccount) + acctReq := horizonclient.AccountRequest{AccountID: t.tradingAccount} + account, e := t.api.AccountDetail(acctReq) if e != nil { log.Println(e) return @@ -123,7 +125,7 @@ func (t *Terminator) run() { } } -func convertToAsset(code string, issuer string) horizon.Asset { +func convertToAsset(code string, issuer string) hProtocol.Asset { if code == utils.Native { return utils.Asset2Asset2(build.NativeAsset()) } @@ -131,7 +133,7 @@ func convertToAsset(code string, issuer string) horizon.Asset { } // deleteOffers deletes passed in offers along with the data for the passed in hash -func (t *Terminator) deleteOffers(sellOffers []horizon.Offer, buyOffers []horizon.Offer, botKey model.BotKey, tsMillis int64) { +func (t *Terminator) deleteOffers(sellOffers []hProtocol.Offer, buyOffers []hProtocol.Offer, botKey model.BotKey, tsMillis int64) { ops := []build.TransactionMutator{} ops = append(ops, t.sdex.DeleteAllOffers(sellOffers)...) ops = append(ops, t.sdex.DeleteAllOffers(buyOffers)...) diff --git a/trader/trader.go b/trader/trader.go index 67fb311ac..6414ff98b 100644 --- a/trader/trader.go +++ b/trader/trader.go @@ -9,7 +9,7 @@ import ( "github.com/nikhilsaraf/go-tools/multithreading" "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/kelp/api" "github.com/stellar/kelp/model" @@ -21,7 +21,7 @@ const maxLumenTrust float64 = math.MaxFloat64 // Trader represents a market making bot, which is composed of various parts include the strategy and various APIs. type Trader struct { - api *horizon.Client + api *horizonclient.Client ieif *plugins.IEIF assetBase hProtocol.Asset assetQuote hProtocol.Asset @@ -51,7 +51,7 @@ type Trader struct { // MakeBot is the factory method for the Trader struct func MakeBot( - api *horizon.Client, + api *horizonclient.Client, ieif *plugins.IEIF, assetBase hProtocol.Asset, assetQuote hProtocol.Asset,