1

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'));

  • Try address(uint256(sha256(msg.sender,'block.timestamp'))); – goodvibration Feb 12 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. – Premnath Reddyvari Feb 12 at 11:47
  • i also tried address(sha256(abi.encodePacked(msg.sender,'block.timestamp'))); – Premnath Reddyvari Feb 12 at 11:49
  • Try keccak256 instead of sha256. – goodvibration Feb 12 at 12:13
  • no even this didn't help – Premnath Reddyvari Feb 12 at 13:23
0
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))));
| improve this answer | |
  • 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. – Premnath Reddyvari Feb 13 at 10:40
  • Just remove quotes around block.timestamp. – Mikhail Vladimirov Feb 13 at 10:45
  • tried it didn't work – Premnath Reddyvari Feb 14 at 5:31
  • How packagemapping is defined? – Mikhail Vladimirov Feb 14 at 5:52
  • Thanks it worked but showing warning , checked with warning where it can be ignored . Doesn't effect compiling in smartcontract – Premnath Reddyvari Feb 14 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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