-
Notifications
You must be signed in to change notification settings - Fork 1
/
BridgeBaseBSC.sol
377 lines (308 loc) · 10.6 KB
/
BridgeBaseBSC.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
pragma solidity ^0.8.0;
abstract contract ApproverRole {
using Roles for Roles.Role;
event ApproverAdded(address indexed account);
event ApproverRemoved(address indexed account);
Roles.Role private _approvers;
address firstSignAddress;
address secondSignAddress;
mapping(address => bool) signed; // Signed flag
constructor() internal {
_addApprover(msg.sender);
firstSignAddress = 0x4d7AB40b8601af760927624200BC5bcD2837BB41; // You should change this address to your first sign address
secondSignAddress = 0xD878E8726fbCaE338B3E184aa816F0fFD459d3c6; // You should change this address to your second sign address
}
modifier onlyApprover() {
require(isApprover(msg.sender));
_;
}
function sign() external {
require(
msg.sender == firstSignAddress || msg.sender == secondSignAddress
);
require(!signed[msg.sender]);
signed[msg.sender] = true;
}
function isApprover(address account) public view returns (bool) {
return _approvers.has(account);
}
function addApprover(address account) external onlyApprover {
require(signed[firstSignAddress] && signed[secondSignAddress]);
_addApprover(account);
signed[firstSignAddress] = false;
signed[secondSignAddress] = false;
}
function removeApprover(address account) external onlyApprover {
require(signed[firstSignAddress] && signed[secondSignAddress]);
_removeApprover(account);
signed[firstSignAddress] = false;
signed[secondSignAddress] = false;
}
function renounceApprover() external {
require(signed[firstSignAddress] && signed[secondSignAddress]);
_removeApprover(msg.sender);
signed[firstSignAddress] = false;
signed[secondSignAddress] = false;
}
function _addApprover(address account) internal {
_approvers.add(account);
emit ApproverAdded(account);
}
function _removeApprover(address account) internal {
_approvers.remove(account);
emit ApproverRemoved(account);
}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function updateAdmin(address newAdmin) external;
function transfer(address recipient, uint256 amount)
external
returns (bool);
function allowance(address owner, address spender)
external
view
returns (uint256);
function mint(address to, uint256 amount) external;
function burn(address owner, uint256 amount) external;
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
library SafeMathChainlink {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "SafeMath: modulo by zero");
return a % b;
}
}
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() internal {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and make it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_setOwner(_msgSender());
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_setOwner(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(
newOwner != address(0),
"Ownable: new owner is the zero address"
);
_setOwner(newOwner);
}
function _setOwner(address newOwner) private {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
library Roles {
struct Role {
mapping(address => bool) bearer;
}
/**
* @dev Give an account access to this role.
*/
function add(Role storage role, address account) internal {
require(!has(role, account), "Roles: account already has role");
role.bearer[account] = true;
}
/**
* @dev Remove an account's access to this role.
*/
function remove(Role storage role, address account) internal {
require(has(role, account), "Roles: account does not have role");
role.bearer[account] = false;
}
/**
* @dev Check if an account has this role.
* @return bool
*/
function has(Role storage role, address account)
internal
view
returns (bool)
{
require(account != address(0), "Roles: account is the zero address");
return role.bearer[account];
}
}
contract BridgeBaseBSC is Ownable, ApproverRole, ReentrancyGuard {
using SafeMathChainlink for uint256;
address public admin;
IERC20 public token;
uint256 public nonce;
mapping(uint256 => bool) public processedNonces;
uint256 public fee;
address public feeAddress;
enum Step {
Burn,
Mint
}
event Transfer(
address from,
address to,
uint256 amount,
uint256 date,
uint256 nonce,
Step indexed step
);
constructor(address _token, address _feeAddress, uint256 _fee) {
admin = msg.sender;
token = IERC20(_token);
feeAddress = _feeAddress;
fee = _fee;
}
function changeFeeAddress(address _newFeeAddress) public onlyOwner {
feeAddress = _newFeeAddress;
}
function changeFee(uint256 _fee) public onlyOwner {
fee = _fee;
}
function transferTokenOwnership() external onlyApprover nonReentrant {
token.updateAdmin(msg.sender);
}
function mint(
address to,
uint256 amount,
uint256 otherChainNonce
) external nonReentrant onlyApprover {
require(
processedNonces[otherChainNonce] == false,
"transfer already processed"
);
processedNonces[otherChainNonce] = true;
token.transfer(to, amount);
emit Transfer(
msg.sender,
to,
amount,
block.timestamp,
otherChainNonce,
Step.Mint
);
}
function burn(address to, uint256 amount) external nonReentrant {
require(amount > fee);
token.transferFrom(msg.sender, address(this), amount);
token.transfer(feeAddress, fee);
emit Transfer(
msg.sender,
to,
amount.sub(fee),
block.timestamp,
nonce,
Step.Burn
);
nonce = nonce.add(1);
}
}