1

I am simply trying to call my contracts on a private blockchain from a nodeJS app after a call from the browser but I keep getting this error in my node:

2017-03-01T21:07:34.782560+00:00 app[web.1]: TypeError: MyContract is not a constructor
2017-03-01T21:07:34.782562+00:00 app[web.1]:     at /app/app.js:77:30
2017-03-01T21:07:34.782563+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2017-03-01T21:07:34.782563+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/route.js:131:13)
2017-03-01T21:07:34.782567+00:00 app[web.1]:     at module.exports (/app/node_modules/express-stormpath/lib/middleware/authentication-required.js:25:12)
2017-03-01T21:07:34.782567+00:00 app[web.1]:     at /app/node_modules/express-stormpath/lib/middleware/get-user.js:154:11
2017-03-01T21:07:34.782568+00:00 app[web.1]:     at /app/node_modules/express-stormpath/lib/helpers/expand-account.js:145:5
2017-03-01T21:07:34.782568+00:00 app[web.1]:     at /app/node_modules/async/dist/async.js:3694:9
2017-03-01T21:07:34.782569+00:00 app[web.1]:     at /app/node_modules/async/dist/async.js:359:16
2017-03-01T21:07:34.782569+00:00 app[web.1]:     at iteratorCallback (/app/node_modules/async/dist/async.js:935:13)
2017-03-01T21:07:34.782570+00:00 app[web.1]:     at /app/node_modules/async/dist/async.js:843:16

Here is how i call my contract, what am I doing wrong ?

app.post('/api/getOrganisationDetails', stormpath.authenticationRequired, function(req, res){
    var rawString = req.user.customData;
    var stringJson = JSON.stringify(rawString);
    var customData = JSON.parse(stringJson);
    var web3 = new Web3();

    web3.setProvider(new web3.providers.HttpProvider(customData.ethereum_provider));
    var coinbase = web3.eth.coinbase;
    var balance = web3.eth.getBalance(coinbase);

    var name = req.body.name;
    var ABI = [{"constant":false,"inputs":[],
            "name":"get",
            "outputs":[{"name":"","type":"int8"}],
            "type":"function"}];

    // creation of contract object
    var MyContract = web3.eth.contract(ABI);
    var myContractInstance = new MyContract('0x90eb73c0bb07b0a5428b3d3ac2a5b7461bdd038b');
    var result = myContractInstance.get(name);
    console.log(result);
    res.status(200);
    res.end("hello world");
});

1 Answer 1

1

Instead of:

var myContractInstance =
    new MyContract('0x90eb73c0bb07b0a5428b3d3ac2a5b7461bdd038b');

you should have

var myContractInstance =
    MyContract.new({from:<fromAddr>, data:<compiledContractCode>, gas:<value>});

For example:

var myContractInstance =
    MyContract.new({from:'0xbc4773626fed4d763e5d02e3aaef85be6ec67994',
                    data:'0x... <compiledCode in hex> ...', gas:1000000});

Also, a simple way to get the ABI and compiled code for the above call is as follows (where MyContractSource is your Solidity code as a string, and the field name under MyContractCompiled - which has info and code sub-fields is the name of your contract in the Solidity source):

var MyContractCompiled = web3.eth.compile.solidity(MyContractSource);
var MyContract = web3.eth.contract(MyContractCompiled.MyContract.info.abiDefinition);
var myContractInstance =
    MyContract.new({from:'0xbc4773626fed4d763e5d02e3aaef85be6ec67994',
           data:MyContractCompiled.MyContract.code, gas:1000000});

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.