@@ -62,10 +62,17 @@ contract AlignedLayerServiceManager is
6262 "Batch was already submitted "
6363 );
6464
65+ if (msg .value > 0 ) {
66+ batchersBalances[msg .sender ] += msg .value ;
67+ }
68+
69+ require (batchersBalances[msg .sender ] > 0 , "Batcher balance is empty " );
70+
6571 BatchState memory batchState;
6672
6773 batchState.taskCreatedBlock = uint32 (block .number );
6874 batchState.responded = false ;
75+ batchState.batcherAddress = msg .sender ;
6976
7077 batchesState[batchMerkleRoot] = batchState;
7178
@@ -77,6 +84,8 @@ contract AlignedLayerServiceManager is
7784 bytes32 batchMerkleRoot ,
7885 NonSignerStakesAndSignature memory nonSignerStakesAndSignature
7986 ) external {
87+ uint256 initialGasLeft = gasleft ();
88+
8089 /* CHECKING SIGNATURES & WHETHER THRESHOLD IS MET OR NOT */
8190
8291 // Note: This is a hacky solidity way to see that the element exists
@@ -91,6 +100,12 @@ contract AlignedLayerServiceManager is
91100 batchesState[batchMerkleRoot].responded == false ,
92101 "Batch already responded "
93102 );
103+
104+ require (
105+ batchersBalances[batchesState[batchMerkleRoot].batcherAddress] > 0 ,
106+ "Batcher has no balance "
107+ );
108+
94109 batchesState[batchMerkleRoot].responded = true ;
95110
96111 /* CHECKING SIGNATURES & WHETHER THRESHOLD IS MET OR NOT */
@@ -113,6 +128,24 @@ contract AlignedLayerServiceManager is
113128 );
114129
115130 emit BatchVerified (batchMerkleRoot);
131+
132+ // Calculate estimation of gas used, check that batcher has sufficient funds
133+ // and send transaction cost to aggregator.
134+ uint256 finalGasLeft = gasleft ();
135+
136+ // FIXME: should we add 21000 gas from the transfer + some additional for the other steps (~40k gas)?
137+ uint256 txCost = (initialGasLeft - finalGasLeft + 21000 ) * tx .gasprice ;
138+
139+ require (
140+ batchersBalances[batchesState[batchMerkleRoot].batcherAddress] >=
141+ txCost,
142+ "Batcher has not sufficient funds for paying this transaction "
143+ );
144+
145+ batchersBalances[
146+ batchesState[batchMerkleRoot].batcherAddress
147+ ] -= txCost;
148+ payable (msg .sender ).transfer (txCost);
116149 }
117150
118151 function verifyBatchInclusion (
@@ -149,4 +182,12 @@ contract AlignedLayerServiceManager is
149182 verificationDataBatchIndex
150183 );
151184 }
185+
186+ function balanceOf (address account ) public view returns (uint256 ) {
187+ return batchersBalances[account];
188+ }
189+
190+ receive () external payable {
191+ batchersBalances[msg .sender ] += msg .value ;
192+ }
152193}
0 commit comments