diff --git a/pkg/exchange/max/convert.go b/pkg/exchange/max/convert.go index 6cf67d018..c1fe3dfb8 100644 --- a/pkg/exchange/max/convert.go +++ b/pkg/exchange/max/convert.go @@ -341,6 +341,26 @@ func convertWebSocketOrderUpdate(u max.OrderUpdate) (*types.Order, error) { }, nil } +func convertWithdrawStatusV3(status max.WithdrawStatus) types.WithdrawStatus { + switch status { + + case max.WithdrawStatusPending: + return types.WithdrawStatusSent + + case max.WithdrawStatusOK: + return types.WithdrawStatusCompleted + + case max.WithdrawStatusFailed: + return types.WithdrawStatusFailed + + case max.WithdrawStatusCancelled: + return types.WithdrawStatusCancelled + + } + + return types.WithdrawStatus(status) +} + func convertWithdrawStatus(state max.WithdrawState) types.WithdrawStatus { switch state { diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index ce52d09da..3852aadf5 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -824,8 +824,7 @@ func (e *Exchange) QueryWithdrawHistory( limit := 1000 txIDs := map[string]struct{}{} - emptyTime := time.Time{} - if startTime == emptyTime { + if startTime.IsZero() { startTime, err = e.getLaunchDate() if err != nil { return nil, err @@ -867,7 +866,7 @@ func (e *Exchange) QueryWithdrawHistory( } // we can convert this later - status := convertWithdrawStatus(d.State) + status := convertWithdrawStatusV3(d.Status) txIDs[d.TxID] = struct{}{} withdraw := types.Withdraw{ @@ -881,11 +880,10 @@ func (e *Exchange) QueryWithdrawHistory( TransactionFee: d.Fee, TransactionFeeCurrency: d.FeeCurrency, Network: d.NetworkProtocol, - // WithdrawOrderID: d.WithdrawOrderID, - // Network: d.Network, - Status: status, - OriginalStatus: string(d.State), + Status: status, + OriginalStatus: string(d.State), } + allWithdraws = append(allWithdraws, withdraw) } diff --git a/pkg/strategy/xalign/strategy.go b/pkg/strategy/xalign/strategy.go index 73496b1eb..40b0a6f01 100644 --- a/pkg/strategy/xalign/strategy.go +++ b/pkg/strategy/xalign/strategy.go @@ -145,8 +145,9 @@ func (s *Strategy) detectActiveWithdraw( } for _, withdraw := range withdraws { + log.Infof("checking withdraw status: %s", withdraw.String()) switch withdraw.Status { - case types.WithdrawStatusProcessing, types.WithdrawStatusSent, types.WithdrawStatusAwaitingApproval: + case types.WithdrawStatusSent, types.WithdrawStatusProcessing, types.WithdrawStatusAwaitingApproval: return &withdraw, nil } } diff --git a/pkg/types/withdraw.go b/pkg/types/withdraw.go index ef3e6d788..4a8ae5755 100644 --- a/pkg/types/withdraw.go +++ b/pkg/types/withdraw.go @@ -4,6 +4,8 @@ import ( "fmt" "time" + "github.com/slack-go/slack" + "github.com/c9s/bbgo/pkg/fixedpoint" ) @@ -47,7 +49,7 @@ func cutstr(s string, maxLen, head, tail int) string { } func (w Withdraw) String() (o string) { - o = fmt.Sprintf("%s WITHDRAW %8f %s -> ", w.Exchange, w.Amount.Float64(), w.Asset) + o = fmt.Sprintf("%s WITHDRAW %s %s -> ", w.Exchange, w.Amount.String(), w.Asset) if len(w.Network) > 0 && w.Network != w.Asset { o += w.Network + ":" @@ -68,6 +70,7 @@ func (w Withdraw) String() (o string) { o += fmt.Sprintf(" TxID: %s", cutstr(w.TransactionID, 12, 4, 4)) } + o += fmt.Sprintf(" STATUS: %s (%s)", w.Status, w.OriginalStatus) return o } @@ -75,6 +78,67 @@ func (w Withdraw) EffectiveTime() time.Time { return w.ApplyTime.Time() } +func (w *Withdraw) SlackAttachment() slack.Attachment { + var fields []slack.AttachmentField + + if len(w.TransactionID) > 0 { + fields = append(fields, slack.AttachmentField{ + Title: "TransactionID", + Value: w.TransactionID, + Short: false, + }) + } + + if w.TransactionFee.Sign() > 0 { + fields = append(fields, slack.AttachmentField{ + Title: "Transaction Fee", + Value: fmt.Sprintf("%s %s", w.TransactionFee.String(), w.TransactionFeeCurrency), + Short: false, + }) + } + + if len(w.Status) > 0 { + fields = append(fields, slack.AttachmentField{ + Title: "Status", + Value: fmt.Sprintf("%s (%s)", w.Status, w.OriginalStatus), + Short: false, + }) + } + + return slack.Attachment{ + Color: withdrawStatusSlackColor(w.Status), + Title: fmt.Sprintf("Withdraw %s %s To %s (Network %s)", w.Amount.String(), w.Asset, w.Address, w.Network), + // TitleLink: "", + Pretext: "", + Text: "", + // ServiceName: "", + // ServiceIcon: "", + // FromURL: "", + // OriginalURL: "", + Fields: fields, + Footer: fmt.Sprintf("Apply Time: %s", w.ApplyTime.Time().Format(time.RFC3339)), + // FooterIcon: "", + } +} + +func withdrawStatusSlackColor(status WithdrawStatus) string { + switch status { + + case WithdrawStatusCompleted: + return "good" + + case WithdrawStatusFailed: + return "red" + + case WithdrawStatusCancelled: + return "gray" + + default: + return "gray" + + } +} + type WithdrawalOptions struct { Network string AddressTag string