forked from matter-labs/era-system-contracts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSystemCaller.sol
76 lines (66 loc) · 2.46 KB
/
SystemCaller.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import {SYSTEM_CALL_CALL_ADDRESS, MSG_VALUE_SIMULATOR_IS_SYSTEM_BIT, SystemContractsCaller, CalldataForwardingMode} from "../libraries/SystemContractsCaller.sol";
import "../libraries/Utils.sol";
address constant REAL_MSG_VALUE_SYSTEM_CONTRACT = address(0x8009);
// Proxy that sets system call, does the same thing as `ExtraAbiCaller.zasm`, but can be called with callee abi, which is more convenient.
// Also updates the real balance of the callee.
contract SystemCaller {
address immutable to;
constructor(address _to) {
to = _to;
}
// The library method will not work, because it uses the MsgValueSimulator test address.
fallback() external payable {
address callAddr = SYSTEM_CALL_CALL_ADDRESS;
address _to = to;
bytes memory data = msg.data;
uint32 dataStart;
assembly {
dataStart := add(data, 0x20)
}
uint32 dataLength = uint32(Utils.safeCastToU32(data.length));
uint256 farCallAbi = SystemContractsCaller.getFarCallABI(
0,
0,
dataStart,
dataLength,
Utils.safeCastToU32(gasleft()),
// Only rollup is supported for now
0,
CalldataForwardingMode.UseHeap,
false,
true
);
bool success;
if (msg.value == 0) {
// Doing the system call directly
assembly {
success := call(_to, callAddr, 0, 0, farCallAbi, 0, 0)
}
} else {
address msgValueSimulator = REAL_MSG_VALUE_SYSTEM_CONTRACT;
// We need to supply the mask to the MsgValueSimulator to denote
// that the call should be a system one.
uint256 forwardMask = MSG_VALUE_SIMULATOR_IS_SYSTEM_BIT;
assembly {
success := call(msgValueSimulator, callAddr, callvalue(), _to, farCallAbi, forwardMask, 0)
}
}
uint256 returnDataSize;
assembly {
returnDataSize := returndatasize()
}
bytes memory returnData = new bytes(returnDataSize);
assembly {
returndatacopy(add(returnData, 0x20), 0, returnDataSize)
switch success
case 0 {
revert(add(returnData, 0x20), returnDataSize)
}
default {
return(add(returnData, 0x20), returnDataSize)
}
}
}
}