-
Notifications
You must be signed in to change notification settings - Fork 10
/
Bridge.sol
430 lines (388 loc) · 17.2 KB
/
Bridge.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// SPDX-License-Identifier: No License (None)
pragma solidity ^0.8.0;
interface IBEP20TokenCloned {
// initialize cloned token just for BEP20TokenCloned
function initialize(address newOwner, string calldata name, string calldata symbol, uint8 decimals) external;
function mint(address user, uint256 amount) external;
function burnFrom(address account, uint256 amount) external returns(bool);
function burn(uint256 amount) external returns(bool);
}
/**
* @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
* deploying minimal proxy contracts, also known as "clones".
*
* > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
* > a minimal bytecode implementation that delegates all calls to a known, fixed address.
*
* The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
* (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
* deterministic method.
*
* _Available since v3.4._
*/
library Clones {
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create opcode, which should never revert.
*/
function clone(address implementation) internal returns (address instance) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create(0, ptr, 0x37)
}
require(instance != address(0), "ERC1167: create failed");
}
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create2 opcode and a `salt` to deterministically deploy
* the clone. Using the same `implementation` and `salt` multiple time will revert, since
* the clones cannot be deployed twice at the same address.
*/
function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create2(0, ptr, 0x37, salt)
}
require(instance != address(0), "ERC1167: create2 failed");
}
/**
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
*/
function predictDeterministicAddress(
address implementation,
bytes32 salt,
address deployer
) internal pure returns (address predicted) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
mstore(add(ptr, 0x38), shl(0x60, deployer))
mstore(add(ptr, 0x4c), salt)
mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
predicted := keccak256(add(ptr, 0x37), 0x55)
}
}
/**
* @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
*/
function predictDeterministicAddress(address implementation, bytes32 salt)
internal
view
returns (address predicted)
{
return predictDeterministicAddress(implementation, salt, address(this));
}
}
// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
library TransferHelper {
function safeApprove(address token, address to, uint value) internal {
// bytes4(keccak256(bytes('approve(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
}
function safeTransfer(address token, address to, uint value) internal {
// bytes4(keccak256(bytes('transfer(address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
}
function safeTransferFrom(address token, address from, address to, uint value) internal {
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
}
function safeTransferETH(address to, uint value) internal {
(bool success,) = to.call{value:value}(new bytes(0));
require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
}
}
abstract contract Ownable {
address internal _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () {
_owner = msg.sender;
emit OwnershipTransferred(address(0), msg.sender);
}
/**
* @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() == msg.sender, "Ownable: caller is not the owner");
_;
}
/**
* @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");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
contract CallistoBridge is Ownable {
using TransferHelper for address;
address constant MAX_NATIVE_COINS = address(31); // addresses from address(1) to MAX_NATIVE_COINS are considered as native coins
// CLO = address(1)
struct Token {
address token; // foreign token address
bool isWrapped; // is native token wrapped of foreign
}
uint256 public threshold; // minimum number of signatures required to approve swap
address public tokenImplementation; // implementation of wrapped token
address public feeTo; // send fee to this address
address[] public authorities; // list of authorities
bool public frozen; // if frozen - swap will not work
uint256 public wrapNonce; // the last nonce used to create wrapped token address begin with 0xCC....
mapping(address => bool) isAuthority; // authority has to sign claim transaction (message)
mapping(uint256 => mapping(bytes32 => bool)) public isTxProcessed; // chainID => txID => isProcessed
mapping(uint256 => mapping(address => Token)) public tokenPair; // chainID => native token address => Token struct
mapping(uint256 => mapping(address => address)) public tokenForeign; // chainID => foreign token address => native token
event SetAuthority(address authority, bool isEnable);
event SetFeeTo(address previousFeeTo, address newFeeTo);
event SetThreshold(uint256 threshold);
event Deposit(address indexed token, address indexed sender, uint256 value, uint256 toChainId, address toToken);
event Claim(address indexed token, address indexed to, uint256 value, bytes32 txId, uint256 fromChainId, address fromToken);
event Fee(address indexed sender, uint256 fee);
event CreatePair(address toToken, bool isWrapped, address fromToken, uint256 fromChainId);
event Frozen(bool status);
constructor (address _tokenImplementation) {
require(_tokenImplementation != address(0), "Wrong tokenImplementation");
tokenImplementation = _tokenImplementation;
feeTo = msg.sender;
threshold = 1;
}
modifier notFrozen() {
require(!frozen, "Bridge is frozen");
_;
}
// get number of authorities
function getAuthoritiesNumber() external view returns(uint256) {
return authorities.length;
}
// Owner or Authority may freeze bridge in case of anomaly detection
function freeze() external {
require(msg.sender == owner() || isAuthority[msg.sender]);
frozen = true;
emit Frozen(true);
}
// Only owner can manually unfreeze contract
function unfreeze() external onlyOwner {
frozen = false;
emit Frozen(false);
}
// set/remove Authority address
function setAuthority(address authority, bool isEnable) external onlyOwner{
require(authority != address(0), "Zero address");
if (isEnable) {
require(!isAuthority[authority], "Authority already enabled");
require(authorities.length < 50, "Too much authorities"); // to avoid OUT_OF_GAS exception
isAuthority[authority] = true;
authorities.push(authority);
} else {
require(isAuthority[authority], "Authority already disabled");
isAuthority[authority] = false;
uint n = authorities.length; // use local variable to save gas
for (uint i = 0; i < n; i++) { // maximum number of authorities is 50
if(authorities[i] == authority) {
authorities[i] = authorities[n-1];
authorities.pop();
break;
}
}
}
emit SetAuthority(authority, isEnable);
}
// set fee receiver address
function setFeeTo(address newFeeTo) external onlyOwner{
require(newFeeTo != address(0), "Zero address");
address previousFeeTo = feeTo;
feeTo = newFeeTo;
emit SetFeeTo(previousFeeTo, newFeeTo);
}
// set threshold - minimum number of signatures required to approve swap
function setThreshold(uint256 _threshold) external onlyOwner{
require(threshold != 0 && threshold <= authorities.length, "Wrong threshold");
threshold = _threshold;
emit SetThreshold(threshold);
}
// returns `nonce` to use in `createWrappedToken()` to create address starting with 0xCC.....
function calculateNonce() external view returns(uint256 nonce, address addr) {
nonce = wrapNonce;
address implementation = tokenImplementation;
while (true) {
nonce++;
addr = Clones.predictDeterministicAddress(implementation, bytes32(nonce));
if (uint160(addr) & uint160(0xfF00000000000000000000000000000000000000) == uint160(0xCc00000000000000000000000000000000000000))
break;
}
}
// Create wrapped token for foreign token
function createWrappedToken(
address fromToken, // foreign token address
uint256 fromChainId, // foreign chain ID where token deployed
string memory name, // wrapped token name
string memory symbol, // wrapped token symbol
uint8 decimals, // wrapped token decimals (should be the same as in original token)
uint256 nonce // nonce to create wrapped token address begin with 0xCC....
)
external
onlyOwner
{
require(fromToken != address(0), "Wrong token address");
require(tokenForeign[fromChainId][fromToken] == address(0), "This token already wrapped");
require(nonce > wrapNonce, "Nonce must be higher then wrapNonce");
wrapNonce = nonce;
address wrappedToken = Clones.cloneDeterministic(tokenImplementation, bytes32(nonce));
IBEP20TokenCloned(wrappedToken).initialize(owner(), name, symbol, decimals);
tokenPair[fromChainId][wrappedToken] = Token(fromToken, true);
tokenForeign[fromChainId][fromToken] = wrappedToken;
emit CreatePair(wrappedToken, true, fromToken, fromChainId); //wrappedToken - wrapped token contract address
}
// Create pair from foreign wrapped token to native token
function createPair(address toToken, address fromToken, uint256 fromChainId) external onlyOwner {
require(tokenPair[fromChainId][toToken].token == address(0), "Pair already exist");
tokenPair[fromChainId][toToken] = Token(fromToken, false);
tokenForeign[fromChainId][fromToken] = toToken;
emit CreatePair(toToken, false, fromToken, fromChainId);
}
function depositTokens(
address token, // token that user send (if token address < 32, then send native coin)
uint256 value, // tokens value
uint256 toChainId // destination chain Id where will be claimed tokens
)
external
payable
notFrozen
{
Token memory pair = tokenPair[toChainId][token];
require(pair.token != address(0), "There is no pair");
uint256 fee = msg.value;
if (token <= MAX_NATIVE_COINS) {
require(value <= msg.value, "Wrong value");
fee -= value;
} else {
if(pair.isWrapped) {
IBEP20TokenCloned(token).burnFrom(msg.sender, value);
} else {
token.safeTransferFrom(msg.sender, address(this), value);
}
}
if (fee != 0) {
feeTo.safeTransferETH(fee);
emit Fee(msg.sender, fee);
}
emit Deposit(token, msg.sender, value, toChainId, pair.token);
}
// claim
function claim(
address token, // token to receive
bytes32 txId, // deposit transaction hash on fromChain
address to, // user address
uint256 value, // value of tokens
uint256 fromChainId, // chain ID where user deposited
bytes calldata sig // authority signature
)
external
{
bytes[] memory s = new bytes[](1);
s[0] = sig;
_claim(token, txId, to, value, fromChainId, s);
}
// claim Multi signature
function claimMultiSig(
address token, // token to receive
bytes32 txId, // deposit transaction hash on fromChain
address to, // user address
uint256 value, // value of tokens
uint256 fromChainId, // chain ID where user deposited
bytes[] calldata sig // authority signature
)
external
{
_claim(token, txId, to, value, fromChainId, sig);
}
// claim
function _claim(
address token, // token to receive
bytes32 txId, // deposit transaction hash on fromChain
address to, // user address
uint256 value, // value of tokens
uint256 fromChainId, // chain ID where user deposited
bytes[] memory sig // authority signature
)
internal
notFrozen
{
uint256 t;
require(!isTxProcessed[fromChainId][txId], "Transaction already processed");
Token memory pair = tokenPair[fromChainId][token];
require(pair.token != address(0), "There is no pair");
isTxProcessed[fromChainId][txId] = true;
bytes32 messageHash = keccak256(abi.encodePacked(token, to, value, txId, fromChainId, block.chainid));
messageHash = prefixed(messageHash);
for (uint i = 0; i < sig.length; i++) {
if (isAuthority[recoverSigner(messageHash, sig[i])]) t++;
}
require(threshold <= t, "Require more signatures");
if (token <= MAX_NATIVE_COINS) {
to.safeTransferETH(value);
} else {
if(pair.isWrapped) {
IBEP20TokenCloned(token).mint(to, value);
} else {
token.safeTransfer(to, value);
}
}
emit Claim(token, to, value, txId, fromChainId, pair.token);
}
// Signature methods
function splitSignature(bytes memory sig)
internal
pure
returns (uint8 v, bytes32 r, bytes32 s)
{
require(sig.length == 65);
assembly {
// first 32 bytes, after the length prefix
r := mload(add(sig, 32))
// second 32 bytes
s := mload(add(sig, 64))
// final byte (first byte of the next 32 bytes)
v := byte(0, mload(add(sig, 96)))
}
}
function recoverSigner(bytes32 message, bytes memory sig)
internal
pure
returns (address)
{
uint8 v;
bytes32 r;
bytes32 s;
(v, r, s) = splitSignature(sig);
return ecrecover(message, v, r, s);
}
// Builds a prefixed hash to mimic the behavior of eth_sign.
function prefixed(bytes32 hash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
}
}