1

Im trying to figure out the best way to deploy a contract programatically using ethers.js

with web3 I can just do:

 const contractInstance = new this.web3.eth.Contract(contractObject.abi)
    var deployTx
    debug(contractInstance)
    if (params === undefined || params.length === 0) {
      deployTx = contractInstance.deploy({
        data: contractObject.bytecode
      })
    } else {
      deployTx = contractInstance.deploy({
        data: contractObject.bytecode,
        arguments: params
      })
    }
    const data = await deployTx.encodeABI()

How ever with Web 3 I also seem to need an address? and to get that address I have to firs manually deploy it; Some clarity around the topic would really help.

I've tried to find resources specifying this but they are not what what im looking for I think?

Thanks in advance

  • How ever with Web 3 I also seem to need an address? and to get that address I have to firs manually deploy it - no you don't need an address. – goodvibration Jun 29 at 19:39
  • could you please elaborate or point me in the right direction? What I have gathered is this: The documentation doesnt seem to go in too deep into the deployment itself? – Lucas Rodriguez Benitez Jun 29 at 19:47
1

You can deploy a contract using Ethers.js' ContractFactory.

import { ContractFactory } from 'ethers';

const factory = new ContractFactory(contractAbi, contractByteCode);

// If your contract requires constructor args, you can specify them here
const contract = await factory.deploy(deployArgs);

console.log(contract.address);
console.log(contract.deployTransaction);

More information can be found in the documentation, found here: https://docs.ethers.io/v5/api/contract/contract-factory/

| improve this answer | |
  • 1
    this returns an error Cannot read property 'sendTransaction' of null – Lucas Rodriguez Benitez Jun 30 at 3:23

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.