1

Working with the Parity client and web3 Javascript API on a private chain, I tested the following code:

pragma solidity ^0.4.7;

contract simple {
    function twice(int a) constant returns(int) {
        return 2*a;
    }
}

The above code was compiled and deployed with the following Javascript code:

var compiledSimple = web3.eth.compile.solidity(<source>);
var contractSimple = web3.eth.contract(<ABI>);

contractSimple.new( { from: <fromAddress>, data: compiledSimple, gas: "0x493e0" },
    function(err, contract) {
        if (err) {
            console.log('ERROR: ' + err);
            return;
        }   
        if (!contract.address) {
            console.log('Address to be assigned... txnHash: ', contract.transactionHash);
        }
        else {
            console.log("Simple contract DEPLOYED at address: " + contract.address);
        }
    }
);

Now, I could call the function twice(int), which returned the correct result i.e. the input integer doubled.

Then, I modified the code to the following:

pragma solidity ^0.4.7;

contract owned {
    address owner;
    function owned() {
        owner = msg.sender;
    }
    function getOwner() constant returns(address) {
        return owner;
    }
}

contract simple is owned {
    function twice(int a) constant returns(int) {
        return 2*a;
    }
}

After deploying this with the same Javascript code, I find that I can call getOwner() and get the correct <fromAddress> that was specified when contract was deployed. However, calling twice(int) always returns 0, whatever the input value may be. It looks as if only the base contract got deployed, and not the derived contract.

Here is the ABI specified when deploying the later code:

var ABI_SIMPLE = [ 
  {"constant":true,
   "inputs":[{"name":"a","type":"int256"}],
   "name":"twice",
   "outputs":[{"name":"","type":"int256"}],
   "payable":false,
   "type":"function"},
  {"constant":true,
   "inputs":[],
   "name":"getOwner",
   "outputs":[{"name":"","type":"address"}],
   "payable":false,
   "type":"function"}
];

Am I missing something here? Really appreciate any help.

EDIT: Code calling the contract

Here is the code that calls the contract function twice(int). It is part of a NodeJS application, and ABI_SIMPLE (given above) and ADDRESS_SIMPLE are defined as constants outside the code block below. HTTP request has header Content-Type: application/json:

var invokeSimple = function(req, res) {
    var inVal = parseInt(req.params.input);
    var simple = web3.eth.contract(ABI_SIMPLE).at(ADDRESS_SIMPLE);
    var result = simple.twice(inVal);
    res.status(200).send({result: result});
}
  • How do you call the contract? – Mikko Ohtamaa Mar 9 '17 at 20:57
  • Added Javascript code calling the contract function. – Ajoy Bhatia Mar 9 '17 at 21:49
  • Can you add a confirmation that your inVal is a correctly parsed integer. This code does not fully demonstrate what goes into the function, as it comes from an external source and the reader needs to guess. – Mikko Ohtamaa Mar 10 '17 at 15:38
  • Thanks for looking into this, @MikkoOhtamaa . I have a console.log statement after reading inVal, which prints out correct integer value. I had taken it out to get the minimal code. – Ajoy Bhatia Mar 10 '17 at 17:39
  • By presented all facts in this question, it should return the value 20. I suggest you deploy the contract on a public testnet so its testable there or try to add som other clues what's going on here. I suggest you try to publish on testnet so that the source code is verified on testnet.etherscan.io. – Mikko Ohtamaa Mar 10 '17 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.