|
1 | | -use std::str::FromStr; |
2 | | -use std::sync::Arc; |
| 1 | +pub(crate) mod payment_service; |
| 2 | +pub(crate) mod service_manager; |
| 3 | +pub mod utils; |
3 | 4 |
|
4 | | -use aligned_sdk::eth::batcher_payment_service::BatcherPaymentServiceContract; |
5 | | -use ethers::prelude::k256::ecdsa::SigningKey; |
6 | | -use ethers::prelude::*; |
7 | | -use gas_escalator::{Frequency, GeometricGasPrice}; |
8 | | -use log::info; |
9 | | - |
10 | | -use crate::{config::ECDSAConfig, types::errors::BatcherSendError}; |
11 | | - |
12 | | -#[derive(Debug, Clone, EthEvent)] |
13 | | -pub struct BatchVerified { |
14 | | - pub batch_merkle_root: [u8; 32], |
15 | | -} |
16 | | - |
17 | | -pub type SignerMiddlewareT = |
18 | | - SignerMiddleware<GasEscalatorMiddleware<Provider<RetryClient<Http>>>, Wallet<SigningKey>>; |
19 | | - |
20 | | -pub type BatcherPaymentService = BatcherPaymentServiceContract<SignerMiddlewareT>; |
21 | | - |
22 | | -const MAX_RETRIES: u32 = 15; // Max retries for the retry client. Will only retry on network errors |
23 | | -const INITIAL_BACKOFF: u64 = 1000; // Initial backoff for the retry client in milliseconds, will increase every retry |
24 | | -const GAS_MULTIPLIER: f64 = 1.125; // Multiplier for the gas price for gas escalator |
25 | | -const GAS_ESCALATOR_INTERVAL: u64 = 12; // Time in seconds between gas escalations |
26 | | - |
27 | | -#[derive(Debug, Clone)] |
28 | | -pub struct CreateNewTaskFeeParams { |
29 | | - pub fee_for_aggregator: U256, |
30 | | - pub fee_per_proof: U256, |
31 | | - pub gas_price: U256, |
32 | | - pub respond_to_task_fee_limit: U256, |
33 | | -} |
34 | | - |
35 | | -impl CreateNewTaskFeeParams { |
36 | | - pub fn new( |
37 | | - fee_for_aggregator: U256, |
38 | | - fee_per_proof: U256, |
39 | | - gas_price: U256, |
40 | | - respond_to_task_fee_limit: U256, |
41 | | - ) -> Self { |
42 | | - CreateNewTaskFeeParams { |
43 | | - fee_for_aggregator, |
44 | | - fee_per_proof, |
45 | | - gas_price, |
46 | | - respond_to_task_fee_limit, |
47 | | - } |
48 | | - } |
49 | | -} |
50 | | - |
51 | | -pub fn get_provider(eth_rpc_url: String) -> Result<Provider<RetryClient<Http>>, anyhow::Error> { |
52 | | - let provider = Http::from_str(eth_rpc_url.as_str()) |
53 | | - .map_err(|e| anyhow::Error::msg(format!("Failed to create provider: {}", e)))?; |
54 | | - |
55 | | - let client = RetryClient::new( |
56 | | - provider, |
57 | | - Box::<ethers::providers::HttpRateLimitRetryPolicy>::default(), |
58 | | - MAX_RETRIES, |
59 | | - INITIAL_BACKOFF, |
60 | | - ); |
61 | | - |
62 | | - Ok(Provider::<RetryClient<Http>>::new(client)) |
63 | | -} |
64 | | - |
65 | | -pub async fn get_batcher_payment_service( |
66 | | - provider: Provider<RetryClient<Http>>, |
67 | | - ecdsa_config: ECDSAConfig, |
68 | | - contract_address: String, |
69 | | -) -> Result<BatcherPaymentService, anyhow::Error> { |
70 | | - let chain_id = provider.get_chainid().await?; |
71 | | - |
72 | | - let escalator = GeometricGasPrice::new(GAS_MULTIPLIER, GAS_ESCALATOR_INTERVAL, None::<u64>); |
73 | | - |
74 | | - let provider = GasEscalatorMiddleware::new(provider, escalator, Frequency::PerBlock); |
75 | | - |
76 | | - // get private key from keystore |
77 | | - let wallet = Wallet::decrypt_keystore( |
78 | | - &ecdsa_config.private_key_store_path, |
79 | | - &ecdsa_config.private_key_store_password, |
80 | | - )? |
81 | | - .with_chain_id(chain_id.as_u64()); |
82 | | - |
83 | | - let signer = Arc::new(SignerMiddleware::new(provider, wallet)); |
84 | | - |
85 | | - let service_manager = |
86 | | - BatcherPaymentService::new(H160::from_str(contract_address.as_str())?, signer); |
87 | | - |
88 | | - Ok(service_manager) |
89 | | -} |
90 | | - |
91 | | -pub async fn try_create_new_task( |
92 | | - batch_merkle_root: [u8; 32], |
93 | | - batch_data_pointer: String, |
94 | | - proofs_submitters: Vec<Address>, |
95 | | - fee_params: CreateNewTaskFeeParams, |
96 | | - payment_service: &BatcherPaymentService, |
97 | | -) -> Result<TransactionReceipt, BatcherSendError> { |
98 | | - let call = payment_service |
99 | | - .create_new_task( |
100 | | - batch_merkle_root, |
101 | | - batch_data_pointer, |
102 | | - proofs_submitters, |
103 | | - fee_params.fee_for_aggregator, |
104 | | - fee_params.fee_per_proof, |
105 | | - fee_params.respond_to_task_fee_limit, |
106 | | - ) |
107 | | - .gas_price(fee_params.gas_price); |
108 | | - |
109 | | - info!("Creating task for: {}", hex::encode(batch_merkle_root)); |
110 | | - |
111 | | - let pending_tx = call.send().await.map_err(|err| match err { |
112 | | - ContractError::Revert(err) => BatcherSendError::TransactionReverted(err.to_string()), |
113 | | - _ => BatcherSendError::UnknownError(err.to_string()), |
114 | | - })?; |
115 | | - |
116 | | - pending_tx |
117 | | - .await |
118 | | - .map_err(|err| BatcherSendError::UnknownError(err.to_string()))? |
119 | | - .ok_or(BatcherSendError::ReceiptNotFound) |
120 | | -} |
| 5 | +pub use utils::get_provider; |
0 commit comments