📜 [專欄新文章] 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影片,追蹤數超過80萬的網紅kinryyy,也在其Youtube影片中提到,one day i will open the best asian restaurant so you guys can eat the best MERCH: https://pumpkinsmerch.com/ Join the pumpkins membership: https://w...
「i will swap」的推薦目錄:
- 關於i will swap 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於i will swap 在 Sebrinah Yeo Facebook 的最讚貼文
- 關於i will swap 在 Elaine73 Facebook 的最佳貼文
- 關於i will swap 在 kinryyy Youtube 的最佳解答
- 關於i will swap 在 Hak Me Youtube 的最佳貼文
- 關於i will swap 在 BrandonTan91 Youtube 的最佳貼文
- 關於i will swap 在 Does "swap A for B" imply a direction? - English Stack Exchange 的評價
- 關於i will swap 在 Everyone SWAPPED rooms for 4 days!! Who will win a TV in ... 的評價
- 關於i will swap 在 Is there a standardized method to swap two variables in Python? 的評價
- 關於i will swap 在 Swap Spot | Kolding - Facebook 的評價
i will swap 在 Sebrinah Yeo Facebook 的最讚貼文
Lately with so many cases around, boosting up our immune is crucial! Am glad I have my solutions that works and do you know food also plays a part as well. That's why I opted to swap my regular fresh milk that we simply grab and go at the mart, to a more specific targeted nutrition that will give us extra benefits in return.
Like the ProVital Immuna Plus, the first adult nutritious milk, that is featuring its unique proposition that provide Triple Immunity Protection from yeast beta-glucan, selenium and vitamin C that strengthens the body’s immune function!
More deets on "Activate your Immunity, Activate your Life" on my web https://www.sebrinahyeo.com/2021/08/PROVITAL-Immuna-Plus-Malaysia.html
🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾🇲🇾
Also apart from this campaign, PROVITAL Immuna Plus would like to celebrate the admired efforts of local heroes via “Healthier Malaysians, Stronger Malaysia”, programme that aims to recognise, honour and celebrate ordinary Malaysians doing extraordinary things in making a difference in their communities today. We urge the public to join our efforts in showing our appreciation for inspiring Malaysians who have been championing a cause for the betterment of society by nominating them to be a Local Hero too.
The programme is now open to nominations. The public can nominate deserving Malaysians who have carried out activities or initiatives that serve or meet a community need. Nominations can be submitted via https://forms.gle/7mURUZrv5vrfHyPQ8 from 19 July 2021 until 8 August 2021 midnight. Each Local Hero will receive a one (1) year supply of PROVITAL Immuna Plus adult nutritious milk and have their stories featured on PROVITAL Immuna Plus’ digital platforms.
〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️〰️
Music credits to :
Music: Early Hours
Musician: Ikson
i will swap 在 Elaine73 Facebook 的最佳貼文
Yesterday was Mother’s Day... but everyday is Children’s Day I think 🤔🤔🤔😂😂😂 Double tap if you agree! 😜 Baby shark doo dooo dooo ~ 🎶 No doubt the catchiest song OF ALL TIME! 😅
.
My kids actually still enjoy dancing to @pinkfong.official’s songs at home so boy ARE WE EXCITED TO CATCH THE BABY SHARK SHOW IN PERSON this weekend!!! 🎉 Holla if you’re heading there too!
.
🦈Baby Shark LIVE: The Hidden Treasure will be held at The Theatre at @mediacorp on the 13th, 15th, 16th, 22nd & 23rd May 2021 at timings 10am | 1pm | 4pm | 7pm // with 4 shows each day! 🎤 (Swap test not needed)
.
Due to the latest restrictions, tickets are running out fast! Head over to my da jie @lecinlurvee’s page to win tickets! ❤️💨💨💨
.
#BabySharkLIVETheHiddenTreasure #liveinconcert #pinkfong
i will swap 在 kinryyy Youtube 的最佳解答
one day i will open the best asian restaurant so you guys can eat the best
MERCH: https://pumpkinsmerch.com/
Join the pumpkins membership: https://www.youtube.com/channel/UC-pDutOeU-xRK0B9J73V9eQ/join
Download Alarmy App: https://bit.ly/3uiXZlT
-
watch me getting confused by girls: https://youtu.be/tH_YXSqgx5o
listen to my cover of BTS Blue & Grey: https://youtu.be/7tGd2MRBY6w
-
Instagram @kinryyy► https://www.instagram.com/kinryyy/
Facebook ► https://www.facebook.com/kinryyyfb
VK ► https://vk.com/kinryyy
Collaboration ► kinryanmusic@gmail.com
--------------------------------------------------------------------------------------
#asianboy #Alarmy #MyDailySuccessHabits #korea #oppa
i will swap 在 Hak Me Youtube 的最佳貼文
#核酸檢測 #新冠肺炎 #黑咪vlogmas #vlogmas
‼️Farfetch做緊雙12優惠,折扣貨品額外又多8折‼️趁呢個機會買禮物俾自己俾人都正呀!
?當然我又揀咗啲單品同大家分享??
?? Longchamp Le Pilage Bag https://bit.ly/2VQxvIv
?? Chloe Any Chain Leather Bag https://bit.ly/33T8yAs
?? Longchamp Tote Bag https://bit.ly/3lV5YjI
?? Chloe Tess Bag https://bit.ly/2JDtJjk
?? Karl Lagerfeld Jacket https://bit.ly/3qG6aHk
?? Stuart Weitzman Ankle Boots https://bit.ly/36VzotI
?? Off-white Sweatshirt https://bit.ly/2IqpmHz
?另外,呢幾件我覺得買嚟送俾人做禮物都好正喎??
?? Moschino Teddy Phone Case https://bit.ly/3gp81LV
?? Tory Burch Cosmetic Bag https://bit.ly/3oKxaDN
?? Marc Jacobs Hair Clip https://bit.ly/33SJYzY
?? Off-White Top-zip Cardholder https://bit.ly/2JZ0hnx
?? Off-White Scarf https://bit.ly/33PuEDR
大家快啲去睇,好多都好抵!買完唔啱return係free架,呢個就係點解我鍾意上網買喇~
⭐️ Women Sale Page: https://bit.ly/2V2H8n1
⭐️ Men Sale Page: https://bit.ly/3lbmTOy
Information Featured:-
Conte Medical 童珀醫療 https://en.contemedical.com/dualswab
Fee: HK$1880/person for same day report
Follow Me:-
? My Instagram: @iamhakme
? My Facebook: www.facebook.com/hakmebeauty
? Blog: www.hakmebeauty.com
? Weibo: https://www.weibo.com/iamhakme
? 小紅書: http://bit.ly/2W2LcSx
? Shop My Looks: https://goxi.ps/iamhakme
Follow Hakme Beauty:-
?黑咪店地址: https://www.hakmebeauty.com/store-locations/
?黑咪店路線圖: http://bit.ly/2W3pUZr
?黑咪店Online: https://shop.hakmebeauty.com
?黑咪店Instagram: @hakmebeauty
?黑咪店Facebook Page: https://www.facebook.com/hakmebeautyltd/
************************************************************************************
Disclaimer: This video is created and edited by my editor. All the content are my own thoughts. As always, all opinions are based on my experience and honesty. Products are either purchased by me or for those which are sent by PR are marked with an “*”. For any collaboration with brands which involves monetary payment, an indication will be in the video so that you are aware of the collaboration. Some of the links used above might be affiliate links and please be aware that I will earn a % of commission if you decide to buy through the affiliate links.
i will swap 在 BrandonTan91 Youtube 的最佳貼文
The annual Solstice event is here again in Pokemon GO with Solrock and Lunatone being the main Pokemon in which they will swap region during this event. However, since Zekrom is still in raids, I decided to focus my attention more on Zekrom as it is a Pokemon which is more useful than the Solstice event Pokemon.
Join me as I travel, eat and play Pokémon GO the way a local trainer would in their own country and city. Visiting one country and one city at at time through Pokémon GO.
Subscribe if you would like to watch my Pokémon GO travel adventure as I visit different countries, cities and communities, https://tinyurl.com/y65jruw2
Social:
Instagram: https://www.instagram.com/brandontan91/
TWITTER: https://twitter.com/brandontan91
Vlogging Set-Up:
Camera - Sony RX 100 Mark 7
Mic - Rode VideoMicro
Screen Recorder - iPad Mini 5
Pokémon is Copyright Gamefreak, Nintendo and The Pokémon Company 2001-2016
All images and names owned and trademarked by Nintendo, Niantic, The Pokémon Company, and Gamefreak are property of their respective owners.
i will swap 在 Everyone SWAPPED rooms for 4 days!! Who will win a TV in ... 的推薦與評價
Everyone SWAPPED rooms for 4 days!! Who will win a TV in their room?! The Ohana Adventure. ... <看更多>
i will swap 在 Does "swap A for B" imply a direction? - English Stack Exchange 的推薦與評價
swap someting for something: to exchange something you have [or in hypothetical situations would see yourselves having] for something else: Most ... ... <看更多>