@@ -86,7 +86,7 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
8686 return nil , err
8787 }
8888
89- err = w .checkRespondToTaskFeeLimit (tx , txOpts , batchIdentifierHash , senderAddress )
89+ err = w .checkAggAndBatcherHaveEnoughBalance (tx , txOpts , batchIdentifierHash , senderAddress )
9090 if err != nil {
9191 return nil , err
9292 }
@@ -117,7 +117,7 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
117117 onGasPriceBumped (txOpts .GasPrice )
118118 }
119119
120- err = w .checkRespondToTaskFeeLimit (tx , txOpts , batchIdentifierHash , senderAddress )
120+ err = w .checkAggAndBatcherHaveEnoughBalance (tx , txOpts , batchIdentifierHash , senderAddress )
121121 if err != nil {
122122 return nil , retry.PermanentError {Inner : err }
123123 }
@@ -131,6 +131,7 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
131131
132132 receipt , err := utils .WaitForTransactionReceiptRetryable (w .Client , w .ClientFallback , tx .Hash (), timeToWaitBeforeBump )
133133 if receipt != nil {
134+ w .checkIfAggregatorHadToPaidForBatcher (receipt , txOpts , batchIdentifierHash )
134135 return receipt , nil
135136 }
136137
@@ -148,33 +149,44 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
148149 return retry .RetryWithData (respondToTaskV2Func , retry .MinDelay , retry .RetryFactor , 0 , retry .MaxInterval , 0 )
149150}
150151
151- func (w * AvsWriter ) checkRespondToTaskFeeLimit (tx * types.Transaction , txOpts bind.TransactOpts , batchIdentifierHash [32 ]byte , senderAddress [20 ]byte ) error {
152- aggregatorAddress := txOpts .From
153- simulatedCost := new (big.Int ).Mul (new (big.Int ).SetUint64 (tx .Gas ()), tx .GasPrice ())
154- w .logger .Info ("Simulated cost" , "cost" , simulatedCost )
155-
156- // Get RespondToTaskFeeLimit
152+ // Calculates the transaction cost from the receipt and compares it with the batcher respondToTaskFeeLimit
153+ // if the tx cost was higher, then it means the aggregator has paid the difference for the batcher (txCost - respondToTaskFeeLimit) and so metrics are updated accordingly.
154+ // otherwise nothing is done.
155+ func (w * AvsWriter ) checkIfAggregatorHadToPaidForBatcher (receipt * types.Receipt , txOpts bind.TransactOpts , batchIdentifierHash [32 ]byte ) {
157156 batchState , err := w .BatchesStateRetryable (& bind.CallOpts {}, batchIdentifierHash )
158157 if err != nil {
159- // Fallback also failed
160- // Proceed to check values against simulated costs
161- w .logger .Error ("Failed to get batch state" , "error" , err )
162- w .logger .Info ("Proceeding with simulated cost checks" )
163- return w .compareBalances (simulatedCost , aggregatorAddress , senderAddress )
158+ return
164159 }
165- // At this point, batchState was successfully retrieved
166- // Proceed to check values against RespondToTaskFeeLimit
167160 respondToTaskFeeLimit := batchState .RespondToTaskFeeLimit
168- w .logger .Info ("Batch RespondToTaskFeeLimit" , "RespondToTaskFeeLimit" , respondToTaskFeeLimit )
169161
170- if respondToTaskFeeLimit .Cmp (simulatedCost ) < 0 {
171- aggregatorDifferenceToPay := new (big.Int ).Sub (simulatedCost , respondToTaskFeeLimit )
172- aggregatorDifferenceToPayInEth := utils .WeiToEth (aggregatorDifferenceToPay )
173- w .metrics .AddAggregatorGasPaidForBatcher (aggregatorDifferenceToPayInEth )
162+ txCost := new (big.Int ).Mul (big .NewInt (int64 (receipt .GasUsed )), txOpts .GasPrice )
163+ // todo: 70_000 should come from the config or at least be a constant
164+ txCost = txCost .Add (txCost , big .NewInt (70_0000 ))
165+
166+ if respondToTaskFeeLimit .Cmp (txCost ) > 0 {
167+ aggregatorDifferencePaid := new (big.Int ).Sub (txCost , respondToTaskFeeLimit )
168+ aggregatorDifferencePaidInEth := utils .WeiToEth (aggregatorDifferencePaid )
169+ w .metrics .AddAggregatorGasPaidForBatcher (aggregatorDifferencePaidInEth )
174170 w .metrics .IncAggregatorPaidForBatcher ()
175- w .logger .Warnf ("cost of transaction is higher than Batch.RespondToTaskFeeLimit, aggregator will pay the for the difference, aprox: %vethers" , aggregatorDifferenceToPayInEth )
171+ w .logger .Warnf ("cost of transaction was higher than Batch.RespondToTaskFeeLimit, aggregator has paid the for the difference, aprox: %vethers" , aggregatorDifferencePaidInEth )
172+ return
176173 }
174+ }
175+
176+ func (w * AvsWriter ) checkAggAndBatcherHaveEnoughBalance (tx * types.Transaction , txOpts bind.TransactOpts , batchIdentifierHash [32 ]byte , senderAddress [20 ]byte ) error {
177+ w .logger .Info ("Checking if aggregator and batcher have enough balance for the transaction" )
178+ aggregatorAddress := txOpts .From
179+ txCost := new (big.Int ).Mul (new (big.Int ).SetUint64 (tx .Gas ()), txOpts .GasPrice )
180+ w .logger .Info ("Transaction cost" , "cost" , txCost )
177181
182+ batchState , err := w .BatchesStateRetryable (& bind.CallOpts {}, batchIdentifierHash )
183+ if err != nil {
184+ w .logger .Error ("Failed to get batch state" , "error" , err )
185+ w .logger .Info ("Proceeding to check balances against transaction cost" )
186+ return w .compareBalances (txCost , aggregatorAddress , senderAddress )
187+ }
188+ respondToTaskFeeLimit := batchState .RespondToTaskFeeLimit
189+ w .logger .Info ("Checking balance against Batch RespondToTaskFeeLimit" , "RespondToTaskFeeLimit" , respondToTaskFeeLimit )
178190 return w .compareBalances (respondToTaskFeeLimit , aggregatorAddress , senderAddress )
179191}
180192
0 commit comments