@@ -12,30 +12,52 @@ defmodule AlignedLayerServiceManager do
1212 nil -> raise ( "Invalid ENVIRONMENT var in .env" )
1313 end
1414
15- config_file_path = case @ aligned_config_file do
16- nil -> raise ( "ALIGNED_CONFIG_FILE not set in .env" )
17- file -> file
15+ config_file_path =
16+ case @ aligned_config_file do
17+ nil -> raise ( "ALIGNED_CONFIG_FILE not set in .env" )
18+ file -> file
19+ end
20+
21+ payment_service_path =
22+ "../contracts/script/deploy/config/#{ @ environment } /batcher-payment-service.#{ @ environment } .config.json"
23+
24+ { status_aligned_config , config_json_string } = File . read ( config_file_path )
25+ { status_payment_service , payment_service_json_string } = File . read ( payment_service_path )
26+
27+ case status_aligned_config do
28+ :ok ->
29+ Logger . debug ( "Aligned config file read successfully" )
30+
31+ :error ->
32+ raise (
33+ "Config file not read successfully, did you run make create-env? If you did,\n make sure Alignedlayer config file is correctly stored"
34+ )
1835 end
1936
20- { status , config_json_string } = File . read ( config_file_path )
37+ case status_payment_service do
38+ :ok ->
39+ Logger . debug ( "Payment service file read successfully" )
2140
22- case status do
23- :ok -> Logger . debug ( "File read successfully" )
24- :error -> raise ( "Config file not read successfully, did you run make create-env? If you did,\n make sure Alignedlayer config file is correctly stored" )
41+ :error ->
42+ raise (
43+ "Payment service file not read successfully, did you run make create-env? If you did,\n make sure Alignedlayer config file is correctly stored"
44+ )
2545 end
2646
2747 @ aligned_layer_service_manager_address Jason . decode! ( config_json_string )
2848 |> Map . get ( "addresses" )
2949 |> Map . get ( "alignedLayerServiceManager" )
3050
31- @ first_block (
32- case @ environment do
33- "devnet" -> 0
34- "holesky" -> 1728056
35- "mainnet" -> 20020000
36- _ -> raise ( "Invalid environment" )
37- end
38- )
51+ @ gas_per_proof Jason . decode! ( payment_service_json_string )
52+ |> Map . get ( "amounts" )
53+ |> Map . get ( "gasPerProof" )
54+
55+ @ first_block ( case @ environment do
56+ "devnet" -> 0
57+ "holesky" -> 1_728_056
58+ "mainnet" -> 20_020_000
59+ _ -> raise ( "Invalid environment" )
60+ end )
3961
4062 use Ethers.Contract ,
4163 abi_file: "lib/abi/AlignedLayerServiceManager.json" ,
@@ -58,12 +80,13 @@ defmodule AlignedLayerServiceManager do
5880 case events do
5981 { :ok , [ ] } -> [ ]
6082 { :ok , list } -> Enum . map ( list , & extract_new_batch_event_info / 1 )
61- { :error , reason } -> raise ( "Error fetching events: #{ Map . get ( reason , "message" ) } " )
83+ { :error , reason } -> raise ( "Error fetching events: #{ Map . get ( reason , "message" ) } " )
6284 end
6385 end
6486
6587 def extract_new_batch_event_info ( event ) do
6688 new_batch = parse_new_batch_event ( event )
89+
6790 { :ok ,
6891 % NewBatchInfo {
6992 address: event |> Map . get ( :address ) ,
@@ -97,10 +120,14 @@ defmodule AlignedLayerServiceManager do
97120 def extract_batch_response ( { _status , % NewBatchInfo { } = batch_creation } ) do
98121 created_batch = batch_creation . new_batch
99122 was_batch_responded = is_batch_responded ( created_batch . batchMerkleRoot )
100- batch_response = case was_batch_responded do
101- true -> fetch_batch_response ( created_batch . batchMerkleRoot )
102- false -> % { block_number: nil , transaction_hash: nil , block_timestamp: nil } #was not verified, fill with nils
103- end
123+
124+ batch_response =
125+ case was_batch_responded do
126+ true -> fetch_batch_response ( created_batch . batchMerkleRoot )
127+ # was not verified, fill with nils
128+ false -> % { block_number: nil , transaction_hash: nil , block_timestamp: nil }
129+ end
130+
104131 % BatchDB {
105132 merkle_root: created_batch . batchMerkleRoot ,
106133 data_pointer: created_batch . batchDataPointer ,
@@ -112,17 +139,23 @@ defmodule AlignedLayerServiceManager do
112139 response_transaction_hash: batch_response . transaction_hash ,
113140 response_timestamp: batch_response . block_timestamp ,
114141 amount_of_proofs: nil ,
115- proof_hashes: nil
142+ proof_hashes: nil ,
143+ cost_per_proof: get_cost_per_proof ( )
116144 }
117145 end
118146
119- #for existing but unverified batches
147+ # for existing but unverified batches
120148 def extract_batch_response ( % Batches { } = unverified_batch ) do
121149 was_batch_responded = is_batch_responded ( unverified_batch . merkle_root )
150+
122151 case was_batch_responded do
123- false -> nil # Do nothing since unverified batch was not yet verified
152+ # Do nothing since unverified batch was not yet verified
153+ false ->
154+ nil
155+
124156 true ->
125157 batch_response = fetch_batch_response ( unverified_batch . merkle_root )
158+
126159 % BatchDB {
127160 merkle_root: unverified_batch . merkle_root ,
128161 data_pointer: unverified_batch . data_pointer ,
@@ -134,6 +167,7 @@ defmodule AlignedLayerServiceManager do
134167 response_transaction_hash: batch_response . transaction_hash ,
135168 response_timestamp: batch_response . block_timestamp ,
136169 amount_of_proofs: unverified_batch . amount_of_proofs ,
170+ cost_per_proof: unverified_batch . cost_per_proof ,
137171 proof_hashes: nil #don't need this value to update an existing but unverified batch, it is on another table
138172 }
139173 end
@@ -150,7 +184,7 @@ defmodule AlignedLayerServiceManager do
150184 def get_batch_verified_events ( % { merkle_root: merkle_root } ) do
151185 event =
152186 AlignedLayerServiceManager.EventFilters . batch_verified ( Utils . string_to_bytes32 ( merkle_root ) )
153- |> Ethers . get_logs ( fromBlock: @ first_block )
187+ |> Ethers . get_logs ( fromBlock: @ first_block )
154188
155189 case event do
156190 { :error , reason } -> { :error , reason }
@@ -179,4 +213,18 @@ defmodule AlignedLayerServiceManager do
179213 end
180214 end
181215
216+ def get_current_gas_price ( ) do
217+ case Ethers . current_gas_price ( ) do
218+ { :ok , gas_price } ->
219+ gas_price
220+ { :error , error } -> raise ( "Error fetching gas price: #{ error } " )
221+ end
222+ end
223+
224+ def get_cost_per_proof ( ) do
225+ case Integer . parse ( @ gas_per_proof ) do
226+ { value , _ } -> value * get_current_gas_price ( )
227+ :error -> raise ( "Error parsing @gas_per_proof" )
228+ end
229+ end
182230end
0 commit comments