📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有221部Youtube影片,追蹤數超過254萬的網紅japanesestuffchannel,也在其Youtube影片中提到,Re-MeNT 'Crayon Shinchan Room'. Shinchan is cute, isn't it? The price is 5600 yen(51 USD). Chapters: 0:00 unboxing 01:19 Shinnosuke 02:34 Action Kamen...
「blind box」的推薦目錄:
blind box 在 Facebook 的精選貼文
😍TODAY!!! 😍
“ARTISTS COLLIDE” 💥 A BLIND BOX SERIES with 3D NFT Toy Collectibles launching on September 10th 12PM EST @ OpenSea
🔗For more info, check out artistscollide.com
blind box 在 Facebook 的最佳解答
#ComingSoon 🧡💜💚
“ARTISTS COLLIDE” 💥 A BLIND BOX SERIES with 3D NFT Toy Collectibles launching on September 10th 12PM EST @ OpenSea
🔗For more info, check out artistscollide.com
blind box 在 japanesestuffchannel Youtube 的精選貼文
Re-MeNT 'Crayon Shinchan Room'. Shinchan is cute, isn't it? The price is 5600 yen(51 USD).
Chapters:
0:00 unboxing
01:19 Shinnosuke
02:34 Action Kamen
04:33 Kantam Robo
05:41 Futon
06:56 Himawari
08:17 Bento
10:29 Breakfast
11:56 Shiro
13:26 compilation
#japanesestuffchannel, #shinchan, #miniature
blind box 在 toysrevil Youtube 的最佳解答
Introducing the "Everyone Needs Break Time Series" blind box series from POP MART X CoolrainLABO - to be available June 25, 2021 worldwide on all channels. Check in with your fav retailer(s) for pricing and availability.
*All #POPMART coverage #onTOYSREVIL: https://bit.ly/35Ezl4c
*All #CoolrainLABO features on #TOYSREVIL: https://bit.ly/35E2AUQ
Track used: "Experience Nature Experience You" by South London HiFi
............................
*If you like or enjoy what I do, please do consider buying me a coffee at:
https://www.buymeacoffee.com/TOYSREVIL
*Donations also accepted via PayPal ("toysrevil@yahoo.com").
THANK YOU for your support of TOYSREVIL!
..............................
READ #toynews & Pop Culture on:
- http://toysrevil.blogspot.com
TOYSREVIL SOCIALS:
Instagram: https://instagram.com/toysrevil...
Twitter: http://twitter.com/toysrevil
Facebook: https://www.fb.com/toysreviler
SHOP @
- https://toysrevil.bigcartel.com...
- http://cardshoppe/bigcartel.com...
WHO-IS "TOYSREVIL"?
- https://about.me/toysrevil
..............................
blind box 在 MAZK TV Youtube 的最佳解答
Mùa dịch này chơi thôi chưa đủ, phải tậu thêm vài bí kíp ảo thuật nữa nha mọi người. Đảm bảo ai cũng hú hồn chim én với 3 trò ảo thuật cực xịn nhưng lại siêu đơn giản này nha.
=================================
Hãy nhấn SUBSCRIBE/ĐĂNG KÝ tại https://s.creatory.vn/mazktv ngay để theo dõi những video mới nhất từ MAZK!
=================================
Bí ẩn sau màn ảo thuật CƯA TAY ?! | 4 TRÒ ẢO THUẬT SIÊU ĐƠN GIẢN tại nhà | Mazk Phá Phách #139
#MazkPhaPhach #MazkTv #OopsMazk
=================================
Cám ơn các bạn đã theo dõi! Đừng quên xếp hạng cho video này.
Nếu các bạn thích video này, hãy để nhấn like, share và để lại comment.
=================================
YOUTUBE CHANNEL MAZK ► : http://www.youtube.com/c/OopsMazk
? FACEBOOK FANPAGE MAZK: ► : https://www.facebook.com/mcOopsMazk
? TIKTOK MAZK ► : https://www.tiktok.com/@oops.mazk
? INSTAGRAM ► : www.instagram.com/oops.mazk
=================================
✩ Website Creatory: https://www.creatory.vn
✩ Facebook Fanpage Creatory: http://facebook.com/CreatoryVN
✩ Liên hệ hợp tác / For Business: partners@creatory.vn
=================================
© Bản quyền thuộc về Mazk TV
© Copyright by Mazk TV Channel ☞ Do not Reup
blind box 在 沙巴盲盒潮玩Sabah Blind Box - Home | Facebook 的推薦與評價
100% Genuine, Original Products Only. Authorised and Licensed. This is a place where all people... M-1-78, First Floor, City Mall, Jalan Lintas, 88300... ... <看更多>