forked from RareSkills/Udemy-Yul-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Video-16-Receiving-Contract-Calls.sol
80 lines (66 loc) · 1.93 KB
/
Video-16-Receiving-Contract-Calls.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;
/**
To Learn
1. calldataload
2. imitating function selectors
3. switch statement
4. yul functions with arguments
5. functions that return
6. exit from a function without returning
7. validating calldata
**/
contract CalldataDemo {
fallback() external {
assembly {
let cd := calldataload(0) // always loads 32 bytes
// d2178b0800000000000000000000000000000000000000000000000000000000
let selector := shr(0xe0, cd) // shift right 224 bits to get last 4 bytes
// 00000000000000000000000000000000000000000000000000000000d2178b08
// unlike other languages, switch does not "fall through"
switch selector
case 0xd2178b08 /* get2() */
{
returnUint(2)
}
case 0xba88df04 /* get99(uint256) */
{
returnUint(getNotSoSecretValue())
}
default {
revert(0, 0)
}
function getNotSoSecretValue() -> r {
if lt(calldatasize(), 36) {
revert(0, 0)
}
let arg1 := calldataload(4)
if eq(arg1, 8) {
r := 88
leave
}
r := 99
}
function returnUint(v) {
mstore(0, v)
return(0, 0x20)
}
}
}
}
interface ICalldataDemo {
function get2() external view returns (uint256);
function get99(uint256) external view returns (uint256);
}
contract CallDemo {
ICalldataDemo public target;
constructor(ICalldataDemo _a) {
target = _a;
}
function callGet2() external view returns (uint256) {
return target.get2();
}
function callGet99(uint256 arg) external view returns (uint256) {
return target.get99(arg);
}
}