0

This is a sequence of another question I asked.

I have the following contract

contract User {
    string publicKey;

    function setPublicKey(string _publicKey)
        onlyOwner
    {
        publicKey = _publicKey;
    }

    function getPublicKey() returns (string _publicKey) {
        return publicKey;
    }

}

I have set the string to be

5030480389847504049639477069961901518251822532055915812015454717740961852942027334498786032153069913846973579859702642411497486328669598344496363306620763652362226170692287240855050713537819594000868803228735504934019483458523068727831434237837325563417285328158635879852481735301070609988900677951635437637299660610442447448957534545817

I called the setPublicKey() method with the string value and got back the transaction hash 0x789894f0b5a85cc45e317cb51af27a4d28858e3b64c64ee00118f4543d7a5de7 (it's a private chain).

I then called getPublicKey() but just got back an empty reply. Why does this happen?

This seems to be pretty basic but I've googled and checked here and didn't find any information that could answer it.

EDIT: Adding getPublicKey() function

// Example calling function
function getPublicKey(publicAddress, contractAddress, contractName) {
    // Get the contract code from contracts
    const input = fs.readFileSync('contracts/' + contractName + '.sol').toString();
    const output = solc.compile(input);
    // The trailing ':' is needed otherwise it crashes
    const bytecode = output.contracts[':' + contractName].bytecode;
    const abi = JSON.parse(output.contracts[':' + contractName].interface);
    const contract = web3.eth.contract(abi).at(contractAddress);
    contract.getPublicKey.call((err, res) => {
        if (err) {
            console.log(err);
        } else {
            console.log("Public Key is ");
            console.log(res);
        }
    })
}

It's a function in my node app.

  • did you increase the gas value? and can you also share how you are calling getPublicKey() function. – Sanchit Aug 11 '17 at 13:25
  • Haven't increased the gas value yet and would like to avoid it if possible. Just added the code for the function, cheers! – mcansado Aug 11 '17 at 13:30
  • You can also try to save smaller string value and after that, if getPublicKey() works then it would indicate that it's a problem related to gas value. – Sanchit Aug 11 '17 at 13:36
  • Maybe you don't mined~ – BinGoBinBin Aug 11 '17 at 13:37
  • @Sanchit will try that and report back. And I mined, have made that mistake in the past :) – mcansado Aug 11 '17 at 13:42
2

Sorry, I did not check your code properly earlier, there are some small changes required.

Please check below code, it works for me.

pragma solidity ^0.4.8;

    contract MyTest {

        string public publicKey;

        function setPublicKey(string _publicKey)
        {
            publicKey = _publicKey;
        }

        function getPublicKey() constant returns (string _publicKey) {
            return publicKey;
        }
    }

The getter function has to be constant to return value. That's the main mistake.

Hope this helps.

  • You were right, I had to increase the gas value. Sorry for the stubbornness there! Increasing the gas + this code solved the problem. Thanks! – mcansado Aug 11 '17 at 16:24
0

This works for me. Just delete the _publicKey in the returns string.

contract User {
    string publicKey;

    function setPublicKey(string _publicKey)
    {
        publicKey = _publicKey;
    }

    function getPublicKey() returns (string) {
        return publicKey;
    }

 }
  • This works if I try to set the string to some small ones but if I try to set it to 5030480389847504049639477069961901518251822532055915812015454717740961852942027334498786032153069913846973579859702642411497486328669598344496363306620763652362226170692287240855050713537819594000868803228735504934019483458523068727831434237837325563417285328158635879852481735301070609988900677951635437637299660610442447448957534545817, it just returns an empty response. – mcansado Aug 11 '17 at 15:24

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.