📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有15部Youtube影片,追蹤數超過32萬的網紅Chiitan 妖精ちぃたん,也在其Youtube影片中提到,カワウソちぃたんです☆高評価、チャンネル登録よろしくお願いします! Twitter→https://twitter.com/chiitan7407 Instagram→https://www.instagram.com/chiitan7407/ TikTok→https://www.tiktok.c...
「smart draw」的推薦目錄:
- 關於smart draw 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於smart draw 在 Nadia Heng 邢敏敦 Facebook 的精選貼文
- 關於smart draw 在 Dairy & Cream Facebook 的最佳貼文
- 關於smart draw 在 Chiitan 妖精ちぃたん Youtube 的最佳貼文
- 關於smart draw 在 NK Studio Youtube 的最讚貼文
- 關於smart draw 在 NK Studio Youtube 的最讚貼文
- 關於smart draw 在 SmartDraw - Home | Facebook 的評價
smart draw 在 Nadia Heng 邢敏敦 Facebook 的精選貼文
Hey y’all!
Have you been doing takeout food daily? Wanna learn how to cook but you have no time to slave over the stove?
What if there is an appliance that cuts your cooking time yet cooks a variety of dishes with just a touch of a button?😉
Catch me and Chef Nik Michael Imran live on Panasonic Malaysia tomorrow.
🗓24 September 2021
⏰8.30pm
📍 https://www.facebook.com/events/541037053637017/
Chef Nik is gonna share how we can cook smart using the Panasonic Cubie oven.
Tune in as we’ve got heaps of giveaways and lucky draw prizes. You can even get your hands on the Cubie oven for an insanely good price on top of all the cool gifts with purchase.
See you tomorrow!
#PanasonicCookingMalaysia
smart draw 在 Dairy & Cream Facebook 的最佳貼文
I’ve always been a regular fan of Quaker Oats since young and I’ve been taking it on a daily basis since I started working. I’m happy to know that it has launched two new Granola flavors namely Quaker Oats Mixed Fruits Granola and Quaker Oats Golden Nuts Granola which contain at least 54% whole grain oats rich in protein, dietary fibre, and beta- glucan.
What’s more, they are excellent source of fibre, very low in sodium and do not contain any Artificial Flavours or Added Colours. Sweet, crunchy and moreish, they are simply one of the perfect ways to jumpstart the day. Each pack is sold at $10.90 each at NTUC, Cold Storage, Sheng Siong, Redmart, Pandamart!
From 15 Sep – 12 Oct 2021, @quaker is also having a lucky draw where you stand a chance to win exciting prizes with the purchase of any 2 Quaker products. Prizes include a Smart Watch, Stojo Cup, CapitaVouchers and many more. T&C applies.
#singapore #desserts #sgdessert #sgfoodie #yummy #love #sgfood #foodporn #igsg #sandwich #instafood #beautifulcuisines #sgbakes #bonappetit #bake #スイーツ #feedfeed #pastry #breakfast #sgbake #brunch #quakersg #granola #snacks #sgpromo
smart draw 在 Chiitan 妖精ちぃたん Youtube 的最佳貼文
カワウソちぃたんです☆高評価、チャンネル登録よろしくお願いします!
Twitter→https://twitter.com/chiitan7407
Instagram→https://www.instagram.com/chiitan7407/
TikTok→https://www.tiktok.com/@love2chiitan
Facebook→https://www.facebook.com/Chiitan-2483554918535678/
小さなちぃたん☆チャンネル→ https://www.youtube.com/channel/UCAOj...
Twitter小さなちぃたん→ https://twitter.com/otter_world
Blog→http://ameblo.jp/kawaii-other/

smart draw 在 NK Studio Youtube 的最讚貼文
So guys. Sekarang semua benda di hujung jari. Anda boleh membeli belah dengan menggunakan MY Smart Shopper. Follow @officialmysmartshopper. Jangan risau! Boleh dapat Cash Point! Dan Reward Point! Bukan kata beli, kalau gi kedai tak bawak wallet pun still boleh pakai apps untuk bayar. Sebab ada e-wallet iaitu MSS PAY!! Skrg mereka tengah buat campaign #MariSiniShopping . Dengan setiap pembelian minimum RM10, anda berpeluang join lucky draw! Kalau belanja offline, korang boleh main game kat dalam apps tau! Cubalah! Try! Klik link ni :
http://bit.ly/mss_naszkenyang

smart draw 在 NK Studio Youtube 的最讚貼文
Lakonan : @naszkenyang, @fardtinted_, @omarnxjmi
Sponsored by: MY Smart Shopper
So guys. Sekarang semua benda di hujung jari. Anda boleh membeli belah dengan menggunakan MY Smart Shopper. Follow @officialmysmartshopper. Jangan risau! Boleh dapat Cash Point! Dan Reward Point! Bukan kata beli, kalau gi kedai tak bawak wallet pun still boleh pakai apps untuk bayar. Sebab ada e-wallet iaitu MSS PAY!!
Skrg mereka tengah buat campaign #MariSiniShopping . Dengan setiap pembelian minimum RM10, anda berpeluang join lucky draw! Kalau belanja offline, korang boleh main game kat dalam apps tau! Cubalah! Try! Klik link ni http://bit.ly/mss_naszkenyang

smart draw 在 SmartDraw - Home | Facebook 的美食出口停車場
SmartDraw is the easiest way to create charts and diagrams. Here's why: 1. It's automated. SmartDraw is the only software that makes it possible for anyone ... ... <看更多>