我有一个使用方法exchange的代理实现,还有一个带有以下代码的方法exchange2的委托程序:
function exchange2(int128 i, int128 j, uint256 dx, uint256 min_dy) public {
(bool success, bytes memory result) = delegationTarget.delegatecall(
abi.encodeWithSignature("exchange(int128,int128,uint256,uint256)", i, j, dx, min_dy)
);
if (!success) {
if (result.length > 0) {
revert(string(result));
} else {
revert();
}
}
// pay cashback上面这个简单的代码大约有2k的气体开销,下面是openzeppelin代理代码:
function _delegate(address implementation) internal {
// // solhint-disable-next-line no-inline-assembly
assembly {
// Copy msg.data. We take full control of memory in this inline assembly
// block because it will not return to Solidity code. We overwrite the
// Solidity scratch pad at memory position 0.
calldatacopy(0, 0, calldatasize())
// Call the implementation.
// out and outsize are 0 because we don't know the size yet.
let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)
// Copy the returned data.
returndatacopy(0, 0, returndatasize())
switch result
// delegatecall returns 0 on error.
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}
fallback () external {
_delegate(delegationTarget);
}oz代码的问题是,我不知道如何从代理的exchange方法将调用委托给exchange2实现。
如果我知道abi.encodeWithSignature调用,有什么方法可以优化signature of exchange(int128,int128,uint256,uint256) - 0x3df02124调用呢?每次函数调用时,传递与参数相同的字符串听起来很浪费。
发布于 2021-02-06 01:03:52
abi.encodePacked(bytes4(keccak256(signature)),abi.encode(参数.)是对abi.encodeWithSignature的逼近
您可以在合同中将0x3df02124保存为byte4变量,并使用上述代码的调整:
bytes4 functionSig = 0x3df02124
abi.encodePacked(functionSig, abi.encode(parameters...))https://ethereum.stackexchange.com/questions/93068
复制相似问题