Skip to content

Commit 96cc5f2

Browse files
authored
[Loopring 3.6] skip fee distribution in submitBlocks (#1623)
1 parent 9980c25 commit 96cc5f2

16 files changed

+191
-193
lines changed

packages/loopring_v3.js/src/exchange_v3.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ export class ExchangeV3 {
104104
const exchangeCreationTimestamp = (await this.exchange.methods
105105
.getBlockInfo(0)
106106
.call()).timestamp;
107-
const genesisMerkleRoot = new BN((await this.exchange.methods.
108-
getMerkleRoot()
109-
.call()).slice(2), 16).toString(10);
107+
const genesisMerkleRoot = new BN(
108+
(await this.exchange.methods.getMerkleRoot().call()).slice(2),
109+
16
110+
).toString(10);
110111

111112
this.shutdown = false;
112113
this.shutdownStartTime = 0;
@@ -229,10 +230,7 @@ export class ExchangeV3 {
229230
Constants.BINARY_TREE_DEPTH_TOKENS / 2
230231
);
231232
balancesMerkleTree.newTree(
232-
hasher([
233-
0,
234-
storageMerkleTree.getRoot()
235-
]).toString(10)
233+
hasher([0, storageMerkleTree.getRoot()]).toString(10)
236234
);
237235
this.merkleTree = new SparseMerkleTree(
238236
Constants.BINARY_TREE_DEPTH_ACCOUNTS / 2
@@ -247,10 +245,7 @@ export class ExchangeV3 {
247245
Constants.BINARY_TREE_DEPTH_TOKENS / 2
248246
);
249247
account.balancesMerkleTree.newTree(
250-
hasher([
251-
0,
252-
storageMerkleTree.getRoot()
253-
]).toString(10)
248+
hasher([0, storageMerkleTree.getRoot()]).toString(10)
254249
);
255250
for (const tokenID of Object.keys(account.balances)) {
256251
const balanceValue = account.balances[Number(tokenID)];
@@ -262,10 +257,7 @@ export class ExchangeV3 {
262257
const storageValue = balanceValue.storage[Number(orderID)];
263258
balanceValue.storageTree.update(
264259
Number(orderID),
265-
hasher([
266-
storageValue.data,
267-
storageValue.storageID
268-
]).toString(10)
260+
hasher([storageValue.data, storageValue.storageID]).toString(10)
269261
);
270262
}
271263
account.balancesMerkleTree.update(
@@ -677,8 +669,7 @@ export class ExchangeV3 {
677669
const timestamp = Number(ethereumBlock.timestamp);
678670

679671
// Get the block data from the transaction data
680-
const submitBlocksFunctionSignature = "0xde6fd7d0";
681-
//const submitBlocksFunctionSignature = "0x14867212";
672+
const submitBlocksFunctionSignature = "0x8dadd3af";
682673

683674
const transaction = await this.web3.eth.getTransaction(
684675
event.transactionHash

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

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

9998
struct Constants

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

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

5049
event DepositRequested(
@@ -156,6 +155,16 @@ abstract contract IExchangeV3 is IExchange
156155
view
157156
returns (IDepositContract);
158157

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+
159168
// -- Constants --
160169
/// @dev Returns a list of constants used by the exchange.
161170
/// @return constants The list of constants.
@@ -330,11 +339,7 @@ abstract contract IExchangeV3 is IExchange
330339
/// - data: The data for this block
331340
/// - offchainData: Arbitrary data, mainly for off-chain data-availability, i.e.,
332341
/// the multihash of the IPFS file that contains the block data.
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-
)
342+
function submitBlocks(ExchangeData.Block[] calldata blocks)
338343
external
339344
virtual;
340345

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

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

6+
import "../../lib/AddressUtil.sol";
67
import "../../lib/EIP712.sol";
8+
import "../../lib/ERC20SafeTransfer.sol";
79
import "../../lib/MathUint.sol";
810
import "../iface/IAgentRegistry.sol";
911
import "../iface/IExchangeV3.sol";
@@ -25,6 +27,8 @@ import "./libtransactions/TransferTransaction.sol";
2527
/// @author Daniel Wang - <[email protected]>
2628
contract ExchangeV3 is IExchangeV3
2729
{
30+
using AddressUtil for address;
31+
using ERC20SafeTransfer for address;
2832
using MathUint for uint;
2933
using ExchangeAdmins for ExchangeData.State;
3034
using ExchangeBalances for ExchangeData.State;
@@ -130,6 +134,26 @@ contract ExchangeV3 is IExchangeV3
130134
return state.depositContract;
131135
}
132136

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+
133157
// -- Constants --
134158
function getConstants()
135159
external
@@ -310,19 +334,13 @@ contract ExchangeV3 is IExchangeV3
310334
return state.blocks[blockIdx];
311335
}
312336

313-
function submitBlocks(
314-
ExchangeData.Block[] calldata blocks,
315-
address payable feeRecipient
316-
)
337+
function submitBlocks(ExchangeData.Block[] calldata blocks)
317338
external
318339
override
319340
nonReentrant
320341
onlyOwner
321342
{
322-
state.submitBlocks(
323-
blocks,
324-
feeRecipient
325-
);
343+
state.submitBlocks(blocks);
326344
}
327345

328346
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;
1918
using ERC20SafeTransfer for address;
2019
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: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ library ExchangeBlocks
3232
event BlockSubmitted(
3333
uint indexed blockIdx,
3434
bytes32 merkleRoot,
35-
bytes32 publicDataHash,
36-
uint blockFee
35+
bytes32 publicDataHash
3736
);
3837

3938
event ProtocolFeesUpdated(
@@ -89,8 +88,7 @@ library ExchangeBlocks
8988

9089
function submitBlocks(
9190
ExchangeData.State storage S,
92-
ExchangeData.Block[] memory blocks,
93-
address payable feeRecipient
91+
ExchangeData.Block[] memory blocks
9492
)
9593
public
9694
{
@@ -105,28 +103,18 @@ library ExchangeBlocks
105103
// Hash all the public data to a single value which is used as the input for the circuit
106104
publicDataHashes[i] = blocks[i].data.fastSHA256();
107105
// Commit the block
108-
commitBlock(
109-
S,
110-
blocks[i],
111-
feeRecipient,
112-
publicDataHashes[i]
113-
);
106+
commitBlock(S, blocks[i], publicDataHashes[i]);
114107
}
115108

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

124113
// == Internal Functions ==
125114

126115
function commitBlock(
127116
ExchangeData.State storage S,
128117
ExchangeData.Block memory _block,
129-
address payable _feeRecipient,
130118
bytes32 _publicDataHash
131119
)
132120
private
@@ -168,20 +156,17 @@ library ExchangeBlocks
168156
);
169157

170158
// Process conditional transactions
171-
uint blockFeeETH = processConditionalTransactions(
159+
processConditionalTransactions(
172160
S,
173161
offset,
174162
_block.data,
175163
_block.auxiliaryData,
176164
inputTimestamp
177165
);
178166

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

186171
S.merkleRoot = merkleRootAfter;
187172

@@ -270,7 +255,6 @@ library ExchangeBlocks
270255
uint32 timestamp
271256
)
272257
private
273-
returns (uint blockFeeETH)
274258
{
275259
// The length of the auxiliary data needs to match the number of conditional transactions
276260
uint numConditionalTransactions = data.toUint32(offset);
@@ -306,33 +290,32 @@ library ExchangeBlocks
306290
);
307291
txDataOffset += 1;
308292

309-
uint txFeeETH = 0;
310293
if (txType == ExchangeData.TransactionType.DEPOSIT) {
311-
txFeeETH = DepositTransaction.process(
294+
DepositTransaction.process(
312295
S,
313296
ctx,
314297
data,
315298
txDataOffset,
316299
txAuxiliaryData[i].data
317300
);
318301
} else if (txType == ExchangeData.TransactionType.WITHDRAWAL) {
319-
txFeeETH = WithdrawTransaction.process(
302+
WithdrawTransaction.process(
320303
S,
321304
ctx,
322305
data,
323306
txDataOffset,
324307
txAuxiliaryData[i].data
325308
);
326309
} else if (txType == ExchangeData.TransactionType.TRANSFER) {
327-
txFeeETH = TransferTransaction.process(
310+
TransferTransaction.process(
328311
S,
329312
ctx,
330313
data,
331314
txDataOffset,
332315
txAuxiliaryData[i].data
333316
);
334317
} else if (txType == ExchangeData.TransactionType.ACCOUNT_UPDATE) {
335-
txFeeETH = AccountUpdateTransaction.process(
318+
AccountUpdateTransaction.process(
336319
S,
337320
ctx,
338321
data,
@@ -346,7 +329,6 @@ library ExchangeBlocks
346329
revert("UNSUPPORTED_TX_TYPE");
347330
}
348331

349-
blockFeeETH = blockFeeETH.add(txFeeETH);
350332
prevTxDataOffset = txDataOffset;
351333
}
352334
}

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

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

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

8382
S.numPendingForcedTransactions++;

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

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

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

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

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

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

0 commit comments

Comments
 (0)