Check out a free preview of the full A Tour of Web 3: Ethereum & Smart Contracts with Solidity course

The "Replacing Functions from the Diamond" Lesson is part of the full, A Tour of Web 3: Ethereum & Smart Contracts with Solidity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen replaces a function in contract A with a new return value. After the replacement, the function can still be called at the same address by using the diamond. If a function is removed, it can still be called using the diamond but an error will be thrown indicating the function no longer exists.


Transcript from the "Replacing Functions from the Diamond" Lesson

>> We've just made a deploy, which I think is pretty exciting and we called it from the diamond contract. Now, let's add a little something, I'm gonna use as particularly good software engineering practices of simply coping and pasting code. So now it's like this, let's replaceA, so I wanna have all the same code, the only difference is I'm just simply gonna go replace.

I know I could make this into a nice, beautiful thing, okay, but just for now sometimes it's easy just to get stuff done. So there we go, we have replace we have nothing to do and let's go back to A and let's make it return something slightly higher, right.

So now let's go back to our cut and we're gonna do this, we need to go replaceA, right but the problem is what does A need? A needs a diamond, we need the address from the diamond pretty much, so let's just get the diamond address. And I'm gonna erase the little Kneisel thing and just have this as the diamond address we'll return it also down here.

Why were we trying the diamond address? I don't know, this just is part of it. There we go, so we just need the pass and the diamond address and we will have everything fantastic, so again let's copy this, paste it in there, there we go. This should pretty much work is my assumptions and so let's go in there and I forgot one last thing.

No, never mind, we deploy A every time, perfect. So let's deploy A, so now if we've done this correct, we've now deployed A, right and we call this before we deployed. So fantastic, so let's now try the next part which is let's recall A without the replacement this time.

So let's go back here and recall A, notice we got 53, we did not change the address of our contract. We didn't do anything, we simply just said, here you go and it worked, it replaced the function that we had stored in there. It's actually pretty special, I think this is just I'm just still so excited, I sometimes feel like it just front row here about as excited as I am.

But that is like awesome, it solves one of the worst parts about contract development which is when we change your address we're just host. There's like so many things you're gonna have to take care of, think about all that storage transfer, any of the extra information would just be a nightmare.

Said now it's pretty easy, we can just do that, we could set some variables if we want to, we just set it on the diamond. That's just how it works because we're all being called with delegate calls. So let's do one last one, just in case to really prove it.

Let's go delete A there we go and we'll call it remove and of course the address is gonna be that really convenient 0x 0. Look at that thank you tab nine it just knew I wanted 40 zeros, it could just feel it. And so once we do that we're gonna do the cut fantastic, order now remove A and then we're gonna call A.

So let's write this, delete A and let's actually make this into a function, it's not making a new function we'll just be lazy. There we go let's delete A, we've deleted A, now let's call A bump bump function does not exist, look at that. So we're actually adding replacing and deleting methods off of the diamond, it works very very shockingly well.

And how it does it, of course is by using those little function selectors right here that go in. And are able to encode your functions in a way that they can look up by using a mapping. And so this makes most sense, I think that every concept about this makes sense.

But the deployment script is obviously significantly harder than the ones we built thus far. But it all makes sense because it has to be slightly more complicated and ultimately this opens the door for you to be able to create as complex of a script you need. You break them up and there you go, now the one hard part will be obviously storage I showed you that little app share storage, technically we could share storage.

But you also have to have shared functionality, so you can imagine that your library, where are you, do I even have a library? If you make the library long as everything, a lib right long as everything inside of it has a scope. Get something, has a scope of internal, you don't have to deploy the library contract and make external calls to it.

You can just simply import it in and use those so long as they're all internal there's no public ones you can just use it kind of for free. And that can be the way you share code. It does increase your contract size, but at least anything that you need across multiple of them you can just use this.

And so, that tends to be as far as I can tell the best way to develop contracts to date on Aetherium.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now