An Analysis of a Good NFT Contract - Radik Sienkiewicz

web3con 2022 · 5 Mar 2022 · 31:48

Radik Sienkiewicz, a UX consultant moving into web3 development, walks step by step through the live Crypto Coven NFT smart contract. The review explains why a clean contract matters once it is deployed, then examines gas savings from skipping ERC721Enumerable, Merkle proof allow lists, reusable modifiers, pre-approved marketplace listing and ERC-2981 royalty handling. It also unpacks a gifting bug in version one that led to a redeployed contract, and closes with takeaways for anyone writing their own NFT contract.

Speakers

Topics

From web3con 2022, hosted by Developer DAO.

Transcript

this talk is sponsored by ipfs and filecoin ipfs and the filecoin foundation are fully distributed open source organizations that aim to help radically improve the internet and drive breakthroughs in computing which can help move humanity forward learn more at filecoin.io and ipfs.io an analysis of a good nft contract based on the cryptocurrency smart contract so hi i'm radhikshenkovic i'm a senior ux conversion communication consultant currently transitioning into full-time web 3 development and you can find me at velvetshockdog.com

where i document my wealthtrade journey so who is this stock for so first of all it's for smart contract developers obviously but also for smart contract learners because there are a few very interesting tips and tricks things that you can do with your smart contracts that you are usually not done so a lot to learn here also if you like witches you will love crypto government so definitely tag along as well so why is having good contract important it's one and done once you deploy it it's on the blockchain and there's no taking back you cannot just go onto the server and

edit it and fix it uh for example cryptopunx uh one of the most valuable nft collections out there and cryptocurrent that i'm reviewing as well both those nft collections are version two actually uh both collections were deployed and at some point there was a bug uh and there's no fixing the bag so it had the both collections had to be deployed so what we are seeing and what we are thinking is is the main version one it's actually version two uh with version one being somewhere on the blockchain as well just not being used so why else is a good contract

important gas savings so both from the creators point of view when you create and launch and deploy the uranus collection and from the users and trainers point of view when you mint when you list when you trade the the nfds uh all of that requires gas and if it's optimized everybody wins two other things royalties and security royalties is what you're as an nfd creator this is your livelihood when it's being traded on the second market you you get royalties so it's best for you to stay in control of

that and security so that somebody down the line doesn't come and just drain whatever you have earned so what is crypto cover so cryptocommon is a collection of 9999 digital witches which is not really the case uh because it's 9906 actually but we'll see later on why it was a bug in the contract just uh a sneak peek but it's also much more than just a profile picture and of the collection because there is a story behind it there are all the details the consistency between everything the story art even the

the smart contract everything else so uh a very very interesting collection and at every aspect including the contract so why is the contract awesome gas savings i've mentioned already readability and simplicity very well written code whitelist approach different from what most uh nfts do right now using miracle tree no listing fees uh so when when you mint your your witch your nifty and you want to to sell it you need to to pay a fee

to to actually list it to to enable that collection to be listed this contract has a function that you don't have to pay that fee it's it's been pre-approved and royalties uh is also handled interestingly uh more on that later so in the words of cryptic oven which is worth a million murmurs on the wind so let's just see what the code does and how it looks so the way it starts yeah i'll just go through the whole contract on with which you can find on ether scan wherever else

and it starts unapologetically on brands you have a nice historically cool ascii word art crypto coven in large letters and also it's both historical and modern because it uses uses the latest solidity version 0.8 further down the line uh a bunch of imports from open satellite uh industry standard but what's in interesting both what is included and what is omitted from uh from those imports so erc 721 is included but not

erc721 enumerable which is what most of the nft collections use this is for gas savings and it uses a different approach than innumerable it doesn't make things easier but it makes things more optimized another is ierc2981 so that's for handling royalties and merkle proof for for allow list claiming methods more on that later as well so just uh to

give a little note here there's a lot of code in this presentation and i don't have a lot of time i have a maximum of 20 minutes so i will go briefly through all of that uh but there's a lot of very interesting details here so at the end i'm also including the link to the presentation where i list all of those uh simple uh small maybe not that simple but small details that uh that you can take a look later on uh if if i wanted and if i could i would i could go on for more than an hour on that or even more uh but as i have only 20 minutes i will go briefly over all

the functions all the code uh there is a lot of interesting tips and tricks to discuss but if you want to look at all the details you can find the presentation the link to the presentation at the very end so you can read it later and go through all the code yourself so to start with the the modifiers they're all the beautiful modifiers uh so instead of just having logic included inside each and every function most of that logic is just taken outside into modifiers which makes the code clean reusable

readable and the functions using those modifiers are the thing of beauty which we will see in a second so this this is the first and major function to mint so it's one of the main things you can do with an nft uh you can mint it and as you can this is the whole function uh and this is actually this is the whole code uh one single loop and it wouldn't be even a loop if uh if you could just mint one one token one nft but you can mint up to three

so that's why the loop is here everything else is modifiers so you can see it now the re-entrant modifier is correct payment modifier is public sale active can you mint the witches and do you have the maximum which is per wallet or not all of that very clear very understandable uh put on top of that function as modifiers and i think really what can i say another function is mint community style community sale this one is a little bit more complex between because this one needs to make a few more checks so can you

can you mint are you on the community sale list and this uses uh merkle proof and you have your uh your your code that you generate i think it was generated in the in the discord channel uh and then you come with that uh miracle proof and you you enter it when minting which is then uh verified with this uh function a few more checks if you have no more than three which is if there are which is left to mint and so

it updates the number of community bridges in in the mapping so adds your address uh to to that mapping so that you cannot mint anymore uh and then actually means another one is claim function so crypto government reserved 250 which is for gifting for two i don't know influencers or community members uh or anyone else that they thought would need to have it would want to have it so they don't have to pay for it they're just gifted it so the claim is a function to actually take those switches to claim those switches

again miracle proof for verification that you're actually on that list that you can't mint and get that gifted which uh max is at 250. uh it checks if you have claimed the witch already or not uh and after claiming it adds you to the mapping uh which it actually compares against here and and then you can you can meet your witch uh safely there is a bunch of different admin functions that i will not go through because they are pretty self-explanatory and they just usually do one thing

uh and and that's it used for for modifiers so set base uri so this is for ipfs uh everything else is pretty self-explanatory said verification hash is public sale active or not is community self active or not uh set the merkle roots for a community list and proclaim list both mentioned already reserved for gifting and the last one is also interesting so as i mentioned previously this is version two of the contract uh in version one of the contract there were 128 which is minted before the contract broke so this function actually takes those 122 128 which is

and moves those to the new contract so that those who minted those do not have to do it again so now what broke in uh version one of the contract so this this is an interesting one uh because we can learn from this uh uh the gift which is function was at fault so so this is uh the function from version one of the contract so this is the faulty function and the this is where the actual error uh occurred so uh what what it was doing it was doing that uh

gift witches um an array of addresses it takes an array of addresses to to gift to and uh it first it checks the the number of gifted witches yeah so the requirement is that you cannot have more than three witches per wallet so this requirement here is it checks if if you can even get that which so if you already have three witches in your wallet uh the gift which is function just skips you because three is the maximum that you can have per wallet uh so it checks if you have three if if you do then it

skips it doesn't give you that which and that is actually what eventually broke this uh so it checks that requirement uh and then it means those uh it yeah it gives those those switches so it it gives it takes that um array of addresses and [Music] one by one it checks if that address has the maximum amount of which is or not and and that means uh but let's see what what it does and how it does it so it takes let's say

that the array has three addresses uh this uh this loop has uh has three addresses in the array so for and the second address let's say has three maximum widgets so at the first pass of this loop uh i is zero uh address has less than three three which is so we can uh we can mint uh we can we can gift the the witch so this is how the safe mint uh function uh safe safe mint goal here looks like uh total let's say total supplies at one hundred uh so total supply to this address plus zero plus one so i is zero for for this

loop uh and total supply this is for to actually generate the token id so what's the token id of this switch so at the first pass it's it means uh a witch with a token address 101 to this address fine we go to the second bloop and the second loop we have that that address that already has three witches in the wallet uh so what it does uh it it skips this one uh because it uh it fails here it fails uh this requirement if balance of addresses has less than maximum which is provided it

does not it has the maximum which is per wallet so it does nothing and then the third address third wallet what it does is i is now two zero one and two address has less than three uh so we can mint so what it does is it takes total supply which is 100 it adds two uh this is i from the loop uh and it it adds one to it uh so actually if all the wallets had less than maximum uh amount of which is it would be three means of address um token id 101 token nd102 one and token 8103

but one one skipped uh and we have token id 101 and token 8103 what happens so those are minted this is fine this function worked but then when someone next comes and wants to mint uh what so this is what the minting function looks like take the sender's address take the total supply and add one to it and that's the uh the token id but total supply at this point is actually 102 not 103 because one has been skipped here

through gifting so it takes 102 this is the total supply and it adds one to it so actually the token id is 103. and it tries to mint uh a witch with token id 103 but it's already been minted here and the contract breaks so that's how version one of the contract stopped working because you cannot mint uh with the same token token id and at uh what was that at 128 which is this happened the contract stopped working so how can we fix this so on the left uh this is the old function that we have

just reviewed and this is how it looks now in uh in the new new contract so first of all uh this function was split in two so the the logic was uh moved into the modifier uh so now it looks much more elegant so only under can gift which is uh and instead of let's just look here in more detail so instead of just taking total supply adding i uh which is the iterator and adding one to it uh which is not not safe to do there is a next token id function which would never skip a number and is only

assigned the correct token id and it works only when actually the uh that which is minted or or gifted so it doesn't skip any numbers it doesn't skip any token ids and that's a simple fix for for this uh this function so that that's the current version of the contract it works everything is fine so look out for that uh little supply but there is another bug in in the contract and it's actually with uh with gifted witches again so remember i said that it's

it was supposed to be 9999 which is but it's actually 9000 nine hundred and six this is the reason why uh so what was happening uh first of all let's see what uh what was initialized max which is was at nine nine nine nine and as i mentioned also before uh the team decided that they will have 250 witches to to gift to whoever they uh they want to and this is the modifier one of the modifiers can mint witches so what it takes uh it takes uh the token counter number so

how many witches have already been minted uh it takes how many which is you want to mint could be from one two or three three is a maximum uh and it looks if it's uh smaller than max switches which is 999 minus 250 because you cannot mint those 250 so the maximum that you can you could buy on the market was 1 749 so this minus this so so this modifier was looking if uh it's less than 1749

if it is then it meets but while the uh the sale was happening while people were minting uh the team was actually gifting the witches i don't know if you already see the problem or not or maybe you read through the rest of the slide already so the team was already gifting the witches so what happens when team gift the witches while the sale is happening uh so let's say uh the team gifted so yeah let's say what happened uh in reality that while the sale was happening the team gifted 93 witches out of those 250 uh

and when the counter reached 1749 which is here uh you could not longer you could no longer meet but uh it doesn't mean that uh 9749 witches were sold which was uh the intention of of the team of cryptocurrent and uh to to have uh this number uh at the end so this is the number that they wanted to sell uh but it's the number of what they sold including 93 which is that they already gifted

at this point this modifier when you tried to buy didn't allow to buy any more witches and because this uh max gifted witches was also hard coded at 250 they could not increase the number of gifted witches as well so they ended up with uh 996 switches in total instead of 9999 and that's it so how to fix it and there is an easy fix you need to make that this one if you want to gift while people are buying you need to make this uh

the right side of equation dynamic so whenever someone is gifted a witch you need to make to increase this uh right side of equation by one this way uh this side of equation at at this point nine seven four would be larger by 93. so uh 93 more widgets could have been bought and they would end up with 9999 which is in total so that's that's a simple fix for that uh this this is the current version of the contract so there will not be a new one so it's actually this is how many which is they have

ended up with 9906 and that's it uh two more factions to withdraw so they these are pretty standard pretty self-explanatory uh you can uh yeah when someone pays you buys your witches and pays you or when you you get your royalties you actually need to be able to withdraw those functions to your uh to your wallet to be paid for for your work so this is the the first function to get the if so address this balance you you get all the balance uh of these that had that the contract has

and transfer it to um to your wallet and it's only owner so actually it prevents anybody else from doing that uh well you also can withdraw tokens not not just these that's the the second the second function here uh another one uh function that i mentioned already is zero gas so when you have a which uh when you have an nft and you want to sell it on openc for example what you have to do is you you need to initialize that specific collection so even if you have 10 witches for example uh you initialize only once you initialize the collection

and then you can start selling that but it can be prevented in the smart contract by the enft creator that the whole collection is already pre-approved pre-initialized so when you want to list your nft when you want to sell it you don't actually need to pay gas so it's building goodwill for those who want to sell it it's creating them savings another interesting things is how cryptocurrent treats royalties um instead of platform specific that openc for example provides they they have a

specific eip2981 startup standard which is could be helpful further down the line when openc is no longer the de facto monopoly that it is right now uh and when trading elsewhere will be more common or when you actually want to update your royalties or do some more fancy stuff with it uh further down the line but another bug in the current uh contract that what what they have done is uh the return of this uh this function what is what it returned is the address of the receiver

so who will receive that uh that amount of royalty and relative amount so in in their case it's five percent so it's your sale price times prime times 100 uh with safe safemod uh but yeah remember they the cryptocurrent uses the the latest 0.8 uh solidity version but as absolutely 0.6 smart contract cannot receive if directly without without implementing a receive function so if there is no receive function uh you cannot send if to to that contract uh but that was already

live but because of the thanks to royalty registry uh cryptocum could overwrite the receiver address so uh they actually yeah so this receiver is the the actual the address of this contract uh of the cryptocurrent contract so so they overrode it uh to be a receiver a different address of a different wallet uh and they could uh save the the royalty uh this way otherwise they would probably be stuck with no royalties and they would probably need to relaunch the contract and there is simple fix for that there's just like one liner that you add to be able to

receive uh if to your contract which is receive external payable that's it very simple fix so what did nail what they could could have done better they nailed readability and simplicity and they nailed gas savings building goodwill whitelist approach and interesting one which is becoming more and more common now in the industry but uh it wasn't when they launched it no listing fees um royalties uh yeah and what they could have done better gift languages of two bugs uh in two versions of the contract uh could have been handled better while

this configuration they they saved their ass with that but uh yeah it's a simple thing that they needed to keep in mind uh another one that i haven't mentioned before is that they could have stored all the art on ipfs currently they only store the metadata on the ipfs but all the art those beautiful witches that they have is stored on amazon's aws servers if those goes down or if they stop paying or if anything happens uh all the empty collection can break but they they have a way to fix that they they have the base url uh function

that they can update it so in the future they can go to ipfs eventually uh but yeah they may or may not do that if needed key points to remember that's more of a summary to for you to to get back to and to maybe learn from merkle proof royalties pre-approved total supply to be careful with it so if if you're working on your version of smart contract keep those in mind have a look how how they did that have a look at those modifiers i think of beauty and try to to do uh to do it yourself so this yeah picture was worth a

thousand words which probably a million this is my witch uh i i have bought that i own this nfd this is her name and this is actually actually also what the crypto government is doing differently every which has a name usually nfts have just the numbers like nft number of 5525 cryptocurrent which is all have the their names like all 996 have unique names mine is uh the binary scroll sources and acknowledgements uh so i i went through the

current and alt contract analyzing it step by step uh also the cryptocurrency smart contract developer channel uh was uh invaluable in helping so i i contacted her on twitter we had a chat she was very helpful in replying to everything or clarifying all the questions that i had so thanks a lot for that and a few more sources of twitter thread cryptocurrent contract box uh article also by xiameo and this is the link to this presentation for you to go through all the code uh to to take a look in more detail so

thanks let's get in touch this is where you can find me velvetchart.com uh twitter handle my email thanks a lot