Error: invalid signer when deploying with ContractFactory

I want to deploy a simple smart contract from frontend using contracFactory (https://docs.ethers.io/v5/api/contract/contract-factory/).
I have been trying in different opportunities for around a year now, without sucess.Im having such a hard time that Im wondering if is this even posible?? or my approach is not correct.

Generally I test in remix and I copy the abi and the bytecode from remix aswell…

here is an example

async function contractdeploy(){
let abi =  ( HERE I PASTED THE BYTECODE I COPIED FROM REMIX)

let bytecode = ( HERE I PASTED THE ABI I COPIED FROM REMIX)

const ethers = Moralis.web3Library;
const provider = await Moralis.enableWeb3()

let factory = new ethers.ContractFactory(abi, bytecode, provider);
let contract = await factory.deploy("Hello World");
console.log(contract.address);
console.log(contract.deployTransaction.hash);
await contract.deployed()  

}

PS. just in case here is the demo contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract HelloWorld {
   string public message;
   constructor(string memory initMessage) {
      message = initMessage;
   }
   function update(string memory newMessage) public {
      message = newMessage;
   }
}

Try using const signer = provider.getSigner() and use that in ContractFactory instead of provider.

1 Like

This works on a full ERC1155 contract for me:

const provider = await new ethers.providers.Web3Provider(window.ethereum);
const signer = await provider.getSigner(Address);
const factory = await new ethers.ContractFactory(JSON.parse(ABI), ByteCode, signer);
const contract = await factory.deploy();
const receipt = await contract.deployTransaction.wait();
1 Like

YEES, I got it working, thanks so much for your feedback!

Here’s what worked:

const ethers = Moralis.web3Library;
const provider = await new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner() 
const factory = await new ethers.ContractFactory(abi, bytecode, signer);

let contract = await factory.deploy("Hello World");
console.log('tx hash: ', contract.deployTransaction.hash);

const receipt = await contract.deployTransaction.wait();
console.log('receipt', receipt)
1 Like