@@ -50,8 +50,8 @@ type LighterError struct {
5050 Message string `json:"message"`
5151}
5252
53- func (e * LighterError ) Error () error {
54- return fmt .Errorf ("lighter error: code %d, message: %s" , e .Code , e .Message )
53+ func (e * LighterError ) Error () string {
54+ return fmt .Sprintf ("lighter error: code %d, message: %s" , e .Code , e .Message )
5555}
5656
5757func (tx * LighterTx ) UnmarshalJSON (data []byte ) error {
@@ -60,18 +60,14 @@ func (tx *LighterTx) UnmarshalJSON(data []byte) error {
6060 return err
6161 }
6262
63- if tx .Code != 200 {
64- return fmt .Errorf ("lighter error: code %d, message: %s" , tx .Code , tx .Info )
65- }
66-
6763 if tx .Type == TxTypeL2Transfer {
6864 var t * Transfer
6965 if err := json .Unmarshal ([]byte (tx .Info ), & t ); err != nil {
7066 return fmt .Errorf ("failed to unmarshal transfer: %w" , err )
7167 }
7268 tx .Transfer = t
7369 } else {
74- return fmt .Errorf ("invalid transaction type: %d" , tx .Type )
70+ return fmt .Errorf ("unsupported transaction type: %d" , tx .Type )
7571 }
7672
7773 return nil
@@ -86,17 +82,17 @@ func NewLighterAPI() *LighterAPI {
8682 retryClient .RetryMax = TX_NOT_FOUND_RETRIES - 1 // RetryMax is a number of retries after an initial attempt
8783 retryClient .RetryWaitMin = TX_NOT_FOUND_RETRY_WAIT
8884 retryClient .RetryWaitMax = TX_NOT_FOUND_RETRY_WAIT
89- retryClient .CheckRetry = lighterCheckRetry
85+ retryClient .CheckRetry = LighterCheckRetry
9086 retryClient .Logger = log .Logger
9187
9288 return & LighterAPI {
9389 HTTPClient : retryClient .StandardClient (),
9490 }
9591}
9692
97- // lighterCheckRetry checks if we should retry based on Lighter API error codes
98- func lighterCheckRetry ( ctx context. Context , resp * http. Response , err error ) ( bool , error ) {
99- // Don't retry on context cancellation or client errors
93+ // LighterCheckRetry checks if we should retry the request.
94+ // Retries when: error code is TX_NOT_FOUND_ERROR_CODE (21500) or response has code 200 but missing/empty info.
95+ func LighterCheckRetry ( ctx context. Context , resp * http. Response , err error ) ( bool , error ) {
10096 if ctx .Err () != nil {
10197 return false , ctx .Err ()
10298 }
@@ -105,7 +101,6 @@ func lighterCheckRetry(ctx context.Context, resp *http.Response, err error) (boo
105101 return false , err
106102 }
107103
108- // Only retry on 200 OK with transaction not found error
109104 if resp .StatusCode == http .StatusOK {
110105 body , err := io .ReadAll (resp .Body )
111106 if err != nil {
@@ -114,14 +109,22 @@ func lighterCheckRetry(ctx context.Context, resp *http.Response, err error) (boo
114109 resp .Body .Close ()
115110 resp .Body = io .NopCloser (bytes .NewReader (body ))
116111
117- s := new (LighterTx )
118- if err := json .Unmarshal (body , s ); err != nil {
119- e := new (LighterError )
120- if err := json .Unmarshal (body , e ); err != nil {
121- return false , fmt .Errorf ("failed to unmarshal response body: %s, with error: %w" , string (body ), err )
122- }
123- if e .Code == TX_NOT_FOUND_ERROR_CODE || e .Code == TX_FOUND_STATUS_CODE {
124- return true , nil
112+ // First try to unmarshal as LighterError
113+ e := new (LighterError )
114+ if err := json .Unmarshal (body , e ); err == nil && e .Code == TX_NOT_FOUND_ERROR_CODE {
115+ // Retry on TX_NOT_FOUND_ERROR_CODE
116+ return true , nil
117+ }
118+
119+ // Check if it's a LighterTx with code 200 but missing info (before custom UnmarshalJSON runs)
120+ var raw map [string ]interface {}
121+ if err := json .Unmarshal (body , & raw ); err == nil {
122+ if code , ok := raw ["code" ].(float64 ); ok && code == TX_FOUND_STATUS_CODE {
123+ info , hasInfo := raw ["info" ].(string )
124+ // Retry if info is missing or empty
125+ if ! hasInfo || info == "" {
126+ return true , nil
127+ }
125128 }
126129 }
127130 }
@@ -149,11 +152,7 @@ func (a *LighterAPI) GetTx(hash string) (*LighterTx, error) {
149152
150153 s := new (LighterTx )
151154 if err := json .Unmarshal (body , s ); err != nil {
152- e := new (LighterError )
153- if err := json .Unmarshal (body , e ); err != nil {
154- return nil , fmt .Errorf ("failed to unmarshal response body: %s, with error: %w" , string (body ), err )
155- }
156- return nil , e .Error ()
155+ return nil , err
157156 }
158157
159158 return s , nil
0 commit comments