Skip to content

Commit a0fc2be

Browse files
committed
refactor: merge public and wallet clients
1 parent c3ae2f6 commit a0fc2be

File tree

1 file changed

+30
-41
lines changed
  • apps/price_pusher/src/evm

1 file changed

+30
-41
lines changed

Diff for: apps/price_pusher/src/evm/evm.ts

+30-41
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ import {
3636
InternalRpcError,
3737
InsufficientFundsError,
3838
Chain,
39+
publicActions,
40+
Client,
41+
RpcSchema,
42+
WalletActions,
43+
PublicActions,
3944
} from "viem";
4045

4146
import { mnemonicToAccount } from "viem/accounts";
@@ -46,6 +51,18 @@ type PythContract = GetContractReturnType<
4651
PublicClient | WalletClient
4752
>;
4853

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+
4966
const UNKNOWN_CHAIN_CONFIG = {
5067
name: "Unknown",
5168
nativeCurrency: {
@@ -159,8 +176,7 @@ export class EvmPriceListener extends ChainPriceListener {
159176

160177
export class EvmPricePusher implements IPricePusher {
161178
private customGasStation?: CustomGasStation;
162-
private publicClient: PublicClient;
163-
private walletClient: WalletClient;
179+
private client: SuperWalletClient;
164180
private pythContract: PythContract;
165181
private pusherAddress: `0x${string}` | undefined;
166182
private lastPushAttempt: PushAttempt | undefined;
@@ -176,9 +192,8 @@ export class EvmPricePusher implements IPricePusher {
176192
customGasStation?: CustomGasStation
177193
) {
178194
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();
182197
}
183198

184199
// The pubTimes are passed here to use the values that triggered the push.
@@ -222,7 +237,7 @@ export class EvmPricePusher implements IPricePusher {
222237
throw e;
223238
}
224239

225-
const fees = await this.publicClient.estimateFeesPerGas();
240+
const fees = await this.client.estimateFeesPerGas();
226241

227242
this.logger.debug({ fees }, "Estimated fees");
228243

@@ -233,11 +248,11 @@ export class EvmPricePusher implements IPricePusher {
233248

234249
// Try to re-use the same nonce and increase the gas if the last tx is not landed yet.
235250
if (this.pusherAddress === undefined) {
236-
this.pusherAddress = this.walletClient.account!.address;
251+
this.pusherAddress = this.client.account!.address;
237252
}
238253

239254
const lastExecutedNonce =
240-
(await this.publicClient.getTransactionCount({
255+
(await this.client.getTransactionCount({
241256
address: this.pusherAddress,
242257
})) - 1;
243258

@@ -297,8 +312,8 @@ export class EvmPricePusher implements IPricePusher {
297312
this.gasLimit !== undefined
298313
? BigInt(Math.round(this.gasLimit))
299314
: undefined,
300-
chain: this.walletClient.chain,
301-
account: this.walletClient.account!,
315+
chain: this.client.chain,
316+
account: this.client.account!,
302317
}
303318
);
304319

@@ -431,7 +446,7 @@ export class EvmPricePusher implements IPricePusher {
431446

432447
private async waitForTransactionReceipt(hash: `0x${string}`): Promise<void> {
433448
try {
434-
const receipt = await this.publicClient.waitForTransactionReceipt({
449+
const receipt = await this.client.waitForTransactionReceipt({
435450
hash: hash,
436451
});
437452

@@ -496,58 +511,32 @@ export class PythContractFactory {
496511
}
497512

498513
/**
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).
519515
*
520516
* @returns Pyth contract
521517
*/
522518
createPythContract(): PythContract {
523519
return getContract({
524520
address: this.pythContractAddress,
525521
abi: PythAbi,
526-
client: this.createPublicClient(),
522+
client: this.createClient(),
527523
});
528524
}
529525

530526
hasWebsocketProvider(): boolean {
531527
return isWsEndpoint(this.endpoint);
532528
}
533529

534-
createPublicClient(): PublicClient {
535-
return createPublicClient({
536-
transport: PythContractFactory.getTransport(this.endpoint),
537-
chain: PythContractFactory.getChain(this.chainId),
538-
});
539-
}
540-
541530
getAccount(): Account {
542531
return mnemonicToAccount(this.mnemonic);
543532
}
544533

545-
createWalletClient(): WalletClient {
534+
createClient(): SuperWalletClient {
546535
return createWalletClient({
547536
transport: PythContractFactory.getTransport(this.endpoint),
548537
account: mnemonicToAccount(this.mnemonic),
549538
chain: PythContractFactory.getChain(this.chainId),
550-
});
539+
}).extend(publicActions);
551540
}
552541

553542
// Get the chain corresponding to the chainId. If the chain is not found, it will return

0 commit comments

Comments
 (0)