Skip to content

Commit 692dea3

Browse files
authored
[Loopring 3.6] remove deposit fee (#1611)
1 parent ea59d99 commit 692dea3

14 files changed

+272
-277
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ library ExchangeData
8383
struct Deposit
8484
{
8585
uint96 amount;
86-
uint32 timestamp; // only valid before 2105 (85 years to go)
87-
uint64 fee;
86+
uint64 timestamp;
8887
}
8988

9089
// A forced withdrawal request.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ abstract contract IExchangeV3 is IExchange
5050
event DepositRequested(
5151
address owner,
5252
address token,
53-
uint96 amount,
54-
uint fee
53+
uint96 amount
5554
);
5655

5756
event ForcedWithdrawalRequested(

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ contract DefaultDepositContract is IDepositContract, Claimable
9494
ifNotZero(amount)
9595
returns (uint96 amountReceived)
9696
{
97+
uint ethToReturn = 0;
98+
9799
if (isETHInternal(token)) {
98-
require(msg.value == amount, "INVALID_ETH_DEPOSIT");
100+
require(msg.value >= amount, "INVALID_ETH_DEPOSIT");
99101
amountReceived = amount;
102+
ethToReturn = msg.value - amount;
100103
} else {
101-
require(msg.value == 0, "INVALID_TOKEN_DEPOSIT");
102-
103104
bool checkBalance = needCheckBalance[token];
104105
uint balanceBefore = checkBalance ? ERC20(token).balanceOf(address(this)) : 0;
105106

@@ -109,6 +110,12 @@ contract DefaultDepositContract is IDepositContract, Claimable
109110
uint diff = balanceAfter.sub(balanceBefore);
110111
amountReceived = uint96(diff);
111112
require(uint(amountReceived) == diff, "OUT_OF_RANGE");
113+
114+
ethToReturn = msg.value;
115+
}
116+
117+
if (ethToReturn > 0) {
118+
from.sendETHAndVerify(ethToReturn, gasleft());
112119
}
113120
}
114121

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

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ library ExchangeDeposits
2424
event DepositRequested(
2525
address owner,
2626
address token,
27-
uint96 amount,
28-
uint fee
27+
uint96 amount
2928
);
3029

3130
function deposit(
@@ -47,8 +46,7 @@ library ExchangeDeposits
4746
uint16 tokenID = S.getTokenID(tokenAddress);
4847

4948
// Transfer the tokens to this contract
50-
(uint96 amountDeposited, uint64 fee) = transferDeposit(
51-
S,
49+
uint96 amountDeposited = S.depositContract.deposit{value: msg.value}(
5250
from,
5351
tokenAddress,
5452
amount,
@@ -57,47 +55,14 @@ library ExchangeDeposits
5755

5856
// Add the amount to the deposit request and reset the time the operator has to process it
5957
ExchangeData.Deposit memory _deposit = S.pendingDeposits[to][tokenID];
60-
_deposit.timestamp = uint32(block.timestamp);
58+
_deposit.timestamp = uint64(block.timestamp);
6159
_deposit.amount = _deposit.amount.add96(amountDeposited);
62-
_deposit.fee = _deposit.fee.add64(fee);
6360
S.pendingDeposits[to][tokenID] = _deposit;
6461

6562
emit DepositRequested(
6663
to,
6764
tokenAddress,
68-
uint96(amountDeposited),
69-
fee
70-
);
71-
}
72-
73-
function transferDeposit(
74-
ExchangeData.State storage S,
75-
address from,
76-
address tokenAddress,
77-
uint96 amount,
78-
bytes memory extraData
79-
)
80-
private
81-
returns (
82-
uint96 amountDeposited,
83-
uint64 fee
84-
)
85-
{
86-
IDepositContract depositContract = S.depositContract;
87-
uint depositValueETH = 0;
88-
if (msg.value > 0 && (tokenAddress == address(0) || depositContract.isETH(tokenAddress))) {
89-
depositValueETH = amount;
90-
fee = uint64(msg.value.sub(amount));
91-
} else {
92-
fee = uint64(msg.value);
93-
}
94-
95-
// Transfer the tokens to the deposit contract (excluding the ETH fee)
96-
amountDeposited = depositContract.deposit{value: depositValueETH}(
97-
from,
98-
tokenAddress,
99-
amount,
100-
extraData
65+
uint96(amountDeposited)
10166
);
10267
}
10368
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ library ExchangeWithdrawals
6969
msg.sender.sendETHAndVerify(feeSurplus, gasleft());
7070
}
7171

72-
require(S.pendingForcedWithdrawals[accountID][tokenID].timestamp == 0, "WITHDRAWAL_ALREADY_PENDING");
72+
require(
73+
S.pendingForcedWithdrawals[accountID][tokenID].timestamp == 0,
74+
"WITHDRAWAL_ALREADY_PENDING"
75+
);
7376

74-
S.pendingForcedWithdrawals[accountID][tokenID].owner = owner;
75-
S.pendingForcedWithdrawals[accountID][tokenID].timestamp = uint32(block.timestamp);
76-
S.pendingForcedWithdrawals[accountID][tokenID].fee = uint64(withdrawalFeeETH);
77+
S.pendingForcedWithdrawals[accountID][tokenID] = ExchangeData.ForcedWithdrawal({
78+
owner: owner,
79+
timestamp: uint32(block.timestamp),
80+
fee: uint64(withdrawalFeeETH)
81+
});
7782

7883
S.numPendingForcedTransactions++;
7984

@@ -140,7 +145,6 @@ library ExchangeWithdrawals
140145
);
141146

142147
uint amount = deposit.amount;
143-
uint fee = deposit.fee;
144148

145149
// Reset the deposit request
146150
delete S.pendingDeposits[owner][tokenID];
@@ -156,9 +160,6 @@ library ExchangeWithdrawals
156160
gasleft(),
157161
false
158162
);
159-
160-
// Return the fee
161-
owner.sendETHAndVerify(fee, gasleft());
162163
}
163164

164165
function withdrawFromApprovedWithdrawals(

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ library DepositTransaction
3939
bytes memory /*auxiliaryData*/
4040
)
4141
internal
42-
returns (uint feeETH)
42+
returns (uint)
4343
{
4444
// Read in the deposit
4545
Deposit memory deposit = readTx(data, offset);
@@ -55,27 +55,17 @@ library DepositTransaction
5555
// Also note the original deposit.amount can be zero!
5656
if (deposit.amount > 0) {
5757
require(pendingDeposit.amount >= deposit.amount, "INVALID_AMOUNT");
58-
feeETH = pendingDeposit.amount == deposit.amount?
59-
uint(pendingDeposit.fee):
60-
uint(pendingDeposit.fee).mul(deposit.amount) / pendingDeposit.amount;
61-
62-
pendingDeposit.fee = uint64(uint(pendingDeposit.fee).sub(feeETH));
6358
pendingDeposit.amount = uint96(uint(pendingDeposit.amount).sub(deposit.amount));
6459
}
6560

6661
// If the deposit was fully consumed, reset it so the storage is freed up
6762
// and the owner receives a gas refund.
6863
if (pendingDeposit.amount == 0) {
69-
// Give the owner the remaining fee
70-
feeETH = feeETH.add(uint(pendingDeposit.fee));
71-
// Reset the deposit data
72-
pendingDeposit.fee = 0;
73-
pendingDeposit.timestamp = 0;
64+
delete S.pendingDeposits[deposit.owner][deposit.tokenID];
65+
} else {
66+
S.pendingDeposits[deposit.owner][deposit.tokenID] = pendingDeposit;
7467
}
7568

76-
// Update the data in storage
77-
S.pendingDeposits[deposit.owner][deposit.tokenID] = pendingDeposit;
78-
7969
//emit DepositProcessed(owner, accountID, tokenID, amount);
8070
}
8171

packages/loopring_v3/test/testBasicDepositContract.ts renamed to packages/loopring_v3/test/testDefaultDepositContract.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,31 @@ contract("DefaultDepositContract", (accounts: string[]) => {
5959
});
6060
});
6161

62-
it("should not be able to send a wrong amount of ETH in a deposit", async () => {
63-
const exchange = exchange1;
64-
await depositContract.initialize(exchange1);
65-
66-
await expectThrow(
67-
depositContract.deposit(
68-
owner1,
69-
Constants.zeroAddress,
70-
new BN(2),
71-
"0x",
72-
{
73-
from: exchange,
74-
value: new BN(1)
75-
}
76-
),
77-
"INVALID_ETH_DEPOSIT"
78-
);
79-
await expectThrow(
80-
depositContract.deposit(owner1, token.address, new BN(1), "0x", {
81-
from: exchange,
82-
value: new BN(1)
83-
}),
84-
"INVALID_TOKEN_DEPOSIT"
85-
);
86-
});
62+
// it("should not be able to send a wrong amount of ETH in a deposit", async () => {
63+
// const exchange = exchange1;
64+
// await depositContract.initialize(exchange1);
65+
66+
// await expectThrow(
67+
// depositContract.deposit(
68+
// owner1,
69+
// Constants.zeroAddress,
70+
// new BN(2),
71+
// "0x",
72+
// {
73+
// from: exchange,
74+
// value: new BN(1)
75+
// }
76+
// ),
77+
// "INVALID_ETH_DEPOSIT"
78+
// );
79+
// await expectThrow(
80+
// depositContract.deposit(owner1, token.address, new BN(1), "0x", {
81+
// from: exchange,
82+
// value: new BN(1)
83+
// }),
84+
// "INVALID_TOKEN_DEPOSIT"
85+
// );
86+
// });
8787
});
8888

8989
describe("anyone", () => {

0 commit comments

Comments
 (0)