0

Having a weird issue with Solidity and web3.js 1.0. Any help would be much appreciated.

Here's my Solidity code:

pragma solidity ^0.4.16;

contract Test {

    address public contractOwner;

    function TestContract() public {
        contractOwner = msg.sender;
    }

    function foo() constant returns(address) {
        return msg.sender;
    }

}

However calling the automatically-generated getter method for the address public contractOwner produces an unexpected result.

> var deployed;
undefined
> var contract = new web3.eth.Contract(abi)
undefined
> accounts[0]
'0x78924d231848a2d37781e42A2Fe30737F19b6c5E'
> contract.deploy({data: bytecode}).send({
... from: accounts[0],
... gas: 3000000
... }).then((_deployed) => { deployed = _deployed })
Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }
> deployed.setProvider(provider)
true
> deployed.methods.foo().call().then(console.log)
Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }
> 0x78924d231848a2d37781e02A2Fe30v37F19b6c5E
> deployed.methods.contractOwner().call().then(console.log)
Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }
> 0x0000000000000000000000000000000000000000

As you can see, foo() works perfectly, but contractOwner() returns an empty address. Should it not be 0x78924d231848a2d37781e42A2Fe30737F19b6c5E, which is what's passed as the from attribute during deployment?

3

The issue is the mismatch between the contract name, Test, and the name of what was probably meant to be your constructor, TestContract.

Because TestContract was never executed, owner was never assigned a value, so it retains its zero value (address(0)). Rename either the contract or the function so it works as a constructor.

  • Deleted my answer because your's correct. – Florian Castelain Jan 10 '18 at 8:52

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.