2020-10-12 14:46:06 +00:00
|
|
|
package cmdutil
|
|
|
|
|
|
|
|
import (
|
2020-11-09 08:34:35 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance"
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/max"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-02-02 03:44:07 +00:00
|
|
|
func NewExchangeStandard(n types.ExchangeName, key, secret string) (types.Exchange, error) {
|
|
|
|
if len(key) == 0 || len(secret) == 0 {
|
|
|
|
return nil, errors.New("binance: empty key or secret")
|
2020-10-26 09:00:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
switch n {
|
|
|
|
|
|
|
|
case types.ExchangeBinance:
|
|
|
|
return binance.New(key, secret), nil
|
|
|
|
|
|
|
|
case types.ExchangeMax:
|
|
|
|
return max.New(key, secret), nil
|
|
|
|
|
2020-10-26 09:00:17 +00:00
|
|
|
default:
|
2020-11-09 08:34:35 +00:00
|
|
|
return nil, fmt.Errorf("unsupported exchange: %v", n)
|
2020-10-26 09:00:17 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
2020-10-26 09:00:17 +00:00
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
|
2021-02-02 03:44:07 +00:00
|
|
|
func NewExchangeWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.Exchange, error) {
|
|
|
|
if len(varPrefix) == 0 {
|
|
|
|
varPrefix = n.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
key := viper.GetString(varPrefix + "-api-key")
|
|
|
|
secret := viper.GetString(varPrefix + "-api-secret")
|
|
|
|
if len(key) == 0 || len(secret) == 0 {
|
|
|
|
return nil, errors.New("max: empty key or secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewExchangeStandard(n, key, secret)
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:00:17 +00:00
|
|
|
// NewExchange constructor exchange object from viper config.
|
|
|
|
func NewExchange(n types.ExchangeName) (types.Exchange, error) {
|
|
|
|
return NewExchangeWithEnvVarPrefix(n, "")
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|