1
1
use {
2
2
crate :: {
3
+ api:: ChainId ,
3
4
chain:: {
4
5
eth_gas_oracle:: EthProviderOracle ,
5
6
reader:: {
9
10
EntropyReader ,
10
11
RequestedWithCallbackEvent ,
11
12
} ,
13
+ traced_client:: {
14
+ RpcMetrics ,
15
+ TracedClient ,
16
+ } ,
12
17
} ,
13
18
config:: EthereumConfig ,
14
19
} ,
22
27
abi:: RawLog ,
23
28
contract:: {
24
29
abigen,
25
- ContractError ,
26
30
EthLogDecode ,
27
31
} ,
28
32
core:: types:: Address ,
34
38
} ,
35
39
prelude:: {
36
40
BlockId ,
41
+ JsonRpcClient ,
37
42
PendingTransaction ,
38
43
TransactionRequest ,
39
44
} ,
@@ -67,15 +72,19 @@ abigen!(
67
72
"../../target_chains/ethereum/entropy_sdk/solidity/abis/IEntropy.json"
68
73
) ;
69
74
70
- pub type SignablePythContract = PythRandom <
75
+ pub type SignablePythContractInner < T > = PythRandom <
71
76
LegacyTxMiddleware <
72
77
GasOracleMiddleware <
73
- NonceManagerMiddleware < SignerMiddleware < Provider < Http > , LocalWallet > > ,
74
- EthProviderOracle < Provider < Http > > ,
78
+ NonceManagerMiddleware < SignerMiddleware < Provider < T > , LocalWallet > > ,
79
+ EthProviderOracle < Provider < T > > ,
75
80
> ,
76
81
> ,
77
82
> ;
83
+ pub type SignablePythContract = SignablePythContractInner < Http > ;
84
+ pub type InstrumentedSignablePythContract = SignablePythContractInner < TracedClient > ;
85
+
78
86
pub type PythContract = PythRandom < Provider < Http > > ;
87
+ pub type InstrumentedPythContract = PythRandom < Provider < TracedClient > > ;
79
88
80
89
/// Middleware that converts a transaction into a legacy transaction if use_legacy_tx is true.
81
90
/// We can not use TransformerMiddleware because keeper calls fill_transaction first which bypasses
@@ -157,32 +166,7 @@ impl<M: Middleware> Middleware for LegacyTxMiddleware<M> {
157
166
}
158
167
}
159
168
160
- impl SignablePythContract {
161
- pub async fn from_config (
162
- chain_config : & EthereumConfig ,
163
- private_key : & str ,
164
- ) -> Result < SignablePythContract > {
165
- let provider = Provider :: < Http > :: try_from ( & chain_config. geth_rpc_addr ) ?;
166
- let chain_id = provider. get_chainid ( ) . await ?;
167
- let gas_oracle = EthProviderOracle :: new ( provider. clone ( ) ) ;
168
- let wallet__ = private_key
169
- . parse :: < LocalWallet > ( ) ?
170
- . with_chain_id ( chain_id. as_u64 ( ) ) ;
171
-
172
- let address = wallet__. address ( ) ;
173
-
174
- Ok ( PythRandom :: new (
175
- chain_config. contract_addr ,
176
- Arc :: new ( LegacyTxMiddleware :: new (
177
- chain_config. legacy_tx ,
178
- GasOracleMiddleware :: new (
179
- NonceManagerMiddleware :: new ( SignerMiddleware :: new ( provider, wallet__) , address) ,
180
- gas_oracle,
181
- ) ,
182
- ) ) ,
183
- ) )
184
- }
185
-
169
+ impl < T : JsonRpcClient + ' static + Clone > SignablePythContractInner < T > {
186
170
/// Submit a request for a random number to the contract.
187
171
///
188
172
/// This method is a version of the autogenned `request` method that parses the emitted logs
@@ -249,10 +233,54 @@ impl SignablePythContract {
249
233
Err ( anyhow ! ( "Request failed" ) . into ( ) )
250
234
}
251
235
}
236
+
237
+ pub async fn from_config_and_provider (
238
+ chain_config : & EthereumConfig ,
239
+ private_key : & str ,
240
+ provider : Provider < T > ,
241
+ ) -> Result < SignablePythContractInner < T > > {
242
+ let chain_id = provider. get_chainid ( ) . await ?;
243
+ let gas_oracle = EthProviderOracle :: new ( provider. clone ( ) ) ;
244
+ let wallet__ = private_key
245
+ . parse :: < LocalWallet > ( ) ?
246
+ . with_chain_id ( chain_id. as_u64 ( ) ) ;
247
+
248
+ let address = wallet__. address ( ) ;
249
+
250
+ Ok ( PythRandom :: new (
251
+ chain_config. contract_addr ,
252
+ Arc :: new ( LegacyTxMiddleware :: new (
253
+ chain_config. legacy_tx ,
254
+ GasOracleMiddleware :: new (
255
+ NonceManagerMiddleware :: new ( SignerMiddleware :: new ( provider, wallet__) , address) ,
256
+ gas_oracle,
257
+ ) ,
258
+ ) ) ,
259
+ ) )
260
+ }
261
+ }
262
+
263
+ impl SignablePythContract {
264
+ pub async fn from_config ( chain_config : & EthereumConfig , private_key : & str ) -> Result < Self > {
265
+ let provider = Provider :: < Http > :: try_from ( & chain_config. geth_rpc_addr ) ?;
266
+ Self :: from_config_and_provider ( chain_config, private_key, provider) . await
267
+ }
268
+ }
269
+
270
+ impl InstrumentedSignablePythContract {
271
+ pub async fn from_config (
272
+ chain_config : & EthereumConfig ,
273
+ private_key : & str ,
274
+ chain_id : ChainId ,
275
+ metrics : Arc < RpcMetrics > ,
276
+ ) -> Result < Self > {
277
+ let provider = TracedClient :: new ( chain_id, & chain_config. geth_rpc_addr , metrics) ?;
278
+ Self :: from_config_and_provider ( chain_config, private_key, provider) . await
279
+ }
252
280
}
253
281
254
282
impl PythContract {
255
- pub fn from_config ( chain_config : & EthereumConfig ) -> Result < PythContract > {
283
+ pub fn from_config ( chain_config : & EthereumConfig ) -> Result < Self > {
256
284
let provider = Provider :: < Http > :: try_from ( & chain_config. geth_rpc_addr ) ?;
257
285
258
286
Ok ( PythRandom :: new (
@@ -262,8 +290,23 @@ impl PythContract {
262
290
}
263
291
}
264
292
293
+ impl InstrumentedPythContract {
294
+ pub fn from_config (
295
+ chain_config : & EthereumConfig ,
296
+ chain_id : ChainId ,
297
+ metrics : Arc < RpcMetrics > ,
298
+ ) -> Result < Self > {
299
+ let provider = TracedClient :: new ( chain_id, & chain_config. geth_rpc_addr , metrics) ?;
300
+
301
+ Ok ( PythRandom :: new (
302
+ chain_config. contract_addr ,
303
+ Arc :: new ( provider) ,
304
+ ) )
305
+ }
306
+ }
307
+
265
308
#[ async_trait]
266
- impl EntropyReader for PythContract {
309
+ impl < T : JsonRpcClient + ' static > EntropyReader for PythRandom < Provider < T > > {
267
310
async fn get_request (
268
311
& self ,
269
312
provider_address : Address ,
@@ -330,7 +373,7 @@ impl EntropyReader for PythContract {
330
373
user_random_number : [ u8 ; 32 ] ,
331
374
provider_revelation : [ u8 ; 32 ] ,
332
375
) -> Result < U256 > {
333
- let result: Result < U256 , ContractError < Provider < Http > > > = self
376
+ let result = self
334
377
. reveal_with_callback (
335
378
provider,
336
379
sequence_number,
0 commit comments