📜 [專欄新文章] Gas Efficient Card Drawing in Solidity
✍️ Ping Chen
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Assign random numbers as the index of newly minted NFTs
Scenario
The fun of generative art NFT projects depends on randomness. The industry standard is “blind box”, where both the images’ serial number and the NFTs’ index are predetermined but will be shifted randomly when the selling period ends. (They call it “reveal”) This approach effectively solves the randomness issue. However, it also requires buyers to wait until the campaign terminates. What if buyers want to know the exact card right away? We’ll need a reliable onchain card drawing solution.
The creator of Astrogator🐊 isn’t a fan of blind boxes; instead, it thinks unpacking cards right after purchase is more interesting.
Spec
When initializing this NFT contract, the creator will determine the total supply of it. And there will be an iterable function that is randomly picking a number from the remaining pool. The number must be in range and must not collide with any existing ones.
Our top priority is accessibility/gas efficiency. Given that gas cost on Ethereum is damn high nowadays, we need an elegant algorithm to control gas expanse at an acceptable range.
Achieving robust randomness isn’t the primary goal here. We assume there’s no strong financial incentive to cheat, so the RNG isn’t specified. Implementers can bring their own source of randomness that they think is good enough.
Implementation
Overview
The implementation is pretty short and straightforward. Imagine there’s an array that contains all remaining(unsold) cards. When drawIndex() is called, it generates a (uniform) random seed to draw a card from the array, shortens the array, and returns the selected card.
Algorithm
Drawing X cards from a deck with the same X amount of cards is equal to shuffling the deck and dealing them sequentially. It’s not a surprise that our algorithm is similar to random shuffling, and the only difference is turning that classic algo into an interactive version.
A typical random shuffle looks like this: for an array with N elements, you randomly pick a number i in (0,N), swap array[0] and array[i], then choose another number i in (1,N), swap array[1] and array[i], and so on. Eventually, you’ll get a mathematically random array in O(N) time.
So, the concept of our random card dealing is the same. When a user mints a new card, the smart contract picks a number in the array as NFT index, then grabs a number from the tail to fill the vacancy, in order to keep the array continuous.
Tweak
Furthermore, as long as the space of the NFT index is known, we don’t need to declare/initialize an array(which is super gas-intensive). Instead, assume there’s such an array that the n-th element is n, we don’t actually initialize it (so it is an array only contains “0”) until the rule is broken.
For the convenience of explanation, let’s call that mapping cache. If cache[i] is empty, it should be interpreted as i instead of 0. On the other hand, when a number is chosen and used, we’ll need to fill it up with another unused number. An intuitive method is to pick a number from the end of the array, since the length of the array is going to decrease by 1.
By doing so, the gas cost in the worst-case scenario is bound to be constant.
Performance and limitation
Comparing with the normal ascending index NFT minting, our random NFT implementation requires two extra SSTORE and one extra SLOAD, which cost 12600 ~ 27600 (5000+20000+2600) excess gas per token minted.
Theoretically, any instantly generated onchain random number is vulnerable. We can restrict contract interaction to mitigate risk. The mitigation is far from perfect, but it is the tradeoff that we have to accept.
ping.eth
Gas Efficient Card Drawing in Solidity was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有71部Youtube影片,追蹤數超過1萬的網紅iPhone瘋先生,也在其Youtube影片中提到,iPhone 13 Pro vs. iPhone 12 Pro穩定測試,你覺得哪邊比較穩? iPhone 13開箱: https://mrmad.com.tw/iphone-13-unpacking...
「unpacking」的推薦目錄:
unpacking 在 Joanna Lim Facebook 的最佳解答
Busy week ahead! 😣 Packing and Unpacking = nightmare!
unpacking 在 Facebook 的最讚貼文
Pastel Pet Cat 🐈❤️✨
_
Moved into new home and I’m sleeping at 12am and waking up at 8am . ( used to sleep at 3-4 am then wake up at 11am 🤣 )
There is a lot of unpacking to do and new furniture coming in , can’t wait to get everything done so I can do new photoshoot soon !
_
Pastel Pet Cat available as Patr🌸n Reward this month ! 30 photos for this set in total ✨
Link in Bio !
_
📸 @kenn_tee
#blessed #nekogirl #kemonomimi #neko #nekomimi #pastelhair
unpacking 在 iPhone瘋先生 Youtube 的最佳貼文
iPhone 13 Pro vs. iPhone 12 Pro穩定測試,你覺得哪邊比較穩?
iPhone 13開箱: https://mrmad.com.tw/iphone-13-unpacking
unpacking 在 白同學DIY教室 Youtube 的最讚貼文
產品購買窗口:唯一支持白同學
白同學專屬官網汽車百貨連結 https://www.inco.com.tw/events/btx?rcode=BTX
覺得內容精采想打賞贊助白同學的朋友們,歡迎您贊助打賞~謝謝
綠界斗內入口 https://p.ecpay.com.tw/7799A89
unpacking 在 還不過來Come.188 Youtube 的精選貼文
Fried instant noodles with scallop sauce for the first time is really delicious
Unpacking today-Xiangxiang Xiaoyu dried chili scallop sauce, really a good helper for cooking
*If you need Sub in other languages, please click cc, automatic translation, choose a language, it will be more interesting
Facebook:https://www.facebook.com/come.188
Instagram:https://www.instagram.com/come.188/
--------------------------
**Material
Scallop sauce: moderate amount
Egg: 1
Steamed noodles (instant noodles): 1 pack
Hongxi Mushroom (Shiitake Mushroom): moderate amount
Water lotus vegetable (green vegetable): moderate amount
Carrots: moderate amount
Pork or beef or seafood: moderate amount
---------------------------------
**Recipe
Scallop sauce :as needed
egg: 1
Steamed Noodles (Instant Noodles): 1
Hongxi Mushroom (Shiitake Mushroom): as needed
Water lotus vegetable(vegetables): as needed
carrot: as needed
Pork or beef or seafood: as needed
-------------------------------------------------- ----
Easy cooking series:
https://youtube.com/playlist?list=PLFThU8PKal0YKLOgeuvZ90Ax8FUVTYXjP
Gourmet snack series:
https://youtube.com/playlist?list=PLFThU8PKal0Ycx1WvL4CYS1VbDnxFUiGg
Tourist Attractions Series:
https://youtube.com/playlist?list=PLFThU8PKal0YeZ23kWie807gfT92UHa3a
White noise series:
https://youtube.com/playlist?list=PLFThU8PKal0bLjS_cWzNJLyAymnXxd9fU
-------------------------------------------------- -
0:00 Delicious fried instant noodles
0:07 Materials and preparation
0:33 Simple steps to stir-fry instant noodles
1:49 Fried instant noodles
2:01 credits
*Translation:
1. Fried instant noodles
2.Fried instant noodles
3. Yang げインスタントラーメン
4.튀긴 인스턴트 국수
5.फ्राइड इंस्टेंट नूडल्स
6.نودلز مقلية سريعة التحضير
7.Mì gói
8.บะหมี่กึ่งสำเร็จรูปทอด
9.Frixum instant noodles
10. Жареная лапша быстрого приготовления
#Fried Instant Noodles
#Fried noodles
#Scallop sauce
#Fried Noodles
#Instant Noodles
unpacking 在 #unpacking - Explore | Facebook 的推薦與評價
Unpacking is a zen puzzle game from Witch Beam about the familiar experience of pulling possessions out of boxes and fitting them into a new home. Part block- ... ... <看更多>