Skip to content

Commit

Permalink
Update according to API update in July
Browse files Browse the repository at this point in the history
  • Loading branch information
eynzhang committed Sep 25, 2020
1 parent 23fb5c2 commit 9560d47
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 69 deletions.
72 changes: 56 additions & 16 deletions cmd/accountclientexample/accountclientexample.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
func RunAllExamples() {
getAccountInfo()
getAccountBalance()
getAccountAssetValuation()
transferAccount()
getAccountHistory()
getAccountLedger()
transferFromFutureToSpot()
transferFromSpotToFuture()
getPointBalance()
transferPoint()
}

func getAccountInfo() {
Expand All @@ -31,6 +34,32 @@ func getAccountInfo() {
}
}

func getAccountBalance() {
client := new(client.AccountClient).Init(config.AccessKey, config.SecretKey, config.Host)
resp, err := client.GetAccountBalance(config.AccountId)
if err != nil {
applogger.Error("Get account balance error: %s", err)
} else {
applogger.Info("Get account balance: id=%d, type=%s, state=%s, count=%d",
resp.Id, resp.Type, resp.State, len(resp.List))
if resp.List != nil {
for _, result := range resp.List {
applogger.Info("Account balance: %+v", result)
}
}
}
}

func getAccountAssetValuation() {
client := new(client.AccountClient).Init(config.AccessKey, config.SecretKey, config.Host)
resp, err := client.GetAccountAssetValuation("spot", "USD", 0)
if err != nil {
applogger.Error("Get account asset valuation error: %s", err)
} else {
applogger.Info("Get account asset valuation, balance: %s, timestamp: %d", resp.Data.Balance, resp.Data.Timestamp)
}
}

func transferAccount() {
client := new(client.AccountClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := account.TransferAccountRequest{
Expand All @@ -51,22 +80,6 @@ func transferAccount() {
}
}

func getAccountBalance() {
client := new(client.AccountClient).Init(config.AccessKey, config.SecretKey, config.Host)
resp, err := client.GetAccountBalance(config.AccountId)
if err != nil {
applogger.Error("Get account balance error: %s", err)
} else {
applogger.Info("Get account balance: id=%d, type=%s, state=%s, count=%d",
resp.Id, resp.Type, resp.State, len(resp.List))
if resp.List != nil {
for _, result := range resp.List {
applogger.Info("Account balance: %+v", result)
}
}
}
}

func getAccountHistory() {
client := new(client.AccountClient).Init(config.AccessKey, config.SecretKey, config.Host)
getAccountHistoryOptionalRequest := account.GetAccountHistoryOptionalRequest{}
Expand Down Expand Up @@ -116,3 +129,30 @@ func transferFromSpotToFuture() {
applogger.Info("Transfer from spot to future success: id=%d", resp)
}
}

func getPointBalance() {
client := new(client.AccountClient).Init(config.AccessKey, config.SecretKey, config.Host)
resp, err := client.GetPointBalance(config.SubUids)
if err != nil {
applogger.Error("Get point balance error: %s", err)
} else {
applogger.Info("Get point balance: id=%s, balance=%s, state=%s, count=%d",
resp.Data.AccountId, resp.Data.AccountBalance, resp.Data.AccountStatus, len(resp.Data.GroupIds))
if resp.Data.GroupIds != nil {
for _, result := range resp.Data.GroupIds {
applogger.Info("Account balance: %+v", result)
}
}
}
}

func transferPoint() {
client := new(client.AccountClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := account.TransferPointRequest{FromUid: "125753978", ToUid:"128654685", GroupId: 0, Amount:"0"}
resp, err := client.TransferPoint(request)
if err != nil {
applogger.Error("Transfer points error: %s", err)
} else {
applogger.Info("Transfer point success: id=%s, time=%d", resp.Data.TransactId, resp.Data.TransactTime)
}
}
2 changes: 1 addition & 1 deletion cmd/commonclientexample/commonclientexample.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func getSymbols() {
} else {
applogger.Info("Get symbols, count=%d", len(resp))
for _, result := range resp {
applogger.Info("symbol=%s, BaseCurrency=%s, QuoteCurrency=%s", result.Symbol, result.BaseCurrency, result.QuoteCurrency)
applogger.Info("symbol: %s, %+v", result.Symbol, result)
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions cmd/subuserclientexample/subuserclientexample.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,55 +67,63 @@ func unlockSubUser() {
func setSubUserTradbleMarket() {
client := new(client.SubUserClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := subuser.SetSubUserTradableMarketRequest{
SubUids: config.SubUid,
SubUids: config.SubUids,
AccountType: "isolated-margin",
Activation: "deactivated",
}
resp, err := client.SetSubUserTradableMarket(request)
if err != nil {
applogger.Error("Deactivate sub user error: %s", err)
} else {
applogger.Info("Deactivate sub user success: %+v", resp)
for _, result := range resp {
applogger.Info("Deactivate sub user success: %+v", result)
}
}

request = subuser.SetSubUserTradableMarketRequest{
SubUids: config.SubUid,
SubUids: config.SubUids,
AccountType: "isolated-margin",
Activation: "activated",
}
resp, err = client.SetSubUserTradableMarket(request)
if err != nil {
applogger.Error("Activate sub user error: %s", err)
} else {
applogger.Info("Activate sub user: %+v", resp)
for _, result := range resp {
applogger.Info("Activate sub user success: %+v", result)
}
}
}


func setSubUserTransferability() {
client := new(client.SubUserClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := subuser.SetSubUserTransferabilityRequest{
SubUids: config.SubUid,
SubUids: config.SubUids,
AccountType: "spot",
Transferrable: false,
}
resp, err := client.SetSubUserTransferability(request)
if err != nil {
applogger.Error("Deactivate sub user error: %s", err)
} else {
applogger.Info("Deactivate sub user success: %+v", resp)
for _, result := range resp {
applogger.Info("Deactivate sub user success: %+v", result)
}
}

request = subuser.SetSubUserTransferabilityRequest{
SubUids: config.SubUid,
SubUids: config.SubUids,
AccountType: "spot",
Transferrable: true,
}
resp, err = client.SetSubUserTransferability(request)
if err != nil {
applogger.Error("Activate sub user error: %s", err)
} else {
applogger.Info("Activate sub user: %+v", resp)
for _, result := range resp {
applogger.Info("Activate sub user: %+v", result)
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package config

var Host = "api.huobi.pro"
var AccessKey = "xxxx"
var AccountId = "xxxx"
var SubUid int64 = 1234567890
var AccountId = "1234"
var SubUid int64 = 5678
var SubUids string = "5678"
76 changes: 76 additions & 0 deletions pkg/client/accountclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ func (p *AccountClient) GetAccountBalance(accountId string) (*account.AccountBal
return nil, errors.New(getResp)
}

// Returns the valuation of the total assets of the account in btc or fiat currency.
func (p *AccountClient) GetAccountAssetValuation(accountType string, valuationCurrency string, subUid int64) (*account.GetAccountAssetValuationResponse, error) {
request := new(model.GetRequest).Init()
request.AddParam("accountType", accountType)
if valuationCurrency != "" {
request.AddParam("valuationCurrency", valuationCurrency)
}
if subUid != 0 {
request.AddParam("subUid", strconv.FormatInt(subUid, 10))
}

url := p.privateUrlBuilder.Build("GET", "/v2/account/asset-valuation", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}
result := account.GetAccountAssetValuationResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Code == 200 {
return &result, nil
}

return nil, errors.New(getResp)
}

func (p *AccountClient) TransferAccount(request account.TransferAccountRequest) (*account.TransferAccountResponse, error) {
postBody, jsonErr := model.ToJson(request)
if jsonErr != nil {
Expand Down Expand Up @@ -192,3 +220,51 @@ func (p *AccountClient) FuturesTransfer(request account.FuturesTransferRequest)
}
return result.Data, nil
}


// Returns the point balance of specified user's account
func (p *AccountClient) GetPointBalance(subUid string) (*account.GetPointBalanceResponse, error) {
request := new(model.GetRequest).Init()
request.AddParam("subUid", subUid)

url := p.privateUrlBuilder.Build("GET", "/v2/point/account", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}
result := account.GetPointBalanceResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Code == 200 {
return &result, nil
}

return nil, errors.New(getResp)
}

// Transfer points between spot account and future contract account
func (p *AccountClient) TransferPoint(request account.TransferPointRequest) (*account.TransferPointResponse, error) {
postBody, jsonErr := model.ToJson(request)
if jsonErr != nil {
return nil, jsonErr
}

url := p.privateUrlBuilder.Build("POST", "/v2/point/transfer", nil)
postResp, postErr := internal.HttpPost(url, postBody)
if postErr != nil {
return nil, postErr
}

result := account.TransferPointResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Code == 200 {
return &result, nil
}

return nil, errors.New(postResp)
}
10 changes: 10 additions & 0 deletions pkg/model/account/getaccountassetvaluationresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package account

type GetAccountAssetValuationResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data *struct {
Balance string `json:"balance"`
Timestamp int64 `json:"timestamp"`
}
}
16 changes: 16 additions & 0 deletions pkg/model/account/getpointbalanceresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package account

type GetPointBalanceResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data *struct {
AccountId string `json:"accountId"`
AccountStatus string `json:"accountStatus"`
AccountBalance string `json:"acctBalance"`
GroupIds []struct {
GroupId int64 `json:"groupId"`
ExpiryDate int64 `json:"expiryDate"`
RemainAmount string `json:"remainAmount"`
}
}
}
8 changes: 8 additions & 0 deletions pkg/model/account/transferpointrequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package account

type TransferPointRequest struct {
FromUid string `json:"fromUid"`
ToUid string `json:"toUid"`
GroupId int64 `json:"groupId"`
Amount string `json:"amount"`
}
10 changes: 10 additions & 0 deletions pkg/model/account/transferpointresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package account

type TransferPointResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data *struct {
TransactId string `json:"transactId"`
TransactTime int64 `json:"transactTime"`
}
}
28 changes: 16 additions & 12 deletions pkg/model/common/getsymbolsresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ type GetSymbolsResponse struct {
}

type Symbol struct {
BaseCurrency string `json:"base-currency"`
QuoteCurrency string `json:"quote-currency"`
PricePrecision int `json:"price-precision"`
AmountPrecision int `json:"amount-precision"`
SymbolPartition string `json:"symbol-partition"`
Symbol string `json:"symbol"`
State string `json:"state"`
ValuePrecision int `json:"value-precision"`
MinOrderAmt decimal.Decimal `json:"min-order-amt"`
MaxOrderAmt decimal.Decimal `json:"max-order-amt"`
MinOrderValue decimal.Decimal `json:"min-order-value"`
LeverageRatio decimal.Decimal `json:"leverage-ratio"`
BaseCurrency string `json:"base-currency"`
QuoteCurrency string `json:"quote-currency"`
PricePrecision int `json:"price-precision"`
AmountPrecision int `json:"amount-precision"`
SymbolPartition string `json:"symbol-partition"`
Symbol string `json:"symbol"`
State string `json:"state"`
ValuePrecision int `json:"value-precision"`
LimitOrderMinOrderAmt decimal.Decimal `json:"limit-order-min-order-amt"`
LimitOrderMaxOrderAmt decimal.Decimal `json:"limit-order-max-order-amt"`
SellMarketMinOrderAmt decimal.Decimal `json:"sell-market-min-order-amt"`
SellMarketMaxOrderAmt decimal.Decimal `json:"sell-market-max-order-amt"`
BuyMarketMaxOrderValue decimal.Decimal `json:"buy-market-max-order-value"`
MinOrderValue decimal.Decimal `json:"min-order-value"`
MaxOrderValue decimal.Decimal `json:"max-order-value"`
LeverageRatio decimal.Decimal `json:"leverage-ratio"`
}
Loading

0 comments on commit 9560d47

Please sign in to comment.