📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有7部Youtube影片,追蹤數超過14萬的網紅JP THE WAVY,也在其Youtube影片中提到,"Pick N Choose (Remix) feat. Only U & Young Coco" available at : https://lnk.to/WAVY_Deluxe Lyrics by JP THE WAVY, NVMBRTWNTYNICE, Only U, Young Co...
「pick n choose」的推薦目錄:
- 關於pick n choose 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於pick n choose 在 Toby育兒樂 Facebook 的精選貼文
- 關於pick n choose 在 wongnai.com Facebook 的最佳貼文
- 關於pick n choose 在 JP THE WAVY Youtube 的最讚貼文
- 關於pick n choose 在 JP THE WAVY Youtube 的最讚貼文
- 關於pick n choose 在 JP THE WAVY Youtube 的最佳解答
- 關於pick n choose 在 Pick 'n' choose - Facebook 的評價
- 關於pick n choose 在 Metrology Deptt raids 'Pick N Choose' for cheating ... 的評價
- 關於pick n choose 在 The number of different ways to choose $k$ out of $n$ unique ... 的評價
pick n choose 在 Toby育兒樂 Facebook 的精選貼文
懶人keep fit之選
夏天到喇🌞準備好你嘅summer body未?Tea time口痕痕,加上而家天氣又咁熱,食返杯乳酪做下午茶就最好不過,Keep fit得黎仲好消暑😎揀啱食乜嘢好重要💁♀️
希臘乳酪被譽為減肥恩物,當中蛋白質愈高,飽肚感愈持久,有助減肥🥰以我平時最鍾意嘅就係澳洲製 @FarmersUnionYogurt.hk 有多款口味選擇🤤每杯乳酪提供十億個益生菌,促進腸胃健康。你仲可以根據自己嘅喜愛加入堅果、生果、燕麥等一齊口感就更加豐富啦❤️佢哋嘅乳酪口感厚滑質地頗稀和順滑😌味道濃郁健康之選
BB-12™️ is a trademark of Chr.Hansen A/S
購物點🛒
AEON
Food Le Parc
Fusion
International
Jasons Market Place
Oliver's
ThreeSixty
Taste
惠康Wellcome以及
PARKnSHOP
#希臘式乳酪 #口感厚滑 #味道濃郁 #夏日甜點 #keepfit之選 #Farmers Union #farmersunion #farmersunionyogurt #ad
Translation:
Yogurt is best for tea time, not only does it keep you fit but also cools you off.
Greek yogurt is considered a weight loss friendly food, with high protein, and keeps you full for longer, which helps with weight loss.”
@Farmersunionyogurt.hk has lots of flavours to choose from. Not only filled with probiotics, but u also get to pick n choose ur nuts, fruits, and oatmeal for a richer taste/ flavour. Their yogurt is smooth. The most flavourful and healthiest choice.
BB-12™️ is a trademark of Chr.Hansen A/S
Shopping point 🛒
AEON
Food Le Parc
Fusion
International
pick n choose 在 wongnai.com Facebook 的最佳貼文
ทาโร่บอลนุ่ม ๆ ก็ต้อง จิ่วเฟิ่น ทาโร่บอล เลยค่า✨ พร้อมเสิร์ฟความฟินกับ #โปรสุดคุ้ม คลายร้อน ด้วยจิ่วเฟิ่นเย็น ๆ สารพัดท็อปปิ้งสุดกุ๊กกิ๊ก เลือกได้ตามใจชอบ! ตบท้ายด้วยชานมไข่มุก ฟองนุ่มฟู สดชื่นหัวใจ😝💖 กดสั่ง Pickup ตอนนี้ผ่านแอปฯ LINE MAN ฟรี! แล้วไปรับที่ร้านได้เลย
.
📍 Jioufen
✅ กดสั่ง Pickup รับที่ร้าน : ฟรี! ไม่มีค่าใช้จ่ายเพิ่มเติม :
https://bit.ly/3e9GJaj
.
🎉 ฟินสุดด! ยิ่งสั่ง Pickup ยิ่งได้ลุ้นรับดีลฟรี!!!
มูลค่ากว่า 7,700฿ วันนี้ - 25 มิ.ย. 63 : bit.ly/2XVKACg
.
💥 แจกโปรสุดฟิน
👉🏼 1 แถม 1 เมื่อสั่งจิ่วเฟิ่นทาโร่บอล เฉาก๊วย แปะก๊วย พุทรา 69.- (จากปกติ 138.-)
👉🏼 ซื้อ 2 แถม 1 เมื่อสั่งชานมไข่มุก 2 แก้ว รับฟรี! ชานมไข่มุก 78.- (จากปกติ 117.-)
.
อ่านรีวิวร้านเพิ่มเติมได้ที่
https://bit.ly/3hrMfXO
.
🙆♀ ขอบคุณรีวิวจาก #สมาชิกWongnai โดยคุณ P'pam Sweet
_________________________
#Wongnai #WongnaiBacktoLife
เริ่มต้นใช้ชีวิต New Normal ไปดัวยกันกับ Wongnai ค้นหาข้อมูลร้านอาหารจากทั่วประเทศ และ แบ่งปันรีวิวกันได้ที่แอป Wongnai ▶️ ดาวน์โหลดฟรีที่นี่ >> http://bit.ly/2U0bdns
Soft Taro ball must be @[562138230958696:274: ci ̀ w fe ì n thā rò bxl] ✨ Ready to serve you satisfied with #valuable promotion. Cool down with cool jiwfen. Many topping topping. Choose as you like! Slap the end with bubble tea. Soft, fluffy, fresh heart 😝💖 Order Pickup now via LINE MAN app for free! Then pick up at the shop
.
📍 Jioufen
✅ Order pickup at shop: FREE! No extra charge:
https://bit.ly/3e9GJaj
.
🎉 So happy! The more you order pickup, the more you get a chance to win a free deal!!!
Worth more than 7,700 ฿ today-25 mi Sep. 63: bit.ly/2XVKACg
.
💥 Giving away a super satisfying promotion.
👉🏼 Buy 1 get 1 free. When you order Jiw Fen Taro Ball, Chao, Kuay, Jujujujujujujujujujujujujuj
👉🏼 Buy 2 get 1 free when you order 2 cup of bubble tea, get free! Bubble tea 78.- (from normal 117.-)
.
Read more shop reviews at
https://bit.ly/3hrMfXO
.
🙆 ♀ Thank you for review from #Wongnai members by Khun P ' pam Sweet
_________________________
#Wongnai #WongnaiBacktoLife
Start living in New Normal cuddle together with Wongnai. Find restaurant information across the country and share cuddle reviews at Wongnai app. ▶️ Free download here >> http://bit.ly/2U0bdnsTranslated
pick n choose 在 JP THE WAVY Youtube 的最讚貼文
"Pick N Choose (Remix) feat. Only U & Young Coco"
available at : https://lnk.to/WAVY_Deluxe
Lyrics by JP THE WAVY, NVMBRTWNTYNICE, Only U, Young Coco
Music by Pulp.K & Puckafall
Mixed by JIGG
Artwork by JP THE WAVY & DOPE
Motion Graphics by JACKSON kaki
-JP THE WAVY
Official HP:https://sorrywavy.jp
Instagram : https://www.instagram.com/sorry_wavy
Twitter : https://twitter.com/sorry_wavy
YouTube : @JP THE WAVY
-Only U
Instagram : https://www.instagram.com/i_am_onlyu
YouTube : @Only U
-Young Coco
Instagram : https://www.instagram.com/youngsavagecoco
YouTube : @Young Coco
#JPTHEWAVY #WAVYTAPE2DELUXE #WAVYTAPE2 #OnlyU #YoungCoco #PickNChoose
![post-title](https://i.ytimg.com/vi/_14U7MuqlCs/hqdefault.jpg)
pick n choose 在 JP THE WAVY Youtube 的最讚貼文
"Pick N Choose feat. LEX"
available at : http://smarturl.it/wavy_tape2
Music by Pulp.K
Mixed by JIGG
Director : Nasty Men$ah
Dop : Karetaka Kamiike
Lighting Director : Akihiro Numata
Producer : Takahiro Tamura
Hair : Kyota Ono
-JP THE WAVY
Official HP:https://sorrywavy.jp
Instagram : https://www.instagram.com/sorry_wavy
Twitter : https://twitter.com/sorry_wavy
YouTube:@JP THE WAVY
-LEX
Instagram : https://www.instagram.com/lex_zx_lex_0
YouTube : @LEX *
![post-title](https://i.ytimg.com/vi/CSEGSyHqjW8/hqdefault.jpg)
pick n choose 在 JP THE WAVY Youtube 的最佳解答
"Pick N Choose feat. LEX"
available at : http://smarturl.it/wavy_tape2
Lyrics by JP THE WAVY, NVMBRTWNTYNICE, LEX
Music by Pulp.K
Mixed by JIGG
Artwork by JP THE WAVY & DOPE
Motion Graphics by JACKSON kaki
-JP THE WAVY
Official HP:https://sorrywavy.jp
Instagram : https://www.instagram.com/sorry_wavy
Twitter : https://twitter.com/sorry_wavy
YouTube : @JP THE WAVY
-LEX
Instagram : https://www.instagram.com/lex_zx_lex_0
YouTube : @LEX *
----------------------------------------------------------------------------------
" LIFE IS WAVY & WAVY TAPE 2 RELEASE TOUR 2021 "
4/23(金)京都 | Chambers
4/24(土)広島 | HERBIE HIROSHIMA
4/28(水・祝前日)札幌 | KING XMHU
5/1(土)青森 | DNA AOMORI
5/2(日)仙台 | LUXS+
5/3(月・祝)新潟 | NEXS NIIGATA
5/8(土)名古屋 | CAFE IBIZA
5/15(土)熊本 | SPACE KUMAMOTO
5/22(土)長野 | Mole Hall
5/29(土)大分 | freedom
5/30(日)北九州 | CLUB CUBE ★
6/5(土)福岡 | voodoo Lounge
6/6(日)鹿児島 | Duckbill ★
6/12(土)石川 | DOUBLE KANAZAWA ★
6/19(土)岩手 | MORIOKA FACES
6/25(金) 愛媛 | CLUB BIBROS
6/26(土)香川 | SOUND BAR RAD
沖縄 & 大阪 COMING SOON!!!
★:DAY TIME / 未成年入場可能
※詳細は各会場にお問合わせください
----------------------------------------------------------------------------------
#JPTHEWAVY #LEX #PickNChoose #WAVYTAPE2
![post-title](https://i.ytimg.com/vi/zvOczl1bDUk/hqdefault.jpg)
pick n choose 在 Metrology Deptt raids 'Pick N Choose' for cheating ... 的推薦與評價
... in Srinagar, accordingly a team from Legal Metrology Department raided 'Pick N Choose' and found that MRP was increased on the products ... ... <看更多>
pick n choose 在 The number of different ways to choose $k$ out of $n$ unique ... 的推薦與評價
Start by arranging all of your n items in some canonical linear order. You then go through the line, picking some number of copies of each ... ... <看更多>
pick n choose 在 Pick 'n' choose - Facebook 的推薦與評價
Pick 'n' choose · Page · Health/beauty · picknchoosebd.com · Rating · 5.0 (299 Reviews). ... <看更多>