move deposit type to global type and add max deposit history support

This commit is contained in:
c9s 2020-10-11 17:35:59 +08:00
parent 18054bc9c6
commit 2d246c3f71
4 changed files with 165 additions and 16 deletions

View File

@ -105,6 +105,9 @@ func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since
status = "failure"
case 6:
status = "completed"
default:
status = fmt.Sprintf("unsupported code: %d", d.Status)
}
txIDs[d.TxID] = struct{}{}
@ -128,17 +131,7 @@ func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since
return allWithdraws, nil
}
type Deposit struct {
Time time.Time `json:"time"`
Amount float64 `json:"amount"`
Asset string `json:"asset"`
Address string `json:"address"`
AddressTag string `json:"addressTag"`
TransactionID string `json:"txId"`
Status string `json:"status"`
}
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []Deposit, err error) {
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
startTime := since
txIDs := map[string]struct{}{}
for startTime.Before(until) {
@ -165,18 +158,20 @@ func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since,
}
// 0(0:pending,6: credited but cannot withdraw, 1:success)
status := ""
status := types.DepositStatus(fmt.Sprintf("code: %d", d.Status))
switch d.Status {
case 0:
status = "pending"
status = types.DepositPending
case 6:
status = "credited"
// https://www.binance.com/en/support/faq/115003736451
status = types.DepositCredited
case 1:
status = "success"
status = types.DepositSuccess
}
txIDs[d.TxID] = struct{}{}
allDeposits = append(allDeposits, Deposit{
allDeposits = append(allDeposits, types.Deposit{
Time: time.Unix(0, d.InsertTime*int64(time.Millisecond)),
Asset: d.Asset,
Amount: d.Amount,

View File

@ -83,6 +83,52 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
}, nil
}
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
deposits, err := e.client.AccountService.NewGetDepositHistoryRequest().
Currency(asset).
From(since.Unix()).
To(until.Unix()).Do(ctx)
if err != nil {
return nil, err
}
for _, d := range deposits {
allDeposits = append(allDeposits, types.Deposit{
Time: time.Unix(d.CreatedAt, 0),
Amount: util.MustParseFloat(d.Amount),
Asset: d.Currency,
Address: "", // not supported
AddressTag: "", // not supported
TransactionID: d.TxID,
Status: convertDepositState(d.State),
})
}
return allDeposits, err
}
func convertDepositState(a string) types.DepositStatus {
switch a {
case "submitting", "submitted", "checking":
return types.DepositPending
case "accepted":
return types.DepositSuccess
case "rejected":
return types.DepositRejected
case "cancelled":
return types.DepositCancelled
case "suspect", "refunded":
}
return types.DepositStatus(a)
}
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
accounts, err := e.client.AccountService.Accounts()
if err != nil {

View File

@ -1,5 +1,7 @@
package max
import "context"
type AccountService struct {
client *RestClient
}
@ -108,3 +110,79 @@ func (s *AccountService) Me() (*UserInfo, error) {
return &m, nil
}
type Deposit struct {
Currency string `json:"currency"`
CurrencyVersion string `json:"currency_version"` // "eth"
Amount string `json:"amount"`
Fee string `json:"fee"`
TxID string `json:"txid"`
State string `json:"state"`
Confirmations int `json:"confirmations"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type GetDepositHistoryRequestParams struct {
*PrivateRequestParams
Currency string `json:"currency"`
From int64 `json:"from,omitempty"` // seconds
To int64 `json:"to,omitempty"` // seconds
State string `json:"state,omitempty"` // submitting, submitted, rejected, accepted, checking, refunded, cancelled, suspect
Limit int `json:"limit,omitempty"`
}
type GetDepositHistoryRequest struct {
client *RestClient
params GetDepositHistoryRequestParams
}
func (r *GetDepositHistoryRequest) State(state string) *GetDepositHistoryRequest {
r.params.State = state
return r
}
func (r *GetDepositHistoryRequest) Currency(currency string) *GetDepositHistoryRequest {
r.params.Currency = currency
return r
}
func (r *GetDepositHistoryRequest) Limit(limit int) *GetDepositHistoryRequest {
r.params.Limit = limit
return r
}
func (r *GetDepositHistoryRequest) From(from int64) *GetDepositHistoryRequest {
r.params.From = from
return r
}
func (r *GetDepositHistoryRequest) To(to int64) *GetDepositHistoryRequest {
r.params.To = to
return r
}
func (r *GetDepositHistoryRequest) Do(ctx context.Context) (deposits []Deposit, err error) {
req, err := r.client.newAuthenticatedRequest("GET", "v2/deposits", &r.params)
if err != nil {
return deposits, err
}
response, err := r.client.sendRequest(req)
if err != nil {
return deposits, err
}
if err := response.DecodeJSON(&deposits); err != nil {
return deposits, err
}
return deposits, err
}
func (s *AccountService) NewGetDepositHistoryRequest() *GetDepositHistoryRequest {
return &GetDepositHistoryRequest{
client: s.client,
}
}

30
pkg/types/deposit.go Normal file
View File

@ -0,0 +1,30 @@
package types
import "time"
type DepositStatus string
const (
DepositOther = DepositStatus("")
DepositPending = DepositStatus("pending")
DepositRejected = DepositStatus("rejected")
DepositSuccess = DepositStatus("success")
DepositCancelled = DepositStatus("cancelled")
// created but can not withdraw
DepositCredited = DepositStatus("credited")
)
type Deposit struct {
Time time.Time `json:"time"`
Amount float64 `json:"amount"`
Asset string `json:"asset"`
Address string `json:"address"`
AddressTag string `json:"addressTag"`
TransactionID string `json:"txId"`
Status DepositStatus `json:"status"`
}