📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有109部Youtube影片,追蹤數超過2萬的網紅嫣紅,也在其Youtube影片中提到,粵(香港黃金海岸酒店) 屯門香港青山公路1號 香港黃金海岸酒店 低層地下 電話: 2452 8668 黃金海岸酒店staycation: https://youtu.be/Gvgzebrp3fg 老爺煎豬扒: https://youtu.be/XvC_Vh5XEEs ?? 本c9已開Insta...
「card shuffle」的推薦目錄:
- 關於card shuffle 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於card shuffle 在 Engadget Facebook 的最佳解答
- 關於card shuffle 在 Patrick Kun Facebook 的精選貼文
- 關於card shuffle 在 嫣紅 Youtube 的最佳解答
- 關於card shuffle 在 嫣紅 Youtube 的最佳解答
- 關於card shuffle 在 南方家園 Youtube 的最佳貼文
- 關於card shuffle 在 Creating a card shuffling program Java - Stack Overflow 的評價
- 關於card shuffle 在 Write a method to shuffle a deck of cards. It must be a perfect ... 的評價
- 關於card shuffle 在 22 Shuffle Card Designs ideas - Pinterest 的評價
- 關於card shuffle 在 When you randomly shuffle a deck of cards, what is the ... 的評價
card shuffle 在 Engadget Facebook 的最佳解答
Newegg Shuffle aims to give everyone a fair shot at a new console or graphics card.
card shuffle 在 Patrick Kun Facebook 的精選貼文
When you think card shuffling is too basic for you.
🔻TAG 2 friends who CAN'T shuffle
card shuffle 在 嫣紅 Youtube 的最佳解答
粵(香港黃金海岸酒店)
屯門香港青山公路1號
香港黃金海岸酒店 低層地下
電話: 2452 8668
黃金海岸酒店staycation:
https://youtu.be/Gvgzebrp3fg
老爺煎豬扒:
https://youtu.be/XvC_Vh5XEEs
??
本c9已開Instagram, 有興趣請進來聊聊天:
http://instagram.com/redbouquet118/
BGM :
Mary Had A Little Lamb (instrumental) by The Green Orbs from YouTube Audio Library: https://www.youtube.com/audiolibrary_...
Kevin MacLeod創作的「Five Card Shuffle」是依據 創用 CC (姓名標示) 4.0 授權使用。 https://creativecommons.org/licenses/by/4.0/
來源: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100227
演出者: http://incompetech.com/
# 黃金海岸酒店
# 粵
card shuffle 在 嫣紅 Youtube 的最佳解答
??
本c9已開Instagram, 有興趣請進來聊聊天:
http://instagram.com/redbouquet118/
BGM :
「Kevin MacLeod」創作的「Happy Bee」是根據「Creative Commons Attribution」(https://creativecommons.org/licenses/by/4.0/) 授權使用
來源:http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1300014
演出者:http://incompetech.com/
Kevin MacLeod創作的「Five Card Shuffle」是依據 創用 CC (姓名標示) 4.0 授權使用。 https://creativecommons.org/licenses/by/4.0/
來源: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100227
演出者: http://incompetech.com/
card shuffle 在 南方家園 Youtube 的最佳貼文
把照片貼在牆上,過去一年看起來就沒那麼空白了。
–
【作家一日】
作家不寫作的時候都在做什麼?
南方家園拍攝五位作家的一天,
一起來看看作家不為人知的(私)生活。
–
《這裡的電亮那裡的光》 👉 https://bit.ly/3mLYWzZ
作家|田品回 👉 https://www.facebook.com/tianpinhui
設計|陳恩安
出版|南方家園
–
◆影片製作◆
導演、攝影|田品回
後製|黃琪樺
◆配樂使用◆
Kevin MacLeod創作的「Five Card Shuffle」是依據 創用 CC (姓名標示) 4.0 授權使用。 https://creativecommons.org/licenses/by/4.0/
來源: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100227
演出者: http://incompetech.com/
-
Kevin MacLeod創作的「Easy Lemon 60 Second」是依據 創用 CC (姓名標示) 4.0 授權使用。 https://creativecommons.org/licenses/by/4.0/
來源: http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1200077
演出者: http://incompetech.com/
card shuffle 在 Write a method to shuffle a deck of cards. It must be a perfect ... 的推薦與評價
It must be a perfect shuffle – in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator ... ... <看更多>
card shuffle 在 22 Shuffle Card Designs ideas - Pinterest 的推薦與評價
Feb 7, 2021 - Explore Paul Pintilie's board "Shuffle Card Designs" on Pinterest. See more ideas about playing cards design, playing cards art, card design. ... <看更多>
card shuffle 在 Creating a card shuffling program Java - Stack Overflow 的推薦與評價
... <看更多>