0

Problem: All method calls to my deployed contract return null

Versions:

Geth: 1.9.9-unstable Git Commit: c2d65d34d5c6f27b8d1a52280964023a3eefd66e

solc: Version: 0.5.13+commit.5b0b510c.Linux.g++

web3: [email protected]

An ERC20 contract takes four arguments in the constructor. My constructor is defined like this:

  string public name;
  string public symbol;
  address public admin; // For debugging
  uint8 public decimals;
  uint256 public totalSupply;
  event Deployed(address indexed _admin, uint256 indexed _totalSupply, string _name); // For debugging

  constructor(string memory _tokenName, string memory _tokenSymbol, uint8 _decimals, uint256 _initialSupply) public {
    name = _tokenName;
    symbol = _tokenSymbol;
    decimals = _decimals;
    totalSupply = _initialSupply;

    admin = msg.sender; // For debugging
    balanceOf[msg.sender] = totalSupply;

    emit Deployed(msg.sender, totalSupply, _tokenName); // For debugging
  }

I use this JavaScript code to deploy an ERC20 contract to my private test net.

var contractObject = web3.eth.contract(<ABI definition>);
var submittedContract = contractObject.new('DKK', 'DKK', 1, 1000000,  {data:'<bin>', gas: 20000000}, <callback>);

Calling eth.getTransactionReceipt(submittedContract.transactionHash); returns (only relevant fields included):

eth.getTransactionReceipt("0xeb512f29ef2789b592b19acbbb78f8c6be6f7eaed96460c239e0a5d8c20e1dd2");
{
  contractAddress: "0xe23cfc0248b83a4bce8f9701a86e08e9cf9c59f4",
  ...
  logs: [{
      ...
      data: "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003444b4b0000000000000000000000000000000000000000000000000000000000",
      ...
      topics: ["0xf0bbe877a87290e236ea898bbce0d78d6ac7d54d62e32744b129798a2669e39b", "0x00000000000000000000000075bf9b4280b7963ddb1e9198b3aca5b1e02dcf57", "0x00000000000000000000000000000000000000000000000000000000000f4240"],
      ...
  }],
  status: "0x1",
...
}

The status field should indicate that there were no errors, but I am not sure that my constructor values are set correctly. Can you see that from the topics field?

When I then call contractObject.at("0xe23cfc0248b83a4bce8f9701a86e08e9cf9c59f4").totalSupply();, it returns 0. But it should return 1000000.

6
  • 1
    You're missing await in that last line of code (or some other means of handling the Promise object returned from the call to totalSupply). Commented Dec 3, 2019 at 21:37
  • 1
    And yes, I can see it in your topics: topics[2] == 0xf4240 == 1000000. Commented Dec 3, 2019 at 21:38
  • 1
    BTW, your code implies that you're still on web3.js v0.x. It might be a good time to switch to v1.x (I'd recommend v1.2.1). Just beware that there are several API-breaking changes. Commented Dec 3, 2019 at 21:40
  • Yeah. I built this in 2017 and last looked at it in 2018. dep.admin().then(console.log); gives me TypeError: 'then' is not a function at <anonymous>:1:1 and await dep.admin(); (anonymous): Line 1:7 Unexpected identifier (and 1 more errors) Commented Dec 3, 2019 at 22:18
  • But it seems that the constructor is called correctly, right? So that just means that I am making a mistake when I call the methods on the contract. Commented Dec 3, 2019 at 22:28

1 Answer 1

1

Following my comments above, try this:

async function test() {
    var contractObject = web3.eth.contract(...);
    var submittedContract = await contractObject.new(...);
    var totalSupply = await contractObject.at(...).totalSupply();
    console.log(totalSupply);
}

test();
16
  • When I call await in front of anything I get "unexpected identifier". Calling web3.version from the geth terminal returns 'api: "0.20.1"'. Should or can I upgrade web3js? Commented Dec 4, 2019 at 12:31
  • From what I read here on Stack Exchange (old answer though) that is not possible: ethereum.stackexchange.com/a/1844/6106 Commented Dec 4, 2019 at 12:37
  • 1
    @ThorkilVærge: You can only do that from inside an async function, as shown in the code above. Please, to start with, try this example exactly as is (i.e., place it in a separate file and execute that file via node from a command line). Commented Dec 4, 2019 at 12:52
  • Got it. I am trying this with [email protected] now. But I don't know how to get the contract address which goes into the at function. address is undefined on the submittedContract object. Commented Dec 4, 2019 at 18:48
  • Adding a sleep(60s) such that the tx was mined and following your recipe above totalSupply still returns 0 and admin returns "0x". Commented Dec 4, 2019 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.