New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Creating a New Contract

BaseMon, Oct 9, 2023, 12:00 AM

Create a smart contract that can deploy new contracts using the "new" keyword. Explore a simple example of a contract that stores and compliments the owner's name. Then, expand the functionality to allow anyone to create their own complementer contract. Get hands-on experience in deploying and interacting with these contracts! 🚀🔥 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord

Transcript

in this lesson we are going to create a smart contract that creates a new contract using the new keyword let's start with a simple example in Mayan we want to create a contract that can store its owner name and have a function that complements upon each function call so let's start creating as we said we are going to store the owner's name and at construction we are gonna simply pass any name and we are going to assign the value underscore name to name okay and now let's create a function called complement and this function complement is simply going to be external and it is going to be a view that is going to return a string memory okay and we simply are gonna return using the string dot concat function we are gonna say you look great today so that's a compliment and then we are going to include the name in this case we simply say name okay let's leave a space here all right so the contract complementer is simply going to store a name and then I can simply call it complement and I'm gonna receive a compliment Let's test it let's deploy and let's pass the name let's say Pedro deploy the contract was deployed and as we can see we can access the name Pedro but we can call the function complement and it works I'm getting you look great today Pedro which is good now imagine we want to extend the complementary contract functionality to allow anybody to create their own version so my friends can deploy their own complementer how do we do that we are going to create a contract called complementer Factory that is actually going to deploy new complementary contracts and then we are going to create a function called create a complementer okay that is going to also receive a string and we're gonna say that is gonna return an address the address of the newly created complementer contract okay and then we simply say complementer new contract is going to be equals to new complementer as you can see we are simply using the new keyword okay but then since complementer requires a string we simply pass that string and we are gonna return remember an address and we simply cast new contract that is of type complementer to address and that should work okay now let's test it since I'm using the same file for the two contracts be sure that you select the complementer factory and I'm gonna simply deploy and now I'm gonna create a complementer and I'm gonna say the name Pedro 2. okay and I create a complementer okay these function call should have given to us the following output and this is the decoded output that is essentially going to be the address of the new contract okay and now in order to test it I'm going to Simply select complementer but instead of deploying a new instance I'm gonna say add address and that's going to generate a complementary instance okay and now let's call complement all right you look great today Pedro 2. all right it work so now we can create new complementary contracts and also our friends can come and create their own complementary contracts

Automatic transcript — names and jargon may be misspelled.