Skip to content

Commit 5be8b38

Browse files
committed
added delegate call example to implementation of Upgradable Contracts learning project: A comprehensive educational resource for building upgradeable smart contracts on Ethereum and Base blockchain using Foundry and OpenZeppelin. Includes proxy patterns (Transparent, UUPS, Beacon), security best practices, test suites, deployment scripts, and detailed documentation with architectural explanations. Features SmallProxy.sol example, Foundry configuration, and structured learning modules for developers. - src/sublesson/DelegateCallExample.sol
1 parent 900322c commit 5be8b38

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.26;
3+
4+
// NOTE: Deploy this contract first
5+
contract B {
6+
// NOTE: storage layout must be the same as contract A
7+
uint256 public num;
8+
address public sender;
9+
uint256 public value;
10+
11+
function setVars(uint256 _num) public payable {
12+
num = _num;
13+
sender = msg.sender;
14+
value = msg.value;
15+
}
16+
}
17+
18+
contract A {
19+
uint256 public num;
20+
address public sender;
21+
uint256 public value;
22+
23+
event DelegateResponse(bool success, bytes data);
24+
event CallResponse(bool success, bytes data);
25+
26+
// Function using delegatecall
27+
function setVarsDelegateCall(address _contract, uint256 _num)
28+
public
29+
payable
30+
{
31+
// A's storage is set; B's storage is not modified.
32+
(bool success, bytes memory data) = _contract.delegatecall(
33+
abi.encodeWithSignature("setVars(uint256)", _num)
34+
);
35+
36+
emit DelegateResponse(success, data);
37+
}
38+
39+
// Function using call
40+
function setVarsCall(address _contract, uint256 _num) public payable {
41+
// B's storage is set; A's storage is not modified.
42+
(bool success, bytes memory data) = _contract.call{value: msg.value}(
43+
abi.encodeWithSignature("setVars(uint256)", _num)
44+
);
45+
46+
emit CallResponse(success, data);
47+
}
48+
}

0 commit comments

Comments
 (0)