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

Loading player…

Multiple Inheritance

BaseMon, Oct 9, 2023, 12:00 AM

Learn the powerful concept of multiple inheritance and dynamic behavior in Solidity contracts. You'll see how to inherit from multiple contracts and override functions to achieve dynamic execution based on contract type. Don't miss this essential knowledge for flexible and efficient smart contract development! 🚀💡 📚 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

all right now what we're gonna do is we're gonna create another contract called contract C and what are we gonna do is we are gonna copy this function okay we're gonna say this is a Ctrl C and now we are gonna do something called multiple inheritance which means we are going to inherit from multiple contracts okay however there is an error the problem here is that we are inheriting from multiple contracts that contain the same implementation and by doing that we are forced to use a feature of solidity called function overriding okay which means that the base contracts need to specify the functions as virtual and then the contract a will need to call explicitly or will need to override a particular implementation so what we're gonna do here is we are going to say that this function is virtual we are going to say also that this function is virtual and now in the contract a we are going to say function who am I that overrides the contract B the contract C that is going to be external pure and it's gonna return a string memory okay and then here what we can do is red darn contract B Dot who am I okay and there is a small problem we need to change this to be public this to be public also and now and this one to be public okay and then this is working okay now we are saying that who am I is overriding both implementations from Ctrl C and B and then we are calling explicitly the function from contract B so let's test deploy and then who am I I'm contract B now we can do something different let's imagine in this moment that sometimes you want to call Contract B who am I but in other times you want to call Contract see who am I how do you add this Dynamic behavior in the contracts you could do it by having a norm let's say we create a Norm called type and this is gonna Define some values none contract B type and contract C type okay so what we can do is we can define a variable called type that is going to be called contract type okay and then let's create a Constructor and in the Constructor we are gonna initialize the contract type that is going to be equals to an option that we are passing during the creation of the contract and this is going to be called initial type and we simply initialize as initial type okay now that we have this contract type we basically can do this we can say if contract type is equals to type Dot contract be type then we return the contract B implementation okay now we can say if contract type is equals to type contract C type then we return contract C Dot who am I okay and if none of those options are specified what we can say about the file return contract a okay all right so there is a problem let's see okay this is declared as pure however now we are accessing State variables for that reason this has to be a view the same case for the contract be who am I implementation and the same case for this implementation okay and now it's working properly Let's test I have control a selected and I'm going to specify let's say zero I'm deploying and then if I ask who am I I should expect to get Contract a okay I have contract a now let's add another function to allow us to modify the type the contract type let's create a function called change type that is gonna receive a new type that is going to be simply external and what we do is that the contract type now is going to be equals to the new type okay Let's test this contract and I'm gonna say again that the default value is zero so I should expect to get Contract a now let's say I want to change it for 1 which is the contract B and now let's call who am I contract B now let's change it to be the control C okay and then who am I contract C all right so this is a way to reuse code from base contracts but also add some sort of dynamism to the code

Automatic transcript — names and jargon may be misspelled.