5

According to the web3.js docs I'd expect it shouldn't be necessary to use callbacks. When no callback is passed, promise-like "promiEvents" should be returned. Why is the ouput of the following example "result undefined"?

  try {
    const result = await web3.currentProvider.send({
      method: "eth_sendPrivateTransaction",
      params: [ signedTx ],
      jsonrpc: "2.0",
      id: new Date().getTime()
    });
    console.log('result', result);
  } catch (err) {
    console.error(err);
  }

It seems like there's an unresolved issue on GitHub related to this. Is it still necessary to use callbacks with web3.currentProvider.send() in web3 version 1.3.3?

Edit: WebsocketProvider for example declares send(payload, callback) as follows:

WebsocketProvider.prototype.send = function (payload, callback) {
    ...
};

What seems to be missing is a send(payload) function that takes just one argument and returns a Promise (or "promiEvent"?)

5
  • What is the send() function you are using? I don't see it in the documentation. send() can only be called on a contract method. Do you want to use sendTransaction()?.
    – Undead8
    Commented Mar 17, 2021 at 13:40
  • It's the send function of the current provider. I've added WebsocketProvider as example.
    – pwagner
    Commented Mar 17, 2021 at 23:09
  • Not all functions return PromiEvents. Some only return a regular Pormise, some return a PromiEvent. When they do return a PromiEvent, it's explicitly written in the doc (see this for instance web3js.readthedocs.io/en/v1.2.11/web3-eth.html#sendtransaction). The send() function you are calling directly on the provider is not even in the doc, so I would not assume that it should return a PromiEvent.
    – Undead8
    Commented Mar 17, 2021 at 23:51
  • Thanks, I realize it's a design choice of each provider, if they want to follow that web3's promiEvent convention. Maybe there's an elegant way to extend providers to support async/await in the send() fuction. @Undead8, I would accept this as answer, if there's no more activity here.
    – pwagner
    Commented Mar 20, 2021 at 14:17
  • I copied my comment as an answer.
    – Undead8
    Commented Mar 20, 2021 at 17:57

2 Answers 2

3
+50

Not all functions return PromiEvents. Some only return a regular Promise, some return a PromiEvent. When they do return a PromiEvent, it's explicitly written in the doc (see this for instance ).

The send() function you are calling directly on the provider is not even in the doc, so I would not assume that it should return a PromiEvent.

2

You can use promisify to transform the function into one that can be called with async/await. Something like this:

import util from "util";

const sendRpc = util.promisify((this.web3.currentProvider as IpcProvider).send)
    .bind(this.web3.currentProvider);

try {

    const response = await sendRpc({
        jsonrpc: "2.0",
        method: "admin_nodeInfo",
        params: [],
        id: (new Date()).getTime(),
    });
} catch(error) {
    ...
}

I'm using TypeScript in this example but the syntax should be similar for regular ES6.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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