1

I'm using the Embark-Framework to handle my DApp development. I made a new contract that has the public address owner. Example below:

pragma solidity ^0.4.7;
contract MyContract {

  address public owner = msg.sender ;
  uint public creationTime = now;

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

}

On the front-end side, I type:

var c = web3.eth.contract(MyContract.abi).at(MyContract.address)
c.getOwner()

And the output is a constantly changing string... Meaning the owner is constantly changing. Any ideas why?

I can do the same with using the promise() formulation of embark (i.e., running MyContract.getOwner().then(...). Still, value keeps changing.

EDIT: Screenshots provided:

enter image description here enter image description here enter image description here

| improve this question | | | | |
3

I'm guessing it's because getOwner() is not marked as constant. So what you'd be seeing is a transaction hash, not the owner. If I'm right, you'll be able to see that what it's giving you is actually 32 bytes of hexidecimal, not 20 (for an address).

But there's an even simpler way. Since you've made owner public, solidity has automatically made a getter for you! You can just call c.owner() to get the owner.

| improve this answer | | | | |
  • thanks, you were right on both accounts, well done! Declaring the function constant worked with getOwner(), and calling c.owner() also worked. Thanks so much. – David Mar 21 '17 at 1:47
  • One more question @MatthewSchmidt: why do you need to mark it as constant? When can you return a non constant value? If you return anything other than the tx hash, can it be not a constant? – David Mar 21 '17 at 12:42
  • 1
    constant means that it should not effect the state of the contract, so it can (reasonably) safely be simulated by the node without calling it. Otherwise, the node will actually send a transaction, which is why it will return a transaction hash (since it isn't instant.) – Matthew Schmidt Mar 21 '17 at 14:30
0

try to change the name of the return value :

pragma solidity ^0.4.7;
contract MyContract {

  address public owner = msg.sender ;
  uint public creationTime = now;

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

}

enter image description here

| improve this answer | | | | |
  • nope, doesn't fix it :( – David Mar 20 '17 at 21:30
  • Is it possible that it's something that has to do with the embark-framework? I can post a screenshot too if that's helpful... – David Mar 20 '17 at 21:53
  • posted a screenshot – David Mar 20 '17 at 22:01
  • Just following up on this, @BadrBellaj, any ideas? – David Mar 21 '17 at 1:11

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.