1

I'm new in solidity and I'd like to have a little bit of debug.

this is my simple example (I'm using web3 1.0)

I have a simple function in my contract

function bar(address _address_usr) public  {
        uint res = 5;
        emit foo('test',res);       
    }

And I have test about it

.....
it("...", async () => {

        let livello = await instance.methods.bar(accounts[0]).call();


        instance.events.foo(function (error, event) {
            console.log(event);
        })
            .on('data', function (event) {
                console.log("OK");
                console.log(event); 
            })
            .on('changed', function (event) {
                console.log("OK");
            })
            .on('error', console.error);    

    });
....

I'd like to know the result of res. I tried with event but I have this error

err Error: The current provider doesn't support subscriptions: HttpProvider

Can I print some result/var in my console like console.log or something like that?

Thanks

UPDATE

My web3 istance

helpers.js

const Web3 = require("web3") // import web3 v1.0 constructor

// use globally injected web3 to find the currentProvider and wrap with web3 v1.0
const getWeb3 = () => {
    const myWeb3 = new Web3(web3.currentProvider)
    return myWeb3
}

// assumes passed-in web3 is v1.0 and creates a function to receive contract name
const getContractInstance = (web3) => (contractName) => {
    const artifact = artifacts.require(contractName) // globally injected artifacts helper
    const deployedAddress = artifact.networks[artifact.network_id].address
    const instance = new web3.eth.Contract(artifact.abi, deployedAddress)
    return instance
}

module.exports = { getWeb3, getContractInstance }

my test.js

const {getWeb3, getContractInstance} = require("./helpers")
const web3 = getWeb3()
const getInstance = getContractInstance(web3)
  • I recommend looking at how open zeppelin implement tests, they have helper functions to parse events, github.com/OpenZeppelin/openzeppelin-solidity/blob/master/test/…, but honestly I miss the old way they did things :P. – GrandFleet Nov 19 '18 at 17:18
  • Try to share the code where you create the web3 instance, I think you might be using websocket functionality and that's where it's failing, to catch the error, do a try catch when generating the instance, to fix the problem don't use ws, or use a web socket compatible node – Enrique Alcazar Nov 19 '18 at 21:30
  • @EnriqueAlcazar I updated my question. the only way to read variable inside my code is events? – monkeyUser Nov 20 '18 at 10:16
  • If you want to debug, you can use remix, they have a pretty good debugger, also remember that you can use strings as a second parameter to your reverts, and you can create constant functions (views) to read any parameter in your blockchain – Enrique Alcazar Nov 20 '18 at 10:34
  • Check that web3 you are using is correct, IE: console.log it and look if the params are correct – Enrique Alcazar Nov 20 '18 at 10:35
2

Your error states that you are using HttpProvider for web3 and as per the web3 documentation here, it states:

Object - HttpProvider: The HTTP provider is deprecated, as it won’t work for subscriptions.

and this error is appearing from this code:

// throw error, if provider doesnt support subscriptions
    if(!this.options.requestManager.provider.on) {
        var err2 = new Error('The current provider doesn\'t support subscriptions: '+ this.options.requestManager.provider.constructor.name);
        this.callback(err2, null, this);
        this.emit('error', err2);
        return this;
    }

I think for event subscription you should try to use other alternatives of provider.

UPDATE: To get the value of event parameters you can change the code like this:

   it("...", async () => {

            let livello = await instance.methods.bar(accounts[0]).send();
            console.log(livello.events.foo.returnValues);
}

This will print the event params values. For more, visit: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

Furthermore, if you are looking to print the variables value only, you should rather use view/pure function without events. For more info, visit: https://solidity.readthedocs.io/en/v0.5.0/contracts.html#functions

  • I updated my question – monkeyUser Nov 20 '18 at 10:16
  • Are you looking to get the values of event variables from transaction? – Aniket Nov 20 '18 at 10:22
  • I'd like to have a method to debug my contract I want to get the val of res for istance, Event is a good approach? – monkeyUser Nov 20 '18 at 10:25
  • remove .call() from your function call and you will get it in event params. – Aniket Nov 20 '18 at 10:28
  • I have the smae error The current provider doesn't support subscriptions: HttpProvider – monkeyUser Nov 20 '18 at 10:34

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.