Skip to content

Commit 720c225

Browse files
author
wangdong
committed
Revert "[Loopring 3.6] skip fee distribution in submitBlocks (#1622)"
This reverts commit 6d5ef9d.
1 parent 6d5ef9d commit 720c225

File tree

12 files changed

+131
-154
lines changed

12 files changed

+131
-154
lines changed

packages/loopring_v3/contracts/core/iface/ExchangeData.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ library ExchangeData
9292
struct ForcedWithdrawal
9393
{
9494
address owner;
95-
uint64 timestamp;
95+
uint32 timestamp;
96+
uint64 fee;
9697
}
9798

9899
struct Constants

packages/loopring_v3/contracts/core/iface/IExchangeV3.sol

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ abstract contract IExchangeV3 is IExchange
4343
event BlockSubmitted(
4444
uint indexed blockIdx,
4545
bytes32 merkleRoot,
46-
bytes32 publicDataHash
46+
bytes32 publicDataHash,
47+
uint blockFee
4748
);
4849

4950
event DepositRequested(
@@ -155,16 +156,6 @@ abstract contract IExchangeV3 is IExchange
155156
view
156157
returns (IDepositContract);
157158

158-
// @dev Exchange owner withdraws fees from the exchange.
159-
// @param token Fee token address
160-
// @param feeRecipient Fee recipient address
161-
function withdrawExchangeFees(
162-
address token,
163-
address feeRecipient
164-
)
165-
external
166-
virtual;
167-
168159
// -- Constants --
169160
/// @dev Returns a list of constants used by the exchange.
170161
/// @return constants The list of constants.
@@ -339,7 +330,11 @@ abstract contract IExchangeV3 is IExchange
339330
/// - data: The data for this block
340331
/// - offchainData: Arbitrary data, mainly for off-chain data-availability, i.e.,
341332
/// the multihash of the IPFS file that contains the block data.
342-
function submitBlocks(ExchangeData.Block[] calldata blocks)
333+
/// @param feeRecipient The address that will receive the onchain block rewards
334+
function submitBlocks(
335+
ExchangeData.Block[] calldata blocks,
336+
address payable feeRecipient
337+
)
343338
external
344339
virtual;
345340

packages/loopring_v3/contracts/core/impl/ExchangeV3.sol

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
pragma solidity ^0.7.0;
44
pragma experimental ABIEncoderV2;
55

6-
import "../../lib/AddressUtil.sol";
76
import "../../lib/EIP712.sol";
8-
import "../../lib/ERC20SafeTransfer.sol";
97
import "../../lib/MathUint.sol";
108
import "../iface/IAgentRegistry.sol";
119
import "../iface/IExchangeV3.sol";
@@ -27,8 +25,6 @@ import "./libtransactions/TransferTransaction.sol";
2725
/// @author Daniel Wang - <[email protected]>
2826
contract ExchangeV3 is IExchangeV3
2927
{
30-
using AddressUtil for address;
31-
using ERC20SafeTransfer for address;
3228
using MathUint for uint;
3329
using ExchangeAdmins for ExchangeData.State;
3430
using ExchangeBalances for ExchangeData.State;
@@ -134,26 +130,6 @@ contract ExchangeV3 is IExchangeV3
134130
return state.depositContract;
135131
}
136132

137-
function withdrawExchangeFees(
138-
address token,
139-
address recipient
140-
)
141-
external
142-
override
143-
nonReentrant
144-
onlyOwner
145-
{
146-
require(recipient != address(0), "INVALID_ADDRESS");
147-
148-
if (token == address(0)) {
149-
uint amount = address(this).balance;
150-
recipient.sendETHAndVerify(amount, gasleft());
151-
} else {
152-
uint amount = ERC20(token).balanceOf(address(this));
153-
token.safeTransferAndVerify(recipient, amount);
154-
}
155-
}
156-
157133
// -- Constants --
158134
function getConstants()
159135
external
@@ -334,13 +310,19 @@ contract ExchangeV3 is IExchangeV3
334310
return state.blocks[blockIdx];
335311
}
336312

337-
function submitBlocks(ExchangeData.Block[] calldata blocks)
313+
function submitBlocks(
314+
ExchangeData.Block[] calldata blocks,
315+
address payable feeRecipient
316+
)
338317
external
339318
override
340319
nonReentrant
341320
onlyOwner
342321
{
343-
state.submitBlocks(blocks);
322+
state.submitBlocks(
323+
blocks,
324+
feeRecipient
325+
);
344326
}
345327

346328
function getNumAvailableForcedSlots()

packages/loopring_v3/contracts/core/impl/libexchange/ExchangeAdmins.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import "./ExchangeMode.sol";
1515
/// @author Brecht Devos - <[email protected]>
1616
library ExchangeAdmins
1717
{
18+
using MathUint for uint;
1819
using ERC20SafeTransfer for address;
1920
using ExchangeMode for ExchangeData.State;
20-
using MathUint for uint;
2121

2222
event OperatorChanged(
2323
uint indexed exchangeId,

packages/loopring_v3/contracts/core/impl/libexchange/ExchangeBlocks.sol

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ library ExchangeBlocks
3232
event BlockSubmitted(
3333
uint indexed blockIdx,
3434
bytes32 merkleRoot,
35-
bytes32 publicDataHash
35+
bytes32 publicDataHash,
36+
uint blockFee
3637
);
3738

3839
event ProtocolFeesUpdated(
@@ -88,7 +89,8 @@ library ExchangeBlocks
8889

8990
function submitBlocks(
9091
ExchangeData.State storage S,
91-
ExchangeData.Block[] memory blocks
92+
ExchangeData.Block[] memory blocks,
93+
address payable feeRecipient
9294
)
9395
public
9496
{
@@ -103,18 +105,28 @@ library ExchangeBlocks
103105
// Hash all the public data to a single value which is used as the input for the circuit
104106
publicDataHashes[i] = blocks[i].data.fastSHA256();
105107
// Commit the block
106-
commitBlock(S, blocks[i], publicDataHashes[i]);
108+
commitBlock(
109+
S,
110+
blocks[i],
111+
feeRecipient,
112+
publicDataHashes[i]
113+
);
107114
}
108115

109116
// Verify the blocks - blocks are verified in a batch to save gas.
110-
verifyBlocks(S, blocks, publicDataHashes);
117+
verifyBlocks(
118+
S,
119+
blocks,
120+
publicDataHashes
121+
);
111122
}
112123

113124
// == Internal Functions ==
114125

115126
function commitBlock(
116127
ExchangeData.State storage S,
117128
ExchangeData.Block memory _block,
129+
address payable _feeRecipient,
118130
bytes32 _publicDataHash
119131
)
120132
private
@@ -156,17 +168,20 @@ library ExchangeBlocks
156168
);
157169

158170
// Process conditional transactions
159-
processConditionalTransactions(
171+
uint blockFeeETH = processConditionalTransactions(
160172
S,
161173
offset,
162174
_block.data,
163175
_block.auxiliaryData,
164176
inputTimestamp
165177
);
166178

179+
// Transfer the onchain block fee to the operator
180+
_feeRecipient.sendETHAndVerify(blockFeeETH, gasleft());
181+
167182
// Emit an event
168183
uint numBlocks = S.numBlocks;
169-
emit BlockSubmitted(numBlocks, merkleRootAfter, _publicDataHash);
184+
emit BlockSubmitted(numBlocks, merkleRootAfter, _publicDataHash, blockFeeETH);
170185

171186
S.merkleRoot = merkleRootAfter;
172187

@@ -255,6 +270,7 @@ library ExchangeBlocks
255270
uint32 timestamp
256271
)
257272
private
273+
returns (uint blockFeeETH)
258274
{
259275
// The length of the auxiliary data needs to match the number of conditional transactions
260276
uint numConditionalTransactions = data.toUint32(offset);
@@ -290,32 +306,33 @@ library ExchangeBlocks
290306
);
291307
txDataOffset += 1;
292308

309+
uint txFeeETH = 0;
293310
if (txType == ExchangeData.TransactionType.DEPOSIT) {
294-
DepositTransaction.process(
311+
txFeeETH = DepositTransaction.process(
295312
S,
296313
ctx,
297314
data,
298315
txDataOffset,
299316
txAuxiliaryData[i].data
300317
);
301318
} else if (txType == ExchangeData.TransactionType.WITHDRAWAL) {
302-
WithdrawTransaction.process(
319+
txFeeETH = WithdrawTransaction.process(
303320
S,
304321
ctx,
305322
data,
306323
txDataOffset,
307324
txAuxiliaryData[i].data
308325
);
309326
} else if (txType == ExchangeData.TransactionType.TRANSFER) {
310-
TransferTransaction.process(
327+
txFeeETH = TransferTransaction.process(
311328
S,
312329
ctx,
313330
data,
314331
txDataOffset,
315332
txAuxiliaryData[i].data
316333
);
317334
} else if (txType == ExchangeData.TransactionType.ACCOUNT_UPDATE) {
318-
AccountUpdateTransaction.process(
335+
txFeeETH = AccountUpdateTransaction.process(
319336
S,
320337
ctx,
321338
data,
@@ -329,6 +346,7 @@ library ExchangeBlocks
329346
revert("UNSUPPORTED_TX_TYPE");
330347
}
331348

349+
blockFeeETH = blockFeeETH.add(txFeeETH);
332350
prevTxDataOffset = txDataOffset;
333351
}
334352
}

packages/loopring_v3/contracts/core/impl/libexchange/ExchangeWithdrawals.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ library ExchangeWithdrawals
7676

7777
S.pendingForcedWithdrawals[accountID][tokenID] = ExchangeData.ForcedWithdrawal({
7878
owner: owner,
79-
timestamp: uint64(block.timestamp)
79+
timestamp: uint32(block.timestamp),
80+
fee: uint64(withdrawalFeeETH)
8081
});
8182

8283
S.numPendingForcedTransactions++;

packages/loopring_v3/contracts/core/impl/libtransactions/AccountUpdateTransaction.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ library AccountUpdateTransaction
5353
bytes memory auxiliaryData
5454
)
5555
internal
56+
returns (uint /*feeETH*/)
5657
{
5758
// Read the account update
5859
AccountUpdate memory accountUpdate = readTx(data, offset);

packages/loopring_v3/contracts/core/impl/libtransactions/DepositTransaction.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ library DepositTransaction
3939
bytes memory /*auxiliaryData*/
4040
)
4141
internal
42+
returns (uint)
4243
{
4344
// Read in the deposit
4445
Deposit memory deposit = readTx(data, offset);

packages/loopring_v3/contracts/core/impl/libtransactions/TransferTransaction.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ library TransferTransaction
5858
bytes memory auxiliaryData
5959
)
6060
internal
61+
returns (uint /*feeETH*/)
6162
{
6263
// Read the transfer
6364
Transfer memory transfer = readTx(data, offset);

packages/loopring_v3/contracts/core/impl/libtransactions/WithdrawTransaction.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ library WithdrawTransaction
7979
bytes memory auxiliaryData
8080
)
8181
internal
82+
returns (uint feeETH)
8283
{
8384
Withdrawal memory withdrawal = readTx(data, offset);
8485
WithdrawalAuxiliaryData memory auxData = abi.decode(auxiliaryData, (WithdrawalAuxiliaryData));
@@ -133,6 +134,9 @@ library WithdrawTransaction
133134
require(withdrawal.amount == 0, "UNAUTHORIZED_WITHDRAWAL");
134135
}
135136

137+
// Get the fee
138+
feeETH = forcedWithdrawal.fee;
139+
136140
// delete the withdrawal request and free a slot
137141
delete S.pendingForcedWithdrawals[withdrawal.accountID][withdrawal.tokenID];
138142
S.numPendingForcedTransactions--;

0 commit comments

Comments
 (0)