0

Is there a way to deploy a contract on goerli infuria, using web3.js and metamask so I don't have to sign the transaction, or is it possible to use metamask to sign it, without having to provide the address' private key? The following code works when using Ganache, for local development, but not when trying to test the contract on goerli Infuria test net.

    const web3 = new Web3(new Web3.providers.HttpProvider(props.smartContract.deployed_network)); //https://goerli.infura.io/v3/ my api key

    const accounts = await ethereum.request({ method: 'eth_accounts' });
    const sender = accounts[0];

    const Contract = await new web3.eth.Contract(JSON.parse(props.smartContract.abi))
        .deploy({data: JSON.parse(props.smartContract.bytecode).object})
        .send({from: sender, gas: 3000000})
        .then(function (res) {
            if (typeof res.options.address !== "undefined") {
                message.value = "The contract was deployed successfully!"
            } else {
                message.value = "Something went wrong! Please check to see if the contract was deployed!"
            }
        });

The error I get when running the script:

Returned error: The method eth_sendTransaction does not exist/is not available

EDIT: thanks AlexAcc

I changed

const web3 = new Web3(new Web3.providers.HttpProvider(props.smartContract.deployed_network)); //https://goerli.infura.io/v3/ my api key

with

const web3 = new Web3(window.ethereum)

and now works via Metamask Metamask example

1 Answer 1

0

Yes sure, of course you can. Infura is a node provider, it gives you a working URL to connect to the blockchain, and you can interact with the node trough Metamask, signing transactions in the browser, without exposing the private key.

You need to have Web3 provider injected in the browser:

let smartContractInstance = new web3.eth.Contract(abi, addr_contract);

    function YOUR_JS_FUNCTION(fromaddr) {
    
      let gaslimit;
      smartContractInstance.methods.YOUR_FUNCTION(
          param1, //your function's imput params
          param2
        ).estimateGas({
          nonce: web3.utils.toHex(nonce),
          gasPrice: gasPrice,
          from: fromaddr,
          chainId: web3.utils.toHex(chainId),
        })
        .then(function(gas) {
    
          console.log("gaslimit: " + web3.utils.toHex(gas));
          gasLimit = web3.utils.toHex(gas);
    
          nft_sc.methods.YOUR_FUNCTION(
              param1, //your function's imput params
              param2
            ).send({
              nonce: web3.utils.toHex(nonce),
              gasPrice: gasPrice,
              gasLimit: gasLimit,
              from: fromaddr,
              chainId: web3.utils.toHex(chainId)
            })
            .then(function(result) {
              console.log("Result:");
              console.log(result);
            })
            .catch(function(err) {
              endLoading();
              console.error("ERROR: Function execution");
              console.error(err);
            });
    
        })
        .catch(function(error) {
          console.log("ERROR estimateGas());
          console.log(error);
        });
    }
1
  • Thanks, I changed: const web3 = new Web3(new Web3.providers.HttpProvider(props.smartContract.deployed_network)); with const web3 = new Web3(window.ethereum) and now it works via Metamask. Commented Feb 18, 2023 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.