-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathLoopringV3.sol
209 lines (180 loc) · 5.76 KB
/
LoopringV3.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// SPDX-License-Identifier: Apache-2.0
// Copyright 2017 Loopring Technology Limited.
pragma solidity ^0.7.0;
import "../../lib/AddressUtil.sol";
import "../../lib/ERC20SafeTransfer.sol";
import "../../lib/MathUint.sol";
import "../../lib/ReentrancyGuard.sol";
import "../iface/IExchangeV3.sol";
import "../iface/ILoopringV3.sol";
/// @title LoopringV3
/// @dev This contract does NOT support proxy.
/// @author Brecht Devos - <[email protected]>
/// @author Daniel Wang - <[email protected]>
contract LoopringV3 is ILoopringV3, ReentrancyGuard
{
using AddressUtil for address payable;
using MathUint for uint;
using ERC20SafeTransfer for address;
address public immutable override lrcAddress;
// -- Constructor --
constructor(
address _lrcAddress,
address payable _protocolFeeVault,
address _blockVerifierAddress
)
Claimable()
{
require(address(0) != _lrcAddress, "ZERO_ADDRESS");
lrcAddress = _lrcAddress;
updateSettingsInternal(_protocolFeeVault, _blockVerifierAddress, 0, 0);
}
// == Public Functions ==
function updateSettings(
address payable _protocolFeeVault,
address _blockVerifierAddress,
uint _forcedWithdrawalFee
)
external
override
nonReentrant
onlyOwner
{
updateSettingsInternal(
_protocolFeeVault,
_blockVerifierAddress,
_forcedWithdrawalFee,
SETTING_UPDATE_DELAY + block.timestamp
);
}
function updateProtocolFeeSettings(
uint8 _protocolTakerFeeBips,
uint8 _protocolMakerFeeBips
)
external
override
nonReentrant
onlyOwner
{
protocolTakerFeeBips = _protocolTakerFeeBips;
protocolMakerFeeBips = _protocolMakerFeeBips;
emit SettingsUpdated(block.timestamp, 0);
}
function getExchangeStake(
address exchangeAddr
)
public
override
view
returns (uint)
{
return exchangeStake[exchangeAddr];
}
function burnExchangeStake(
uint amount
)
external
override
nonReentrant
returns (uint burnedLRC)
{
burnedLRC = exchangeStake[msg.sender];
if (amount < burnedLRC) {
burnedLRC = amount;
}
if (burnedLRC > 0) {
lrcAddress.safeTransferAndVerify(protocolFeeVault, burnedLRC);
exchangeStake[msg.sender] = exchangeStake[msg.sender].sub(burnedLRC);
totalStake = totalStake.sub(burnedLRC);
}
emit ExchangeStakeBurned(msg.sender, burnedLRC);
}
function depositExchangeStake(
address exchangeAddr,
uint amountLRC
)
external
override
nonReentrant
returns (uint stakedLRC)
{
require(amountLRC > 0, "ZERO_VALUE");
lrcAddress.safeTransferFromAndVerify(msg.sender, address(this), amountLRC);
stakedLRC = exchangeStake[exchangeAddr].add(amountLRC);
exchangeStake[exchangeAddr] = stakedLRC;
totalStake = totalStake.add(amountLRC);
emit ExchangeStakeDeposited(exchangeAddr, amountLRC);
}
function withdrawExchangeStake(
address recipient,
uint requestedAmount
)
external
override
nonReentrant
returns (uint amountLRC)
{
uint stake = exchangeStake[msg.sender];
amountLRC = (stake > requestedAmount) ? requestedAmount : stake;
if (amountLRC > 0) {
lrcAddress.safeTransferAndVerify(recipient, amountLRC);
exchangeStake[msg.sender] = exchangeStake[msg.sender].sub(amountLRC);
totalStake = totalStake.sub(amountLRC);
}
emit ExchangeStakeWithdrawn(msg.sender, amountLRC);
}
function getProtocolFeeValues()
public
override
view
returns (
uint8 takerFeeBips,
uint8 makerFeeBips
)
{
return (protocolTakerFeeBips, protocolMakerFeeBips);
}
// == Internal Functions ==
function updateSettingsInternal(
address payable _protocolFeeVault,
address _blockVerifierAddress,
uint _forcedWithdrawalFee,
uint _nextEffectiveTime
)
private
{
require(address(0) != _protocolFeeVault, "ZERO_ADDRESS");
require(address(0) != _blockVerifierAddress, "ZERO_ADDRESS");
require(
_forcedWithdrawalFee <= 0.5 ether,
"FORCED_WITHDRAWAL_FEE_TOO_HIGH"
);
if (_nextEffectiveTime == 0) {
// effect immediately
protocolFeeVault = _protocolFeeVault;
blockVerifierAddress = _blockVerifierAddress;
forcedWithdrawalFee = _forcedWithdrawalFee;
} else {
// delayed effect
cachedSettings = CachedSettings({
protocolFeeVault: _protocolFeeVault,
blockVerifierAddress: _blockVerifierAddress,
forcedWithdrawalFee: _forcedWithdrawalFee
});
nextEffectiveTime = _nextEffectiveTime;
}
emit SettingsUpdated(block.timestamp, _nextEffectiveTime);
}
function applyUpdate() external onlyOwner {
// TODO(add ownership check)
require(nextEffectiveTime > 0, "NO_ANY_UPDATES");
require(nextEffectiveTime <= block.timestamp, "NOT_ENABLED_YET");
protocolFeeVault = payable(cachedSettings.protocolFeeVault);
blockVerifierAddress = cachedSettings.blockVerifierAddress;
forcedWithdrawalFee = cachedSettings.forcedWithdrawalFee;
// clear updates
nextEffectiveTime = 0;
delete cachedSettings;
emit SettingsApplied(block.timestamp);
}
}