How The Immutable X NFT Contract Works

Transcript

Toggle full transcript
00:00

Hello and welcome to chapter two of the Create Configure and Sell NFT collections on ImmutableX course.

00:10

In this chapter we're going to cover how to create NFTs on ImmutableX. This first lesson covers how the ImmutableX NFT contract works.

00:20

This chapter centres around a series of practical labs where we will interact with some source code to mint our first NFT

00:30

on ImmutableX. In this lesson, we're going to read through the Solidity code that makes up the smart contract that sits behind an immutable x NFT.

00:40

In order to mint an NFT on ImmutableX the Solidity smart contract of your NFT asset needs to implement two interfaces.

00:50

The first is the one that you can see here on the screen. IMintable. IMintable, as you can see, is a fairly simple interface.

01:00

It has a single method mintFor that takes an address, a quantity, and a series of bytes.

01:10

Now this mintFor method is what ImmutableX calls when it is minting an NFT the first time that it is withdrawn

01:20

from ImmutableX to Ethereum. The second interface that your contract needs to extend is

01:30

the Ownable interface, which is exposed by Open Zeppelin. And this just indicates that the token can actually be owned by someone.

01:40

Now, ImmutableX provide us the mental abstract class to make it easier to create an immutable X compatible contract.

01:50

You can see that the IMintable class here extends the owner and IMintable interfaces. Now, before we dive into the detail about what this Mintable class does,

02:00

let's have a look at a simple implementation of an NFT that extends this abstract class.

02:10

And here it is here, our asset class. And you can see that our asset class extends ERC-721 that again we're getting from Open Zeppelin and the IMintable abstract base

02:20

class provided by ImmutableX. By extending these two base classes, we're able to have a fairly simple

02:30

30 line Solidity file that creates an IMX compatible NFT. There's three methods here that we needed to implement.

02:40

One of them is the constructor which calls into the constructors of both ERC-721 and IMintable. It's up to you what you put in your constructor as long as you can call these

02:50

underlying constructors underneath. In this case, we take the address of the owner that we want to own

03:00

the NFT that's being minted. The name and symbol of that NFT and a base. Hooray. The other important one here is there's an address being cast in that denotes the

03:10

IMX layer one smart contract. This then gets passed into the IMintable base class and we'll talk in a second

03:20

about what that actually does. The other two methods are the mintFor method and the baseUri method. The mintFor method is an abstract method exposed by the IMintable

03:30

base class and the baseUri method is an abstract method exposed by the ERC-721 base class.

03:40

Mintfor is called by the IMintable Base class and will see the circumstances under which that gets cold. And essentially what we're doing here is we're just allocating to the safe Mint

03:50

method that's provided by ERC-721. The way this works is the Mintable base class handles the implementation

04:00

of when ImmutableX calls our contract to mint an NFT, when that NFTs first being withdrawn to Layer 1 from ImmutableX.

04:10

And so in this case, given we're implementing the ERC-721, which is proxying that call through to the 721 code.

04:20

Additionally, we just need to implement how the ERC-721 contract can figure out what the baseUri is for this smart contract.

04:30

In this case, the implementation is simple and we simply pass it in at the point in time that we construct this particular NFT through the constructor

04:40

and then pass that through. But equally, you can do more complex implementations if you want to. So we've covered our assets so far here.

04:50

This is the most basic implementation, but you can add other things if you want to. The real magic then is what happens in mint or bought or sold.

05:00

So let's have a look at that. Now, the key thing here that needs to be implemented is from the IMintable interface we had that meant for method.

05:10

That's the signature that IMX Smart contract is expecting for the moment in time when it needs to mint an NFT when it's being withdrawn to layer one.

05:20

And we can see here that there's an implementation of that meant for method. It takes the three parameters that we talked about before a user address,

05:30

a quantity and the Minting blob. And in this implementation, there's a few things going on here. Firstly, because we are meeting an NFT, if the quantity is greater than one, then we

05:40

actually throw an error. Secondly, we take this minting blob and we actually call this split command here.

05:50

Now this is provided in the utils slash Minting dot sole file. And essentially what it does is it allows for a

06:00

byte string that looks something like this token ID colon blueprint, where a blueprint can be any string that you want to pass through. This is simply the way that ImmutableX encodes this information.

06:10

Acknowledging that whenever you meet an NFT on ImmutableX, there will be an integer token ID and there will be a blueprint string that had been passed through

06:20

to the ImmutableX call. And we'll see that later on in the lapse in this chapter. Understanding the actual implementation of this method is left as an exercise to the

06:30

watcher. So once we've split out from our Minting blob byte array into the separate token ID and by

06:40

the right blueprint, we can then call our _mintFor method. And this is what we looked at in our asset.sol File.

06:50

So this is a abstract method that is left for you to implement. And in our basic implementation here, we simply, as we said before, delegate

07:00

to ERC-721. And in fact, in this case, we're ignoring that memory byte string. But if you wanted to, you could actually use it.

07:10

The other thing then that we do is we actually store that blueprint against that token ID, and that's stored in this blueprints property,

07:20

which you can see up here is a mapping from 256 bit unsigned integer to a byte string. And then we simply emit this AssetMinted event which is defined up here.

07:30

Now, in this case, we're not actually using this blueprint at all. But again, if you want to do more advanced use cases, then you can start using the

07:40

blueprint within your contract. The last thing that we need to cover here is this only owner or IMX method, which we're actually applying here as part of

07:50

the Mint for. This is really important because what it does is essentially delegates access

08:00

so that the IMX smart contract on Layer 1 has the permission to call this mintFor method on our behalf.

08:10

So if we come back to the constructor of this Mintable class. We can see that there's two addresses past in: the owner and the IMX address.

08:20

So if we go back to our asset.sol we can see we passed through an owner that we took in the constructor and an IMX address that we also

08:30

took in the constructor. The IMX address is a fixed address depending on the network that you're talking to. And if you go to the official IMX Contracts GitHub site

08:40

and scroll down on the Readme, you can actually find the official contract address for both the Goerli network and

08:50

mainnet as well. Thanks for joining me for this description of the ImmutableX compatible NFT Smart Contract. Now that we've done this, let's start diving into the labs,

09:00

which you can join me for in the next lesson.