@@ -12,7 +12,6 @@ import {
12
12
} from "../utils" ;
13
13
import { PythAbi } from "./pyth-abi" ;
14
14
import { Logger } from "pino" ;
15
- import { isWsEndpoint } from "../utils" ;
16
15
import {
17
16
PriceServiceConnection ,
18
17
HexString ,
@@ -21,94 +20,38 @@ import {
21
20
import { CustomGasStation } from "./custom-gas-station" ;
22
21
import { PushAttempt } from "../common" ;
23
22
import {
24
- PublicClient ,
25
- Transport ,
26
- WalletClient ,
27
- createPublicClient ,
28
- createWalletClient ,
29
- getContract ,
30
- defineChain ,
31
- http ,
32
- webSocket ,
33
- Address ,
34
- GetContractReturnType ,
35
23
WatchContractEventOnLogsParameter ,
36
24
TransactionExecutionError ,
37
- Account ,
38
25
BaseError ,
39
26
ContractFunctionRevertedError ,
40
27
FeeCapTooLowError ,
41
28
InternalRpcError ,
42
29
InsufficientFundsError ,
43
- Chain ,
44
- publicActions ,
45
- Client ,
46
- RpcSchema ,
47
- WalletActions ,
48
- PublicActions ,
49
30
} from "viem" ;
50
31
51
- import { mnemonicToAccount } from "viem/accounts" ;
52
- import * as chains from "viem/chains" ;
53
-
54
- type PythContract = GetContractReturnType <
55
- typeof PythAbi ,
56
- PublicClient | WalletClient
57
- > ;
58
-
59
- export type SuperWalletClient <
60
- transport extends Transport = Transport ,
61
- chain extends Chain | undefined = Chain ,
62
- account extends Account | undefined = Account
63
- > = Client <
64
- transport ,
65
- chain ,
66
- account ,
67
- RpcSchema ,
68
- PublicActions < transport , chain , account > & WalletActions < chain , account >
69
- > ;
70
-
71
- const UNKNOWN_CHAIN_CONFIG = {
72
- name : "Unknown" ,
73
- nativeCurrency : {
74
- name : "Unknown" ,
75
- symbol : "Unknown" ,
76
- decimals : 18 ,
77
- } ,
78
- rpcUrls : {
79
- default : {
80
- http : [ ] ,
81
- } ,
82
- } ,
83
- } ;
32
+ import { PythContract } from "./pyth-contract" ;
33
+ import { SuperWalletClient } from "./super-wallet" ;
84
34
85
35
export class EvmPriceListener extends ChainPriceListener {
86
- private pythContractFactory : PythContractFactory ;
87
- private pythContract : PythContract ;
88
- private logger : Logger ;
89
-
90
36
constructor (
91
- pythContractFactory : PythContractFactory ,
37
+ private pythContract : PythContract ,
92
38
priceItems : PriceItem [ ] ,
93
- logger : Logger ,
39
+ private watchEvents : boolean ,
40
+ private logger : Logger ,
94
41
config : {
95
42
pollingFrequency : DurationInSeconds ;
96
43
}
97
44
) {
98
45
super ( config . pollingFrequency , priceItems ) ;
99
46
100
- this . pythContractFactory = pythContractFactory ;
101
- this . pythContract = this . pythContractFactory . createPythContract ( ) ;
47
+ this . pythContract = pythContract ;
102
48
this . logger = logger ;
103
49
}
104
50
105
51
// This method should be awaited on and once it finishes it has the latest value
106
52
// for the given price feeds (if they exist).
107
53
async start ( ) {
108
- // It is possible to watch the events in the non-ws endpoints, either by getFilter
109
- // or by getLogs, but it is very expensive and our polling mechanism does it
110
- // in a more efficient way.
111
- if ( this . pythContractFactory . hasWebsocketProvider ( ) ) {
54
+ if ( this . watchEvents ) {
112
55
this . logger . info ( "Watching target network pyth contract events..." ) ;
113
56
this . startWatching ( ) ;
114
57
} else {
@@ -180,26 +123,20 @@ export class EvmPriceListener extends ChainPriceListener {
180
123
}
181
124
182
125
export class EvmPricePusher implements IPricePusher {
183
- private customGasStation ?: CustomGasStation ;
184
- private client : SuperWalletClient ;
185
- private pythContract : PythContract ;
186
126
private pusherAddress : `0x${string } ` | undefined ;
187
127
private lastPushAttempt : PushAttempt | undefined ;
188
128
189
129
constructor (
190
130
private connection : PriceServiceConnection ,
191
- pythContractFactory : PythContractFactory ,
131
+ private client : SuperWalletClient ,
132
+ private pythContract : PythContract ,
192
133
private logger : Logger ,
193
134
private overrideGasPriceMultiplier : number ,
194
135
private overrideGasPriceMultiplierCap : number ,
195
136
private updateFeeMultiplier : number ,
196
137
private gasLimit ?: number ,
197
- customGasStation ?: CustomGasStation
198
- ) {
199
- this . customGasStation = customGasStation ;
200
- this . pythContract = pythContractFactory . createPythContract ( ) ;
201
- this . client = pythContractFactory . createClient ( ) ;
202
- }
138
+ private customGasStation ?: CustomGasStation
139
+ ) { }
203
140
204
141
// The pubTimes are passed here to use the values that triggered the push.
205
142
// This is an optimization to avoid getting a newer value (as an update comes)
@@ -468,86 +405,3 @@ export class EvmPricePusher implements IPricePusher {
468
405
) ;
469
406
}
470
407
}
471
-
472
- export class PythContractFactory {
473
- private endpoint : string ;
474
- private mnemonic : string ;
475
- private pythContractAddress : Address ;
476
- private chainId : number ;
477
-
478
- private constructor (
479
- endpoint : string ,
480
- mnemonic : string ,
481
- pythContractAddress : Address ,
482
- chainId : number
483
- ) {
484
- this . endpoint = endpoint ;
485
- this . mnemonic = mnemonic ;
486
- this . pythContractAddress = pythContractAddress ;
487
- this . chainId = chainId ;
488
- }
489
-
490
- static async create (
491
- endpoint : string ,
492
- mnemonic : string ,
493
- pythContractAddress : Address
494
- ) : Promise < PythContractFactory > {
495
- const chainId = await createPublicClient ( {
496
- transport : PythContractFactory . getTransport ( endpoint ) ,
497
- } ) . getChainId ( ) ;
498
- return new PythContractFactory (
499
- endpoint ,
500
- mnemonic ,
501
- pythContractAddress ,
502
- chainId
503
- ) ;
504
- }
505
-
506
- /**
507
- * This method creates a web3 Pyth contract with payer (based on mnemonic).
508
- *
509
- * @returns Pyth contract
510
- */
511
- createPythContract ( ) : PythContract {
512
- return getContract ( {
513
- address : this . pythContractAddress ,
514
- abi : PythAbi ,
515
- client : this . createClient ( ) ,
516
- } ) ;
517
- }
518
-
519
- hasWebsocketProvider ( ) : boolean {
520
- return isWsEndpoint ( this . endpoint ) ;
521
- }
522
-
523
- getAccount ( ) : Account {
524
- return mnemonicToAccount ( this . mnemonic ) ;
525
- }
526
-
527
- createClient ( ) : SuperWalletClient {
528
- return createWalletClient ( {
529
- transport : PythContractFactory . getTransport ( this . endpoint ) ,
530
- account : mnemonicToAccount ( this . mnemonic ) ,
531
- chain : PythContractFactory . getChain ( this . chainId ) ,
532
- } ) . extend ( publicActions ) ;
533
- }
534
-
535
- // Get the chain corresponding to the chainId. If the chain is not found, it will return
536
- // an unknown chain which should work fine in most of the cases. We might need to update
537
- // the viem package to support new chains if they don't work as expected with the unknown
538
- // chain.
539
- private static getChain ( chainId : number ) : Chain {
540
- return (
541
- Object . values ( chains ) . find ( ( chain ) => chain . id === chainId ) ||
542
- defineChain ( { id : chainId , ...UNKNOWN_CHAIN_CONFIG } )
543
- ) ;
544
- }
545
-
546
- private static getTransport ( endpoint : string ) : Transport {
547
- if ( isWsEndpoint ( endpoint ) ) {
548
- return webSocket ( endpoint ) ;
549
- } else {
550
- return http ( endpoint ) ;
551
- }
552
- }
553
- }
0 commit comments