This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
7366a03
commit 8afec86
Showing
8 changed files
with
244 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package plugins | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/interstellar/kelp/api" | ||
"github.com/interstellar/kelp/model" | ||
"github.com/interstellar/kelp/support/utils" | ||
"github.com/stellar/go/clients/horizon" | ||
) | ||
|
||
// sdexFeed represents a pricefeed from the SDEX | ||
type sdexFeed struct { | ||
sdex *SDEX | ||
assetBase *horizon.Asset | ||
assetQuote *horizon.Asset | ||
} | ||
|
||
// ensure that it implements PriceFeed | ||
var _ api.PriceFeed = &sdexFeed{} | ||
|
||
// makeSDEXFeed creates a price feed from buysell's url fields | ||
func makeSDEXFeed(url string) (*sdexFeed, error) { | ||
urlParts := strings.Split(url, "/") | ||
|
||
baseAsset, e := parseHorizonAsset(urlParts[0]) | ||
if e != nil { | ||
return nil, fmt.Errorf("unable to convert base asset url to sdex asset: %s", e) | ||
} | ||
quoteAsset, e := parseHorizonAsset(urlParts[1]) | ||
if e != nil { | ||
return nil, fmt.Errorf("unable to convert quote asset url to sdex asset: %s", e) | ||
} | ||
|
||
tradingPair := &model.TradingPair{ | ||
Base: model.Asset(utils.Asset2CodeString(*baseAsset)), | ||
Quote: model.Asset(utils.Asset2CodeString(*quoteAsset)), | ||
} | ||
sdexAssetMap := map[model.Asset]horizon.Asset{ | ||
tradingPair.Base: *baseAsset, | ||
tradingPair.Quote: *quoteAsset, | ||
} | ||
sdex := MakeSDEX( | ||
privateSdexHackVar.API, | ||
"", | ||
"", | ||
"", | ||
"", | ||
privateSdexHackVar.Network, | ||
nil, | ||
0, | ||
0, | ||
true, | ||
tradingPair, | ||
sdexAssetMap, | ||
) | ||
|
||
return &sdexFeed{ | ||
sdex: sdex, | ||
assetBase: baseAsset, | ||
assetQuote: quoteAsset, | ||
}, nil | ||
} | ||
|
||
func parseHorizonAsset(assetString string) (*horizon.Asset, error) { | ||
parts := strings.Split(assetString, ":") | ||
code := parts[0] | ||
issuer := parts[1] | ||
|
||
asset, e := utils.ParseAsset(code, issuer) | ||
if e != nil { | ||
return nil, fmt.Errorf("could not read horizon asset from string (%s): %s", assetString, e) | ||
} | ||
|
||
return asset, e | ||
} | ||
|
||
// GetPrice returns the SDEX mid price for the trading pair | ||
func (s *sdexFeed) GetPrice() (float64, error) { | ||
orderBook, e := s.sdex.GetOrderBook(s.sdex.pair, 1) | ||
if e != nil { | ||
return 0, fmt.Errorf("unable to get sdex price: %s", e) | ||
} | ||
|
||
topBidPrice := orderBook.Bids()[0].Price | ||
topAskPrice := orderBook.Asks()[0].Price | ||
|
||
centerPrice := topBidPrice.Add(*topAskPrice).Scale(0.5).AsFloat() | ||
return centerPrice, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters