-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path35_DelegateCall.sol
46 lines (32 loc) · 1.32 KB
/
35_DelegateCall.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EmployeeRecord{
uint id;
mapping(uint=>string) public EmployeeMapping;
event Log(uint indexed,string);
function addEmployeeDetails(string calldata _name) external{
EmployeeMapping[id++]=_name;
emit Log(id,_name);
}
}
/* Note:
- There exists a special variant of a message call, named delegatecall
which is identical to a message call apart from the fact
that the code at the target address is executed in the context of the calling contract
and msg.sender and msg.value do not change their values.
- This means that a contract can dynamically load code from a different address at runtime.
Storage, current address and balance still refer to the calling contract,
only the code is taken from the called address.
*/
// We will call this contract using the delegatecall
contract EmployeeDB{
// Please maintain the order of state variables for which the delegatecall will be performed.
// Here, we are only using one State Variable.
uint id;
function delegateCallExample(address _contractAddress,string calldata _name) external{
(bool status,) = _contractAddress.delegatecall(
abi.encodeWithSelector(EmployeeRecord.addEmployeeDetails.selector, _name)
);
require(status,"Delegate Call Failed");
}
}