[Ethereum] How to interact with the Rinkeby contract via truffle console

rinkebytruffletruffle-console

I've been using truffle console to interact with a contract which I developed locally with Ganache. I'd like to be able to call the contract's methods using the console once deployed to the Rinkeby network in the same way that I've been able to do so with Ganache.

I'm aware that I can do this using Remix but I'm wondering if I can do this using the terminal as well. In other words, is there a config option for the console to interact with Rinkeby?

Best Answer

You should be able to use truffle console --network rinkeby.

You will need to configure truffle-config.js properly, e.g. using Infura and the hdwallet-provider package:

const HDWalletProvider = require('@truffle/hdwallet-provider');
const infuraURL = 'https://rinkeby.infura.io/v3/YOUR-INFURA-PROJECT-ID';
const fs = require('fs');
const mnemonic = fs.readFileSync('.secret').toString().trim();

module.exports = {
  networks: {
    rinkeby: {
      provider: () => new HDWalletProvider(mnemonic, infuraURL),
      network_id: 4,
      gas: 5500000,        
    },
// additional configuration...
}

Your mnemonic should be stored in .secret and the account will need to have Rinkeby testnet Ether to pay for transaction gas.

Related Topic