solidity基础-delegateCall
delegateCall 用于重定向父合约;
A 合约越来调用B1, 将地址改为 B2 就可以调用 B2
合约B1
contract B { uint public num; uint public value; function setVars(uint _num) public payable{ num = _num; value = msg.value; } }
合约A
contract A{ uint public num; uint public value; address public b; function setVars(uint _num) public payable{ (bool success, bytes memory data) = b.delegatecall( abi.encodeWithSignature("setVars(uint256)", _num) ); } function setB(address _b) external{ b = _b; } }
部署
获得 num = 1
B2
contract B2 { uint public num; uint public value; function setVars(uint _num) public payable{ num = _num + 2; value = msg.value; } }
编译部署完 B2, 地址设置 B2 合约
调用 合约 A 的 setVars 获得 num = 3;