2

How to make API for Ethereum smart contract in Node.js? Can anyone send good examples? Is it even a standard way to make API in Node.js so it can interact with any interface because most examples I can find function from front-end only? I have made the code with web3, shows expected output but doesnt commit anything to a new block like it does with truffle. My code-

var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log("Talking with a geth server", web3.version.api);

function test(){
  var sourceCode = fs.readFileSync('contracts/digitalToken.sol','utf8')
  var compiled = solc.compile(sourceCode);
  console.log(compiled.contracts[":digitalToken"]);
  var abiArray = compiled.contracts[":digitalToken"].interface;
  abiArray = JSON.parse(abiArray);
  var MyContract = web3.eth.contract(abiArray);
  var address = "0x1f81b5fa31d049b70556628cc01f22d362c41084";
  var contractInstance = MyContract.at(address);
  console.log(contractInstance);
  var value=executeAddvid(contractInstance)
      console.log({message: "Success",value:value});
      console.log('after passing--');
      console.log({message: "Success",value:value});
  var value1=readVidByUser(contractInstance);
    console.log({message: "Success",value:value1});
      console.log('after passing--');
      console.log({message: "Success",value:value1});


}

function executeAddvid(contractInstance){
  record = contractInstance;
  return record.addVid.call('0x0000000000000000000000000000000000000124',5,"ronaldo","0xc9f5a0572d95d5dea1331edcc192dacef51ecbb7",1000000000000000000);
}

function readVidByUser(contractInstance){
  record = contractInstance;
  return record.readVidByUser.call('0x0000000000000000000000000000000000000124','0x1f81b5fa31d049b70556628cc01f22d362c41084');
}


test();
5

We have created APIs in node.js very recently. You can take a look at this repo for reference -

https://github.com/Imaginea/lms/blob/master/server/routes.js

The UI talks to node.js middleware and then middleware talks to Ethereum node.

Basically, you would have to use the Web3JS library to implement it, please check the code for more details.

Hope this helps.

  • Hey I looked at your code thanks for that, its pretty much what Im doing...I'm using web3 module and my smart-contract transactions are executing and returning expected output but the problem is that when i query them back or do further function executions on the basis of previous ones, it acts like previous execution of functionality never took place. what could be the reason..its like the transactions aren't commiting in blockchain. – Shubhabrata Mukherjee Jul 3 '17 at 14:16
  • 1
    Are you deploying the smart contract with the help of truffle or deploying the contract completely using web3? If you see the code which I shared, we have not deployed the contract using web3 but deployed with truffle and saved the deployed contract address in some file. Are you doing the same way? – Sanchit Jul 3 '17 at 14:22
  • DId same as you, deployed separately and then use the resulted contract address as hardcoded value in node app. – Shubhabrata Mukherjee Jul 3 '17 at 14:24
  • Then I can comment further on the issue, If I look into the code. Would be great if you can update the question with some part of your code. – Sanchit Jul 3 '17 at 14:27
  • These are my logs for test-rpc, when I did executions through truffle it gave an extra block in result with transaction hash. But when I do the same with node it just gives me these calls as log but doesnt make a new block. – Shubhabrata Mukherjee Jul 3 '17 at 14:28
0

Check out the web3 npm module. Everything is there.

  • I'm using web3 module and my smart-contract transactions are executing and returning expected output but the problem is that when i query them back or do further function executions on the basis of previous ones, it acts like previous execution of functionality never took place. what could be the reason..its like the transactions aren't commiting in blockchain. – Shubhabrata Mukherjee Jul 3 '17 at 13:58
  • Sounds like a problem with your test chain. What are you using for that? – Ninja Dragon Jul 3 '17 at 14:03
  • We're using test-rpc network, we've checked the smart-contract through truffle. Here is the funny bit, if I do some execution through truffle it commits it, and I can see the changes reflect in Nodejs app. But I cant do any functionality invoking through nodejs itself. It gives expected output but doesnt commit. – Shubhabrata Mukherjee Jul 3 '17 at 14:14
0

The problem was that I added the .call functionality for invoking every smartcontract function when its only needed for constant function that are only used to read values. Thanks for the support.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.