1
+ import axios , { AxiosAdapter , AxiosError , AxiosResponse , InternalAxiosRequestConfig } from 'axios'
1
2
import type { Signer } from '@chorus-one/signer'
2
3
import type {
3
4
TonNetworkConfig ,
@@ -17,7 +18,9 @@ import {
17
18
MessageRelaxed ,
18
19
SendMode ,
19
20
Address ,
20
- TransactionDescriptionGeneric
21
+ TransactionDescriptionGeneric ,
22
+ beginCell ,
23
+ storeMessage
21
24
} from '@ton/ton'
22
25
import { createWalletTransferV4 , externalMessage , sign } from './tx'
23
26
import * as tonMnemonic from 'tonweb-mnemonic'
@@ -133,8 +136,45 @@ export class TonBaseStaker {
133
136
* @returns A promise which resolves once the TonStaker instance has been initialized.
134
137
*/
135
138
async init ( ) : Promise < void > {
139
+ const rateLimitRetryAdapter : AxiosAdapter = async ( config : InternalAxiosRequestConfig ) : Promise < AxiosResponse > => {
140
+ const maxRetries = 3
141
+ const retryDelay = 1000
142
+
143
+ let retries = 0
144
+
145
+ const defaultAdapter = axios . getAdapter ( axios . defaults . adapter )
146
+ if ( ! defaultAdapter ) {
147
+ throw new Error ( 'Axios default adapter is not available' )
148
+ }
149
+
150
+ while ( retries <= maxRetries ) {
151
+ try {
152
+ // Send the request using the default adapter
153
+ return await defaultAdapter ( config )
154
+ } catch ( err ) {
155
+ const error = err as AxiosError
156
+
157
+ const status = error . response ?. status
158
+
159
+ // If rate limit hit (429), wait and retry
160
+ if ( status === 429 && retries < maxRetries ) {
161
+ retries += 1
162
+ console . log ( `Rate limit hit, try ${ retries } /${ maxRetries } ` )
163
+
164
+ await new Promise ( ( resolve ) => setTimeout ( resolve , retryDelay ) )
165
+ } else {
166
+ throw error
167
+ }
168
+ }
169
+ }
170
+
171
+ // Should never reach this point
172
+ throw new Error ( `Rate limit exceeded after ${ maxRetries } retries.` )
173
+ }
174
+
136
175
this . client = new TonClient ( {
137
- endpoint : this . networkConfig . rpcUrl
176
+ endpoint : this . networkConfig . rpcUrl ,
177
+ httpAdapter : rateLimitRetryAdapter
138
178
} )
139
179
}
140
180
@@ -381,8 +421,16 @@ export class TonBaseStaker {
381
421
const { address, txHash, limit } = params
382
422
383
423
const transactions = await client . getTransactions ( Address . parse ( address ) , { limit : limit ?? 10 } )
384
- const transaction = transactions . find ( ( tx ) => tx . hash ( ) . toString ( 'hex' ) === txHash )
424
+ const transaction = transactions . find ( ( tx ) => {
425
+ // Check tx hash
426
+ if ( tx . hash ( ) . toString ( 'hex' ) === txHash ) return true
427
+
428
+ // Check inMessage tx hash(that is the one we get from broadcast method)
429
+ if ( tx . inMessage && beginCell ( ) . store ( storeMessage ( tx . inMessage ) ) . endCell ( ) . hash ( ) . toString ( 'hex' ) === txHash )
430
+ return true
385
431
432
+ return false
433
+ } )
386
434
if ( transaction === undefined ) {
387
435
return { status : 'unknown' , receipt : null }
388
436
}
0 commit comments