Calling deployed smart contract from node js

+1 vote

Hi. I got this code from git. I have deployed it on my test network. And I want to use a server to call the function in the contract. 

I use truffle serve and a webpage to new the contract. My code is as follows:

app.js

import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
import payontime_artifacts from '../../build/contracts/payontime.json'
var payontime = contract(payontime_artifacts);

window.App = {
    sendCoin : function(){

        var sender = web3.eth.accounts[0];
        var receiver = document.getElementById('receiver').value;
        var amount =         parseInt(document.getElementById('amount').value);
    web3.eth.getBalance(receiver,function(error,result){
        if(!error){
            consol.log("Before transfer: " + result );
        }else{
            console.log("Error: " + error);
        }
    });

    var newContract = payontime.new(receiver,{from:sender, value:amount}).then(
        function(myPay){
            console.log(myPay.getContractAddr.call());
        }).then(
        function(){
            web3.eth.getBalance(receiver,function(error,result){
                if(!error){
                    console.log("After transfer: " + result );
                }else{
                    console.log("Error: " + error);
                }
            });
        });
    }
}

window.addEventListener('load', function() {
  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
    // Use Mist/MetaMask's provider
    window.web3 = new Web3(web3.currentProvider);
  } else {
    console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development");
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  }
  payontime.setProvider(web3.currentProvider);
});

I want to use the app.js log address to call the wakeUp() in the payontime.sol through another application index.js.

const Web3 = require('web3');

/* Connect to ethereum node */
const etherUrl = "http://localhost:8545";
const abi = [{"constant":false,"inputs":[],"name":"wakeUp","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"getContractAddr","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"remitter","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"start","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"getRemitee","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"inputs":[{"name":"receiver","type":"address"}],"payable":true,"type":"constructor"}];

let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(etherUrl));


/*Call the function which already deployed on ethereum network
  Notice: ABI have to modifeid when the smart contract code change*/
var contractInstance = web3.eth.contract(abi).at('0x1d379f2ab48ad20319e9f81cb45af415aa6f2966');
var reply = "false";
reply = contractInstance.wakeUp.call(function(error,result){
    if(error){
        console.log("Error");
        throw error;
    }else{
        return result;
    }
});
console.log(reply);

But I am getting this error: 

BigNumber Error: new BigNumber() not a base 16 number
Sep 26, 2018 in Blockchain by slayer
• 29,370 points
4,000 views

1 answer to this question.

+1 vote
Best answer

contractInstance.wakeUp.call is calling the function as constant, but the function is not defined as constant:

function wakeUp() public returns (string){
    return "success" ; 
}

has to be:

function wakeUp() public constant returns (string){
    return "success" ; 
}
answered Sep 26, 2018 by digger
• 26,740 points

selected May 6, 2019 by Omkar

Related Questions In Blockchain

0 votes
1 answer

How to create tokens in smart contracts from node.js?

Any modification to the Ethereum Blockchain will ...READ MORE

answered Jun 27, 2018 in Blockchain by Shashank
• 10,400 points
1,668 views
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 6, 2018 in Blockchain by aryya
• 7,460 points
808 views
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,100 points
713 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
1,415 views
0 votes
1 answer

How connect library to smart contract from external resources?

pragma solidity ^0.4.0; import "github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol"; contract MathExtended { ...READ MORE

answered Sep 26, 2018 in Blockchain by digger
• 26,740 points
593 views
0 votes
1 answer

How do I send ether from an EOA to a smart contract?

You include ether to send in the ...READ MORE

answered Sep 27, 2018 in Blockchain by digger
• 26,740 points
1,575 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP