ethereum - Call deployed smart contract from node.js -


i have small test smart contract, deployed test network. , want use server call function in contract. here code: payontime.sol

pragma solidity ^0.4.0;  contract payontime{   address public remitter;   address private remittee;   uint value;   bool public start;    /*only owner can use these function*/   modifier onlyowner(){     if(msg.sender != remitter) throw;     _;   }    /*initialize owner*/   function payontime(address receiver) payable{     remitter = msg.sender;     value = msg.value;     remittee = receiver;     start = true;     if(!remittee.send(value)){         throw;     }   }    function wakeup() public returns (string){     return "success" ;    }    function getcontractaddr() public returns(address){     return this;   }    /*get remittee*/   function getremitee() public returns(address){     return remittee;   } } 

i use truffle serve , webpage new contract.

app.js

import { default web3} 'web3'; import { default contract } 'truffle-contract' import payontime_artifacts '../../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 browser (mist/metamask)   if (typeof web3 !== 'undefined') {     console.warn("using web3 detected external source. if find accounts don't appear or have 0 metacoin, ensure you've configured source properly. if using metamask, see following link. feel free delete 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 http://localhost:8545. should remove fallback when deploy live, it's inherently insecure. consider switching metamask development. more info here: http://truffleframework.com/tutorials/truffle-and-metamask");     // fallback - use 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); }); 

app.js logs address 0x1d379f2ab48ad20319e9f81cb45af415aa6f2966 , , want use address call wakeup() in payontime.sol through application index.js.

const web3 = require('web3');  /* connect 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 function deployed on ethereum network   notice: abi have modifeid when 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 there error message: bignumber error: new bignumber() not base 16 number found might caused not fully synced. think have problem when call function in deployed contract. how can call deployed contract web.js?

contractinstance.wakeup.call calling function constant, function not defined constant:

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

has be:

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

if solidity function doesn't change blockchain state , reading data, should define constant.


Comments