Repository navigation
Expand file tree
/
Copy pathMigratable.sol
More file actions
57 lines (51 loc) · 2.3 KB
/
Copy pathMigratable.sol
File metadata and controls
57 lines (51 loc) · 2.3 KB
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
pragma solidity >= 0.6.0;
import { Layer2 } from "../storage/Layer2.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC721 } from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import { MassDeposit, MassMigration, Types } from "../libraries/Types.sol";
import { Deserializer } from "../libraries/Deserializer.sol";
contract Migratable is Layer2 {
using Types for *;
event NewMassMigration(bytes32 checksum, address network, bytes32 merged, uint fee);
function migrateTo(
bytes32 checksum,
bytes calldata
) external {
MassMigration memory migration = Deserializer.massMigrationFromCalldataAt(1);
address to = migration.destination;
bytes32 migrationId = keccak256(abi.encodePacked(checksum, migration.hash()));
require(chain.migrations[migrationId], "MassMigration does not exist");
try Migratable(to).acceptMigration(
migrationId,
migration.migratingLeaves.merged,
migration.migratingLeaves.fee
) {
// TODO: Handle out of gas due to the push pattern => ex: slash proposer using checksum?
// send ETH first
payable(to).transfer(migration.totalETH);
// send ERC20
for(uint i = 0; i < migration.erc20.length; i++) {
IERC20(migration.erc20[i].addr).transfer(to, migration.erc20[i].amount);
}
// send ERC721
for(uint i = 0; i < migration.erc721.length; i++) {
for(uint j = 0; j < migration.erc721[i].nfts.length; j++) {
IERC721(migration.erc721[i].addr).transferFrom(
address(this),
to,
migration.erc721[i].nfts[j]
);
}
}
/// Delete mass migration
delete chain.migrations[migrationId];
} catch {
revert("Dest contract denied migration");
}
}
function acceptMigration(bytes32 checksum, bytes32 merged, uint fee) external virtual {
require(Layer2.allowedMigrants[msg.sender], "Not an allowed departure");
Layer2.chain.committedDeposits[MassDeposit(merged,fee).hash()] += 1;
emit NewMassMigration(checksum, msg.sender, merged, fee);
}
}