Skip to content

Commit

Permalink
Update API according to the update in October 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaaiqiang committed Nov 19, 2020
1 parent cc7e268 commit 67bf522
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/crossmarginclientexample/crossmarginclientexample.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func RunAllExamples() {
marginOrdersRepay()
marginLoanOrders()
marginAccountsBalance()
genernalMarginOrdersRepay()
genernalMarginLoanOrders()
}

// Transfer specific asset from spot trading account to cross margin account.
Expand Down Expand Up @@ -110,3 +112,31 @@ func marginAccountsBalance() {
}
}
}

// Repays general margin loan with you asset in your margin account.
func genernalMarginOrdersRepay() {
request := margin.CrossMarginGeneralReplayLoanOptionalRequest{AccountId: "12345", Currency:"btc", Amount:"0.01"}
client := new(client.CrossMarginClient).Init(config.AccessKey, config.SecretKey, config.Host)
resp, err := client.GeneralRepay(request)
if err != nil {
applogger.Error("Repay error: %s", err)
} else {
for _, order := range resp {
applogger.Info("Repay Order:%+v", order)
}
}
}

// Get the genernal margin orders based on a specific searching criteria.
func genernalMarginLoanOrders() {
optionalRequest := margin.CrossMarginGeneralReplayLoanRecordsOptionalRequest{}
client := new(client.CrossMarginClient).Init(config.AccessKey, config.SecretKey, config.Host)
resp, err := client.GeneralMarginLoanOrders(optionalRequest)
if err != nil {
applogger.Error(err.Error())
} else {
for _, record := range resp {
applogger.Info("Record: RepayId:%v, RepayTime:%v, AccountId:%v, Currency:%s, RepaidAmount:%s, TransactId:%+v", record.RepayId, record.RepayTime, record.AccountId, record.Currency, record.RepaidAmount, record.TransactIds)
}
}
}
73 changes: 73 additions & 0 deletions pkg/client/crossmarginclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/huobirdcenter/huobi_golang/internal/requestbuilder"
"github.com/huobirdcenter/huobi_golang/pkg/model"
"github.com/huobirdcenter/huobi_golang/pkg/model/margin"
"strconv"
)

// Responsible to operate cross margin
Expand Down Expand Up @@ -216,3 +217,75 @@ func (p *CrossMarginClient) MarginAccountsBalance(SubUid string) (*margin.CrossM

return nil, errors.New(getResp)
}

// Repays general margin loan with you asset in your margin account.
func (p *CrossMarginClient) GeneralRepay(request margin.CrossMarginGeneralReplayLoanOptionalRequest) ([]margin.CrossMarginGeneraReplaylLoan, error) {
postBody, jsonErr := model.ToJson(request)
if jsonErr != nil {
return nil, jsonErr
}

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

result := margin.CrossMarginGeneralReplyLoanResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Code != 200 {
return nil, errors.New(postResp)

}
return result.Data, nil
}

// Returns general margin orders based on a specific searching criteria.
func (p *CrossMarginClient) GeneralMarginLoanOrders(optionalRequest margin.CrossMarginGeneralReplayLoanRecordsOptionalRequest) ([]margin.CrossMarginGeneraReplaylLoanRecord, error) {
request := new(model.GetRequest).Init()
if optionalRequest.RepayId != "" {
request.AddParam("repayId", optionalRequest.RepayId)
}
if optionalRequest.AccountId != "" {
request.AddParam("accountId", optionalRequest.AccountId)
}
if optionalRequest.Currency != "" {
request.AddParam("currency", optionalRequest.Currency)
}
if optionalRequest.StartDate != 0 {
request.AddParam("startDate", strconv.FormatInt(optionalRequest.StartDate,10))
}
if optionalRequest.EndDate != 0 {
request.AddParam("endDate", strconv.FormatInt(optionalRequest.EndDate,10))
}
if optionalRequest.Sort != "" {
request.AddParam("sort", optionalRequest.Sort)
}
if optionalRequest.Limit != 0 {
request.AddParam("limit", strconv.Itoa(optionalRequest.Limit))
}
if optionalRequest.FromId != 0 {
request.AddParam("fromId", strconv.FormatInt(optionalRequest.FromId,10))
}

url := p.privateUrlBuilder.Build("GET", "/v2/account/repayment", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}

result := margin.CrossMarginGeneralReplyLoanRecordsResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}
if result.Code == 200 && result.Data != nil {
return result.Data, nil
}

return nil, errors.New(getResp)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package margin

type CrossMarginGeneralReplayLoanOptionalRequest struct {
AccountId string `json:"accountId"`
Currency string `json:"currency"`
Amount string `json:"amount"`
TransactId string `json:"transactId"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package margin

type CrossMarginGeneralReplayLoanRecordsOptionalRequest struct {
RepayId string `json:"repayId"`
AccountId string `json:"accountId"`
Currency string `json:"currency"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
Sort string `json:"sort"`
Limit int `json:"limit"`
FromId int64 `json:"fromId"`
}
21 changes: 21 additions & 0 deletions pkg/model/margin/crossmargingeneralreplayloanrecordsresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package margin

type CrossMarginGeneralReplyLoanRecordsResponse struct {
Code int `json:"code"`
Data []CrossMarginGeneraReplaylLoanRecord `json:"data"`
}
type CrossMarginGeneraReplaylLoanRecord struct {
RepayId int64 `json:"repayId"`
RepayTime int64 `json:"repayTime"`
AccountId int64 `json:"accountId"`
Currency string `json:"currency"`
RepaidAmount string `json:"repaidAmount"`
TransactIds *Transact `json:"transactIds"`
}
type Transact struct {
TransactId int64 `json:"transactId"`
RepaidPrincipal string `json:"repaidPrincipal"`
RepaidInterest string `json:"repaidInterest"`
PaidHt string `json:"paidHt"`
PaidPoint string `json:"paidPoint"`
}
10 changes: 10 additions & 0 deletions pkg/model/margin/crossmargingeneralreplayloanresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package margin

type CrossMarginGeneralReplyLoanResponse struct {
Code int `json:"code"`
Data []CrossMarginGeneraReplaylLoan `json:"data"`
}
type CrossMarginGeneraReplaylLoan struct {
RepayId int64 `json:"repayId"`
RepayTime int64 `json:"repayTime"`
}
1 change: 1 addition & 0 deletions pkg/model/order/subscribeorderv2response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type SubscribeOrderV2Response struct {
Data *struct {
EventType string `json:"eventType"`
Symbol string `json:"symbol"`
AccountId int64 `json:"accountId"`
OrderId int64 `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
OrderSide string `json:"orderSide"`
Expand Down

0 comments on commit 67bf522

Please sign in to comment.