📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有43部Youtube影片,追蹤數超過6萬的網紅Adam Lobo TV,也在其Youtube影片中提到,So it’s been a month since I have reviewed the POCO F3 5G and while there were a lot of great comments, there were lots of complaints as well. So yes,...
「the high numbers」的推薦目錄:
the high numbers 在 Facebook 的最佳貼文
My wife @thelisa_s spotted this in NYC. At first I thought it was a legit ad from the CDC and I was like “nice! CDC is going hard!” But I noticed the crying ‘Mercian and realized this is the work of an artist. Tag them if you know who did this. Seriously though I just spoke to a ER nurse who tested me the other day and he said the ER is seeing high numbers again and at least 95% of the people coming in with bad cases of Covid 19 are non vaccinated younger people. #justgettheshotalready
the high numbers 在 A Happy Mum Facebook 的精選貼文
*This giveaway has ended. Congrats to Wen Lo Ong for being picked as the winner and thanks to all who joined!*
{Giveaway} WIN a complete bundle set of Educational Decals (worth $118) with over 120 decals in six different themes from Evercasa! Two sets to be given away!
The six different themes include ABC Alphabets, My First Words, All Things Transport, I Love My Veggies, Super Fruits and Numbers, Days, and Weather. Made with self-adhesive fabric material with a matte finish and printed with high-quality ink, these decals are removable, repositionable and reusable. How versatile!
I thought this would be a fantastic way for the kindergartener to learn his spelling and for the baby to pick up new words and recognise objects as he grows. It turned out that all the kids love these decals because they are such a great way to spark creativity and have fun while learning.
Let's see, we had a contest where we scrambled up the decals and see who could spot the items fastest. I got them to take turns to spell, count and do math problems, tailoring the questions to their different ages. It was so fun that they wanted to go again and again! These decals are also a good way to jazz up our wall and the kids have been writing surprise messages for me too (swipe to see a couple of them).
Yup, I guess the only thing that limits us is our imagination. Well, we did wish there was an animal theme in the bundle but in any case, we are definitely looking forward to learning more and having more fun with these decals. Check out more designs at https://evercasa.co/ which you can use to tranform your living space.
Giveaway!
To win an Educational Decals bundle set worth $118,
🏠 Follow A Happy Mum and Evercasa.
🏠 Like and share this post.
🏠 Comment on why you would like to win.
🏠 Tag your friends, one per comment. Each counts as an entry.
This giveaway ends on 29 July 2359hrs. One winner will be chosen from FB and the other from IG, so hop over to @ahappymum for more chances to win. It is open to readers residing in Singapore only and is not endorsed by or affiliated with FB in any way. Good luck!
#ahappymum #evercasa #walldecals #educational #makelearningfun #sghome #sgmum #sgkids #sgcontest #sggiveaway #igsg
the high numbers 在 Adam Lobo TV Youtube 的精選貼文
So it’s been a month since I have reviewed the POCO F3 5G and while there were a lot of great comments, there were lots of complaints as well. So yes, in this video I will be giving you my review of the POCO F3 after a month and I will answer all of your complaints about the phone.
If you find this video helpful and would love to watch more, you can SUBSCRIBE here:
https://bit.ly/2HqeKrW
Timecode:
00:00 - Intro
00:42 - Build Quality
01:49 - Display
03:02 - Cameras
03:53 - Sound Quality
04:10 - Navigation
05:39 - Battery
06:00 - Gaming
07:26 - Conclusion
08:10 - Q&A
Get the POCO F3 5G At The Link Below:-
https://rebrand.ly/adampocof3april
*The early bird sales period is finally here, but numbers are limited, so hurry up and get your own POCO F3 for only RM1,399 now!*
???My Desk Setup 2020
https://youtu.be/xK1QUClu-V0
Check out my other videos about POCO:
POCO F3 5G - https://youtu.be/qlz_kV6TkKA (incl. sample photos & videos)
POCO M3 - https://youtu.be/h4JfV2vya4s
POCO F2 Pro - https://youtu.be/o9cn53LieKQ
Xiaomi Pocophone F1 - https://youtu.be/Bwxwr_coYdM
Or check out my entire PHONE REVIEWS playlist!
https://www.youtube.com/playlist?list...
---
ABOUT ME:
Hey you! Thanks for checking out Adam Lobo TV on YouTube!
My name is Adam Lobo, I'm a Tech YouTuber from Kuala Lumpur, Malaysia, who creates high-quality tech reviews on YouTube, Instagram & Facebook and I am currently the only Malaysian Tech YouTuber who produces 6K Resolution content.
My passion is to help everyone to make a purchase decision with all the tech items I get my hands on, where you'll find weekly smartphones, tablets, audio, smart home and other cool tech related videos as well. I produce these videos at least twice a week so do consider subscribing to my channel.
Find me at these Social Media platforms:
YouTube (Adam Lobo TV) - https://www.youtube.com/c/adamlobotv/
Facebook (Adam Lobo TV) - https://www.facebook.com/adamlobotv/
Instagram (@adamlob0) - https://www.instagram.com/adamlob0/
Twitter (@adam_lob0) - https://twitter.com/adam_lobo
Website (Adam Lobo TV) - https://adamlobo.tv/
#adamlobotv #poco #pocof35g
![post-title](https://i.ytimg.com/vi/k6atkOpFi2U/hqdefault.jpg)
the high numbers 在 Laowu老吳 Youtube 的最佳解答
DEVIL MAY CRY IS BACK, BABY!! The legendary stylish action game is better than ever in the ultimate version of this multi-award winning hit.
- New playable character - Dante's brother and arch-rival, Vergil
- Legendary Dark Knight mode - an extremely challenging horde mode featuring huge numbers of enemies
- Turbo mode - make the game even more intense by playing at 1.2x speed
- Ray-tracing support - this next-generation graphics technology brings the already near-photorealistic world of DMC5 to a new level of unparalleled visual splendour
- High Frame-Rate mode - experience super-smooth action
本作針對次世代主機追加支援光線追蹤繪圖技術、縮短讀取時間,讓無數惡魔現身的全新難度[傳說黑騎士]模式、讓新玩家操縱角色「維吉爾」登場等各種讓遊戲全面升級的要素。
LIKE & SHARE 如果你喜歡這個系列哦 ;DD
原版DMC5 Part 1開始看清單: https://bit.ly/2HTaDor
關注我的頻道別錯過任何更新 : http://goo.gl/3TVkJT
關注老吳Facebook個人帳號 : http://goo.gl/c0a2DW
![post-title](https://i.ytimg.com/vi/OYa-W5iS5KY/hqdefault.jpg)
the high numbers 在 capcomasia Youtube 的最佳解答
DEVIL MAY CRY IS BACK, BABY!! The legendary stylish action game is better than ever in the ultimate version of this multi-award winning hit.
New features:
- New playable character - Dante's brother and arch-rival, Vergil
- Legendary Dark Knight mode - an extremely challenging horde mode featuring huge numbers of enemies
- Turbo mode - make the game even more intense by playing at 1.2x speed
- Ray-tracing support - this next-generation graphics technology brings the already near-photorealistic world of DMC5 to a new level of unparalleled visual splendour
- High Frame-Rate mode - experience super-smooth action
https://www.devilmaycry.com/5se/
![post-title](https://i.ytimg.com/vi/ZtCdfKvVgtg/hqdefault.jpg)