Skip to content

Commit

Permalink
Adding GetDepositAddress func
Browse files Browse the repository at this point in the history
  • Loading branch information
saniales authored and wb73 committed Oct 30, 2018
1 parent 35a46e6 commit 990e208
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 99 deletions.
18 changes: 11 additions & 7 deletions bot_helpers/bot_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ import (
)

//InitExchange initialize a new ExchangeWrapper binded to the specified exchange provided.
func InitExchange(exchangeConfig environment.ExchangeConfig, simulatedMode bool, fakeBalances map[string]decimal.Decimal) exchanges.ExchangeWrapper {
func InitExchange(exchangeConfig environment.ExchangeConfig, simulatedMode bool, fakeBalances map[string]decimal.Decimal, depositAddresses map[string]string) exchanges.ExchangeWrapper {
if depositAddresses == nil && !simulatedMode {
return nil
}

var exch exchanges.ExchangeWrapper
switch exchangeConfig.ExchangeName {
case "bittrex":
exch = exchanges.NewBittrexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
exch = exchanges.NewBittrexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey, depositAddresses)
case "bittrexV2":
exch = exchanges.NewBittrexV2Wrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
exch = exchanges.NewBittrexV2Wrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey, depositAddresses)
case "poloniex":
exch = exchanges.NewPoloniexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
exch = exchanges.NewPoloniexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey, depositAddresses)
case "binance":
exch = exchanges.NewBinanceWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
exch = exchanges.NewBinanceWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey, depositAddresses)
case "bitfinex":
exch = exchanges.NewBitfinexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
exch = exchanges.NewBitfinexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey, depositAddresses)
case "hitbtc":
exch = exchanges.NewHitBtcV2Wrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
exch = exchanges.NewHitBtcV2Wrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey, depositAddresses)
default:
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func executeStartCommand(cmd *cobra.Command, args []string) {
fmt.Print("Getting exchange info ... ")
wrappers := make([]exchanges.ExchangeWrapper, len(botConfig.ExchangeConfigs))
for i, config := range botConfig.ExchangeConfigs {
wrappers[i] = helpers.InitExchange(config, botConfig.SimulationModeOn, config.FakeBalances)
wrappers[i] = helpers.InitExchange(config, botConfig.SimulationModeOn, config.FakeBalances, config.DepositAddresses)
}
fmt.Println("DONE")

Expand Down
30 changes: 19 additions & 11 deletions exchanges/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ import (

// BinanceWrapper represents the wrapper for the Binance exchange.
type BinanceWrapper struct {
api *binance.Client
summaries *SummaryCache
candles *CandlesCache
orderbook *OrderbookCache
websocketOn bool
api *binance.Client
summaries *SummaryCache
candles *CandlesCache
orderbook *OrderbookCache
depositAddresses map[string]string
websocketOn bool
}

// NewBinanceWrapper creates a generic wrapper of the binance API.
func NewBinanceWrapper(publicKey string, secretKey string) ExchangeWrapper {
func NewBinanceWrapper(publicKey string, secretKey string, depositAddresses map[string]string) ExchangeWrapper {
client := binance.NewClient(publicKey, secretKey)
return &BinanceWrapper{
api: client,
summaries: NewSummaryCache(),
candles: NewCandlesCache(),
orderbook: NewOrderbookCache(),
websocketOn: false,
api: client,
summaries: NewSummaryCache(),
candles: NewCandlesCache(),
orderbook: NewOrderbookCache(),
depositAddresses: depositAddresses,
websocketOn: false,
}
}

Expand Down Expand Up @@ -278,6 +280,12 @@ func (wrapper *BinanceWrapper) GetBalance(symbol string) (*decimal.Decimal, erro
return nil, errors.New("Symbol not found")
}

// GetDepositAddress gets the deposit address for the specified coin on the exchange.
func (wrapper *BinanceWrapper) GetDepositAddress(coinTicker string) (string, bool) {
addr, exists := wrapper.depositAddresses[coinTicker]
return addr, exists
}

// CalculateTradingFees calculates the trading fees for an order on a specified market.
//
// NOTE: In Binance fees are currently hardcoded.
Expand Down
10 changes: 9 additions & 1 deletion exchanges/bitfinex.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ type BitfinexWrapper struct {
unsubscribeChannels map[string]chan bool
summaries *SummaryCache
orderbook *OrderbookCache
depositAddresses map[string]string
}

// NewBitfinexWrapper creates a generic wrapper of the bittrex API.
func NewBitfinexWrapper(publicKey string, secretKey string) ExchangeWrapper {
func NewBitfinexWrapper(publicKey string, secretKey string, depositAddresses map[string]string) ExchangeWrapper {
return &BitfinexWrapper{
api: bitfinex.NewClient().Auth(publicKey, secretKey),
unsubscribeChannels: make(map[string]chan bool),
summaries: NewSummaryCache(),
orderbook: NewOrderbookCache(),
websocketOn: false,
depositAddresses: depositAddresses,
}
}

Expand Down Expand Up @@ -233,6 +235,12 @@ func (wrapper *BitfinexWrapper) GetBalance(symbol string) (*decimal.Decimal, err
return nil, errors.New("Symbol not found")
}

// GetDepositAddress gets the deposit address for the specified coin on the exchange.
func (wrapper *BitfinexWrapper) GetDepositAddress(coinTicker string) (string, bool) {
addr, exists := wrapper.depositAddresses[coinTicker]
return addr, exists
}

// CalculateTradingFees calculates the trading fees for an order on a specified market.
//
// NOTE: In Bitfinex fees are currently hardcoded.
Expand Down
56 changes: 32 additions & 24 deletions exchanges/bittrex.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,31 @@ type BittrexWrapper struct {
candles *CandlesCache
websocketOn bool
unsubscribeChannels map[*environment.Market]chan bool
depositAddresses map[string]string
}

// NewBittrexWrapper creates a generic wrapper of the bittrex API.
func NewBittrexWrapper(publicKey string, secretKey string) ExchangeWrapper {
return BittrexWrapper{
api: api.New(publicKey, secretKey),
websocketOn: false,
summaries: NewSummaryCache(),
candles: NewCandlesCache(),
func NewBittrexWrapper(publicKey string, secretKey string, depositAddresses map[string]string) ExchangeWrapper {
return &BittrexWrapper{
api: api.New(publicKey, secretKey),
websocketOn: false,
summaries: NewSummaryCache(),
candles: NewCandlesCache(),
depositAddresses: depositAddresses,
}
}

// Name returns the name of the wrapped exchange.
func (wrapper BittrexWrapper) Name() string {
func (wrapper *BittrexWrapper) Name() string {
return "bittrex"
}

func (wrapper BittrexWrapper) String() string {
func (wrapper *BittrexWrapper) String() string {
return wrapper.Name()
}

// GetMarkets gets all the markets info.
func (wrapper BittrexWrapper) GetMarkets() ([]*environment.Market, error) {
func (wrapper *BittrexWrapper) GetMarkets() ([]*environment.Market, error) {
bittrexMarkets, err := wrapper.api.GetMarkets()
if err != nil {
return nil, err
Expand All @@ -75,7 +77,7 @@ func (wrapper BittrexWrapper) GetMarkets() ([]*environment.Market, error) {
}

// GetOrderBook gets the order(ASK + BID) book of a market.
func (wrapper BittrexWrapper) GetOrderBook(market *environment.Market) (*environment.OrderBook, error) {
func (wrapper *BittrexWrapper) GetOrderBook(market *environment.Market) (*environment.OrderBook, error) {
bittrexOrderBook, err := wrapper.api.GetOrderBook(MarketNameFor(market, wrapper), "both")
if err != nil {
return nil, err
Expand All @@ -99,29 +101,29 @@ func (wrapper BittrexWrapper) GetOrderBook(market *environment.Market) (*environ
}

// BuyLimit performs a limit buy action.
func (wrapper BittrexWrapper) BuyLimit(market *environment.Market, amount float64, limit float64) (string, error) {
func (wrapper *BittrexWrapper) BuyLimit(market *environment.Market, amount float64, limit float64) (string, error) {
orderNumber, err := wrapper.api.BuyLimit(MarketNameFor(market, wrapper), decimal.NewFromFloat(amount), decimal.NewFromFloat(limit))
return orderNumber, err
}

// SellLimit performs a limit sell action.
func (wrapper BittrexWrapper) SellLimit(market *environment.Market, amount float64, limit float64) (string, error) {
func (wrapper *BittrexWrapper) SellLimit(market *environment.Market, amount float64, limit float64) (string, error) {
orderNumber, err := wrapper.api.SellLimit(MarketNameFor(market, wrapper), decimal.NewFromFloat(amount), decimal.NewFromFloat(limit))
return orderNumber, err
}

// BuyMarket performs a market buy action.
func (wrapper BittrexWrapper) BuyMarket(market *environment.Market, amount float64) (string, error) {
func (wrapper *BittrexWrapper) BuyMarket(market *environment.Market, amount float64) (string, error) {
panic("Not supported on bittrex")
}

// SellMarket performs a market sell action.
func (wrapper BittrexWrapper) SellMarket(market *environment.Market, amount float64) (string, error) {
func (wrapper *BittrexWrapper) SellMarket(market *environment.Market, amount float64) (string, error) {
panic("Not supported on bittrex")
}

// GetTicker gets the updated ticker for a market.
func (wrapper BittrexWrapper) GetTicker(market *environment.Market) (*environment.Ticker, error) {
func (wrapper *BittrexWrapper) GetTicker(market *environment.Market) (*environment.Ticker, error) {
bittrexTicker, err := wrapper.api.GetTicker(MarketNameFor(market, wrapper))
if err != nil {
return nil, err
Expand All @@ -135,7 +137,7 @@ func (wrapper BittrexWrapper) GetTicker(market *environment.Market) (*environmen
}

// GetMarketSummary gets the current market summary.
func (wrapper BittrexWrapper) GetMarketSummary(market *environment.Market) (*environment.MarketSummary, error) {
func (wrapper *BittrexWrapper) GetMarketSummary(market *environment.Market) (*environment.MarketSummary, error) {
if !wrapper.websocketOn {
summaryArray, err := wrapper.api.GetMarketSummary(MarketNameFor(market, wrapper))
if err != nil {
Expand Down Expand Up @@ -172,12 +174,12 @@ func convertFromBittrexCandle(candle api.Candle) environment.CandleStick {
}

// GetCandles gets the candle data from the exchange.
func (wrapper BittrexWrapper) GetCandles(market *environment.Market) ([]environment.CandleStick, error) {
func (wrapper *BittrexWrapper) GetCandles(market *environment.Market) ([]environment.CandleStick, error) {
panic("Not supported in Bittrex V1")
}

// GetBalance gets the balance of the user of the specified currency.
func (wrapper BittrexWrapper) GetBalance(symbol string) (*decimal.Decimal, error) {
func (wrapper *BittrexWrapper) GetBalance(symbol string) (*decimal.Decimal, error) {
balance, err := wrapper.api.GetBalance(symbol)
if err != nil {
return nil, err
Expand All @@ -186,10 +188,16 @@ func (wrapper BittrexWrapper) GetBalance(symbol string) (*decimal.Decimal, error
return &balance.Available, nil
}

// GetDepositAddress gets the deposit address for the specified coin on the exchange.
func (wrapper *BittrexWrapper) GetDepositAddress(coinTicker string) (string, bool) {
addr, exists := wrapper.depositAddresses[coinTicker]
return addr, exists
}

// CalculateTradingFees calculates the trading fees for an order on a specified market.
//
// NOTE: In Bittrex fees are hardcoded due to the inability to obtain them via API before placing an order.
func (wrapper BittrexWrapper) CalculateTradingFees(market *environment.Market, amount float64, limit float64, orderType TradeType) float64 {
func (wrapper *BittrexWrapper) CalculateTradingFees(market *environment.Market, amount float64, limit float64, orderType TradeType) float64 {
var feePercentage float64
if orderType == MakerTrade {
feePercentage = 0.0025
Expand All @@ -203,12 +211,12 @@ func (wrapper BittrexWrapper) CalculateTradingFees(market *environment.Market, a
}

// CalculateWithdrawFees calculates the withdrawal fees on a specified market.
func (wrapper BittrexWrapper) CalculateWithdrawFees(market *environment.Market, amount float64) float64 {
func (wrapper *BittrexWrapper) CalculateWithdrawFees(market *environment.Market, amount float64) float64 {
panic("Not Implemented")
}

// FeedConnect connects to the feed of the exchange.
func (wrapper BittrexWrapper) FeedConnect(markets []*environment.Market) error {
func (wrapper *BittrexWrapper) FeedConnect(markets []*environment.Market) error {
return ErrWebsocketNotSupported

wrapper.websocketOn = true
Expand All @@ -222,8 +230,8 @@ func (wrapper BittrexWrapper) FeedConnect(markets []*environment.Market) error {

// SubscribeMarketSummaryFeed subscribes to the Market Summary Feed service.
//
// NOTE: Not supported on Bittrex v1 API, use BittrexWrapperV2.
func (wrapper BittrexWrapper) subscribeMarketSummaryFeed(market *environment.Market) {
// NOTE: Not supported on Bittrex v1 API, use *BittrexWrapperV2.
func (wrapper *BittrexWrapper) subscribeMarketSummaryFeed(market *environment.Market) {
results := make(chan api.ExchangeState)

wrapper.api.SubscribeExchangeUpdate(MarketNameFor(market, wrapper), results, wrapper.unsubscribeChannels[market])
Expand All @@ -242,7 +250,7 @@ func (wrapper BittrexWrapper) subscribeMarketSummaryFeed(market *environment.Mar
}

// Withdraw performs a withdraw operation from the exchange to a destination address.
func (wrapper BittrexWrapper) Withdraw(destinationAddress string, coinTicker string, amount float64) error {
func (wrapper *BittrexWrapper) Withdraw(destinationAddress string, coinTicker string, amount float64) error {
_, err := wrapper.api.Withdraw(destinationAddress, coinTicker, decimal.NewFromFloat(amount))
if err != nil {
return err
Expand Down
Loading

0 comments on commit 990e208

Please sign in to comment.