Skip to content

Commit cee4c11

Browse files
author
jeffyanta
authored
Improve swap status poller and error reporting wrt context errors (#44)
1 parent 2e6243b commit cee4c11

2 files changed

Lines changed: 32 additions & 24 deletions

File tree

pkg/code/server/grpc/transaction/v2/errors.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package transaction_v2
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67

@@ -226,8 +227,10 @@ func handleSubmitIntentError(streamer transactionpb.Transaction_SubmitIntentServ
226227

227228
// Case 2: Errors that map to gRPC status errors
228229
switch err {
229-
case ErrTimedOutReceivingRequest:
230+
case ErrTimedOutReceivingRequest, context.DeadlineExceeded:
230231
return status.Error(codes.DeadlineExceeded, err.Error())
232+
case context.Canceled:
233+
return status.Error(codes.Canceled, err.Error())
231234
case transaction.ErrNoAvailableNonces:
232235
return status.Error(codes.Unavailable, "")
233236
}
@@ -281,8 +284,10 @@ func handleSwapError(streamer transactionpb.Transaction_SwapServer, err error) e
281284

282285
// Case 2: Errors that map to gRPC status errors
283286
switch err {
284-
case ErrTimedOutReceivingRequest:
287+
case ErrTimedOutReceivingRequest, context.DeadlineExceeded:
285288
return status.Error(codes.DeadlineExceeded, err.Error())
289+
case context.Canceled:
290+
return status.Error(codes.Canceled, err.Error())
286291
}
287292
return status.Error(codes.Internal, "rpc server failure")
288293
}

pkg/code/server/grpc/transaction/v2/swap.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -380,32 +380,35 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)
380380
}
381381

382382
for {
383-
time.Sleep(time.Second)
384-
385-
statuses, err := s.data.GetBlockchainSignatureStatuses(ctx, []solana.Signature{solana.Signature(txn.Signature())})
386-
if err != nil {
387-
continue
388-
}
383+
select {
384+
case <-time.After(time.Second):
385+
statuses, err := s.data.GetBlockchainSignatureStatuses(ctx, []solana.Signature{solana.Signature(txn.Signature())})
386+
if err != nil {
387+
continue
388+
}
389389

390-
if len(statuses) == 0 || statuses[0] == nil {
391-
continue
392-
}
390+
if len(statuses) == 0 || statuses[0] == nil {
391+
continue
392+
}
393393

394-
if statuses[0].ErrorResult != nil {
395-
log.WithError(statuses[0].ErrorResult).Warn("transaction failed")
396-
return handleSwapStructuredError(streamer, transactionpb.SwapResponse_Error_SWAP_FAILED)
397-
}
394+
if statuses[0].ErrorResult != nil {
395+
log.WithError(statuses[0].ErrorResult).Warn("transaction failed")
396+
return handleSwapStructuredError(streamer, transactionpb.SwapResponse_Error_SWAP_FAILED)
397+
}
398398

399-
if statuses[0].Finalized() {
400-
log.Debug("transaction succeeded and is finalized")
401-
err = streamer.Send(&transactionpb.SwapResponse{
402-
Response: &transactionpb.SwapResponse_Success_{
403-
Success: &transactionpb.SwapResponse_Success{
404-
Code: transactionpb.SwapResponse_Success_SWAP_FINALIZED,
399+
if statuses[0].Finalized() {
400+
log.Debug("transaction succeeded and is finalized")
401+
err = streamer.Send(&transactionpb.SwapResponse{
402+
Response: &transactionpb.SwapResponse_Success_{
403+
Success: &transactionpb.SwapResponse_Success{
404+
Code: transactionpb.SwapResponse_Success_SWAP_FINALIZED,
405+
},
405406
},
406-
},
407-
})
408-
return handleSwapError(streamer, err)
407+
})
408+
return handleSwapError(streamer, err)
409+
}
410+
case <-ctx.Done():
411+
return handleSwapError(streamer, ctx.Err())
409412
}
410413
}
411414
}

0 commit comments

Comments
 (0)