[Ethereum] Ethereum Error: Provider does not support subscriptions: HDWalletProvider

nodejstruffletruffle-hdwallet-providerweb3js

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"
       }
    }

  }
}

Best Answer

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://.

Related Topic