diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index cabad7bf2..92386250e 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -204,17 +204,30 @@ func (e *Exchange) getLaunchDate() (time.Time, error) { 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 { - response, err := e.Client.NewCreateWithdrawService(). - Asset(currency). +func (e *Exchange) Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *types.WithdrawalOptions) error { + req := e.Client.NewCreateWithdrawService(). + Asset(asset). Address(address). - Amount(fmt.Sprintf("%f", amount.Float64())). - Do(ctx) + Amount(fmt.Sprintf("%f", amount.Float64())) + 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 { 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) return nil } diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index df9ec4240..40ef18df1 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -363,11 +363,11 @@ func toMaxSubmitOrder(o types.SubmitOrder) (*maxapi.Order, error) { return &maxOrder, nil } -func (e *Exchange) Withdrawal(ctx context.Context, currency string, amount fixedpoint.Value, address string) error { - currency = toLocalCurrency(currency) +func (e *Exchange) Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *types.WithdrawalOptions) error { + asset = toLocalCurrency(asset) addresses, err := e.client.WithdrawalService.NewGetWithdrawalAddressesRequest(). - Currency(currency). + Currency(asset). Do(ctx) if err != nil { @@ -391,7 +391,7 @@ func (e *Exchange) Withdrawal(ctx context.Context, currency string, amount fixed } response, err := e.client.WithdrawalService.NewWithdrawalRequest(). - Currency(currency). + Currency(asset). Amount(amount.Float64()). AddressUUID(whitelistAddress.UUID). Do(ctx) @@ -539,7 +539,6 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { return a, nil } - func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) { startTime := since 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 } - diff --git a/pkg/strategy/xbalance/strategy.go b/pkg/strategy/xbalance/strategy.go index c2fb32176..a23e630b5 100644 --- a/pkg/strategy/xbalance/strategy.go +++ b/pkg/strategy/xbalance/strategy.go @@ -2,6 +2,7 @@ package xbalance import ( "context" + "encoding/json" "fmt" "math/rand" "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 { Notifiability *bbgo.Notifiability *bbgo.Graceful @@ -115,7 +145,7 @@ type Strategy struct { Interval types.Duration `json:"interval"` - Addresses map[string]string `json:"addresses"` + Addresses map[string]Address `json:"addresses"` MaxDailyNumberOfTransfer int `json:"maxDailyNumberOfTransfer"` MaxDailyAmountOfTransfer fixedpoint.Value `json:"maxDailyAmountOfTransfer"` @@ -213,7 +243,10 @@ func (s *Strategy) checkBalance(ctx context.Context, sessions map[string]*bbgo.E 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") s.Notifiability.Notify("withdrawal request failed, error: %v", err) return diff --git a/pkg/types/exchange.go b/pkg/types/exchange.go index 119f7b926..84a6bb618 100644 --- a/pkg/types/exchange.go +++ b/pkg/types/exchange.go @@ -104,7 +104,7 @@ type ExchangeTransferService 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 { diff --git a/pkg/types/withdraw.go b/pkg/types/withdraw.go index a579a8ef4..4e53392bb 100644 --- a/pkg/types/withdraw.go +++ b/pkg/types/withdraw.go @@ -29,3 +29,9 @@ func (w Withdraw) String() string { func (w Withdraw) EffectiveTime() time.Time { return w.ApplyTime.Time() } + +type WithdrawalOptions struct { + Network string + AddressTag string +} +