@@ -107,7 +107,8 @@ func GetClientWithoutScheme(rpcURL string) (ethclient.Client, string, error) {
107107 return nil , "" , notDeterminedErr
108108}
109109
110- // connects an evm client to the given [rpcURL] supporting [repeatsOnFailure] connection failures
110+ // connects an evm client to the given [rpcURL]
111+ // supports [repeatsOnFailure] failures
111112func GetClient (rpcURL string ) (Client , error ) {
112113 client := Client {
113114 URL : rpcURL ,
@@ -116,10 +117,9 @@ func GetClient(rpcURL string) (Client, error) {
116117 if err != nil {
117118 return client , fmt .Errorf ("failure determining the scheme of url %s: %w" , rpcURL , err )
118119 }
119- client .EthClient , err = utils .Retry (
120- func () (ethclient.Client , error ) {
121- ctx , cancel := utils .GetAPILargeContext ()
122- defer cancel ()
120+ client .EthClient , err = utils .RetryWithContextGen (
121+ utils .GetAPILargeContext ,
122+ func (ctx context.Context ) (ethclient.Client , error ) {
123123 if hasScheme {
124124 return ethclient .DialContext (ctx , rpcURL )
125125 } else {
@@ -136,50 +136,32 @@ func GetClient(rpcURL string) (Client, error) {
136136 return client , err
137137}
138138
139+ // closes underlying ethclient connection
139140func (client Client ) Close () {
140141 client .EthClient .Close ()
141142}
142143
143- func (client Client ) BalanceAt (ctx context.Context , address common.Address , blockNumber * big.Int ) (* big.Int , error ) {
144- return client .EthClient .BalanceAt (ctx , address , blockNumber )
145- }
146-
147- func (client Client ) BlockNumber (ctx context.Context ) (uint64 , error ) {
148- return client .EthClient .BlockNumber (ctx )
149- }
150-
151- func (client Client ) BlockByNumber (ctx context.Context , n * big.Int ) (* types.Block , error ) {
152- return client .EthClient .BlockByNumber (ctx , n )
153- }
154-
155- func (client Client ) FilterLogs (ctx context.Context , query interfaces.FilterQuery ) ([]types.Log , error ) {
156- return client .EthClient .FilterLogs (ctx , query )
157- }
158-
159- func (client Client ) TransactionReceipt (ctx context.Context , hash common.Hash ) (* types.Receipt , error ) {
160- return client .EthClient .TransactionReceipt (ctx , hash )
161- }
162-
163- func ContractAlreadyDeployed (
164- client Client ,
144+ // indicates wether a contract is deployed on [contractAddress]
145+ // supports [repeatsOnFailure] failures
146+ func (client Client ) ContractAlreadyDeployed (
165147 contractAddress string ,
166148) (bool , error ) {
167- if bs , err := GetContractBytecode (client , contractAddress ); err != nil {
149+ if bs , err := client . GetContractBytecode (contractAddress ); err != nil {
168150 return false , err
169151 } else {
170152 return len (bs ) != 0 , nil
171153 }
172154}
173155
174- func GetContractBytecode (
175- client Client ,
156+ // returns the contract bytecode at [contractAddress]
157+ // supports [repeatsOnFailure] failures
158+ func (client Client ) GetContractBytecode (
176159 contractAddressStr string ,
177160) ([]byte , error ) {
178161 contractAddress := common .HexToAddress (contractAddressStr )
179- code , err := utils .Retry (
180- func () ([]byte , error ) {
181- ctx , cancel := utils .GetAPILargeContext ()
182- defer cancel ()
162+ code , err := utils .RetryWithContextGen (
163+ utils .GetAPILargeContext ,
164+ func (ctx context.Context ) ([]byte , error ) {
183165 return client .EthClient .CodeAt (ctx , contractAddress , nil )
184166 },
185167 repeatsOnFailure ,
@@ -196,127 +178,114 @@ func GetContractBytecode(
196178 return code , err
197179}
198180
199- func GetPrivateKeyBalance (
200- client Client ,
181+ // returns the balance for [privateKey]
182+ // supports [repeatsOnFailure] failures
183+ func (client Client ) GetPrivateKeyBalance (
201184 privateKey string ,
202185) (* big.Int , error ) {
203186 addr , err := PrivateKeyToAddress (privateKey )
204187 if err != nil {
205188 return nil , err
206189 }
207- return GetAddressBalance (client , addr .Hex ())
190+ return client . GetAddressBalance (addr .Hex ())
208191}
209192
210- func GetAddressBalance (
211- client Client ,
193+ // returns the balance for [address]
194+ // supports [repeatsOnFailure] failures
195+ func (client Client ) GetAddressBalance (
212196 addressStr string ,
213197) (* big.Int , error ) {
214198 address := common .HexToAddress (addressStr )
215- var (
216- balance * big.Int
217- err error
199+ balance , err := utils .RetryWithContextGen (
200+ utils .GetAPILargeContext ,
201+ func (ctx context.Context ) (* big.Int , error ) {
202+ return client .EthClient .BalanceAt (ctx , address , nil )
203+ },
204+ repeatsOnFailure ,
205+ sleepBetweenRepeats ,
218206 )
219- for i := 0 ; i < repeatsOnFailure ; i ++ {
220- ctx , cancel := utils .GetAPILargeContext ()
221- defer cancel ()
222- balance , err = client .EthClient .BalanceAt (ctx , address , nil )
223- if err == nil {
224- break
225- }
207+ if err != nil {
226208 err = fmt .Errorf ("failure obtaining balance for %s on %s: %w" , addressStr , client .URL , err )
227- ux .Logger .RedXToUser ("%s" , err )
228- time .Sleep (sleepBetweenRepeats )
229209 }
230210 return balance , err
231211}
232212
233- // Returns the gasFeeCap, gasTipCap, and nonce the be used when constructing a transaction from address
234- func CalculateTxParams (
235- client Client ,
236- addressStr string ,
237- ) (* big.Int , * big.Int , uint64 , error ) {
238- baseFee , err := EstimateBaseFee (client )
239- if err != nil {
240- return nil , nil , 0 , err
241- }
242- gasTipCap , err := SuggestGasTipCap (client )
243- if err != nil {
244- return nil , nil , 0 , err
245- }
246- nonce , err := NonceAt (client , addressStr )
247- if err != nil {
248- return nil , nil , 0 , err
249- }
250- gasFeeCap := baseFee .Mul (baseFee , big .NewInt (BaseFeeFactor ))
251- gasFeeCap .Add (gasFeeCap , big .NewInt (MaxPriorityFeePerGas ))
252- return gasFeeCap , gasTipCap , nonce , nil
253- }
254-
255- func NonceAt (
256- client Client ,
213+ // returns the nonce at [address]
214+ // supports [repeatsOnFailure] failures
215+ func (client Client ) NonceAt (
257216 addressStr string ,
258217) (uint64 , error ) {
259218 address := common .HexToAddress (addressStr )
260- var (
261- nonce uint64
262- err error
219+ nonce , err := utils .RetryWithContextGen (
220+ utils .GetAPILargeContext ,
221+ func (ctx context.Context ) (uint64 , error ) {
222+ return client .EthClient .NonceAt (ctx , address , nil )
223+ },
224+ repeatsOnFailure ,
225+ sleepBetweenRepeats ,
263226 )
264- for i := 0 ; i < repeatsOnFailure ; i ++ {
265- ctx , cancel := utils .GetAPILargeContext ()
266- defer cancel ()
267- nonce , err = client .EthClient .NonceAt (ctx , address , nil )
268- if err == nil {
269- break
270- }
227+ if err != nil {
271228 err = fmt .Errorf ("failure obtaining nonce for %s on %s: %w" , addressStr , client .URL , err )
272- ux .Logger .RedXToUser ("%s" , err )
273- time .Sleep (sleepBetweenRepeats )
274229 }
275230 return nonce , err
276231}
277232
278- func SuggestGasTipCap (
279- client Client ,
280- ) (* big.Int , error ) {
281- var (
282- gasTipCap * big.Int
283- err error
233+ // returns the suggested gas tip
234+ // supports [repeatsOnFailure] failures
235+ func (client Client ) SuggestGasTipCap () (* big.Int , error ) {
236+ gasTipCap , err := utils .RetryWithContextGen (
237+ utils .GetAPILargeContext ,
238+ func (ctx context.Context ) (* big.Int , error ) {
239+ return client .EthClient .SuggestGasTipCap (ctx )
240+ },
241+ repeatsOnFailure ,
242+ sleepBetweenRepeats ,
284243 )
285- for i := 0 ; i < repeatsOnFailure ; i ++ {
286- ctx , cancel := utils .GetAPILargeContext ()
287- defer cancel ()
288- gasTipCap , err = client .EthClient .SuggestGasTipCap (ctx )
289- if err == nil {
290- break
291- }
244+ if err != nil {
292245 err = fmt .Errorf ("failure obtaining gas tip cap on %s: %w" , client .URL , err )
293- ux .Logger .RedXToUser ("%s" , err )
294- time .Sleep (sleepBetweenRepeats )
295246 }
296247 return gasTipCap , err
297248}
298249
299- func EstimateBaseFee (
300- client Client ,
301- ) (* big.Int , error ) {
302- var (
303- baseFee * big.Int
304- err error
250+ // returns the estimated base fee
251+ // supports [repeatsOnFailure] failures
252+ func (client Client ) EstimateBaseFee () (* big.Int , error ) {
253+ baseFee , err := utils .RetryWithContextGen (
254+ utils .GetAPILargeContext ,
255+ func (ctx context.Context ) (* big.Int , error ) {
256+ return client .EthClient .EstimateBaseFee (ctx )
257+ },
258+ repeatsOnFailure ,
259+ sleepBetweenRepeats ,
305260 )
306- for i := 0 ; i < repeatsOnFailure ; i ++ {
307- ctx , cancel := utils .GetAPILargeContext ()
308- defer cancel ()
309- baseFee , err = client .EthClient .EstimateBaseFee (ctx )
310- if err == nil {
311- break
312- }
261+ if err != nil {
313262 err = fmt .Errorf ("failure estimating base fee on %s: %w" , client .URL , err )
314- ux .Logger .RedXToUser ("%s" , err )
315- time .Sleep (sleepBetweenRepeats )
316263 }
317264 return baseFee , err
318265}
319266
267+ // Returns the gasFeeCap, gasTipCap, and nonce the be used when constructing a transaction from address
268+ func CalculateTxParams (
269+ client Client ,
270+ addressStr string ,
271+ ) (* big.Int , * big.Int , uint64 , error ) {
272+ baseFee , err := client .EstimateBaseFee ()
273+ if err != nil {
274+ return nil , nil , 0 , err
275+ }
276+ gasTipCap , err := client .SuggestGasTipCap ()
277+ if err != nil {
278+ return nil , nil , 0 , err
279+ }
280+ nonce , err := client .NonceAt (addressStr )
281+ if err != nil {
282+ return nil , nil , 0 , err
283+ }
284+ gasFeeCap := baseFee .Mul (baseFee , big .NewInt (BaseFeeFactor ))
285+ gasFeeCap .Add (gasFeeCap , big .NewInt (MaxPriorityFeePerGas ))
286+ return gasFeeCap , gasTipCap , nonce , nil
287+ }
288+
320289func EstimateGasLimit (
321290 client Client ,
322291 msg interfaces.CallMsg ,
@@ -886,3 +855,19 @@ func PrivateKeyToAddress(privateKey string) (common.Address, error) {
886855 }
887856 return crypto .PubkeyToAddress (pk .PublicKey ), nil
888857}
858+
859+ func (client Client ) BlockNumber (ctx context.Context ) (uint64 , error ) {
860+ return client .EthClient .BlockNumber (ctx )
861+ }
862+
863+ func (client Client ) BlockByNumber (ctx context.Context , n * big.Int ) (* types.Block , error ) {
864+ return client .EthClient .BlockByNumber (ctx , n )
865+ }
866+
867+ func (client Client ) FilterLogs (ctx context.Context , query interfaces.FilterQuery ) ([]types.Log , error ) {
868+ return client .EthClient .FilterLogs (ctx , query )
869+ }
870+
871+ func (client Client ) TransactionReceipt (ctx context.Context , hash common.Hash ) (* types.Receipt , error ) {
872+ return client .EthClient .TransactionReceipt (ctx , hash )
873+ }
0 commit comments