0

I have created a simple contract with Truffle to test things out. I am using testrpc with this.

pragma solidity ^0.4.11;

contract HelloSystem {

    address owner;

    function HelloSystem() {
        owner = msg.sender;
    }

    function remove() {
        if (msg.sender == owner) {
            selfdestruct(owner);
        }
    }
}

After deploying this contract, though I can call remove from truffle console, I am not able to call the owner which is set at the initiation of contract.

Have tried:

truffle(development)> HelloSystem.deployed().then(function(instance){HS = HelloSystem.at(instance.address)})
HS.owner.call().then(console.log)
HS.owner
HS.owner.call()

I am very well able to call remove function on my contract and remove it. But owner is not working. I am sure I am missing something simple, if any one can point it out, would be much wow many thanks.

I created another simple contract with numeric uint balance and made it public.

contract helloWorld {    
    uint public balance;    

    function helloWorld() {
        balance = 1000;
    }
} 

Now, my call to balance works fine.

truffle(development)> helloWorld.deployed().then(function(instance){HW = helloWorld.at(instance.address)})
truffle(development)> HW.balance
{ [Function]
  call: [Function],
  sendTransaction: [Function],
  request: [Function: bound ],
  estimateGas: [Function] }
truffle(development)>

Would this be because of the declaration public/ private?

Update Inline with my argument of public declaration, I tried declaring address public owner still doesn't work. Hence open to your guys. Please help.

3

In your contract, variable owner is not public so HS.owner will be undefined.

To overcome this issue, you can either declare the owner variable as public or create a getter function to get the owner like below:

address public owner;

or

function getOwner() returns (address owner) {
    return owner;
}

As per my understanding, this should be constant return to avoid any gas usage. like below:

function getOwner() constant returns (address owner) {
    return owner;
}

Hope this helps..!

  • Yes it works. Weirdly, I did declare the address as public later when I realised that the reason you pointed out must be the reason. Somehow, possible because I didn't save the code, new change was not compiled :) – Manganese Jul 28 '17 at 5:21
  • 1
    probably file might not got saved or not compiled or not migrated. :) – Abhishek Jul 28 '17 at 5:23

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.