Soldity0.8-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
.
// 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)
);
}
}
标签:sender,contract,Delegatecall,Soldity0.8,num,uint,value,public 来源: https://www.cnblogs.com/zaleswift/p/16538398.html