I have been working with Interacting a smart contract deployed on the local node. I was using `Web3.providers.HttpProvider` until i had to interact with events, as i have understood, i needed to use `Web3.providers.IpcProvider`. After transitioning to IpcProvider. I am receiving following error.
/home/lubuntu/Desktop/Test/node_modules/oboe/dist/oboe-node.js:2325
throw new Error(e.message);
^
Error: Unexpected token o in JSON at position 1
at Timeout._onTimeout (/home/lubuntu/Desktop/Test/node_modules/oboe/dist/oboe-node.js:2325:17)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
Following is how i have written code for interaction with geth / local node.
const express=require('express');
const net = require('net');
const Web3=require('web3');
let app=express();
const PORT=3000;
let web3=new Web3(new Web3.providers.IpcProvider('/home/lubuntu/Project/data/geth.ipc',net));
var abi=[ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "frozenAccount", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "target", "type": "address" }, { "name": "freeze", "type": "bool" } ], "name": "freezeAccount", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256" }, { "name": "tokenName", "type": "string" }, { "name": "tokenSymbol", "type": "string" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "name": "target", "type": "address" }, { "indexed": false, "name": "frozen", "type": "bool" } ], "name": "FrozenFunds", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ];
const contract = new web3.eth.Contract(abi, '0x6621b0431ed6f533575380f2ba4788f44b5049b6');
app.get('/', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});
app.get('/blockchain', function(req,res){
contract.methods.transferFrom("0x029285ee26b9da62f138133a9fe40cb8c2219c82","0xe149cb26b99f176367e32c4ce33bc3bcb455cec6","10000").send({from:'0x029285ee26b9da62f138133a9fe40cb8c2219c82'}, (error, result) => {
if(result.status=='0x1')
console.log("Success")
else if(result.status=='0x0')
console.log("Failed");
});
//Event Test
contract.once('Transfer', {
filter: {from: '0x029285ee26b9da62f138133a9fe40cb8c2219c82', to: '0xe149cb26b99f176367e32c4ce33bc3bcb455cec6'},
fromBlock: 'latest'
}, (error, event) => { console.log(event); });
});
app.get('/blockchain2', function(req,res){
//Working
contract.methods.balanceOf("0xe149cb26b99f176367e32c4ce33bc3bcb455cec6").call({from: '0x029285ee26b9da62f138133a9fe40cb8c2219c82'}, (error, result) => {
console.log(result)
});
//Testing Account create
//Address can be assigned to function calling it or added to database against specific users
//web3.eth.personal.newAccount('abc',function(error,address){
//if(!error){res.send(address);}});
});
app.get('/blockchain3',function(req,res){
var f_account=req.query.id.toLowerCase();
var s_account="alert";
if(f_account=="true")
s_account="true"
else if(f_account=="false")
s_account="";
if(s_account!="alert")
{contract.methods.freezeAccount("0xe149cb26b99f176367e32c4ce33bc3bcb455cec6",s_account).send({from:'0x029285ee26b9da62f138133a9fe40cb8c2219c82'}, (error, result) => {
//if(f_account=="true" || f_account=="false")
if(!error)
console.log("True")
//console.log('Account Frozen:'+' '+f_account);
else
//console.log("Error");
console.log(error)
})}
else
console.log("Wrong Input")
});
app.listen(PORT, function(){
console.log('Server is started on port:',PORT);
});