📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有120部Youtube影片,追蹤數超過52萬的網紅巴哈姆特電玩瘋,也在其Youtube影片中提到,一年一度的農曆七月又到來了!電玩瘋每月鬼月企劃又來啦!今年整理了 2021 年所推出十款值得一玩的恐怖遊戲,現在就讓我們一起把燈關上,來看看那些可怕遊戲吧! 00:00 前言 00:30《侵視者:系統還原 Observer: System Redux》 01:18《DEVOUR》 02:21《恐懼之...
industry medium 在 經濟部中小企業處 Facebook 的精選貼文
【服務.城鄉】🥮聯手臺灣零食業者出擊,搶攻泰國榮獲五星好評🌟
📣因應全球Covid-19疫情持續影響,臺灣旅遊產業思考各種方法維持營運,中企處新南向計畫輔導之智慧旅遊主題聯盟特別號召旅遊上中下游產業,以團體戰模式進軍海外市場💪💪💪
👉聯盟首波主打方案-臺灣零食包,在泰國掀起一陣熱潮,更有泰國消費者熱烈回購,限量上市即銷售一空🤤🤤🤤
🍪 🥮 臺灣零食包看這裡➡ ➡ ➡ https://pse.is/3lsmpm
💬智慧旅遊主題聯盟也設置「My Taiwan Journey」專屬活動網站,以直播、圖文介紹臺灣各式伴手禮及在地遊程,泰國消費者透過趣味競賽回饋分享,成功促進臺泰旅遊交流更緊密❤️
My Taiwan Journey 🔽 🔽 🔽
https://pse.is/3nem4k
#數位創新
#鏈結國際
#智慧旅遊主題聯盟
【Serving City and Country】🥮 Taiwanese snacks developed the Thailand market and won a 5-star rating🌟
📣 Tourism in Taiwan has tried various ways to survive during the pandemic. Smart Tourism Alliance, guided by Small and Medium Enterprise Administration, Ministry of Economic Affairs’s New Southbound Project, was no exception and gathered related industry as a team to develop the overseas market. 💪💪💪
👉Smart Tourism Alliance launched the first full package “Taiwan special snack box” which went viral in Thailand. Customers snapped up as soon as it hit shelves. 🤤🤤🤤
🍪 🥮 Taiwan special snack box➡ ➡ ➡ https://pse.is/3lsmpm
💬 “My Taiwan Journey” is a website built by Smart Tourism Alliance. It introduced Taiwan’s souvenir and local tour through live streaming and posts. The alliance held a special event to invite Thai people to share their thoughts after experiencing products and services from Smart Tourism Alliance. Through this event, a closer relationship between Thailand and Taiwan has been established. ❤️
Find out more about “My Taiwan Journey” 🔽 🔽 🔽
https://pse.is/3nem4k
#DigitalMarketing
#ConnectingwithGlobalMarket
#SmartTourismAlliance
industry medium 在 經濟部中小企業處 Facebook 的最讚貼文
【創業大冒險】#AWE女性創業學院線上小聚超級精實💪❤️
上週與美國在台協會 AIT一起為11位女創菁英導師與133位優秀女性學員,展開一系列產業趨勢與業界經驗分享的精彩活動~
第一天隆重邀請到Shopify與Facebook的講者分享 #B2C跨境電商的趨勢與應用!
接續的主題產業分享講者,也不吝於分享在食品餐飲與民生消費類的紅海中,如何應用電商平台,以數據驅動銷售,掌握海外市場!
第二天由LinkedIn的專家,分享如何在線上找到 #B2B潛在客戶,建立客戶關係。
而主題產業從AR/VR到XR的運用,到生技公司的案例分享,以及製造業如何開拓出美學之路,可說是一場傳統與創新的複合式饗宴😍
第三天則是由創投代表,帶大家了解何謂創投、以及早期創投的關注重點,延續到導師分享,企業因應碳風險的對策,以及建立歷史生態系,打造台灣的新商業模式。
透過豐厚的經驗分享活動,幫助學員掌握產業勢,拓展新業務商機,還能得到募資的指導!
每場次都破百名學員的積極參與,說明AWE學員在創業與學習經驗的熱情,也期待活動後續的互動,能為學員的創業項目帶來不同的觀點與火花🔥🔥🔥
【Startup Adventure】#AWE online meetups exchanged solid industrial knowledge💪❤️
#AWE The Academy for Women Entrepreneurs from 美國在台協會 AIT and MOEA’s #SMEA Small and Medium Enterprise Administration held a series of events last week, sharing industrial trends and exchanging best practices between 11 female facilitators and 133 outstanding participants.
On day 1, the keynote speakers were the leading e-commerce platform and social media platform, Shopify and Facebook. They shed some light on the trend and application of #B2C cross-border e-commerce.
To understand overseas markets better, the speakers in the best practice sessions have given some advice. Such as, in highly competitive industries like the “food & beverage” and “consumer products,” it is critical to fully utilize the e-commerce platform and analyze numbers to drive sales growth.
Day 2, the expert from LinkedIn shared how to identify #B2B potential clients online and build up client relationships.
From AR/VR to XR, bio-tech and manufacturing were the three industries highlighted in this session. The variance and in-depth industrial knowledge were indeed a mingle of tradition and invention!
On day 3, the speaker has taken us through the essence and decision-making focuses of venture capitals. In the sharing of the facilitators, the points were the enterprise solutions of carbon risk and how to build up a historical eco-system, to create business models for Taiwan.
These fruitful events have enlightened the participants with experience sharing, gaining knowledge of industry trends, exploring business opportunities, and obtaining funding guidance!👏
Each meetup has over 100 participants who have shown their passion for creating a business and learning from best practices. We look forward to more interaction in the coming events to bring up different perspectives and sparkles for the very own business for each participant! 🤩
#LetWomenWinTogether
#AWERocks
#AWEnergized
#AWEinTaiwan
#AWE
industry medium 在 巴哈姆特電玩瘋 Youtube 的最讚貼文
一年一度的農曆七月又到來了!電玩瘋每月鬼月企劃又來啦!今年整理了 2021 年所推出十款值得一玩的恐怖遊戲,現在就讓我們一起把燈關上,來看看那些可怕遊戲吧!
00:00 前言
00:30《侵視者:系統還原 Observer: System Redux》
01:18《DEVOUR》
02:21《恐懼之間 Fear surrounds》
03:11《Fears to Fathom - Home Alone》
04:08《Reminiscence》
05:02《小小夢魘 2 Little Nightmares II》
05:39《靈媒 The Medium》
06:10《惡靈古堡 8:村莊 Resident Evil Village》
06:50《翌日 Tsugunohi》
07:43《恐怖之歌 SONG OF HORROR》
#恐怖遊戲
#steam
industry medium 在 Ga-Me TV Youtube 的精選貼文
สนใจ Windows 10 Pro ใช้งานได้ถาวรสามารถสั่งซื้อได้ที่
http://www.facebook.com/GameTVTH
ทุกท่านสามาร Donate สนับสนุนได้ 3 ช่องทาง โดยการ
1. ผ่าน Paypal, Visa, MasterCard
https://streamlabs.com/gametvth/tip
2. ผ่านธนาคาร กสิกร
061-2-70639-5 นาย วิชชุพันธุ์ บาตรโพธิ์
3. ผ่าน True Money และ True Wallet (30 บาทขึ้นไปถึงจะแสดงบนจอนะครับ)
https://tipme.in.th/gametvth
*หลังจากโอนธนาคารสามารถแจ้งชื่อมาได้ที่ แชทสดๆ หรือ Fanpage Facebook เพื่อทำการแสดงชื่อบนหน้า Live ครับผม
ร้านขายเกมของผมเอง Ga-Me Key Store: http://www.gametvth.com/
-----------------------------------------------------------
สเปคที่ใช้กันแมวถาม Update ปี 2020
CPU: Ryzen 9 3950x (16 core 32 Thread)
Ram: Corsair Vengeance RGB PRO 3000MHz 64 GB (4x16GB)
MB: Gigabyte X570 Aorus Master
VGA: Zotac RTX 3080
Power Supply: Corsair RM1000x
SSD: WD Black M2 PCIe NVMe
HDD: Seagate 4TB + 4 TB + 4 TB
CPU Cooler: Thermaltake Floe Riing Rgb 240 Tt Premium
Case: COOLERMASTER MC600P
industry medium 在 Ga-Me TV Youtube 的最讚貼文
สนใจ Windows 10 Pro ใช้งานได้ถาวรสามารถสั่งซื้อได้ที่
http://www.facebook.com/GameTVTH
ทุกท่านสามาร Donate สนับสนุนได้ 3 ช่องทาง โดยการ
1. ผ่าน Paypal, Visa, MasterCard
https://streamlabs.com/gametvth/tip
2. ผ่านธนาคาร กสิกร
061-2-70639-5 นาย วิชชุพันธุ์ บาตรโพธิ์
3. ผ่าน True Money และ True Wallet (30 บาทขึ้นไปถึงจะแสดงบนจอนะครับ)
https://tipme.in.th/gametvth
*หลังจากโอนธนาคารสามารถแจ้งชื่อมาได้ที่ แชทสดๆ หรือ Fanpage Facebook เพื่อทำการแสดงชื่อบนหน้า Live ครับผม
ร้านขายเกมของผมเอง Ga-Me Key Store: http://www.gametvth.com/
-----------------------------------------------------------
สเปคที่ใช้กันแมวถาม Update ปี 2020
CPU: Ryzen 9 3950x (16 core 32 Thread)
Ram: Corsair Vengeance RGB PRO 3000MHz 64 GB (4x16GB)
MB: Gigabyte X570 Aorus Master
VGA: Zotac RTX 3080
Power Supply: Corsair RM1000x
SSD: WD Black M2 PCIe NVMe
HDD: Seagate 4TB + 4 TB + 4 TB
CPU Cooler: Thermaltake Floe Riing Rgb 240 Tt Premium
Case: COOLERMASTER MC600P