solidity delegatecall使用
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Callee {
uint256 public value;
function setValue(uint256 _newValue) public {
value = _newValue;
}
}
contract Caller {
uint256 public value;
/*
1、调用delegateSetValue 状态变量value 会变成_newValue
*/
function delegateSetValue(address callee, uint256 _newValue) public {
// 对函数签名和参数进行编码
bytes memory methodop = abi.encodeWithSignature(
"setValue(uint256)",
_newValue
);
(bool success, ) = callee.delegatecall(methodop);
if(!success){
revert("delegate call failed");
}
}
}