Search code examples
trufflersk

How to configure truffle to connect to RSK Testnet public node?


I'm using the following configuration in truffle

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    },
    rsk_testnet: {
      host: "https://public-node.testnet.rsk.co",
      network_id: '31',
      gasPrice: 59240000, // 0.5924 Gwei
    }
  },
  solc: {
    optimizer: {
      enabled: false
    },
    version: "0.5.17",
    evmVersion: 'petersburg'
  },
};

However I'm getting stuck with intermittent errors with network timeouts. i.e.

Invalid JSON RPC response: "<html>\r\n<head><title>504 Gateway Time-out</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>504 Gateway Time-out</h1></center>\r\n</body>\r\n</html>\r\n"
    at Object.InvalidResponse (node_modules\truffle-hdwallet-provider\dist\webpack:\truffle-hdwallet-provider\web3\node_modules\web3-core-helpers\src\errors.js:42:1)     
    at t.InvalidResponse [as onreadystatechange] (node_modules\truffle-hdwallet-provider\dist\webpack:\truffle-hdwallet-provider\web3\node_modules\web3-providers-http\src\index.js:92:1)
    at t._a [as dispatchEvent] (node_modules\truffle-hdwallet-provider\dist\webpack:\truffle-hdwallet-provider\xhr2-cookies\dist\xml-http-request-event-target.js:27:61)  
    at t.dispatchEvent [as _setReadyState] (node_modules\truffle-hdwallet-provider\dist\webpack:\truffle-hdwallet-provider\xhr2-cookies\dist\xml-http-request.js:208:1)   
    at t._setReadyState [as _onHttpResponseEnd] (node_modules\truffle-hdwallet-provider\dist\webpack:\truffle-hdwallet-provider\xhr2-cookies\dist\xml-http-request.js:318:1)
    at IncomingMessage._onHttpResponseEnd (node_modules\truffle-hdwallet-provider\dist\webpack:\truffle-hdwallet-provider\xhr2-cookies\dist\xml-http-request.js:289:47)   
    at IncomingMessage.emit (events.js:198:15)
    at endReadableNT (_stream_readable.js:1139:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:17)

Solution

  • In your truffle-config.js file:

    (1) Set a variable testnetSeedPhrase to contain a valid BIP-39 mnemonic phrase

    (2) Set a variable gasPriceTestnet to contain the gas price you wish to use denominated in Wei.

    (3) In the exported config object, set the value of config.networks.testnet to be the following.

        ...
        testnet: {
          provider: () => new HDWalletProvider({
            mnemonic: {
              phrase: testnetSeedPhrase,
            },
            providerOrUrl: 'https://public-node.testnet.rsk.co/',
            // Higher polling interval to check for blocks less frequently
            pollingInterval: 15e3,
          }),
          // Ref: http://developers.rsk.co/rsk/architecture/account-based/#chainid
          network_id: 31,
          gasPrice: gasPriceTestnet,
          networkCheckTimeout: 1e6,
          timeoutBlocks: 100,
          // Higher polling interval to check for blocks less frequently
          // during deployment
          deploymentPollingInterval: 15e3,
        },
        ...
    

    (4) Now you can run truffle subcommands with this network selected, for example:

    truffle migrate --network testnet
    

    Note that these options were originally not configurable in Truffle, and were set to hard coded defaults. These were added specifically to be able to support networks with a different block interval!

    Context: