From 5d65b817ef9ba9ac816e158e3218ba9166ee2917 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 5 Aug 2024 16:39:44 +0800 Subject: [PATCH 1/4] max: add withdraw status convert function for v3 --- pkg/exchange/max/convert.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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 { From e03ba63e440f6b1978883f8d5b04bd9525ba1c41 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 5 Aug 2024 16:40:10 +0800 Subject: [PATCH 2/4] max: remove legacy emptyTime --- pkg/exchange/max/exchange.go | 12 +++++------- pkg/strategy/xalign/strategy.go | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) 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..7f5c66e79 100644 --- a/pkg/strategy/xalign/strategy.go +++ b/pkg/strategy/xalign/strategy.go @@ -146,7 +146,7 @@ func (s *Strategy) detectActiveWithdraw( for _, withdraw := range withdraws { switch withdraw.Status { - case types.WithdrawStatusProcessing, types.WithdrawStatusSent, types.WithdrawStatusAwaitingApproval: + case types.WithdrawStatusSent, types.WithdrawStatusProcessing, types.WithdrawStatusAwaitingApproval: return &withdraw, nil } } From a24a11818299ec714cd04b6fd7db90beed29675d Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 6 Aug 2024 18:08:39 +0800 Subject: [PATCH 3/4] xalign: add more withdraw checking logs --- pkg/strategy/xalign/strategy.go | 1 + pkg/types/withdraw.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/strategy/xalign/strategy.go b/pkg/strategy/xalign/strategy.go index 7f5c66e79..40b0a6f01 100644 --- a/pkg/strategy/xalign/strategy.go +++ b/pkg/strategy/xalign/strategy.go @@ -145,6 +145,7 @@ func (s *Strategy) detectActiveWithdraw( } for _, withdraw := range withdraws { + log.Infof("checking withdraw status: %s", withdraw.String()) switch withdraw.Status { case types.WithdrawStatusSent, types.WithdrawStatusProcessing, types.WithdrawStatusAwaitingApproval: return &withdraw, nil diff --git a/pkg/types/withdraw.go b/pkg/types/withdraw.go index ef3e6d788..871ce82c6 100644 --- a/pkg/types/withdraw.go +++ b/pkg/types/withdraw.go @@ -47,7 +47,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 +68,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 } From b4cc893cac192f7dbb28c5204864cc60c316a210 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 6 Aug 2024 18:26:17 +0800 Subject: [PATCH 4/4] types: add SlackAttachment support to types.Withdraw --- pkg/types/withdraw.go | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/pkg/types/withdraw.go b/pkg/types/withdraw.go index 871ce82c6..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" ) @@ -76,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