3

I am using solidity 0.5.0 when i compile code i am throwing exception as Explicit type conversion not allowed from "bytes32" to "address".

code is working fine in remix IDE

enter image description here

and my code is as

function orderItem(uint _itemid,string memory _itemname ) public returns(address){
    address uniqueId = address(sha256(msg.sender,'block.timestamp'));
    packagemapping[uniqueId].isuidgenerated = true;
    packagemapping[uniqueId].itemid = _itemid;
    packagemapping[uniqueId].itemname = _itemname;
    packagemapping[uniqueId].transactionstatus = " Your package is ordered and is under processing ";
    packagemapping[uniqueId].orderstatus = 1;
    packagemapping[uniqueId].customer = msg.sender;
    packagemapping[uniqueId].ordertime = "block.timestamp";
    return uniqueId;
}

error line: address uniqueId = address(sha256(msg.sender,'block.timestamp'));

6
  • Try address(uint256(sha256(msg.sender,'block.timestamp'))); Feb 12, 2020 at 11:44
  • Wrong argument count for function call: 2 arguments given but expected 1. This function requires a single bytes argument. Use abi.encodePacked(...) to obtain the pre-0.5.0 behaviour or abi.encode(...) to use ABI encoding. Feb 12, 2020 at 11:47
  • i also tried address(sha256(abi.encodePacked(msg.sender,'block.timestamp'))); Feb 12, 2020 at 11:49
  • Try keccak256 instead of sha256. Feb 12, 2020 at 12:13
  • no even this didn't help Feb 12, 2020 at 13:23

1 Answer 1

5
address uniqueId = address(bytes20(sha256(abi.encodePacked(msg.sender,'block.timestamp'))));

Also, why 'block.timestamp' is in quotes? Maybe it should be:

address uniqueId = address(bytes20(sha256(abi.encodePacked(msg.sender,block.timestamp))));
5
  • Follow up question after using above solution , packagemapping[uniqueId].ordertime = 'block.timestamp'; IDE is showing error at block.timestamp as Type literal_string "block.timestamp" is not implicitly convertible to expected type uint256. Feb 13, 2020 at 10:40
  • Just remove quotes around block.timestamp. Feb 13, 2020 at 10:45
  • tried it didn't work Feb 14, 2020 at 5:31
  • How packagemapping is defined? Feb 14, 2020 at 5:52
  • Thanks it worked but showing warning , checked with warning where it can be ignored . Doesn't effect compiling in smartcontract Feb 14, 2020 at 6:01

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.