📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有268部Youtube影片,追蹤數超過2,500的網紅佩珊長板Rainey longboard,也在其Youtube影片中提到,青山作伴,白雲為友,滑板少女們乘風穿梭在瑞芳山嶺間的蜿蜒小徑 透過空拍與穿越機極致壯闊的視野,展現了台灣富麗的山巒設色之美。 —— 特別感謝🙏 空拍師 :修伯特 https://www.facebook.com/profile.php?id=1146637091 攝影師:江奇峰 https://...
「shuffling」的推薦目錄:
- 關於shuffling 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於shuffling 在 GIGAZINE Facebook 的最佳貼文
- 關於shuffling 在 實況主-修分靈Shuffling Facebook 的最佳貼文
- 關於shuffling 在 佩珊長板Rainey longboard Youtube 的最佳貼文
- 關於shuffling 在 Kuma Films Youtube 的精選貼文
- 關於shuffling 在 修分靈 Youtube 的精選貼文
- 關於shuffling 在 實況主-修分靈Shuffling 的評價
- 關於shuffling 在 作者shuffling 的總覽(PTT發文,留言,暱稱) - PTT網頁版 的評價
shuffling 在 GIGAZINE Facebook 的最佳貼文
トランプを1枚だけシャッフルすることを繰り返して「完全に混ざった状態」にできるのか?(2020)
https://gigazine.net/news/20200807-the-math-of-card-shuffling/
shuffling 在 實況主-修分靈Shuffling Facebook 的最佳貼文
shuffling 在 佩珊長板Rainey longboard Youtube 的最佳貼文
青山作伴,白雲為友,滑板少女們乘風穿梭在瑞芳山嶺間的蜿蜒小徑
透過空拍與穿越機極致壯闊的視野,展現了台灣富麗的山巒設色之美。
——
特別感謝🙏 空拍師 :修伯特
https://www.facebook.com/profile.php?id=1146637091
攝影師:江奇峰
https://instagram.com/aurore_m51?igshid=e4dk85ca7mc
—
Rider:
長板:佩珊
➠ Instagram :https://instagram.com/rainey_longboar...
➠Facebook :https://www.facebook.com/Rainey-longboard
飄移板:
✿ 追風少女魚板✿
‣‣ https://www.youtube.com/c/追風少女魚板kamab...
‣‣ Instargram ► https://www.instagram
.com/fish.a_a/
‣‣ Facebook ► https://www.facebook.com/fishaa1114/
—
🌟長板教學資訊
➠ Line : @goskate
https://lin.ee/4zfHD7JeF
🎬正職是貓奴,兼職是攝影的超懂玩攝影師
➠ https://instagram.com/aurore_m51?igsh...
🌟 乾爹乾媽合作邀約🌟
➠integritymaple0302@gmail.com
#aerialphotography #landscape #landscapephotography #landscapelovers #visualsoflife #iseetaiwan #longboard #longboarddancing #longboarding #sk8 #longboardgirl #freeskate #長板 #台灣長板 #瑞芳 #台北 #台北景點 #台灣 #秘境 #ドローン #ロンスケ
shuffling 在 Kuma Films Youtube 的精選貼文
Go to http://bit.ly/Kuma10 for a free trial and 10% off your first domain or website #sponsored
↓LINKS TO VIDEOS↓
SUBSCRIBE.
Subscribe→ http://bit.ly/kumaf1lms
More vids → http://bit.ly/kumavids
CONNECT WITH KUMA.
Website: http://www.kumafilms.com/
Facebook: https://www.facebook.com/kumafilms
Instagram: http://instagram.com/kumafilms @kumafilms
Twitter: https://twitter.com/kuma_films @kuma_films
Soundcloud: https://soundcloud.com/kuma-films
SEND STUFF.
PO Box 1253
Orem, UT 84059
VIDEO LINKS.
Cyr Wheel of Fortune: http://bit.ly/cyrwheel
Bubble Skills: http://bit.ly/kumaxbubbles
Free Skates are Strangely Awesome: http://bit.ly/kfxfreeskates
RC Drift Cars in Japan: http://bit.ly/rcdrift
Yoyo Ninja!: http://bit.ly/yoyoninja
Daggle: http://bit.ly/kumaxdaggle2
Fans & Rice Hat Manipulation: https://bit.ly/kuma-flowarrior
Human Hamster Wheel: http://bit.ly/19Ng4i3
Epic Pen Spinning: http://bit.ly/epicpenspinning
EPIC PEN SPINNING EXTRAS: http://bit.ly/xpenspin
nspinning/
This Girl Has Crazy Talented Feet: http://bit.ly/2aG7aDm
Dude with a Cube: http://bit.ly/kfxrubiks
Freestyle Skateboard Session with Isamu: https://youtu.be/8Y-q_WHub7I
Amazing 12 Year Old Freestyle Skateboarder: https://youtu.be/8Y-q_WHub7I
Free Skates are Strangely Awesome 2: http://bit.ly/freeskates
Simple Toy is Oddly Satisfying: http://bit.ly/kumaxspintop
Gravity Defying Spintop Tricks: http://bit.ly/spintop-tricks
Hypnotizing Optical : http://bit.ly/kumaxouka
How Does She Do That with Her Spine?: http://bit.ly/kumaxsofiedossi
Captivating Contact Staff Performance: http://bit.ly/contactstaff
Coming True | Playing with Fire: http://bit.ly/1TJDTZS
Juggling Pool Trick Shots: http://bit.ly/kumaxpooltricks
Epic Freestyle Scooter Tricks: http://bit.ly/kfxscooter
Epic Freestyle Scooter Tricks Part Deaux: http://bit.ly/1Qa83qT
Cigar Box Juggling: http://bit.ly/1K2sAXh
Late Night Ninjas: http://bit.ly/latenightninjas
Japan's #1 Plate Spinner: http://bit.ly/platespinning
Awesome Kids - 9 Year Old Pro Skateboarder: http://bit.ly/awsmxkuma
The Real Lord of the Ring: http://bit.ly/1Eyn7XE
The Bruce Lee of Bartending: http://bit.ly/1XRTo1M
Freestyle Skateboarding at Skatechurch: http://bit.ly/kfxtbsc
Card Shuffling Ninja: http://bit.ly/2zAN8sZ
MUSIC.
NHYX - Nouvelle vague
Soundcloud: https://soundcloud.com/nhyx
Bandcamp: https://nhyx.bandcamp.com/
YouTube: https://www.youtube.com/channel/UCvA9j_zOHkghLM4EAH5bIHw
Facebook: https://www.facebook.com/nhyxmusic/?fref=ts
Instagram: https://www.instagram.com/nhyxmusic/
Twitter: https://twitter.com/Nhyxteam
Label: Pavillonn
Website: https://difymusic.com/pavillonn
Spotify: https://open.spotify.com/user/ljdj5lgd7buql02855efh83t4
Facebook: https://www.facebook.com/pavillonn/
YouTube: https://www.youtube.com/channel/UCvA9j_zOHkghLM4EAH5bIHw
Instagram: https://www.instagram.com/pavillonn/
Spotify: https://open.spotify.com/artist/15hYbx7vg01TZFFgipPAHm
The Camera Gear We Use!
Easy to view list of our camera gear: http://amzn.to/2kWUxKS
shuffling 在 修分靈 Youtube 的精選貼文
本集工商產品:AERO 低延遲真無線遊戲耳機
產品傳送門:https://xround.pros.is/swdgt
(購買時在折扣碼欄位輸入『SHUFFLING』修分靈粉絲專屬折扣碼,直接再給你 $150 的折扣!)
🔹實況在這開(FB粉專)➡https://www.facebook.com/Shuffling810
🔸Instagram➡ https://lihi1.cc/Qgsgn
🔸訂閱Youtube頻道➡https://goo.gl/TW9Ixg
🔹歐付寶贊助我(台灣)➡http://goo.gl/OiGYIW
🔸個人臉書➡https://www.facebook.com/Shuffle0810
📣工商/商品合作請聯繫➡FB粉絲團📣
#灌籃高手
#AERO
shuffling 在 作者shuffling 的總覽(PTT發文,留言,暱稱) - PTT網頁版 的美食出口停車場
作者shuffling 的總覽(PTT發文,留言,暱稱). 發文數量: 178. 收到的『推』: 2343 (52.1%). 收到的『→』: 1937 (43.1%). 收到的『噓』: 213 (4.7%). 留言數量: 90. ... <看更多>
shuffling 在 實況主-修分靈Shuffling 的美食出口停車場
實況主-修分靈Shuffling. 26933 likes · 196 talking about this. 遊戲實況時間:每晚7-12點/在FB粉絲專頁實況YouTube頻道:https://goo.gl/TW9Ixg... ... <看更多>