mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
kucoin: add query accounts api
This commit is contained in:
parent
cd69994647
commit
c8ba3f7c1b
70
examples/kucoin-accounts/main.go
Normal file
70
examples/kucoin-accounts/main.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/exchange/kucoin/kucoinapi"
|
||||
"github.com/joho/godotenv"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().String("kucoin-api-key", "", "okex api key")
|
||||
rootCmd.PersistentFlags().String("kucoin-api-secret", "", "okex api secret")
|
||||
rootCmd.PersistentFlags().String("kucoin-api-passphrase", "", "okex api secret")
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "kucoin-accounts",
|
||||
Short: "kucoin accounts",
|
||||
|
||||
// SilenceUsage is an option to silence usage when an error occurs.
|
||||
SilenceUsage: true,
|
||||
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
accounts, err := client.AccountService.QueryAccounts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("accounts: %+v", accounts)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var client *kucoinapi.RestClient = 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")
|
||||
}
|
||||
|
||||
client = kucoinapi.NewClient()
|
||||
|
||||
key, secret, passphrase := viper.GetString("kucoin-api-key"),
|
||||
viper.GetString("kucoin-api-secret"),
|
||||
viper.GetString("kucoin-api-passphrase")
|
||||
|
||||
if len(key) == 0 || len(secret) == 0 || len(passphrase) == 0 {
|
||||
log.Fatal("empty key, secret or passphrase")
|
||||
}
|
||||
|
||||
client.Auth(key, secret, passphrase)
|
||||
|
||||
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
|
||||
log.WithError(err).Error("cmd error")
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package kucoinapi
|
||||
|
||||
import "github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
|
||||
type AccountService struct {
|
||||
client *RestClient
|
||||
|
@ -35,3 +36,36 @@ func (s *AccountService) QuerySubAccounts() ([]SubAccount, error) {
|
|||
|
||||
return apiResponse.Data, nil
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
ID string `json:"id"`
|
||||
Currency string `json:"currency"`
|
||||
Type string `json:"type"`
|
||||
Balance fixedpoint.Value `json:"balance"`
|
||||
Available fixedpoint.Value `json:"available"`
|
||||
Holds fixedpoint.Value `json:"holds"`
|
||||
}
|
||||
|
||||
func (s *AccountService) QueryAccounts() ([]Account, error) {
|
||||
req, err := s.client.newAuthenticatedRequest("GET", "/api/v1/accounts", nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := s.client.sendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data []Account `json:"data"`
|
||||
}
|
||||
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return apiResponse.Data, nil
|
||||
}
|
|
@ -216,40 +216,6 @@ type BalanceDetail struct {
|
|||
UnrealizedProfitAndLoss fixedpoint.Value `json:"upl"`
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
TotalEquityInUSD fixedpoint.Value `json:"totalEq"`
|
||||
UpdateTime string `json:"uTime"`
|
||||
Details []BalanceDetail `json:"details"`
|
||||
}
|
||||
|
||||
func (c *RestClient) AccountBalances() (*Account, error) {
|
||||
req, err := c.newAuthenticatedRequest("GET", "/api/v5/account/balance", nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := c.sendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var balanceResponse struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data []Account `json:"data"`
|
||||
}
|
||||
|
||||
if err := response.DecodeJSON(&balanceResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(balanceResponse.Data) == 0 {
|
||||
return nil, errors.New("empty account data")
|
||||
}
|
||||
|
||||
return &balanceResponse.Data[0], nil
|
||||
}
|
||||
|
||||
type AssetBalance struct {
|
||||
Currency string `json:"ccy"`
|
||||
Balance fixedpoint.Value `json:"bal"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user