2021-05-23 04:11:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.PersistentFlags().String("okex-api-key", "", "okex api key")
|
|
|
|
rootCmd.PersistentFlags().String("okex-api-secret", "", "okex api secret")
|
|
|
|
rootCmd.PersistentFlags().String("okex-api-passphrase", "", "okex api secret")
|
|
|
|
rootCmd.PersistentFlags().String("symbol", "BNBUSDT", "symbol")
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "okex-book",
|
|
|
|
Short: "okex book",
|
|
|
|
|
|
|
|
// SilenceUsage is an option to silence usage when an error occurs.
|
|
|
|
SilenceUsage: true,
|
|
|
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
symbol := viper.GetString("symbol")
|
|
|
|
if len(symbol) == 0 {
|
|
|
|
return errors.New("empty symbol")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, secret, passphrase := viper.GetString("okex-api-key"),
|
|
|
|
viper.GetString("okex-api-secret"),
|
|
|
|
viper.GetString("okex-api-passphrase")
|
|
|
|
if len(key) == 0 || len(secret) == 0 {
|
|
|
|
return errors.New("empty key, secret or passphrase")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := okexapi.NewClient()
|
|
|
|
client.Auth(key, secret, passphrase)
|
|
|
|
|
2021-05-23 05:44:08 +00:00
|
|
|
log.Infof("ACCOUNT BALANCES:")
|
|
|
|
balanceSummaries, err := client.AccountBalances()
|
2021-05-23 04:11:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-23 05:44:08 +00:00
|
|
|
for _, balanceSummary := range balanceSummaries {
|
2021-05-23 04:11:27 +00:00
|
|
|
log.Infof("%+v", balanceSummary)
|
|
|
|
}
|
|
|
|
|
2021-05-23 05:44:08 +00:00
|
|
|
log.Infof("ASSET BALANCES:")
|
|
|
|
assetBalances, err := client.AssetBalances()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, balance := range assetBalances {
|
|
|
|
log.Infof("%T%+v", balance, balance)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("ASSET CURRENCIES:")
|
|
|
|
currencies, err := client.AssetCurrencies()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, currency := range currencies {
|
|
|
|
log.Infof("%T%+v", currency, currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("MARKET TICKERS:")
|
|
|
|
tickers, err := client.MarketTickers("SPOT")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ticker := range tickers {
|
|
|
|
log.Infof("%T%+v", ticker, ticker)
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker, err := client.MarketTicker("ETH-USDT")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("TICKER:")
|
|
|
|
log.Infof("%T%+v", ticker, ticker)
|
|
|
|
|
2021-05-23 04:11:27 +00:00
|
|
|
_ = ctx
|
|
|
|
// cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if _, err := os.Stat(".env.local"); err == nil {
|
|
|
|
if err := godotenv.Load(".env.local"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
|
|
|
|
|
|
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
|
|
|
log.WithError(err).Error("bind pflags error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
|
|
|
|
log.WithError(err).Error("cmd error")
|
|
|
|
}
|
|
|
|
}
|