Soldity0.8-Delegatecall

delegatecall is a low level function similar to call.

When contract A executes delegatecall to contract BB's code is executed

with contract A's storage, msg.sender and 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)
        );
    }
}
posted @ 2022-08-02 12:11  ZaleSwfit  阅读(18)  评论(0编辑  收藏  举报