📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有682部Youtube影片,追蹤數超過4,050的網紅賈桂琳阿龜,也在其Youtube影片中提到,Pana潘娜結紮全記錄vlog(上)#賈桂琳阿龜 #pana龜 #vlog #cat #結紮 #領養代替購買 歡迎加入line群社團「賈桂琳阿龜粉絲團」聊天唷 賈桂琳阿龜line原創動態貼圖 https://line.me/S/sticker/16629925 ✨我只想要你笑一個和關注✨ ...
「box taipei」的推薦目錄:
- 關於box taipei 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於box taipei 在 瑪格 Facebook 的精選貼文
- 關於box taipei 在 Monster Taipei Facebook 的最佳貼文
- 關於box taipei 在 賈桂琳阿龜 Youtube 的最讚貼文
- 關於box taipei 在 賈桂琳阿龜 Youtube 的最佳解答
- 關於box taipei 在 Jasper Youtube 的精選貼文
- 關於box taipei 在 台北頂級VR餐酒館 BOX Taipei 夜晚娛樂新指標 緊張刺激 ... 的評價
- 關於box taipei 在 東區Box Taipei-好吃又好玩VR體感競技餐酒館,搶救人質大作戰! 的評價
- 關於box taipei 在 全新的娛樂體驗!全球首個VR遊戲主題酒吧“BOX Taipei” 在 ... 的評價
box taipei 在 瑪格 Facebook 的精選貼文
民生社區超值烤肉飯,驚傳9/30熄燈,松山區民表示遺憾:想吃的朋友,趁這幾天去捧場吧!
👍Grill Box 又一間商行→https://margaret.tw/taipei-grill-box/
今晚試了青醬海鮮飯,很滿意!決定月底前至少還要吃一次。疫情影響各行各業,大家都辛苦了呀!請多多支持餐飲業。
box taipei 在 Monster Taipei Facebook 的最佳貼文
MONSTER TAIPEI LOTTERY
網路抽選活動
INSTINCTOY THUNDER LIQUID Complete box 1st series
"7 Clear colors + DIY Clear Blank"
抽選販售商品:
INSTINCTOY THUNDER LIQUID Complete box 1st series
"7 Clear colors + DIY Clear Blank"
尺寸:單款高約 6.2cm (H)
材質:軟膠
售價:一套TWD.6980/box
※一組七款,另附加一款特別款DIY Clear Blank,此為透明素體,可自行DIY。
抽選表單填寫時間:
2021/09/14 (二) 17:00 ~ 2021/09/15 (三) 20:00
抽選表單連結:
https://forms.gle/LR1Yvaqcp9A78ybe7
販售規則:
1.此款商品採線上抽選方式販售,欲參加抽選的朋友請填寫 Google 表單,需登入 Google 帳號,一人一個帳號限填寫一次,如有重複填寫資料者或資料填寫不齊全者將直接取消抽選資格。
2.請於表單開放時間內填寫,逾時不候。
3.抽選將會使用電腦亂數程式隨機抽選,抽選結果將於 2021/09/16 (四) 當天 21:00 前以簡訊及 e-mail 通知中選者,未中選者不另行通知。
4.中選者需於收到中選通知後三日內 2021/09/19 (日) 結束前完成付款,如中選者未在時間內完成交易將直接取消購買資格並列入永久黑名單,請務必確認是否能在規定時間內付款再參與抽選活動。
5.中選者購買方式選擇
(1) Monster Taipei 實體門市取貨付款 (現金/刷卡) 。
(2) 提供上海商銀帳戶匯款郵寄。 (僅限郵寄台灣地區運費$80)
6.參與抽選的朋友如有中選,請遵守以下規定
(1) 選擇至 Monster Taipei 實體門市自取:請務必於指定時間內攜帶身份證件至門市取貨付款,非本人無法領取,不接受親朋好友代領等行為。
(2) 選擇郵寄:此次抽選僅限郵寄台灣地區,請務必於指定時間內付款,抽選表單上填寫的收件人及地址無法更改,欲更改者視同放棄購買資格。
7.申請抽選將視為皆已同意以上規則,Monster Taipei 保有最終販售及修改之權利。
Monster Taipei 台北怪獸國際有限公司
box taipei 在 賈桂琳阿龜 Youtube 的最讚貼文
Pana潘娜結紮全記錄vlog(上)#賈桂琳阿龜 #pana龜 #vlog #cat #結紮 #領養代替購買
歡迎加入line群社團「賈桂琳阿龜粉絲團」聊天唷
賈桂琳阿龜line原創動態貼圖
https://line.me/S/sticker/16629925
✨我只想要你笑一個和關注✨
✨All I want for you is your smile and follow ✨
✨我這麼無聊你還關注我✨
✨超級感謝🥰
👇🏻鏈接底下都關注一下👇🏻😘
🔗https://linktr.ee/jiaguilinagui🔗
❤️喜歡請訂閱 歡迎留言分享❤️
🧡官方Line「@jiaguilineagui」
💛IG「lifei.shappiness」
💚FB「賈桂琳-阿龜」
💙TIKTOK 「wowowwyeah」
💜YouTube 「賈桂琳-阿龜」
🖤郵寄地址「11599南港郵局第2022號信箱」
🤎English address
P.O. BOX 2022
Nangang
Taipei City 11599
Taiwan (R.O.C)
Jiaguilineagui 03-9553565
🤍@賈桂琳阿龜 1978/03/26 屬羊♈️
一個自導自演自剪自閉的🐢感謝您🙏🏻
box taipei 在 賈桂琳阿龜 Youtube 的最佳解答
報告:line的社群開囉~歡迎大家來聊天
🔎「賈桂琳阿龜粉絲團」
✨我只想要你笑一個和關注✨
✨All I want for you is your smile and follow ✨
✨我這麼無聊你還關注我✨
✨超級感謝🥰
👇🏻鏈接底下都關注一下👇🏻😘
🔗https://linktr.ee/jiaguilinagui🔗
❤️喜歡請訂閱 歡迎留言分享❤️
🧡官方Line「@jiaguilineagui」
💛IG「lifei.shappiness」
💚FB「賈桂琳-阿龜」
💙TIKTOK 「wowowwyeah」
💜YouTube 「賈桂琳-阿龜」
🖤郵寄地址「11599南港郵局第2022號信箱」
🤎English address
P.O. BOX 2022
Nangang
Taipei City 11599
Taiwan (R.O.C)
Jiaguilineagui 03-9553565
🤍賈桂琳阿龜1978/03/26 屬羊♈️
一個自導自演自剪自閉的🐢感謝您🙏🏻
box taipei 在 Jasper Youtube 的精選貼文
我很少會這麼說一個廠家,
他們願意改變環境並讓更多人可以進入攝影生態,
這是Mirage帶給我最大的快樂以及希望 !
產品連結我放這邊,
需要的自己看看:
Tilta Mirage
https://reurl.cc/WXrLbD
Vaxis VFX Filter
https://bit.ly/3ibM6cV
#評測
// Artlist 免費 60天 試用 //
https://bit.ly/2UiXCKR
// Artgrid 免費 60天 試用 //
https://bit.ly/3BaAX3U
// Epidemic Sound 免費 30天 試用 //
http://share.epidemicsound.com/CpNw8
► 我的器材 / My Gear
相機 / Camera:
- BMPCC 4K 6K 6K Pro
- Sony A7S3 A7M3 A6400
鏡頭 / Lenses:
- DZO Film Vespid Prime
- Sony SEL 1635GM
- Sony SEL 2470GM
- Sony SEL 20F18G
- Sony SEL 90M28G Macro
- Sony SEL 85GM
- Sony SEL 135GM
- Sigma 18-35 ART
- Sigma 50-100 ART
電動滑軌 / Sliders:
- Smartta SliderMini 2
- YC Onion HotDog Slider 3
- iFootage Shark Slider Nano
空拍機 / Drone:
- DJI Mavic 2 Pro
- DJI Mavic Air 2S
麥克風 / Mic:
- RODE VideoMic NTG
- RODE Wireless GO
- SHURE VP83F
- SHURE SM7B
- DEITY S-Mic 2
- DEITY S-Mic 2S
- DEITY HD-TX
- DEITY BP-TX
- DEITY Duo-RX
燈光 / Light:
- Aputure 60X 120DII 300DII 300X 600D Pro
- Aputure Amaran 100X 200X
三軸穩定器 / Gimbal:
- Feiyu AK4500
- DJI RS2
隔音棉 / Sound-absorbing:
- MayVoice 美聲
►商業合作
Email:cba024538@gmail.com
► 信件寄送地址
【中文】
22199 汐止社后郵局
第 78 號信箱
【英文】
P.O.BOX 78 Xizhi Shehou
New Taipei City 22199 Taiwan ( R.O.C.)
box taipei 在 全新的娛樂體驗!全球首個VR遊戲主題酒吧“BOX Taipei” 在 ... 的推薦與評價
將VR遊戲和酒吧組合在一起會發生什麽?答案就是那會變成一個忘記現實世界的好地方!世界首個VR遊戲酒吧“BOX Taipei”在台灣的台北市開業,為台北的 ... ... <看更多>
box taipei 在 台北頂級VR餐酒館 BOX Taipei 夜晚娛樂新指標 緊張刺激 ... 的推薦與評價
台北頂級VR餐酒館 BOX Taipei 夜晚娛樂新指標 緊張刺激體感競技 ⭐️美味佳餚⭐️經典調酒⭐️最先進VR設備⭐️貴賓歡唱包廂 營業時間平日:17:30-02:00 ... ... <看更多>