📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有1部Youtube影片,追蹤數超過6萬的網紅粒方不插電,也在其Youtube影片中提到,遊戲在這裡買: https://myship.7-11.com.tw/cart/confirm/GM2008191889365 竟然有桌遊禁止你跟隊友溝通? 這個設計超神奇 考驗默契和心電感應的桌遊到底怎麼玩呢? #跟我一起 #宅在家 #跟我一起宅在家 https://boardgamege...
ascending order 在 Facebook 的最佳貼文
Reinventing British - back at Jaan for the third time for their new menu and wow, it seems that Chef Kirk has improved tremendously over the years! This new menu features a lot of Jaan’s specialities, with a little twist (this time, a cleaner one) so we were still excited to still be able to have them!
Starters - plated in ascending order, we were told to start from the bottom (Cucumber meringue) making our way to the top (goose mousse) which was 10/10
Finishing off with the roast leek & potato soup that was served with a side of pastry. DAYUM. The pastry was 😍
For the sake of the word limitation, I highly recommend Jaan for consistent good food, nice selection of wines, cheeses and if you’re a fan of bread 🥖, eggs (eggs hen is bomb), langoustine and Wagyu (melt in your mouth for real) …I think Jaan has perfected them all.
With the stunning views and impeccable service, it’s a 9/10 ⭐️ for both Matt & I, and I think the price tag of $288++ per pax is well-worth it. Unlike some fine dining places, you never leave Jaan hungry!
#mongabongeats #jaan #jaansg #sgfoodies #Mnmcouple
ascending order 在 K-2 Fashionboy Facebook 的最佳解答
【K-2】MAXWDF 雪山 ASCENDING ORDER XXX 極地 雪山山脈 潮流 短T 寬鬆上衣【BB0629】
顏色 : 黑色 / 白色
尺寸 : M / L / XL / XXL
蝦皮商城►https://reurl.cc/GmpEXA
官網訂購(免登入) ►https://reurl.cc/j8yG1n
* * 有體店面,品質保證最放心 * *
(最後一張圖片有尺寸表)
-
多款上衣 ► http://goo.gl/4XtSUa
〈路過可以來看看有沒有需要的款式〉
LINE官方 : https://goo.gl/j7N5qr
IG追蹤 ► https://www.instagram.com/k2hbkai0820/
ascending order 在 粒方不插電 Youtube 的最佳貼文
遊戲在這裡買: https://myship.7-11.com.tw/cart/confirm/GM2008191889365
竟然有桌遊禁止你跟隊友溝通? 這個設計超神奇 考驗默契和心電感應的桌遊到底怎麼玩呢?
#跟我一起 #宅在家
#跟我一起宅在家
https://boardgamegeek.com/boardgame/244992/mind
The Mind is more than just a game. It's an experiment, a journey, a team experience in which you can't exchange information, yet will become one to defeat all the levels of the game.
In more detail, the deck contains cards numbered 1-100, and during the game you try to complete 12, 10, or 8 levels of play with 2, 3, or 4 players. In a level, each player receives a hand of cards equal to the number of the level: one card in level 1, two cards in level 2, etc. Collectively you must play these cards into the center of the table on a single discard pile in ascending order but you cannot communicate with one another in any way as to which cards you hold. You simply stare into one another's eyes, and when you feel the time is right, you play your lowest card. If no one holds a card lower than what you played, great, the game continues! If someone did, all players discard face up all cards lower than what you played, and you lose one life.
You start the game with a number of lives equal to the number of players. Lose all your lives, and you lose the game. You start with one shuriken as well, and if everyone wants to use a shuriken, each player discards their lowest card face up, giving everyone information and getting you closer to completing the level. As you complete levels, you might receive a reward of a shuriken or an extra life. Complete all the levels, and you win!
For an extra challenge, play The Mind in extreme mode with all played cards going onto the stack face down. You don't look at the cards played until the end of a level, losing lives at that time for cards played out of order.

ascending order 在 What Does Ascending Order Mean? Definition and Examples 的相關結果
In maths, ascending order means the process of arranging numbers from smallest to largest from left to right. It can also mean arranging letters or words in ... ... <看更多>
ascending order 在 Ascending Order - Learn Alberta 的相關結果
Arranging numbers (or other items) in ascending order means to arrange them from smallest to largest. Example 1 (with Numbers). The numbers 12, 5, 7, 10, 1, 160 ... ... <看更多>
ascending order 在 What is Ascending Order? - Definition, Facts & Example 的相關結果
Ascending order means to arrange numbers in increasing order, that is, from smallest to largest. ... To arrange numbers in any order, we first need to compare ... ... <看更多>