0

I want to write a simple js script. I follow many tutorial available from Youtube, forums and website but i seem that i cannot interact from the smart contract. i can interact it from remix. btw the smart contract is deployed on my private network.

Web3 = require('web3');

console.log("Hi blinkExample")
const web3 = new Web3(new Web3.providers.HttpProvider('http://192.168.100.130:8043'));

web3.eth.getAccounts()
.then(console.log);
web3.eth.defaultAccount = web3.eth.accounts[0];
var coinbase = web3.eth.coinbase;
var contractAddress = '0x6b9781fe8b355965a7ce99f58b918f711ab5fe1e';
var abi = [
    {
        "constant": false,
        "inputs": [
            {
                "name": "theData",
                "type": "uint256"
            }
        ],
        "name": "setData",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": false,
                "name": "data",
                "type": "uint256"
            }
        ],
        "name": "blinkEvent",
        "type": "event"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getData",
        "outputs": [
            {
                "name": "retData",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "myData",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
];

var blinkContract = web3.eth.Contract(abi, contractAddress);
var data = blinkContract.getData;
console.log('value is' + data);

var event2 = blinkContract.blinkEvent(function(error, result){
    if (!error){
        console.log(result);
    }
});

the console will show account list just fine but displays "value is undefined" then crash for the blinkEvent with typeError: blinkContract.blinkEvent is not a function. The value should be 12 which I set using remix.

1

Assuming you're on web3 v1.x, change this:

var blinkContract = web3.eth.Contract(abi, contractAddress);
var data = blinkContract.getData;
console.log('value is' + data);

To this:

var blinkContract = new web3.eth.Contract(abi, contractAddress);
blinkContract.methods.getData().call().then(function(data) {
    console.log('value is' + data);
});
  • Sorry for the late reply. So its they changed it? I am a noob at js so it is very confusing. I am currently away from my pc. I will try asap. Thank you. – Vid Charensuk Apr 14 at 16:19

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.