1

I am making a flash loan request using web3 , truffle and keep getting the error in an event listener:

The current provider doesn't support subscriptions: HDWalletProvider.

I am connecting to the ropsten test network.

I am using the code below to connect to get emitted events in a smart contract:

const web3 = new Web3();
const eventProvider = new Web3.providers.WebsocketProvider(wss);
 
eventProvider.on('error', e => {
    console.error('WS Infura Error', e);
});

eventProvider.on('connect', function () {
        console.log('WSS Connected');
            setupListeners();
    });

eventProvider.on('end', e => {
    console.log('WS closed');
    console.log('Attempting to reconnect...');

       eventProvider = new Web3.providers.WebsocketProvider(wss);

eventProvider.on('connect', function () {
        console.log('WSS Reconnected');
            setupListeners();
    });
    web3.setProvider(eventProvider);
});
 web3.setProvider(eventProvider);  

The request is:

 contract1.methods.flashloan( args_of_my_use_case ).send({
              from: addr, //designated Metamask wallet address
              gas: gasCost,
              value: 0,
              nonce: accountNonce,
            },
            function (error, data) {
               if(error){
                 invariant.currentlyTrading = false;
                 obj.nowTrading = false;
               }
               console.log('arb data => ', data, '   arb error => ' ,error, appendText);
            });

The connection works and events get fired but one of the responses from a flash loan request is:

Error: The current provider doesn't support subscriptions: HDWalletProvider

My truffle config is:

const dotenv = require('dotenv');
const fs = require('fs');
 
const envConfig = dotenv.config({silent: true});



 const HDWalletProvider = require('@truffle/hdwallet-provider');
 const infuraKey = process.env.INFURA_API_KEY;

const ethPubAddr = process.env.ETH_PUBLIC_ADDRESS;
 const mnemonic = process.env.METAMASK_PVT_KEY; 
 

 console.log("mnemonic -->" , mnemonic);

module.exports = {
   
  networks: {
    // Useful for testing. The `development` name is special - truffle uses it by default
    // if it's defined here and no other network is specified at the command line.
    // You should run a client (like ganache-cli, geth or parity) in a separate terminal
    // tab if you use this network and you must also set the `host`, `port` and `network_id`
    // options below to some value.
    //
    mainnet: {
      provider: function() {
        return new HDWalletProvider(mnemonic, "https://mainnet.infura.io/v3/"+infuraKey);
      },
      network_id: 1,
      from: ethPubAddr

    },
    ropsten: {
      provider: function() {
         return new HDWalletProvider(mnemonic, "wss://ropsten.infura.io/ws/v3/"+infuraKey);
      },
      network_id: 3,
      from: ethPubAddr,   
      gas: 6800000, // Current Ropsten gas limit. See https://ropsten.etherscan.io/block/3141628
      gasPrice: 20000000000 // 1.1 GWei - based on the lower end of current txs getting into blocks currently on Ropsten.
   //   gasLimit: 8000000

    },
  
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    },

  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },

  // Configure your compilers
  compilers: {
    solc: {
       version: '0.6.10',    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
     settings: {          // See the solidity docs for advice about optimization and evmVersion
       /* optimizer: {
          enabled: true,
          runs: 2
        }*/
      //  evmVersion: "byzantium"
       }
    }

  }
}
2

Subscriptions are only supported by WebSockets provider. The URL you are giving is for HTTP provider

        return new HDWalletProvider(mnemonic, "https://mainnet.infura.io/v3/"+infuraKey);`

Change https:// to wss://.

| improve this answer | |
  • 1
    Hi @Mikko, thanks for answering. I apologize for not mentioning that I am on ropsten a test network. It is actually set to wss in my code. I still have the issue though; even with the wss. I will edit the question to show that now. – gbenroscience Jun 25 at 15:53
  • Ok gotcha. Does your code work if you leave out HDWalletProvider and just use WSS provider directly? It almost sounds like Web3.js has a bug to detect this. – Mikko Ohtamaa Jun 26 at 8:22
  • I just searched web3.js repo quickly how this is detected: github.com/ethereum/web3.js/blob/… - might be that a patch for HDWalletProvider might be needed if it lacks on function – Mikko Ohtamaa Jun 26 at 8:25
  • 1
    Yes. I do not remember how it should look like, but web3.js docs should have an example. I have a gut feeling HDWalletProvider is the culprint. – Mikko Ohtamaa Jun 26 at 9:12
  • 1
    If this is the case then I suggest you file an issue directly in Truffle github repository. – Mikko Ohtamaa Jun 27 at 8:13

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.