-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* resolves #15
- Loading branch information
Showing
3 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package binance | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
type Account struct { | ||
MakerCommission int64 | ||
TakerCommission int64 | ||
BuyerCommission int64 | ||
SellerCommission int64 | ||
CanTrade bool | ||
CanWithdraw bool | ||
CanDeposit bool | ||
UpdateTime int64 | ||
Balances []Balance | ||
} | ||
|
||
type Balance struct { | ||
Asset string | ||
Free float64 `json:"free,string"` | ||
Locked float64 `json:"locked,string"` | ||
} | ||
|
||
type accountQuery struct { | ||
recvWindow *int64 | ||
timestamp *int64 | ||
} | ||
|
||
func NewAccountQuery() *accountQuery { | ||
return &accountQuery{} | ||
} | ||
|
||
func (r *accountQuery) RecvWindow(value int64) *accountQuery { | ||
r.recvWindow = &value | ||
return r | ||
} | ||
|
||
func (sdk Sdk) Account(query *accountQuery) (*Account, error) { | ||
req := newRequest("GET", "/api/v3/account"). | ||
Int64Param("recvWindow", query.recvWindow). | ||
Int64Param("timestamp", sdk.clock.Now()). | ||
Sign() | ||
|
||
responseContent, err := sdk.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return parseAccountResponse(responseContent) | ||
} | ||
|
||
func parseAccountResponse(jsonContent []byte) (*Account, error) { | ||
response := &Account{} | ||
err := json.Unmarshal(jsonContent, &response) | ||
return response, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package binance | ||
|
||
import ( | ||
"errors" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSdk_Account(t *testing.T) { | ||
method, url := "GET", "/api/v3/account" | ||
|
||
t.Run("It should convert api response to an account", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockedClock := NewMockClock(ctrl) | ||
mockedClient := NewMockClient(ctrl) | ||
sdk := Sdk{client: mockedClient, clock: mockedClock} | ||
|
||
expectedTimestamp := time.Now().Unix() | ||
mockedClock.EXPECT().Now().Return(&expectedTimestamp) | ||
|
||
expectedRequest := newRequest(method, url). | ||
Param("timestamp", strconv.FormatInt(expectedTimestamp, 10)). | ||
Sign() | ||
|
||
mockedClient. | ||
EXPECT(). | ||
Do(expectedRequest). | ||
MinTimes(1). | ||
Return(validAccountJson(), nil) | ||
|
||
query := NewAccountQuery() | ||
response, _ := sdk.Account(query) | ||
|
||
assert.Equal(t, validAccountResponse(), response) | ||
}) | ||
|
||
t.Run("It should read optional parameters", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockedClock := NewMockClock(ctrl) | ||
mockedClient := NewMockClient(ctrl) | ||
sdk := Sdk{client: mockedClient, clock: mockedClock} | ||
|
||
expectedTimestamp := time.Now().Unix() | ||
mockedClock.EXPECT().Now().Return(&expectedTimestamp) | ||
|
||
expectedRequest := newRequest(method, url). | ||
Param("recvWindow", "2"). | ||
Param("timestamp", strconv.FormatInt(expectedTimestamp, 10)). | ||
Sign() | ||
|
||
mockedClient. | ||
EXPECT(). | ||
Do(expectedRequest). | ||
MinTimes(1). | ||
Return(validAccountJson(), nil) | ||
|
||
query := NewAccountQuery().RecvWindow(2) | ||
response, _ := sdk.Account(query) | ||
|
||
assert.Equal(t, validAccountResponse(), response) | ||
}) | ||
|
||
t.Run("It should return error when api fails", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockedClock := NewMockClock(ctrl) | ||
mockedClient := NewMockClient(ctrl) | ||
sdk := Sdk{client: mockedClient, clock: mockedClock} | ||
|
||
expectedTimestamp := time.Now().Unix() | ||
mockedClock.EXPECT().Now().Return(&expectedTimestamp) | ||
|
||
expectedRequest := newRequest(method, url). | ||
Param("timestamp", strconv.FormatInt(expectedTimestamp, 10)). | ||
Sign() | ||
|
||
mockedClient. | ||
EXPECT(). | ||
Do(expectedRequest). | ||
MinTimes(1). | ||
Return(nil, errors.New("error")) | ||
|
||
query := NewAccountQuery() | ||
_, err := sdk.Account(query) | ||
|
||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("It should return error when response cannot be mapped", func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockedClock := NewMockClock(ctrl) | ||
mockedClient := NewMockClient(ctrl) | ||
sdk := Sdk{client: mockedClient, clock: mockedClock} | ||
|
||
expectedTimestamp := time.Now().Unix() | ||
mockedClock.EXPECT().Now().Return(&expectedTimestamp) | ||
|
||
expectedRequest := newRequest(method, url). | ||
Param("timestamp", strconv.FormatInt(expectedTimestamp, 10)). | ||
Sign() | ||
|
||
mockedClient. | ||
EXPECT(). | ||
Do(expectedRequest). | ||
MinTimes(1). | ||
Return(invalidJson(), nil) | ||
|
||
query := NewAccountQuery() | ||
_, err := sdk.Account(query) | ||
|
||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func validAccountJson() []byte { | ||
return []byte(`{ | ||
"makerCommission": 15, | ||
"takerCommission": 15, | ||
"buyerCommission": 0, | ||
"sellerCommission": 0, | ||
"canTrade": true, | ||
"canWithdraw": true, | ||
"canDeposit": true, | ||
"updateTime": 123456789, | ||
"balances": [ | ||
{ | ||
"asset": "BTC", | ||
"free": "4723846.89208129", | ||
"locked": "0.00000000" | ||
}, | ||
{ | ||
"asset": "LTC", | ||
"free": "4763368.68006011", | ||
"locked": "0.00000000" | ||
} | ||
] | ||
}`) | ||
} | ||
|
||
func validAccountResponse() *Account { | ||
return &Account{ | ||
MakerCommission: 15, | ||
TakerCommission: 15, | ||
BuyerCommission: 0, | ||
SellerCommission: 0, | ||
CanTrade: true, | ||
CanWithdraw: true, | ||
CanDeposit: true, | ||
UpdateTime: 123456789, | ||
Balances: []Balance{ | ||
{ | ||
Asset: "BTC", | ||
Free: 4723846.89208129, | ||
Locked: 0, | ||
}, { | ||
Asset: "LTC", | ||
Free: 4763368.68006011, | ||
Locked: 0, | ||
}, | ||
}, | ||
} | ||
} |