66 Address ,
77 Block ,
88 Chain ,
9- ContractFunctionArgs ,
10- ContractFunctionName ,
119 EstimateContractGasParameters ,
1210 GetContractReturnType ,
1311 Hex ,
@@ -34,6 +32,18 @@ import { Telemetry } from "../server/telemetry";
3432import { lazyCache } from "../utils/cache" ;
3533import { memoize } from "../utils/func" ;
3634
35+ type ViemWriteContractParameters <
36+ TAbi extends Abi | readonly unknown [ ] ,
37+ TFunctionName extends string ,
38+ TChainOverride extends Chain | undefined = undefined ,
39+ > = WriteContractParameters <
40+ TAbi ,
41+ TFunctionName ,
42+ Chain ,
43+ Account ,
44+ TChainOverride
45+ > ;
46+
3747const GAS_LIMIT_RESCALE_PERCENT = 150 ; // Scale gas limit to 150% of estimate
3848const BASE_FEE_RESCALE_PERCENT = 200 ; // Scale base fee to 200% of original in maxFeePerGas calculation
3949const PRIO_FEE_SCALE_RESCALE_PERCENT = 120 ; // Scale priority fee to 120% of original
@@ -107,14 +117,11 @@ export class ViemClient {
107117 ) {
108118 this . account = this . walletClient . account ;
109119 const { waitForTransactionReceipt } = publicClient ;
110- publicClient . waitForTransactionReceipt = ( args ) => {
111- // Viem's default is 6 = ~24s
112- return waitForTransactionReceipt ( {
120+ publicClient . waitForTransactionReceipt = ( args ) =>
121+ waitForTransactionReceipt ( {
113122 ...args ,
114- retryCount : 10 ,
115123 timeout : 60_000 , // Wait at most 1 minute for a tx to confirm
116124 } ) ;
117- } ;
118125 }
119126
120127 getEnsAddress = memoize (
@@ -243,23 +250,11 @@ export class ViemClient {
243250 /** Estimate the gas limit for a tx with a defensive buffer. */
244251 async getGasLimit <
245252 const TAbi extends Abi | readonly unknown [ ] ,
246- TFunctionName extends ContractFunctionName < TAbi , "payable" | "nonpayable" > ,
247- TArgs extends ContractFunctionArgs <
248- TAbi ,
249- "payable" | "nonpayable" ,
250- TFunctionName
251- > ,
253+ TFunctionName extends string ,
252254 TChainOverride extends Chain | undefined = undefined ,
253255 > (
254256 block : Block ,
255- args : WriteContractParameters <
256- TAbi ,
257- TFunctionName ,
258- TArgs ,
259- Chain ,
260- Account ,
261- TChainOverride
262- >
257+ args : ViemWriteContractParameters < TAbi , TFunctionName , TChainOverride >
263258 ) : Promise < bigint > {
264259 try {
265260 // Don't retry. estimateContractGas usually fails because of reverts
@@ -289,23 +284,11 @@ export class ViemClient {
289284 */
290285 async setOverrideParams <
291286 const TAbi extends Abi | readonly unknown [ ] ,
292- TFunctionName extends ContractFunctionName < TAbi , "payable" | "nonpayable" > ,
293- TArgs extends ContractFunctionArgs <
294- TAbi ,
295- "payable" | "nonpayable" ,
296- TFunctionName
297- > ,
287+ TFunctionName extends string ,
298288 TChainOverride extends Chain | undefined = undefined ,
299289 > (
300290 localTxId : number ,
301- args : WriteContractParameters <
302- TAbi ,
303- TFunctionName ,
304- TArgs ,
305- Chain ,
306- Account ,
307- TChainOverride
308- > ,
291+ args : ViemWriteContractParameters < TAbi , TFunctionName , TChainOverride > ,
309292 prevGasFees : { maxFeePerGas : bigint ; maxPriorityFeePerGas : bigint }
310293 ) : Promise < void > {
311294 const block = await this . publicClient . getBlock ( { blockTag : "latest" } ) ;
@@ -362,23 +345,11 @@ export class ViemClient {
362345
363346 private getWriteContractLogMessage <
364347 const TAbi extends Abi | readonly unknown [ ] ,
365- TFunctionName extends ContractFunctionName < TAbi , "payable" | "nonpayable" > ,
366- TArgs extends ContractFunctionArgs <
367- TAbi ,
368- "payable" | "nonpayable" ,
369- TFunctionName
370- > ,
348+ TFunctionName extends string ,
371349 TChainOverride extends Chain | undefined = undefined ,
372350 > (
373351 localTxId : number ,
374- args : WriteContractParameters <
375- TAbi ,
376- TFunctionName ,
377- TArgs ,
378- Chain ,
379- Account ,
380- TChainOverride
381- >
352+ args : ViemWriteContractParameters < TAbi , TFunctionName , TChainOverride >
382353 ) {
383354 return `[VIEM] txId ${ localTxId } from ${ this . walletClient . account . address } : ${ args . functionName } on ${ args . address } chain ${ this . publicClient . chain . id } ` ;
384355 }
@@ -388,22 +359,10 @@ export class ViemClient {
388359 */
389360 async writeContractAndGetReceipt <
390361 const TAbi extends Abi | readonly unknown [ ] ,
391- TFunctionName extends ContractFunctionName < TAbi , "payable" | "nonpayable" > ,
392- TArgs extends ContractFunctionArgs <
393- TAbi ,
394- "payable" | "nonpayable" ,
395- TFunctionName
396- > ,
362+ TFunctionName extends string ,
397363 TChainOverride extends Chain | undefined = undefined ,
398364 > (
399- args : WriteContractParameters <
400- TAbi ,
401- TFunctionName ,
402- TArgs ,
403- Chain ,
404- Account ,
405- TChainOverride
406- >
365+ args : ViemWriteContractParameters < TAbi , TFunctionName , TChainOverride >
407366 ) : Promise < { txHash : Hex ; receipt : TransactionReceipt } > {
408367 const startMs = performance . now ( ) ;
409368 const localTxId = Math . floor ( Math . random ( ) * 1e6 ) ;
0 commit comments