【问题标题】:solidity - get return value of delegatecall with assemblysolidity - 使用程序集获取委托调用的返回值
【发布时间】:2017-08-29 04:07:19
【问题描述】:

我有一份合同 A 和一份合同 B。

合约 A 声明了这个函数:

function getIntValue() constant returns (uint);

对于来自 B 的 delegatecall 合约 A 的 getIntValue 函数,合适的汇编代码是什么?我对组装还不是很有经验,所以到目前为止我只有这个不起作用:

function getContractAIntValue() constant returns (uint c) {
    address addr = address(contractA); // contract A is stored in B.
    bytes4 sig = bytes4(sha3("getIntValue()")); // function signature

    assembly {
        let x := mload(0x40) // find empty storage location using "free memory pointer"
        mstore(x,sig) // attach function signature
        let status := delegatecall(sub(gas, 10000), addr, add(x, 0x04), 0, x, 0x20)
        jumpi(invalidJumpLabel, iszero(status)) // error out if unsuccessful delegatecall
        c := mload(x)
    }
}

【问题讨论】:

    标签: ethereum solidity


    【解决方案1】:

    也许你已经解决了它,因为一年多前就有人问过了,但万一有人还在寻找它......

    address addr = address(contractA); // contract A is stored in B.
    bytes memory sig = abi.encodeWithSignature("getIntValue()"); // function signature
    
    // solium-disable-next-line security/no-inline-assembly
    assembly {
      let result := delegatecall(sub(gas, 10000), addr, add(sig, 0x20), mload(sig), 0, 0)
    
      let size := returndatasize
    
      let ptr := mload(0x40)
      returndatacopy(ptr, 0, size)
    
      // revert instead of invalid() bc if the underlying call failed with invalid() it already wasted gas.
      // if the call returned error data, forward it
      switch result case 0 { revert(ptr, size) }
      default { return(ptr, size) }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多