Skip to content

Commit 907a1c8

Browse files
authored
Merge pull request #1647 from c9s/c9s/add-initial-attempt-for-order-trades-query
FIX: [retry] add initialAttempts to the order trades query backoff
2 parents 7bde48a + 6bb910c commit 907a1c8

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pkg/exchange/retry/order.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,34 @@ func QueryClosedOrdersUntilSuccessfulLite(
144144
return closedOrders, err
145145
}
146146

147+
var ErrTradeFeeIsProcessing = errors.New("trading fee is still processing")
148+
var ErrTradeNotExecutedYet = errors.New("trades not executed yet")
149+
147150
// QueryOrderTradesUntilSuccessful query order's trades until success (include the trading fee is not processing)
148151
func QueryOrderTradesUntilSuccessful(
149152
ctx context.Context, ex types.ExchangeOrderQueryService, q types.OrderQuery,
150153
) (trades []types.Trade, err error) {
154+
// sometimes the api might return empty trades without an error when we query the order too soon,
155+
// so in the initial attempts, we should check the len(trades) and retry the query
156+
var initialAttempts = 3
151157
var op = func() (err2 error) {
152158
trades, err2 = ex.QueryOrderTrades(ctx, q)
159+
if err2 != nil {
160+
return err2
161+
}
162+
163+
initialAttempts--
164+
165+
if initialAttempts > 0 && len(trades) == 0 {
166+
return ErrTradeNotExecutedYet
167+
}
168+
153169
for _, trade := range trades {
154170
if trade.FeeProcessing {
155-
return fmt.Errorf("there are some trades which trading fee is not ready")
171+
return ErrTradeFeeIsProcessing
156172
}
157173
}
174+
158175
return err2
159176
}
160177

0 commit comments

Comments
 (0)