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

The "Diamond Pattern Fallbacks" 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 explains how the Diamond Pattern helps address contracts that are too large and scenarios where a contract needs to be updated. An external fallback method is created to handle any calls to methods that do not exist. In order for the fallback to work, non-existent methods should be defined in an interface.


Transcript from the "Diamond Pattern Fallbacks" Lesson

>> Again, the point of this course is to make it so that you understand how to use smart contracts. You may not exit this course knowing all the semantics you need to be able to create your own NFT. But you will pretty quickly be able to do it because you understand the fundamentals.

I'm really trying to shoot a good fundamental course here. If you wanna quick refresher on syntax. Crypto, honestly one of the best ones I've ever done. It's number one in the world apparently 415,672 plus very odd number to put a plus. By the way usually people make it a little bit more approximate have taken this course.

It's fun, I did it, it was great. They even do some bitwise stuff and kind of do something very similar to what I did with heroes. But they do zombies eat crypto kitties, it's a lot of fun stuff. So we're gonna move on to diamonds are forever, sort of.

So there are a way around the size issue, we could hack our own solution. So you can imagine the solution where alright, let's have contract area we launched that one. Then we have contract B that launches from there, then we point to A. And when people call us we call A and then if we need to update A we can update A.

And then pass in a new dress that calls new A, but we're trying to store everything we can and contract B. And it get really messy really confusing and if you make a bug in contract B, you still have to do the same thing. So then you're like, okay, I'll make a contract C that points to b that B points to a right?

Like it just the problem goes forever, right, you can't really get around bugs that happen. So let's use a well established pattern, so the pattern is called the diamond pattern right here. It effectively addresses two points your contract is too big and you want to be able to update your contractor at some point.

It's the Etherium improvement plan 25,35, from the site here's the motivation for the site will go on. I don't really read a really need to do that. From my own perspective how I like to learn things is, I want to know as much as I need to know that I can use it.

I have written HTTP HB two and Web Sockets for Netflix and I don't need to understand how TCP works to write those, right none of us do. We don't need to know that, we just simply need to know how it works in a sense that okay TCP sends reliable in order data or structured data.

Therefore when I send 10,000k worth of data I'm gonna get 10,000k in the same border on the other side. Now how I interpret that data, that's something that sits on top of TCP. And much like this, you don't need to understand every fine minutiae of how the diamond contract works.

You just simply need an understand enough that A you don't screw yourself. And B, if there's a problem deploying trying to understand a feature, you can just take care of it. And so I really want to make sure we get that, and so first let me kind of whiteboard out the diamond.

So first, I'm just going to draw a really beautiful diamond pattern here, my goodness look at how beautiful that is. Effectively what happens is, that here's the outside world, here is our diamond contract. All the outside world goes to our diamond contract and calls it. Inside of our diamond contract, we can have one or more contracts that are actually executing.

But here's the amazing part, all the data is stored on the diamond contract and we don't know about it. We assume we're working with our own data but that's not what's happening, just for the sake of that. So the diamond will route our call to the appropriate contract each time something gets called, and of course returned back out the data from each one of these calls.

So that it works out correctly, we can go back out the other end as if you're calling a single contract with all these methods. So this sounds great, right? I like to think of it more like a, it's almost feels like a microservice architecture, right? You have your domain and behind it actually is just a huge amount of different services.

And they all come back with a single value back out, it's not like your request actually is routing itself to the correct place. It just does it automatically based on how you set everything up. And so I like to think of it that way, why was it chosen to be called diamonds?

I don't know there's a lot of kind of bespoke language that he uses to describe all the operations. I also find it very confusing to read the documentation, just cuz of that. So I'm going to kind of avoid using their language and just try to describe it more in micro service kind of architecture.

So I think, the rest the world that's how we think, so it's a little easier. I'm just gonna be kind of walking through some technical aspects of how contracts work, memory works, delegate calls work. And you can kind of watch and understand it, because you probably will never really touch most of what I'm gonna do.

You just need to know that it's out there and understand it. So it's my favorite part live coding time, so let's first start off with fallbacks, all right. So a fallback actually was an interesting proposal that used to be available in Mozilla's browser for JavaScript. You could actually put a magic function on any object, and this magic function would be called if anything was called on the object that did not exist.

I think proxy objects do that, now in JavaScript so you can kinda recreate this functionality. But it was really cool cuz you could just have this magic fallback. Python has this right if I'm not mistaken, I'm sure other languages do I have no idea. So let's go let's see where are we let's jump in here and let's create something called fallback dot soul.

In my head I still say shit out of luck every single time I'm gonna go right here and I'm gonna import in hard hat, right? console dot soul. And let's go fall back to that and let's create a internal function. I'll call it foo internal, and it doesn't really matter, I guess I can go here, yeah, why not?

Are just gonna go console dot log, hello world. Let's see Somehow got down in but it worked out, I think I might actually have to call to view because I'm referring to console dot log. I'm not really sure if that is there is not true and there we go, so I created this internal function.

No one can call this function but me and subclasses, then I'm going to create a special function called fallback. Now what I want you to notice about fallback is that I do not put the word function in front of it. Much like constructor, you don't specify it's a function it has a very specific meaning.

Next, I put the word external, fallback functions cannot be called by the contract itself, it may only be called from the outside. Lastly, I put payable, because you don't know if you're gonna get paid or not from this fallback, right, so, we shouldn't dictate that. And so what we're gonna do is I'm gonna like this, I'm gonna call foo.

I'm gonna go console.log fallback and then on top of it, I'm just gonna call it revert and go, you shouldn't be here. Right cuz to do a proper fallback function requires us to break into assembly, assign call data to locations, call out to something else. Assign the return data into other, it's fairly complicated to get it perfectly correct.

So I'm just gonna shortcut it right here and get to right here, so close enough, right? Because I don't need to understand how these work, I just need to understand or I don't need the standard specific minutiae of them. I just need to understand the how. So now that we have that, let's go to our deploy script, I'm gonna deploy fall back to s go bam going here.

Let's see is going here, Yank that, paste it in here. And deploy, fallback, going here, rename that, fallback, fallback, awesome, and is there anything we need, probably not. Fall back, there we go, down and here. Alright, and so instead of calling count actually, why not let's just call count, doesn't really matter, does it?

No, it doesn't. So there we go, we have deployed or fallback. We have an address, and then we're just gonna call count, count does not exist on fallback, correct? So we can't quite do that because ethers is going to fail. Because ethers requires some, it requires an API to hydrate these magic functions.

So I'm gonna kind of cheat, I'm gonna show you a really cool thing. So let's go back to fall back, and let's go like this, I'm gonna erase all this nonsense here. I'm gonna create an interface, let's click this interface. I'm going to call it an AI fallback, I know it's great?

And I'm gonna say there's a function called count that is a public function, and that's that, right? So there we go are actually external, you can only say their external, there we go. So this is an interface. So now what I'm gonna do? I'm gonna go back to my deploy code, and I'm gonna go let's see cost F equals ethers await, get contract at fallback address.

I'm gonna use the address of the fallback contract that we just got done creating, which does not have count or whatever, what did I put in here count as a function. And what I'm gonna use, is I'm gonna use I fall back as what it should be. So if I believe I got these contracts or these things in the correct order.

I'm gonna get an I fall back remember this hard hat they put this method here for me. They've compiled our contracts, they know that there's an interface called I fallback. We're gonna use that interface and apply it to an address that definitely does not have these functions. And then I'm going to call count.

So let's see what happens, let's just jump in here and find a MPX hardhat run and no rink B. We wanna pay some money here for nothing and then let's go network local host just like before. I am running a node perfect, so if I've done everything correctly, this should just launch, there we go.

Look at the error I got, you shouldn't be here, so we did, we executed the fallback on an object, and even more so. Look at what executed, hello world, fallback, which if you look at this thing, Hello worlds from foo fallbacks right here. so we're able to interact with our contract read data call functions do something and then we can call revert or we could return data.

It's a little bit like I said the returning is pretty confusing because we don't actually specify return here. But that doesn't mean you can't return, solidity in the assembly world pretty much allows you to do anything and. You'll find out more I'm going to kind of pull the rug out if you will on just how anything goes in solidity here shortly.

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