0

I finished testing my smart contract functionality with "unlocked accounts" (which don't require complex signing) on a local dev blockchain, but now I would like to deploy on MAINNET and use private keys.

After I deploy, I want to interact with a function in my smart contract using Web3.js, but all the tutorials I've seen use "unlocked accounts" on private local chains which unfortunately aren't helping. What do I have to add to this contract instance in order to broadcast real signed transactions that are valid and will invoke functions on MAINNET.

const Web3 = require('web3');
const myContract = require('./build/contracts/myContract.json');   //because I use Truffle

const init = async () => {
    const web3 = new Web3('https://mainnet.infura.io/v3/MY-INFURA-ID');
    const id = await web3.wth.net.getId();
    const deployedNetwork = myContract.networks[id];
    const contract = new web3.eth.Contract(
        myContract.abi,
        deployedNetwork.address
    );
}

//I have the HDwallets package if that's relevant..

Help would be really really appreciated, I'm lost!

1

You can use HDWallet Provider.

const Web3 = require('web3');
const myContract = require('./build/contracts/myContract.json');
const HDWalletProvider = require("@truffle/hdwallet-provider");

const mnemonicPhrase = 'country roads take home ....';

const init = async () => {
    const provider = new HDWalletProvider({
        mnemonic: {
            phrase: mnemonicPhrase
        },
        providerOrUrl: 'https://mainnet.infura.io/v3/MY-INFURA-ID',
    });

    const web3 = new Web3(provider);
    const id = await web3.eth.net.getId();
    const deployedNetwork = myContract.networks[id];

    const contract = new web3.eth.Contract(
        myContract.abi,
        deployedNetwork.address
    );
}

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.