📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有7部Youtube影片,追蹤數超過7萬的網紅Inorin,也在其Youtube影片中提到,⏰ Live เวลา 2 ทุ่ม ถึง 5 ทุ่มของทุกวันที่ Facebook : https://www.facebook.com/inorinchannel --------------------------------------------------------...
「fan token」的推薦目錄:
- 關於fan token 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於fan token 在 LDA ลดา - Ladies of Digital Age Facebook 的最佳解答
- 關於fan token 在 每日幣研 Facebook 的最讚貼文
- 關於fan token 在 Inorin Youtube 的最讚貼文
- 關於fan token 在 Smorc TV Youtube 的最讚貼文
- 關於fan token 在 Inorin Youtube 的最佳解答
- 關於fan token 在 FAN TOKEN: Vale a pena? Entenda essa “Criptomoeda” 的評價
- 關於fan token 在 Paris Saint-Germain Fan Token 彙整- 區塊客 的評價
fan token 在 LDA ลดา - Ladies of Digital Age Facebook 的最佳解答
สินทรัพย์เปลี่ยนโลก! NFT ช่องทางทำเงินใหม่?
ช่วงนี้ NFT กำลังมาแรง นอกจากกระแสวงการคริปโต
หลายแวดวง เช่น ศิลปะ เกม ไปจนถึง Influencer
ก็หันมาสนใจเรื่องนี้กันมากขึ้น มีการนำไปประยุกต์
ใช้ในรูปแบบต่าง ๆ จน mass ขึ้นเรื่อย ๆ
แต่ทิศทางในอนาคต ของสินทรัพย์ดิจิทัลนี้จะเป็นยังไง?
บทความนี้เราสรุปให้ฟังค่ะ
fan token 在 每日幣研 Facebook 的最讚貼文
【當粉絲經濟遇上加密貨幣—Chiliz😆】
影片傳送門:https://youtu.be/jFuZSe8wUx8
剛加盟巴黎聖日耳曼的球王美斯,薪酬的一部分就是用加密貨幣PSG支付!🤯到底發行這 PSG fan token 的 Chiliz 是何方神聖?為甚麼他們能在粉絲經濟這領域上稱霸?他們未來還有沒有升值潛力?影片最後部分也會有簡單的市況分析,想貼近潮流的朋友千萬別錯過!🤓
---------------------------------------
【Bybit 交易大賽|每日幣研戰隊隊員招募 🔥 】
Bybit 交易大賽2021已經開始啦!最高獎金池高達 6,000,000 USDT!我們現正邀請各幣圈高手加入幣研隊伍「 每日幣研cryptowesearch」戰隊!我們團隊雲集眾多香港及台灣高手, 即使你不是團隊內頭十名也能平均分享獎金池的50%!團隊第一名能獲得我們幣研獨家送出的1,000 USDT獎賞!(無論幣研是否勝出)立即與每日幣研一起組隊參加團隊賽啦,人數不限!還有超多參賽獎賞,比賽報名9月4日截止,快來加入我們吧!🙌🏻
加入我們戰隊👉🏻:https://bit.ly/cryptowesearch-wsot2021
更多參賽詳情👉🏻:https://bit.ly/2W8NFAv
幣研WSOT交易大賽TG群組🤩
每日幣研為促進戰隊間的合作交流,特意建立交易大賽高手TG群組!大家可在群組內與其他來自香港及台灣的虛擬貨幣交易高手交流投資心得,互相分享實時交易紀錄,共同努力爭奪 600萬 USDT獎金!🔥
申請加入群組👉🏻:https://forms.gle/xnfQY7j2PfujJ32m6
fan token 在 Inorin Youtube 的最讚貼文
⏰ Live เวลา 2 ทุ่ม ถึง 5 ทุ่มของทุกวันที่ Facebook : https://www.facebook.com/inorinchannel
---------------------------------------------------------------
สามารถรับชมสตรีมได้ที่ เพจ Facebook Inorin Channel
ถ้าหากชอบช่วยกด ถูกใจ/แชร์ คลิปนี้ หรือ
Subscribe เพื่อดูคลิปใหม่ๆด้วยนะครับ
สำหรับคนที่อยากคอมเม้นอะไรก็คอมเม้นทิ้งไว้ด้านล่างเลย
-------------------------------------------------------------
ติดตามช่อง : https://www.youtube.com/c/Inorinchannel
Fan Page : https://www.facebook.com/inorinchannel/
ถ้าใครวาด FanArt มาให้ สามารถส่งมาได้ที่ Fan Page นะครับ
-------------------------------------------------------------
fan token 在 Smorc TV Youtube 的最讚貼文
ซื้อของในเกมHearthstone ถูกกว่าเติมเองได้ที่นี่: https://goo.gl/LmNvc7
Donateให้พี่มอร์คได้ที่ : https://goo.gl/CQCdo6
*********************************************************
อย่าลืมกด Subscribe เป็นกำลังใจให้พวกเราด้วยนะครับ
กดติดตามช่อง Youtube: https://goo.gl/93WriW
Fan Page: https://goo.gl/HX7aWX
fan token 在 Inorin Youtube 的最佳解答
---------------------------------------------------------------
สามารถรับชมสตรีมได้ที่ เพจ Facebook Inorin Channel
ถ้าหากชอบรบกวนช่วยกด ถูกใจ/แชร์ คลิปนี้ หรือ
Subscribe เพื่อดูคลิปใหม่ๆด้วยนะครับ
สำหรับคนที่อยากคอมเม้นอะไรก็คอมเม้นทิ้งไว้ด้านล่างเลย
-------------------------------------------------------------
ติดตามช่อง : https://www.youtube.com/c/Inorinchannel
Fan Page : https://www.facebook.com/inorinchannel/
ถ้าใครวาด FanArt มาให้ สามารถส่งมาได้ที่ Fan Page นะครับ
-------------------------------------------------------------
fan token 在 Paris Saint-Germain Fan Token 彙整- 區塊客 的價格和怎麼買
標籤: Paris Saint-Germain Fan Token. 新聞消息精選主題 · 「球王」梅西加盟法甲PSG!部分薪酬以加密貨幣支付 · 區塊妹Mel / 2021-08-13. web3hub. 熱門加密貨幣. ... <看更多>
fan token 在 FAN TOKEN: Vale a pena? Entenda essa “Criptomoeda” 的價格和怎麼買
VAMOS INVESTIR JUNTOS? Conheça o Fundo ARCA: https://r.vocemaisrico.com/062cef2d26- Entre para terceira turma do Viver de Renda Cripto, ... ... <看更多>