1

I'm using solidity 0.5.0 and when i try to compile my contract it's giving me this error

Explicit type conversion not allowed from "bytes memory" to "address"

my code is this:

  /// @dev get broker address for endpoint
 function getEndpointBroker(address oracleAddress, bytes32 endpoint) public view returns (address) 
  {


return address(db.getBytes(keccak256(abi.encodePacked('oracles', oracleAddress, endpoint, 'broker'))));
}
1
  • 2
    what does db.getBytes(...) do?
    – Majd TL
    Jun 5, 2019 at 18:54

1 Answer 1

4

Omitted the function db.getBytes() to avoid compilation errors in remix. You can choose:

function getEndpointBroker(address oracleAddress, bytes32 endpoint) public view returns (address) {
        return address(uint160(uint256(keccak256(abi.encodePacked('oracles', oracleAddress, endpoint, 'broker')))));
    }

Or:

function getEndpointBroker(address oracleAddress, bytes32 endpoint) public view returns (address) {
        return address(uint160(bytes20(keccak256(abi.encodePacked('oracles', oracleAddress, endpoint, 'broker')))));
    }

Edit: Solidity doc.

If you convert a type that uses a larger byte size to an address, for example bytes32, then the address is truncated. To reduce conversion ambiguity version 0.4.24 and higher of the compiler force you make the truncation explicit in the conversion. Take for example the address 0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFFCCCC.

You can use address(uint160(bytes20(b))), which results in 0x111122223333444455556666777788889999aAaa, or you can use address(uint160(uint256(b))), which results in 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.