https://solidity-by-example.org/delegatecall/

delegatecall
is a low level function similar to
call
.

When contract

A
executes
delegatecall
to contract
B
,
B
's code is executed

with contract

A
's storage,
msg.sender
and
msg.value
.

delegatecall
是一个低级函数,类似于
call
.

签约时

A
执行
delegatecall
合同
B
,
B
的代码被执行

有合同

A
的存储,
msg.sender
msg.value
.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

// NOTE: Deploy this contract first
contract B {
    // NOTE: storage layout must be the same as contract A
    uint public num;
    address public sender;
    uint public value;

    function setVars(uint _num) public payable {
        num = _num;
        sender = msg.sender;
        value = msg.value;
    }
}

contract A {
    uint public num;
    address public sender;
    uint public value;

    function setVars(address _contract, uint _num) public payable {
        // A's storage is set, B is not modified.
        (bool success, bytes memory data) = _contract.delegatecall(
            abi.encodeWithSignature("setVars(uint256)", _num)
        );
    }
}