你可以传递错误选择器或者错误数据(错误选择器是 Solidity 错误的前四个字节,通常通过 abi.encodeWithSelector 或 abi.encodeWithSignature 编码)。 捕获其他类型的 revert 错误你可以通过 abi.encodeWithSelector 或 abi.encodeWithSignature 编码捕获任何 Solidity 错误。 如果捕获的是 自定义错误,你需要使用 abi.encodeWithSelector 或 abi.encodeWithSignature 来构建错误的数据。5. vm.expectRevert("Custom error message");自定义错误:如果是 Solidity 0.8.x+ 引入的自定义错误,则需要使用 abi.encodeWithSelector 或 abi.encodeWithSignature
address newLogic) external { require(msg.sender == owner); currentLogic.delegatecall(abi.encodeWithSignature setModule(address newModule) external { require(msg.sender == owner); module.delegatecall(abi.encodeWithSignature dc.delegatecall_contract_address; (bool success, ) = _target.delegatecall( abi.encodeWithSignature 可以使用 abi.encodeWithSignature 将函数调用的签名和参数打包。 异常处理: 在 delegatecall 中,被调用的合约抛出的异常会传递到调用者合约。
(bool sent, ) = address(exampleExternalContract).call{ value: contractBalance }(abi.encodeWithSignature (bool sent, ) = address(exampleExternalContract).call{ value: contractBalance }(abi.encodeWithSignature (bool sent, ) = address(exampleExternalContract).call{ value: contractBalance }(abi.encodeWithSignature (bool sent, ) = address(exampleExternalContract).call{ value: contractBalance }(abi.encodeWithSignature (bool sent, ) = address(exampleExternalContract).call{ value: contractBalance }(abi.encodeWithSignature
(address _callee, uint256 _v) external payable { (bool success, ) = _callee.call( abi.encodeWithSignature callee) external view returns (uint256) { (, bytes memory data) = _callee.staticcall( abi.encodeWithSignature delegateSetValue(address _logic, uint256 _v) external { (bool success, ) = _logic.delegatecall( abi.encodeWithSignature
returns (bytes):计算参数的紧密打包编码 abi. encodeWithSelector(bytes4 selector, …) returns (bytes): 计算函数选择器和参数的ABI编码 abi.encodeWithSignature function abiEncode() public constant returns (bytes) { abi.encode(1); // 计算1的ABI编码 return abi.encodeWithSignature
(bool success, bytes memory data) = counter.call( abi.encodeWithSignature("increment()"));require( staticcalldelegatecall 使用当前合约的存储,常用于库合约调用staticcall 是只读调用,无法修改状态(bool success, ) = lib.delegatecall(abi.encodeWithSignature function callUnsafe(address counter) public { (bool success, ) = counter.call( abi.encodeWithSignature testCallInvalidSignature() public { // 模拟 .call 调用不存在函数 (bool success, ) = address(counter).call( abi.encodeWithSignature
(bool sent, ) = address(exampleExternalContract).call{ value: contractBalance }(abi.encodeWithSignature (bool sent, ) = address(exampleExternalContract).call{ value: contractBalance }(abi.encodeWithSignature
; 12 (bool success,) = callbackAddr.call.gas(CALLBACK_GAS)(abi.encodeWithSignature(callbackFUN, queryId ; 21 (bool success,) = callbackAddr.call.gas(CALLBACK_GAS)(abi.encodeWithSignature(callbackFUN, queryId
// ... } } contract B { function bar(address _a) public { _a.delegatecall(abi.encodeWithSignature external { // ... } } contract B { function bar(address _a) public { _a.call(abi.encodeWithSignature
calldatas = new bytes[](1); targets[0] = address(this); values[0] = 0; calldatas[0] = abi.encodeWithSignature calldatas = new bytes[](1); targets[0] = address(this); values[0] = 0; calldatas[0] = abi.encodeWithSignature calldatas = new bytes[](1); targets[0] = address(this); values[0] = 0; calldatas[0] = abi.encodeWithSignature 第一个操作:调用 mockAction targets[0] = address(this); values[0] = 0; calldatas[0] = abi.encodeWithSignature // 第二个操作:调用另一个函数 targets[1] = address(this); values[1] = 0; calldatas[1] = abi.encodeWithSignature
success, bytes memory data) = _addr.call{ value: msg.value, gas: 5000 }(abi.encodeWithSignature public payable { (bool success, bytes memory data) = _addr.call{value: msg.value}( abi.encodeWithSignature
// proxy 调用 foo() 应该成功 (bool ok1, bytes memory res1) = address(proxy).call( abi.encodeWithSignature selfdestruct"); // 代理 delegatecall 再调用 foo() 应该失败 (bool ok2, ) = address(proxy).call(abi.encodeWithSignature
event FunctionCalldata(bytes);//事件,相当于定义特殊的日志函数 bytes memory functionCallfata = abi.encodeWithSignature 我们使用abi.encodeWithSignature() 来获取所需格式的 calldata。emit 记录了我们的 calldata,用于测试。 0x6057361d000000000000000000000000000000000000000000000000000000000000000a 以上字节码就是abi.encodeWithSignature
external { (bool success, bytes memory _data) = _test.call{value: 111, gas:5000}( abi.encodeWithSignature address _test,uint _num) external payable { //签名编码调用 // _test.delegatecall( // abi.encodeWithSignature
require(msg.sender == admin, "Only admin can call this function"); bytes memory callData = abi.encodeWithSignature proxyAdmin() internal view returns (address) { return Address.functionDelegateCall(address(proxy), abi.encodeWithSignature
function testExecuteERC20Transfer() public { // 构造 ERC20 转账数据 bytes memory data = abi.encodeWithSignature test_RevertIf_NotEnoughConfirmations_ExecuteERC20Transfer() public { // 构造 ERC20 转账数据 bytes memory data = abi.encodeWithSignature
, int256 value) public {//注意,method必须为方法签名格式,且方法签名的参数间不能有空格,例如"save(string,int256)" callee.call((abi.encodeWithSignature
keccak256("transfer(address,uint256)")), to, amount ));abi.encodeWithSelector:手动指定函数选择器abi.encodeWithSignature
A: 可以,通过构造 data = abi.encodeWithSignature("transfer(address,uint256)", to, amount) 并把 destination 指为
addr.call(abi.encodeWithSignature("transfer(address,uint256)", 0xSomeAddress, 123)) 上述代码 abi.encodeWithSignature contract, uint _num) public payable { (bool success, bytes memory data) = _contract.delegatecall( abi.encodeWithSignature