Learn to create a contract that creates other contracts.
new
keyword and used it to instantiate memory
arrays with a size based on a variable. You can also use it to write a contract that creates other contracts.
ContractFactory.sol
.
Reveal code
Complimenter
contract is a huge success! People love how it makes them feel and you’ve got customers banging on the doors and windows. Awesome!
The only problem is that it takes time and effort to manually deploy a new version of the contract for each customer. Luckily, there’s a way to write a contract that will act as a self-service portal for your customers.
Start by adding a contract called ComplimenterFactory
. The Remix interface makes things easier if you leave the factory in the same file as Complimenter
.
Add a function called CreateComplimenter
that is public, accepts a string
called _name
, and returns an address
.
Creating a new contract is simple: new Complimenter(_name)
You can also save the return from that instantiation into a variable. This reference can be used to call public functions in the deployed contract, and can be cast to an address. We can use it to get an easy reference to find the copies made by the factory. The end result should look similar to:
Reveal code
ComplimenterFactory
. You’ve been working hard and deserve nice things, so call CreateComplimenter
with your name.
In the terminal, the decoded output will be the address of the new contract.
Complimenter
, then paste the address you copied in the field next to the At Address button which is below the Deploy button.
Complimenter
should appear below ComplimenterFactory
. Test to confirm it works, then try deploying more instances with the factory.
Complimenter
using the ABI from the factory contract, which won’t work.new
keyword. You also learned that you look great today!