2021-02-27 09:24:59 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2021-03-14 02:31:53 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2021-02-27 09:24:59 +00:00
|
|
|
)
|
|
|
|
|
2021-03-14 02:31:53 +00:00
|
|
|
func init() {
|
|
|
|
balancesCmd.Flags().String("session", "", "the exchange session name for querying balances")
|
|
|
|
RootCmd.AddCommand(balancesCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// go run ./cmd/bbgo balances --session=ftx
|
2021-02-27 09:24:59 +00:00
|
|
|
var balancesCmd = &cobra.Command{
|
2022-03-03 03:36:06 +00:00
|
|
|
Use: "balances [--session SESSION]",
|
2022-02-22 08:52:14 +00:00
|
|
|
Short: "Show user account balances",
|
2021-02-27 09:24:59 +00:00
|
|
|
SilenceUsage: true,
|
2022-03-03 08:34:32 +00:00
|
|
|
PreRunE: cobraInitRequired([]string{
|
|
|
|
"session",
|
|
|
|
}),
|
2021-02-27 09:24:59 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := context.Background()
|
2021-03-14 02:31:53 +00:00
|
|
|
|
2021-05-06 15:44:05 +00:00
|
|
|
sessionName, err := cmd.Flags().GetString("session")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-14 02:31:53 +00:00
|
|
|
environ := bbgo.NewEnvironment()
|
|
|
|
|
|
|
|
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:50:26 +00:00
|
|
|
if len(sessionName) > 0 {
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("session %s not found", sessionName)
|
|
|
|
}
|
2021-03-14 02:31:53 +00:00
|
|
|
|
2021-05-06 15:50:26 +00:00
|
|
|
b, err := session.Exchange.QueryAccountBalances(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Print()
|
|
|
|
} else {
|
|
|
|
for _, session := range environ.Sessions() {
|
|
|
|
|
|
|
|
b, err := session.Exchange.QueryAccountBalances(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("SESSION %s", session.Name)
|
|
|
|
b.Print()
|
|
|
|
}
|
2021-02-27 09:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|