ddd009

ddd009

twitter

Contract analysis

Recently, I have been studying smart contract development on the Ethereum (eth) platform. This article is a summary of my study on the Lil Nouns contract. You will learn about the process of Nouns auctions, how to store NFT SVG completely on the chain in a gas-saving way, and how to obtain Nouns traits before the next auction (I have created a tool that can obtain the next Lil Nouns traits in real-time).

Auction Contract#

Those who have participated in Lil Nouns auctions know that after each round of auction, someone needs to manually settle. When you click the "Pick the next lil Noun" button on the official website, your wallet will be prompted to call the settleCurrentAndCreateNewAuction() function of the NounsAuctionHouse contract. This function performs two operations. First, it calls _settleAuction() to settle the previous auction. If no one bids in this round, the Lil Noun of this round will be destroyed. If someone bids, the Lil Noun will be transferred to the winner's wallet. Finally, the bid funds will be transferred to the Lil Nouns DAO.

_

Let's take a look at the implementation of createAuction. When creating an auction, a new noun will be minted first (except for every 10th and 11th Lil Noun, which will be minted for the auction contract). The returned noun ID will be used to update the current auction variable state, and the start and end times will be updated. The settled variable will be set to false.

image

When a user places a bid, it must be greater than or equal to 105% of the previous bid (the fourth require). First, the ETH of the previous bidder will be returned, and then the current bidder and bid will be recorded in the auction variable. Finally, if a bid is placed within 90 seconds before the end of the auction, the current auction will be extended by 90 seconds.

image

Seed Contract#

Now let's talk about how the traits of nouns are determined when minting. Nouns use the blockhash of the previous block and the previous noun ID as a seed to generate a pseudo-random number. Then, this pseudo-random number is used to take the modulus of the total amount of each trait as the value of the trait ID. Of course, the pseudo-random number is right-shifted each time, otherwise, the generated trait ID will always be the same when the total amount of traits is the same.

image

Since it is a pseudo-random number, we can predict the traits of the next nouns as long as we follow the algorithm in the above code. For your convenience, I have created a website where you can see the next Lil Noun in real-time. Just enter the number of the previous Lil Noun in the auction, and you can see the appearance of the next Lil Noun. Of course, you need to ensure that your settle auction transaction is included in the current block.

https://lil-nouns-predict.vercel.app

Finally, let me share a very cool trick. As we all know, storage on Ethereum is very expensive, so most NFT projects choose to store NFT metadata on platforms such as IPFS. Nouns uses the SSTORE2 contract from @0xsequence to optimize gas. SSTORE2 encodes the data into bytecode and deploys it as a smart contract. When reading, it directly reads the EXTCODECOPY of the contract (I just learned about this cool operation for the first time). The only drawback is that it cannot be modified like a state variable, but it is very suitable for storing static data. This way, both the gas for writing and reading data can be significantly optimized.

subscribe://

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.