I have a simple contract that has a setter and getter for a struct called Institution. I have removed most of the properties am using, to simplify the question.
pragma solidity ^0.4.22;
contract Example{
struct Institution {
bytes32 name;
bool exists;
}
event AddedInstitution(bytes32 name);
mapping(bytes32 => Institution) institutions;
//add an institution
function addInstitution(
bytes32 name,
bytes32 upi
) public {
require(!isInstitutionExists(upi));
institutions[upi].name = name;
institutions[upi].exists = true;
emit AddedInstitution(name);
}
//get an institution
function getInstitution(bytes32 upi) public constant returns (bytes32 name){
require(isInstitutionExists(upi));
return (
institutions[upi].name
);
}
//is institution exist
function isInstitutionExists(bytes32 upi) private constant returns (bool){
if (institutions[upi].exists) {
return true;
}
return false;
}
}
The setter works as expected. It adds a new institution and emits an event. But when I call the getter and give it a upi, it returns a location address of the name of the institution instead of giving me the exact name, as shown below:
'0x4d6f6920556e7669766572736974790000000000000000000000000000000000'
The results are consistent in both truffle console and browser.
What is wrong with my code?
stringfirst so whatever client you're using will do the conversion automatically. – user19510 Jun 21 '18 at 23:19stringinstead ofbytes32everywhere, or converting frombytes32tostringjust in your accessor function. But there's no reason to do this.bytes32is a fine way to store a string that has a maximum length of 32 bytes. You just need to convert it to ASCII before displaying it. – user19510 Jun 21 '18 at 23:26web3.toAsciiat front end level? – Sean Jun 21 '18 at 23:31bytes32everywhere and then convert it in the getter (see https://ethereum.stackexchange.com/a/31292/19510), but don't do that. It just wastes gas moving bytes from one place to another when you can just do it on the front-end for free. – user19510 Jun 21 '18 at 23:36web3.toAsciiand I get an error in browser that no such method. Usingweb3.utils.toAsciiworks, as indicated in this question. – Sean Jun 21 '18 at 23:52web3.toAsciiis for the current web3.js released version (0.2x.x). For the 1.0 beta versions,web3.utils.toAsciiis correct. – user19510 Jun 22 '18 at 00:10