From 68abeb826b1fa3272e829308aef856c1cdfc18d6 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 20 Apr 2022 13:28:39 +0800 Subject: [PATCH] maxapi: add account service tests --- pkg/exchange/max/exchange.go | 10 ++--- pkg/exchange/max/maxapi/account.go | 16 ++++++-- pkg/exchange/max/maxapi/account_test.go | 54 +++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 pkg/exchange/max/maxapi/account_test.go diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index 7c437ef31..ae62526d0 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -621,7 +621,7 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { return nil, err } - userInfo, err := e.client.AccountService.NewGetMeRequest() + userInfo, err := e.client.AccountService.NewGetMeRequest().Do(ctx) if err != nil { return nil, err } @@ -630,8 +630,8 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { for _, a := range userInfo.Accounts { balances[toGlobalCurrency(a.Currency)] = types.Balance{ Currency: toGlobalCurrency(a.Currency), - Available: fixedpoint.Must(fixedpoint.NewFromString(a.Balance)), - Locked: fixedpoint.Must(fixedpoint.NewFromString(a.Locked)), + Available: a.Balance, + Locked: a.Locked, } } @@ -828,8 +828,8 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, for _, a := range accounts { balances[toGlobalCurrency(a.Currency)] = types.Balance{ Currency: toGlobalCurrency(a.Currency), - Available: fixedpoint.Must(fixedpoint.NewFromString(a.Balance)), - Locked: fixedpoint.Must(fixedpoint.NewFromString(a.Locked)), + Available: a.Balance, + Locked: a.Locked, } } diff --git a/pkg/exchange/max/maxapi/account.go b/pkg/exchange/max/maxapi/account.go index 686d93046..0c5f36791 100644 --- a/pkg/exchange/max/maxapi/account.go +++ b/pkg/exchange/max/maxapi/account.go @@ -7,6 +7,8 @@ import ( "context" "github.com/c9s/requestgen" + + "github.com/c9s/bbgo/pkg/fixedpoint" ) type AccountService struct { @@ -15,10 +17,12 @@ type AccountService struct { // Account is for max rest api v2, Balance and Type will be conflict with types.PrivateBalanceUpdate type Account struct { - Currency string `json:"currency"` - Balance string `json:"balance"` - Locked string `json:"locked"` - Type string `json:"type"` + Currency string `json:"currency"` + Balance fixedpoint.Value `json:"balance"` + Locked fixedpoint.Value `json:"locked"` + Type string `json:"type"` + FiatCurrency string `json:"fiat_currency"` + FiatBalance fixedpoint.Value `json:"fiat_balance"` } // Balance is for kingfisher @@ -98,6 +102,10 @@ type GetAccountRequest struct { currency string `param:"currency,slug"` } +func (s *AccountService) NewGetAccountRequest() *GetAccountRequest { + return &GetAccountRequest{client: s.client} +} + func (s *AccountService) NewGetWithdrawalHistoryRequest() *GetWithdrawHistoryRequest { return &GetWithdrawHistoryRequest{ client: s.client, diff --git a/pkg/exchange/max/maxapi/account_test.go b/pkg/exchange/max/maxapi/account_test.go new file mode 100644 index 000000000..f62d78b9c --- /dev/null +++ b/pkg/exchange/max/maxapi/account_test.go @@ -0,0 +1,54 @@ +package max + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAccountService_GetAccountsRequest(t *testing.T) { + key, secret, ok := integrationTestConfigured(t, "MAX") + if !ok { + t.SkipNow() + } + + ctx := context.Background() + + client := NewRestClient(ProductionAPIURL) + client.Auth(key, secret) + + req := client.AccountService.NewGetAccountsRequest() + accounts, err := req.Do(ctx) + assert.NoError(t, err) + assert.NotNil(t, accounts) + assert.NotEmpty(t, accounts) + + t.Logf("accounts: %+v", accounts) +} + +func TestAccountService_GetAccountRequest(t *testing.T) { + key, secret, ok := integrationTestConfigured(t, "MAX") + if !ok { + t.SkipNow() + } + + ctx := context.Background() + + client := NewRestClient(ProductionAPIURL) + client.Auth(key, secret) + + req := client.AccountService.NewGetAccountRequest() + req.Currency("twd") + account, err := req.Do(ctx) + assert.NoError(t, err) + assert.NotNil(t, account) + t.Logf("account: %+v", account) + + req2 := client.AccountService.NewGetAccountRequest() + req2.Currency("usdt") + account, err = req.Do(ctx) + assert.NoError(t, err) + assert.NotNil(t, account) + t.Logf("account: %+v", account) +}