📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有13部Youtube影片,追蹤數超過40萬的網紅SLSMusic,也在其Youtube影片中提到,【400,000 Subscribes Special】I played soundtracks from Studio Ghibli written by Joe Hisaihi in a music store located in Taipei. Thank you all for makin...
「taipei keep going」的推薦目錄:
- 關於taipei keep going 在 Taipei Ethereum Meetup Facebook 的最讚貼文
- 關於taipei keep going 在 Facebook 的最佳貼文
- 關於taipei keep going 在 Artlife TW 玩物買取事務所 Facebook 的最佳解答
- 關於taipei keep going 在 SLSMusic Youtube 的最佳貼文
- 關於taipei keep going 在 Ghost Island Media 鬼島之音 Youtube 的精選貼文
- 關於taipei keep going 在 Ghost Island Media 鬼島之音 Youtube 的最佳貼文
- 關於taipei keep going 在 「Taipei keep going」黃珊珊投入北市選戰 - YouTube 的評價
- 關於taipei keep going 在 KeepGoing繼續前進 - Facebook 的評價
taipei keep going 在 Facebook 的最佳貼文
STRONG Nation ™ ITC in Taipei
「8/28」
歡迎妳們加入STRONG Nation的行列
-
也許整個過程中偶爾腦中會浮現自己辦不到、學不會、這很難
這都不要緊
不妨先從一個動作-一組動作-一首歌-一個板塊
慢慢嘗試,一定會找到屬於自己的教學方式與節奏
願意來參加培訓,妳已經很棒了
願意突破自己、嘗試自己不擅長的領域
這都是妳對生活的一種態度💪💪
keep going, keep training.
當然原本就是教練的你們
也期待你們在教學STRONG Nation ™ 的路上,可以越來越熟練上手
一起加油🙌🙌
-
-
接下來十月份在北、中、南部
都會有STRONG Nation ™ 培訓課程
歡迎持續鎖定 Strong Nation Taiwan TWI 台灣全人新創
及
MT Sharen Sharen Tu
MT Jacky Jacky Feng
都會有最新培訓資訊可以參考
我也會在以下這邊出現
11/27 台中
https://www.t-wi.com.tw/zumba_training_detail.html?s=330
12/4台南
https://www.t-wi.com.tw/zumba_training_detail.html?s=331
12/18花蓮
歡迎推薦給你們周圍的教練朋友、老師朋友們👍🏻👍🏻
#STRONGNation #TWI #wearestrongnation #fitness #workout #training
taipei keep going 在 Artlife TW 玩物買取事務所 Facebook 的最佳解答
ArtLife @ Keep on going never give up !!!
@ Photography byMacoHsu inTaipeiTaiwn
#HOW2WORK #POPMART #KasingLung #TheMonster #CAMPING #Labubu #TRAVELLING #BlindBox #Series #台湾 #台北 #Artlife
門市公告 🚫 WARNING:
自2021/07/27二級警戒後,因受COVID-19疫情關係配合相關政策,目前僅開放預約到店自取 (11:00-18:00),門市恕一律無法到店參觀,如無提早訊息實名預約者 (由於場地關係,每次僅限一組最多2人,滯留時間為10分鐘內)恕不開放無法進店,網路商店一切正常購買及出貨,防疫期間不便之處,敬請見諒!!!
● 貼心建議: 因疫情期間關係,請大家盡量多多使用網路購買寄送,保障外出染疫風險
----------------------------------------
因人手不足及時間關係,本店均採 預約制 (RESERVE)
事前予約が必須です!
The Artlife Store by Appointment.
OPEN TIME: 營業時間不固定 每日早上公告時間
歡迎光臨參觀 Welcome to Artlife
----------------------------------------
衛生安全最重要 ⚠⚠⚠ 我們保護您 也請您保護我!!!
入店須知 即日起 進入賣場 請務必全程配戴口罩!!!
Foreigners must wear masks to enter!!!
訪問者は入場するためにマスクを着用する必要があります!!!
因空間關係,所有商品不一定於門市中擺放販售
來店前,歡迎線上訊息先詢問您所需要的商品
----------------------------------------
● YAHOO 線上購物請至 : http://tw.user.bid.yahoo.com/tw/booth/Y1514060726…
● 台灣蝦皮 ONLINE SHOPING : https://shopee.tw/artlifetaipei
● 蝦皮 二 : ONLINE SHOPING
https://shopee.tw/100toytaipei…
----------------------------------------
● Artlife TW 玩物買取事務所
( 藝術 ART / 日本 JAPAN / 設計師 DESIGNER )
● IG / Facebook: Artlife TW 玩物買取事務所
ADDRESS: 108 台灣台北市萬華區中華路一段76巷6號1樓
1F., No.6, Ln. 76, Sec. 1, Zhonghua Rd., Wanhua Dist., Taipei City 10842, Taiwan (R.O.C.)
taipei keep going 在 SLSMusic Youtube 的最佳貼文
【400,000 Subscribes Special】I played soundtracks from Studio Ghibli written by Joe Hisaihi in a music store located in Taipei. Thank you all for making this happened!
↓ More info down below ↓
💬SLSTalk
Finally, 400K Subscribers. It's been a long way! We're really sorry we couldn't celebrate by doing recital, concert or fans event due to the coronavirus issues. Still, we prepared this special video for you, hope you like it.
I've played them a lot at live streaming, but I rarely do videos for Studio Ghibli Animations. Not because I don't like it, it's just because there were too many covers already on YouTube. So I think this time it's a good opportunity to make it a medley with a different style of video, adding some words I'd like to say to you, hope you like this video.
As many of you might know, maybe it's the change of time, or change of the algorithm, change of YouTube, musicians on YouTube lives much harder than 4 or 5 years ago. We all have to find another way out to keep living. So in the past few month we've been working on streaming platforms, digital albums, and now is the Patreon. It would be really helpful that fans support us with those platforms IF you're able and willing to.
And just to be clear, I won't quit doing music videos even if it keep going worse, because doing these things including performing, recording and editing is one of my personal interest, that's why I started all this in the first place, and that's something never change.
Once again, thanks for all the supporters around the world. We couldn't have done it without you. I don't know how far we can go, but as long as there's audience waiting for me, the music would never stop. No matter what way you choose to support us, we truly appreciate it from the bottom of our hearts.
2021.07.23
SLS
🟧Support us on Patreon and Get Rewards!
► https://www.patreon.com/slsmusic
🎁Get MP3, Sheet Music, Exclusive Contents
📖Sheet Music
🎼https://slsmusic.gumroad.com/l/uRVPP
🎼https://www.mymusicsheet.com/slsmusic/40936
✨Synthesia Video
Patreon► https://www.patreon.com/posts/54018633
YT Member► https://youtu.be/xQYLQQ7sJ_I
🎥 Cinematographer: 建中
https://www.instagram.com/billwang1129/
🎥 Assistant: Claire
🎧Find SLSMusic on Spotify, iTunes, Apple Music, Amazon, Deezer and more
All► https://li.sten.to/slsmusic
Spotify► http://bit.ly/SLSMusicSpotify
iTunes► http://bit.ly/SLSiTunes
🎤Social Media
Website: https://www.slsmusic.net
Instagram: https://www.instagram.com/slsmusictw
Twitter: https://twitter.com/slsmusictw
Facebook: https://www.facebook.com/slsmusictt
🥁DONATION
PayPal► https://www.paypal.me/SLSMusic
歐付寶► https://www.bit.ly/DonateSLS
Special Thanks to【Kinstar 金聲樂器】
https://www.kinstar.com.tw/
🙏THANKS YOU FOR SUPPORTING SLSMusic
🔵Patreon "Grand Piano"
Kater of Glencoe、Lyra Mao、かず K・、阿夜、清艵宮、Mikael Mazareth
🟢Patreon "Piano"
Deemo Harlos、LitGrass Chou、Judy Tseng、風和日麗
🔴YouTube Sponser
交替的白晝與黑夜・阿夜・Chia-Chi Chang・清艵宮・Kater
⏰Song List:
0:16 Merry-Go-Round of Life/人生のメリーゴーランド
1:36 Reprise/ふたたび
2:23 One Summer Day/あの夏へ
3:41 Carrying You/君をのせて
5:06 The Legend of Wind / 風の伝説
7:12 A Town with an Ocean View/海の見える街
■──────────────────■
#ghibli #piano #joehisaishi
taipei keep going 在 Ghost Island Media 鬼島之音 Youtube 的精選貼文
Podcasts can have a little army, as a treat.
Join the N8 Nation Army today to help keep this podcast #sustainable!
Waste Not Why Not is starting a PATREON DRIVE to jumpstart our new season. We want to encourage everyone to rethink how we save the environment, and, for $3 a month, you can become part of our campaign. No matter how small the amount, this money goes towards maintaining our equipment, booking recording studios, and keeping our production team alive.
People who can't join us on #Patreon will still be able to enjoy the show in full, for free, forever! But, our Patrons gain exclusive access to new episodes earlier, outtakes that didn't make the final cut, and monthly video hangouts with Nature N8 himself. And, if we get 50 patrons by February, we'll host another live show in Taipei, Taiwan (the country where only 700+ Covid cases are confirmed out of a 23 million population)! Patrons will get a secret link to the livestream and dibs to ask our panel any questions!
Waste not a good environmental podcast! Help us keep this show going, and make our world a better environment.
Support us on Patreon:
http://patreon.com/wastenotwhynot
Follow us on Twitter: #WasteNotWhyNot
https://twitter.com/wastenotpod
Send your questions to:
ask@wastenotwhynot.com
VIDEO CREDIT
Claudia Sheng (Director / Editor)
https://twitter.com/flaskprodx
Nate Maynard (Host / Writer)
https://twitter.com/N8MAY
Emily Y. Wu (Executive Producer)
https://twitter.com/emilyywu
Yu-Chen Lai (Producer)
https://twitter.com/aGuavaEmoji
Ghost Island Media (Production Company)
https://twitter.com/ghostislandme
taipei keep going 在 Ghost Island Media 鬼島之音 Youtube 的最佳貼文
Nature N8 needs your help to keep this podcast #sustainable!
Waste Not Why Not is starting a PATREON DRIVE to jumpstart our new season. We want to encourage everyone to rethink how we save the environment, and, for $3 a month, you can become part of our campaign. No matter how small the amount, this money goes towards maintaining our equipment, booking recording studios, and keeping our production team alive.
People who can't join us on #Patreon will still be able to enjoy the show in full, for free, forever! But, our Patrons gain exclusive access to new episodes earlier, outtakes that didn't make the final cut, and monthly video hangouts with Nature N8 himself. And, if we get 50 patrons by February, we'll host another live show in Taipei, Taiwan (the country where only 700+ Covid cases are confirmed out of a 23 million population)! Patrons will get a secret link to the livestream and dibs to ask our panel any questions!
Waste not a good environmental podcast! Help us keep this show going, and make our world a better environment.
Support us on Patreon:
http://patreon.com/wastenotwhynot
Follow us on Twitter: #WasteNotWhyNot
https://twitter.com/wastenotpod
Send your questions to:
ask@wastenotwhynot.com
VIDEO CREDIT
Claudia Sheng (Director / Editor)
https://twitter.com/flaskprodx
Emily Y. Wu (Executive Producer / Writer)
https://twitter.com/emilyywu
Nate Maynard (Host / Writer)
https://twitter.com/N8MAY
Yu-Chen Lai (Producer)
https://twitter.com/aGuavaEmoji
Ghost Island Media (Production Company)
https://twitter.com/ghostislandme
taipei keep going 在 KeepGoing繼續前進 - Facebook 的推薦與評價
#kaihansiapan 原住民青少年生涯啟蒙工作坊-花東場。 生涯規劃是一個不斷會經歷到,不斷會思考到的課題。 隨著我們的生活跟經歷有所改變,對於喜歡的工作或想 ... <看更多>
taipei keep going 在 「Taipei keep going」黃珊珊投入北市選戰 - YouTube 的推薦與評價
黃珊珊宣布正式投入台北市長選戰,今天(28號)上午在內湖舉辦參選記者會,台北市長柯文哲帶著北市府局處首長到場力挺,也有不少藍綠的里長前來, ... ... <看更多>