A function in my contract when called on the truffle develop command line gives output in BigNumber format. Here is what I wrote on the command line:
instance = contract.at('deployed address')
instance.getAge()
I tried instance.getAge().toNumber() but that throws an error saying that toNumber isn't defined. I checked How to convert BigNumber to Number in Truffle framework? however that doesnt seem to work on command line.
Similary outputs from functions returning bytes32 are in hex.I checked web3 return bytes32 string but web3.utils.toAscii(x) {x is any variable} throws an error
"Cannot read property toAscii of undefined"
I read the stack related to that and that also didn't help.
instance.getname().then(name => web3.toAscii(name))
?
– Rahul Kothari Jan 18 '18 at 11:28getName()declared with eitherconstant,view, orpureafter the function name as suggested above? If it isn't then consider adding it. If you can't add it for some reason useinstance.getname.call()instead. – willjgriff Jan 18 '18 at 12:57instance.getname().then(name => web3.toAscii(name)). It did work, however it gave these strings of\u0000padded after the name. Anyway to remove that? – Rahul Kothari Jan 18 '18 at 15:46instancefrom? And does the function in Solidity return a string, eg it looks similar to thisfunction getName() constant returns (string)? – willjgriff Jan 18 '18 at 15:58contract MyFirst {
bytes32 name;
function set(bytes32 newName) public { name = newName; }
function get() public constant returns (bytes32){ return name; } }`
I use bytes32 not string, because solidity doesnt function well with strings. It throws weird errors.
– Rahul Kothari Jan 18 '18 at 16:33bytes32is a statically sized variable, so you won't be able to put a string into it that is larger than 32 bytes.stringis a dynamically sized variable so you can put as large a string as you want in it. Unfortunately there are currently restrictions on passing dynamically sized variables, such as strings, between contracts and maybe between storage and memory which are probably the source of the weird errors. However, some of these limitations should be fixed in the next few months so hopefully you'll get less weird errors when using strings. – willjgriff Jan 18 '18 at 17:07So this function getname - it should return bytes32. However, it instead returns hex. So I did
– Rahul Kothari Jan 18 '18 at 19:17instance.getname().then(name => web3.toAscii(name)). Now that works fine, except for a bit irritation coming from the paddedu0000. Do you know a way to perhaps convert this hex output back to bytes32 without theseu0000?hexValue.replace(/0+$/, "")so all of it together could look like thisinstance.getname().then(hexValue => hexValue.replace(/0+$/, "")).then(hexNoTrailingZeros => web3.toAscii(hexNoTrailingZeros))– willjgriff Jan 18 '18 at 22:42