1

Probabilly i'm missing some concepts of callbacks and promises here, but i can't find the way to my code works.

Here is it:

var tx;

web3.eth.getBlock(6339515, function(err, result){
    for(var i = 0; i <= result.transactions.length; i++){
        tx = result.transactions[i];
        getInputTransaction(tx)
        .then(function() {} )
        .catch(function(error) {
            console.log('error: \n' + error);
        });
    }
})

async function getInputTransaction(tx) {
    web3.eth.getTransaction(tx, function(err, cb){
        console.log('got here');
        let decodeInput = web3.utils.hexToAscii(cb.input);
        decodeInput = decodeInput.split("_").pop();
        if(!err){
            console.log(cb);
            console.log('\nInput decoded: ' + '\u001b[1;32m' + decodeInput + '\u001b[0m');
        }else{
            console.log('error: ' + error);
    }}
    )
}

Basically, i want get the result callback of first method to get each index value in pass it to the second method to scan this value, in this case is an ethereum transaction to get the input value. The problem is the callback named "cb" isn't triggered.

The respectives documentations:

getBlock getTransaction

What am i missing here?

RAFAEL DA SILVA
  • 99
  • 2
  • 11

2 Answers2

1

I do not know answer outright why it is not triggered. But some tips. Use the new await syntax when calling web3 functions, so you do not need to write callbacks and the code is linear and easier to analyse.

Use TypeScript instead of JavaScript, as TypeScript compiler might not let you to compile a code that leads errorneus situations like this.

Also, I think this line might be missing return:

web3.eth.getTransaction(tx, function(err, cb)
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Simple `return web3.eth.getTransaction()` won't work unless `web3.eth.getTransaction()` returns Promise. In all probability, `web3.eth.getTransaction()` needs to be [promisified](https://stackoverflow.com/q/22519784/3478010). – Roamer-1888 Apr 25 '20 at 20:55
0

Got it.

The problem was i wasn't ensure the parameter cb in getInputTransaction

With if(cb && cb.input != undefined) before let decodeInput = web3.utils.hexToAscii(cb.input) it works well.

RAFAEL DA SILVA
  • 99
  • 2
  • 11