xbalance: add withdrawal options

This commit is contained in:
c9s 2021-05-26 23:24:05 +08:00
parent 8781902b68
commit 967c7e9f9d
5 changed files with 64 additions and 14 deletions

View File

@ -204,17 +204,30 @@ func (e *Exchange) getLaunchDate() (time.Time, error) {
return time.Date(2017, time.July, 14, 0, 0, 0, 0, loc), nil return time.Date(2017, time.July, 14, 0, 0, 0, 0, loc), nil
} }
func (e *Exchange) Withdrawal(ctx context.Context, currency string, amount fixedpoint.Value, address string) error { func (e *Exchange) Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *types.WithdrawalOptions) error {
response, err := e.Client.NewCreateWithdrawService(). req := e.Client.NewCreateWithdrawService().
Asset(currency). Asset(asset).
Address(address). Address(address).
Amount(fmt.Sprintf("%f", amount.Float64())). Amount(fmt.Sprintf("%f", amount.Float64()))
Do(ctx)
if options != nil {
if options.Network != "" {
req.Network(options.Network)
}
if options.AddressTag != "" {
req.Network(options.AddressTag)
}
}
response, err := req.Do(ctx)
if err != nil { if err != nil {
return err return err
} }
if !response.Success {
return fmt.Errorf("withdrawal request failed: %s ID=%s", response.Msg, response.ID)
}
log.Infof("withdrawal request sent, response: %+v", response) log.Infof("withdrawal request sent, response: %+v", response)
return nil return nil
} }

View File

@ -363,11 +363,11 @@ func toMaxSubmitOrder(o types.SubmitOrder) (*maxapi.Order, error) {
return &maxOrder, nil return &maxOrder, nil
} }
func (e *Exchange) Withdrawal(ctx context.Context, currency string, amount fixedpoint.Value, address string) error { func (e *Exchange) Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *types.WithdrawalOptions) error {
currency = toLocalCurrency(currency) asset = toLocalCurrency(asset)
addresses, err := e.client.WithdrawalService.NewGetWithdrawalAddressesRequest(). addresses, err := e.client.WithdrawalService.NewGetWithdrawalAddressesRequest().
Currency(currency). Currency(asset).
Do(ctx) Do(ctx)
if err != nil { if err != nil {
@ -391,7 +391,7 @@ func (e *Exchange) Withdrawal(ctx context.Context, currency string, amount fixed
} }
response, err := e.client.WithdrawalService.NewWithdrawalRequest(). response, err := e.client.WithdrawalService.NewWithdrawalRequest().
Currency(currency). Currency(asset).
Amount(amount.Float64()). Amount(amount.Float64()).
AddressUUID(whitelistAddress.UUID). AddressUUID(whitelistAddress.UUID).
Do(ctx) Do(ctx)
@ -539,7 +539,6 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
return a, nil return a, nil
} }
func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) { func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
startTime := since startTime := since
limit := 1000 limit := 1000
@ -865,4 +864,3 @@ func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (float6
return (util.MustParseFloat(ticker.Sell) + util.MustParseFloat(ticker.Buy)) / 2, nil return (util.MustParseFloat(ticker.Sell) + util.MustParseFloat(ticker.Buy)) / 2, nil
} }

View File

@ -2,6 +2,7 @@ package xbalance
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
"sync" "sync"
@ -108,6 +109,35 @@ func (r *WithdrawalRequest) SlackAttachment() slack.Attachment {
} }
} }
type Address struct {
Address string `json:"address"`
AddressTag string `json:"addressTag"`
Network string `json:"network"`
}
func (a *Address) UnmarshalJSON(body []byte) error {
var arg interface{}
err := json.Unmarshal(body, &arg)
if err != nil {
return err
}
switch argT := arg.(type) {
case string:
a.Address = argT
return nil
}
var newA Address
err = json.Unmarshal(body, &newA)
if err != nil {
return err
}
*a = newA
return nil
}
type Strategy struct { type Strategy struct {
Notifiability *bbgo.Notifiability Notifiability *bbgo.Notifiability
*bbgo.Graceful *bbgo.Graceful
@ -115,7 +145,7 @@ type Strategy struct {
Interval types.Duration `json:"interval"` Interval types.Duration `json:"interval"`
Addresses map[string]string `json:"addresses"` Addresses map[string]Address `json:"addresses"`
MaxDailyNumberOfTransfer int `json:"maxDailyNumberOfTransfer"` MaxDailyNumberOfTransfer int `json:"maxDailyNumberOfTransfer"`
MaxDailyAmountOfTransfer fixedpoint.Value `json:"maxDailyAmountOfTransfer"` MaxDailyAmountOfTransfer fixedpoint.Value `json:"maxDailyAmountOfTransfer"`
@ -213,7 +243,10 @@ func (s *Strategy) checkBalance(ctx context.Context, sessions map[string]*bbgo.E
Amount: requiredAmount, Amount: requiredAmount,
}) })
if err := withdrawalService.Withdrawal(ctx, s.Asset, requiredAmount, toAddress); err != nil { if err := withdrawalService.Withdrawal(ctx, s.Asset, requiredAmount, toAddress.Address, &types.WithdrawalOptions{
Network: toAddress.Network,
AddressTag: toAddress.AddressTag,
}); err != nil {
log.WithError(err).Errorf("withdrawal failed") log.WithError(err).Errorf("withdrawal failed")
s.Notifiability.Notify("withdrawal request failed, error: %v", err) s.Notifiability.Notify("withdrawal request failed, error: %v", err)
return return

View File

@ -104,7 +104,7 @@ type ExchangeTransferService interface {
} }
type ExchangeWithdrawalService interface { type ExchangeWithdrawalService interface {
Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string) error Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *WithdrawalOptions) error
} }
type ExchangeRewardService interface { type ExchangeRewardService interface {

View File

@ -29,3 +29,9 @@ func (w Withdraw) String() string {
func (w Withdraw) EffectiveTime() time.Time { func (w Withdraw) EffectiveTime() time.Time {
return w.ApplyTime.Time() return w.ApplyTime.Time()
} }
type WithdrawalOptions struct {
Network string
AddressTag string
}