1

I have encountered a situation in my script that made me think I don't know the exact use case of the lines below.

(1) const sampleContract = await ethers.getContractFactory("Sample")

(2) const sampleContract = new ethers.Contract(address, interface, signer)

(3) const sampleContract = await ethers.getContractAt("Sample", address, signer)

(4)

const factory = new ethers.ContractFactory(abi, bytecode, wallet)
          const contract = await factory.deploy(options)
          await contract.deployed()

So there are a couple of questions for me regarding the lines above:

  • Which one of the lines ends up deploying a new contract?
  • What does exactly each one do?
  • Are there more tools in the Ethers.js library that have similar functionality to deploy or initialize a contract for interacting with it?
  • If we assume the second line initializes an already created contract in our JavaScript code, what does the new keyword stand for after that the equation mark?

3 Answers 3

2
const sample = await ethers.getContractFactory("Sample")

We can say that the sample constant is an object comprised of the following:

(1) contract's bytecode string

(2) contract's interface object

(3) signer object

I think it's good to practice remembering them as written below:

getContractFactory("name of the contract") ==> { "bytecode" , {interface} , {signer} }

Then it's time to do as below if we want to deploy our contract:

const Sample = await sample.deploy()
          await Sample.deployed()

By putting these together, we want to answer the main question regarding the differences between the mentioned approaches. In that case, we can conclude that (1) and (4) are pretty similar as we can see (4) looks like a process that is just a single step ahead of (1). In other words, we would have the same result by following both approaches.

1

Which one of the lines ends up deploying a new contract?

I believe use case (4) ends up deploying a new contract.

What does exactly each one do?

Use case (1) gets a factory for the instances of contract named "Sample." Use case (2) create an instance by signer (it does not work if the user does not have a wallet or is not connected). Use case (3) is for interacting for an already deployed contract given the contract name "Sample", address of contract, and signer. Use case (4) creates an instance of the ContractFactory for the contract (described by the name of the contract "Sample", interface, and bytecode initcode), deploy the instance, and wait for the instance to finish deploying.

Are there more tools in the Ethers.js library that have similar functionality to deploy or initialize a contract for interacting with it?

These use case scenarios you described have been the most common ones I've seen.

If we assume the second line initializes an already created contract in our JavaScript code, what does the new keyword stand for after that the equation mark?

The new keyword deploys a new version of this contract to the network.

1

These lines of code are related to interacting with Ethereum smart contracts using the ethers.js library. Let's break down each line:

(1)

const sampleContract = await ethers.getContractFactory("Sample")

This line retrieves the contract factory for the "Sample" contract. The getContractFactory function takes the contract's name as a parameter and returns a factory object that can be used to deploy or interact with instances of that contract.

(2)

const sampleContract = new ethers.Contract(address, interface, signer)

This line creates an instance of a smart contract. It takes three parameters: address (the address of an existing deployed contract), interface (the contract's ABI or application binary interface), and signer (an Ethereum account or provider that can sign transactions). This line is typically used when you want to interact with an existing contract on the blockchain.

(3)

const sampleContract = await ethers.getContractAt("Sample", address, signer)

Similar to line (2), this line also creates an instance of a smart contract. However, instead of directly providing the interface, you provide the contract's name ("Sample") and the getContractAt function retrieves the interface for you based on the contract's address. This line is also used to interact with existing contracts.

(4)

const factory = new ethers.ContractFactory(abi, bytecode, wallet)
const contract = await factory.deploy(options)
await contract.deployed()

These lines are used to deploy a new smart contract to the Ethereum network. The ethers.ContractFactory is used to create a contract factory object by providing the contract's ABI (abi) and bytecode (bytecode). Then, factory.deploy(options) initiates the deployment of the contract, where options represents any additional options or parameters required for deployment. Finally, await contract.deployed() waits for the contract deployment to be confirmed and returns the deployed contract instance.

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.