📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有2部Youtube影片,追蹤數超過27萬的網紅Lindie Botes,也在其Youtube影片中提到,Language community, it's time we get back to the heart of what it means to be a polyglot and not hold back on sharing more linguistically diverse cont...
「what is accessibility」的推薦目錄:
what is accessibility 在 IainWithEyes Studios Facebook 的最佳貼文
Taking the time to practice what I’ve learned, and recreate frames that I’m drawn to on a daily basis. I’ve always loved dramatic lighting, because they make you feel. With the accessibility of tech that we have these days, it is easier than ever before to be able to achieve it.
I also got to finally test out my little camera rig that I’ve build. Although this is just a test, I imagine it as an excerpt from a film.
I’ll do more of these down the road, sharing my journey with you as I discover and grow. Definitely looking to do more of these on a larger scale. I’m on the lookout for great stories!
Shot on @sonyalpha a6500 and @sony’s 35mm @ f1.8, with a DIY black pro-mist filter.
what is accessibility 在 Engadget Facebook 的精選貼文
How should companies approach inclusive product design?
what is accessibility 在 Lindie Botes Youtube 的最佳貼文
Language community, it's time we get back to the heart of what it means to be a polyglot and not hold back on sharing more linguistically diverse content. How can we foster a more supportive, accessible and diverse community? ?
There are so many "Polyglot speaks X language" videos but not many that actually share the process and progress behind it. Why are we worried that uploading a video in another language will not get as many views as in English? Isn't language diversity worth more than views? ? ... and yes, that's exactly why this entire video is in Afrikaans! ??
It's not easy and there are so many factors to take into consideration - I feel the struggles too! What are your thoughts? ? (Press CC for English subs)
Afrikaans channel: https://www.youtube.com/channel/UCIedNl4r7Hd8SnXiEjXMbKg
Timestamps:
00:00 Video preview
00:21 English videos and accessibility
01:25 A weird issue in the community
02:10 Changing the status quo
03:14 Is it really a problem?
03:44 How we can fix the problem
04:34 My videos in other languages
05:04 PoLyGLoT SpEaKs 20 LAnGuAgeS and exclusivity
06:09 4 polyglot YouTubers I admire
07:01 Ideas and closing
———
?SOCIALS
Insta → https://www.instagram.com/lindiebotes/
Website & resources → http://lindiebotes.com/
Twitter → https://twitter.com/lindiebee
FB → https://www.facebook.com/lindiebotesvideos/
Ko-fi → https://ko-fi.com/lindiebotes#
✨GOODIES
$10 free italki credits (after first lesson) → https://go.italki.com/LindieBotes
10% off Du Chinese (my favorite app!) enter LINDIE10 at checkout → https://www.duchinese.net/
All discounts → http://lindiebotes.com/discounts
All language resources → https://lindiebotes.com/language-resources/
Merch → https://society6.com/lindiebotes
?ABOUT
Welcome to my channel! My name is Lindie and I share my love for languages through my polyglot progress and language learning tips here. South African by birth, I spent most of my life in France, Pakistan, the UAE and Japan. Now I work as a UI/UX designer in Singapore. I'm a Christian and strive to shine God’s light in all I do. May this channel inspire you to reach your language goals!
New here? Best videos → https://www.youtube.com/playlist?list=PLRCVN94KILKXGx45JKaVBSpPkrpXhrhRe
FAQ → https://lindiebotes.com/faq/
?BOOKS I USE
Practical Chinese Grammar → https://geni.us/PracticalChineseGram
Japanese for Busy People on Amazon → https://geni.us/JapaneseForBusy1
Advanced Japanese for Busy People → https://geni.us/JapaneseForBusy3
Korean Grammar in Use Intermediate → https://geni.us/KoreanGrammarUse
Korean TOPIK exam prep → https://geni.us/TOPIK2prep
Short Stories in Spanish → https://geni.us/spanishshortstories
?EQUIPMENT
Camera → https://geni.us/CanonPowerShotG7
Mic → https://geni.us/RodeSmartLavMicr
Tripod → https://geni.us/ManfrottoTravel
———
Some links are affiliate links, and a percentage goes towards supporting my channel.
Collabs & partnerships: hello@lindiebotes.com
what is accessibility 在 Kento Bento Youtube 的最佳貼文
Official Kento Bento Merch: https://standard.tv/kentobento
Support us on Patreon: https://patreon.com/kentobento
The first 500 people to use this link will get the first 2 months of Skillshare FOR FREE: https://skl.sh/kentobento
Twitter: https://twitter.com/kentobento2015
Facebook: https://facebook.com/kentobento2015
Business Inquiries: kentobento@standard.tv
Other videos you may like:
How Macau Became North Korea's Base of Operations: https://youtu.be/BQ5x8riJ6SA
Why Japan's Great Pyramid of Giza Can't be Built Until 2110: https://youtu.be/w7E6rdmilyE
10 REASONS Why Asians Don't Get FAT: https://youtu.be/xIqJR6xfMro
Has McDonald's Conquered Asia?: https://youtu.be/pgHiRsk2UjY
These Events Will Happen In Asia Before 2050: https://youtu.be/2VAtKVCTA5k
Video Footage Credit:
Hong Kong Strong - Brandon Li
https://youtube.com/channel/UC3stPIuUoCDHG7COfwr0tEA
(most of the absolutely beautiful and dynamic shots of Hong Kong came from Brandon's short film Hong Kong Strong)
Music:
Stranger Danger by Francis Preve
Not For Nothing by Otis McDonald
Grasshopper by Quincas Moreira
Connection by Wayne Jones
Funky Suspense by Bensound
Under Suspense By Lee Rosevere
Channel Description:
We do videos on intriguing & thought-provoking Asiany topics, including stereotypes, history, culture & geography.
Credits:
Researcher/Writer/Narrator/Video Editor: Kento Bento
Motion Graphics: Charlie Rodriguez
Cheerleader: Nina Bento
————————————————————————————————————————
[WHY HONG KONG HAS THE LONGEST LIFE EXPECTANCY?]
50.1 years. According to the World Health Organization, this is the average life expectancy in the country of Sierra Leone. As of now, one of the lowest in the world.
At the other end of the spectrum, we have countries like Italy, Iceland, Singapore and Switzerland all with an average life expectancy well into their 80’s. Now beating all those countries however, is (of course) Japan, topping the list at 83.7 years. (Note that the UN data is a little different). Regardless, this is the country with the longest average life expectancy.
But, this video is not about Japan, because there is a city with an even longer life expectancy.
This is Hong Kong, a city with over 7.4 million people living on about a thousand square kilometres of land. One of the most densely populated places on the planet - a fast paced city that never sleeps.
It’s a city of contradictions with towering buildings next to lush green mountains, street food stalls alongside McDonald’s & KFC’s, markets selling cheap counterfeits in front of designer stores, but the most surprising perhaps is the extremely long life expectancy of the people living in what is one of the most polluted cities in the world.
Hong Kong has an average life expectancy of 84.3 years.
Ironically enough, Hong Kong literally translates to ‘Fragrant Harbour’.
This video will cover:
- Hong Kong weather and climate
- Londons' Great Smog of 1952
- Geographical access to good food
- Cuisine / diet
- Accessibility of walkways and footpaths
- Fitness and habits
- Taichi & Qigong
- Environment
- WHO global network of age-friendly cities
- Hong Kong's healthcare system / primary care / hospital treatment
- Smoking
- Strong family ties
- Mahjong
- Retirement age
- Many older generation Hong Kongers were not boring in China, but in Mainland China
- China's Cultural Revolution