@@ -36,6 +36,11 @@ import {
36
36
InternalRpcError ,
37
37
InsufficientFundsError ,
38
38
Chain ,
39
+ publicActions ,
40
+ Client ,
41
+ RpcSchema ,
42
+ WalletActions ,
43
+ PublicActions ,
39
44
} from "viem" ;
40
45
41
46
import { mnemonicToAccount } from "viem/accounts" ;
@@ -46,6 +51,18 @@ type PythContract = GetContractReturnType<
46
51
PublicClient | WalletClient
47
52
> ;
48
53
54
+ export type SuperWalletClient <
55
+ transport extends Transport = Transport ,
56
+ chain extends Chain | undefined = Chain | undefined ,
57
+ account extends Account | undefined = Account | undefined
58
+ > = Client <
59
+ transport ,
60
+ chain ,
61
+ account ,
62
+ RpcSchema ,
63
+ PublicActions < transport , chain , account > & WalletActions < chain , account >
64
+ > ;
65
+
49
66
const UNKNOWN_CHAIN_CONFIG = {
50
67
name : "Unknown" ,
51
68
nativeCurrency : {
@@ -159,8 +176,7 @@ export class EvmPriceListener extends ChainPriceListener {
159
176
160
177
export class EvmPricePusher implements IPricePusher {
161
178
private customGasStation ?: CustomGasStation ;
162
- private publicClient : PublicClient ;
163
- private walletClient : WalletClient ;
179
+ private client : SuperWalletClient ;
164
180
private pythContract : PythContract ;
165
181
private pusherAddress : `0x${string } ` | undefined ;
166
182
private lastPushAttempt : PushAttempt | undefined ;
@@ -176,9 +192,8 @@ export class EvmPricePusher implements IPricePusher {
176
192
customGasStation ?: CustomGasStation
177
193
) {
178
194
this . customGasStation = customGasStation ;
179
- this . pythContract = pythContractFactory . createPythContractWithWallet ( ) ;
180
- this . publicClient = pythContractFactory . createPublicClient ( ) ;
181
- this . walletClient = pythContractFactory . createWalletClient ( ) ;
195
+ this . pythContract = pythContractFactory . createPythContract ( ) ;
196
+ this . client = pythContractFactory . createClient ( ) ;
182
197
}
183
198
184
199
// The pubTimes are passed here to use the values that triggered the push.
@@ -222,7 +237,7 @@ export class EvmPricePusher implements IPricePusher {
222
237
throw e ;
223
238
}
224
239
225
- const fees = await this . publicClient . estimateFeesPerGas ( ) ;
240
+ const fees = await this . client . estimateFeesPerGas ( ) ;
226
241
227
242
this . logger . debug ( { fees } , "Estimated fees" ) ;
228
243
@@ -233,11 +248,11 @@ export class EvmPricePusher implements IPricePusher {
233
248
234
249
// Try to re-use the same nonce and increase the gas if the last tx is not landed yet.
235
250
if ( this . pusherAddress === undefined ) {
236
- this . pusherAddress = this . walletClient . account ! . address ;
251
+ this . pusherAddress = this . client . account ! . address ;
237
252
}
238
253
239
254
const lastExecutedNonce =
240
- ( await this . publicClient . getTransactionCount ( {
255
+ ( await this . client . getTransactionCount ( {
241
256
address : this . pusherAddress ,
242
257
} ) ) - 1 ;
243
258
@@ -297,8 +312,8 @@ export class EvmPricePusher implements IPricePusher {
297
312
this . gasLimit !== undefined
298
313
? BigInt ( Math . round ( this . gasLimit ) )
299
314
: undefined ,
300
- chain : this . walletClient . chain ,
301
- account : this . walletClient . account ! ,
315
+ chain : this . client . chain ,
316
+ account : this . client . account ! ,
302
317
}
303
318
) ;
304
319
@@ -431,7 +446,7 @@ export class EvmPricePusher implements IPricePusher {
431
446
432
447
private async waitForTransactionReceipt ( hash : `0x${string } `) : Promise < void > {
433
448
try {
434
- const receipt = await this . publicClient . waitForTransactionReceipt ( {
449
+ const receipt = await this . client . waitForTransactionReceipt ( {
435
450
hash : hash ,
436
451
} ) ;
437
452
@@ -496,58 +511,32 @@ export class PythContractFactory {
496
511
}
497
512
498
513
/**
499
- * This method creates a web3 Pyth contract with payer (based on HDWalletProvider). As this
500
- * provider is an HDWalletProvider it does not support subscriptions even if the
501
- * endpoint is a websocket endpoint.
502
- *
503
- * @returns Pyth contract
504
- */
505
- createPythContractWithWallet ( ) : PythContract {
506
- return getContract ( {
507
- address : this . pythContractAddress ,
508
- abi : PythAbi ,
509
- client : {
510
- public : this . createPublicClient ( ) ,
511
- wallet : this . createWalletClient ( ) ,
512
- } ,
513
- } ) ;
514
- }
515
-
516
- /**
517
- * This method creates a web3 Pyth contract with the given endpoint as its provider. If
518
- * the endpoint is a websocket endpoint the contract will support subscriptions.
514
+ * This method creates a web3 Pyth contract with payer (based on mnemonic).
519
515
*
520
516
* @returns Pyth contract
521
517
*/
522
518
createPythContract ( ) : PythContract {
523
519
return getContract ( {
524
520
address : this . pythContractAddress ,
525
521
abi : PythAbi ,
526
- client : this . createPublicClient ( ) ,
522
+ client : this . createClient ( ) ,
527
523
} ) ;
528
524
}
529
525
530
526
hasWebsocketProvider ( ) : boolean {
531
527
return isWsEndpoint ( this . endpoint ) ;
532
528
}
533
529
534
- createPublicClient ( ) : PublicClient {
535
- return createPublicClient ( {
536
- transport : PythContractFactory . getTransport ( this . endpoint ) ,
537
- chain : PythContractFactory . getChain ( this . chainId ) ,
538
- } ) ;
539
- }
540
-
541
530
getAccount ( ) : Account {
542
531
return mnemonicToAccount ( this . mnemonic ) ;
543
532
}
544
533
545
- createWalletClient ( ) : WalletClient {
534
+ createClient ( ) : SuperWalletClient {
546
535
return createWalletClient ( {
547
536
transport : PythContractFactory . getTransport ( this . endpoint ) ,
548
537
account : mnemonicToAccount ( this . mnemonic ) ,
549
538
chain : PythContractFactory . getChain ( this . chainId ) ,
550
- } ) ;
539
+ } ) . extend ( publicActions ) ;
551
540
}
552
541
553
542
// Get the chain corresponding to the chainId. If the chain is not found, it will return
0 commit comments