[Ethereum] deploy contract with ether.js

contract-deploymentethers.js

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

Best Answer

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/

Related Topic