0
votes

How can I interact with my smart contract from node? Im using truffle to deploy my contract to my private chain. But I dont get an instance of the deployed contract in my nodejs code.

I have a small example contract that looks like this

pragma solidity ^0.5.1;
contract MyContract {
 uint num;
 function someFunction(uint _num) public {
   num = _num;
 }
}

Then I compile and migrate the contract to my private chain (ganache-cli)

>truffle compile
...
>truffle migrate
...

2_deploy_contracts.js
=====================

   Deploying 'MyContract'
   ----------------------
   > transaction hash:    0xa9161613e7c398c5425b3bb7c306d494a657193c965203902c5732192b394979
   > Blocks: 0            Seconds: 0
   > contract address:    0x93Da9d36ECcd5eeceBe9b469A65cBbA397b6c85E
   > account:             0x9576316A79287D03c92F9157056e5BCde1cAEc5C
   > balance:             99.99152546
   > gas used:            98463
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00196926 ETH

MyContract Address: 0x93Da9d36ECcd5eeceBe9b469A65cBbA397b6c85E

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00196926 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.00763398 ETH
>

So long everything seems fine. But then I want to interact with my contract on my private chain. So Im using this code:

const path = require('path');
var MyContractABI = require(path.join(__dirname, '../build/contracts/MyContract'))
var Web3 = require('web3');
var provider = new Web3.providers.HttpProvider("http://localhost:8545");
var contract = require("truffle-contract");

var MyContract = contract(MyContractABI);
MyContract.setProvider(provider);

var deployed;
MyContract.deployed().then(function(instance) {
  var deployed = instance;
  return instance.someFunction(5);
}).then(function(result) {
  // Do something with the result or continue with more transactions.
  console.log(result);
});

But this is when it all falls apart. MyContractABI contains the contracts jsons interface. But I get an "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'apply' of undefined". It fails on MyContract.deployed().

Node version: v8.11.3 truffle version: v5.0.0-beta.2

code of example here: https://github.com/manmountain/truffle-example

The full stack trace

(node:5888) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'apply' of undefined at Provider.sendAsync (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:24:36) at RequestManager.sendAsync (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\node_modules\web3\lib\web3\requestmanager.js:80:19) at Object.get [as getNetwork] (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\node_modules\web3\lib\web3\property.js:116:33) at C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:512:27 at new Promise () at Function.detectNetwork (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:503:14) at Function.deployed (C:\Users\goran\Documents\development\truffle-example\node_modules\truffle-contract\contract.js:451:19) at Object. (C:\Users\goran\Documents\development\truffle-example\example\example.js:11:12) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) at startup (bootstrap_node.js:191:16) at bootstrap_node.js:612:3 (node:5888) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:5888) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1

1 Answers

0
votes

As this is your private network you said you need to specify what network is in truffle console —network testnet (give the network name that you have in the truffle.config file) The you should be able to get into the truffle shell corresponding to your network. By default truffle console will take the local network I.e., development default network.