-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30FallbackInputOutput.sol
More file actions
56 lines (43 loc) · 1.43 KB
/
Copy path30FallbackInputOutput.sol
File metadata and controls
56 lines (43 loc) · 1.43 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
/*
# Fallback
fallback is a special function that is executed either when
• a function that does not exist is called or
• Ether is sent directly to a contract but receive() does not exist or msg.data is not empty
fallback has a 2300 gas limit when called by transfer or send.
*/
// fallback can optionally take bytes for input and output
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
// TestFallbackInputOutput -> FallbackInputOutput -> Counter
contract FallbackInputOutput {
address immutable target;
constructor(address _target) {
target = _target;
}
fallback(bytes calldata data) external payable returns (bytes memory) {
(bool ok, bytes memory res) = target.call{value: msg.value}(data);
require(ok, "call failed");
return res;
}
}
contract Counter {
uint public count;
function get() external view returns (uint) {
return count;
}
function inc() external returns (uint) {
count += 1;
return count;
}
}
contract TestFallbackInputOutput {
event Log(bytes res);
function test(address _fallback, bytes calldata data) external {
(bool ok, bytes memory res) = _fallback.call(data);
require(ok, "call failed");
emit Log(res);
}
function getTestData() external pure returns (bytes memory, bytes memory) {
return (abi.encodeCall(Counter.get, ()), abi.encodeCall(Counter.inc, ()));
}
}