📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有2部Youtube影片,追蹤數超過1,780的網紅Johntool,也在其Youtube影片中提到,記得按讚、訂閱我的頻道、開啟小鈴鐺🔔 獲得更多網路賺錢、WordPress架站、工程師的消息! ----------------------- 【WP全方位架站攻略】:https://ibuildwp.com/ 【站長之路】:https://www.johntool.com/webmaster...
what is nft 在 Facebook 的最佳貼文
Time to embrace the unknown and what the future has in store for us. Calling all good hearted cool nerds and tech geeks out there. NFT, here I come.
These songs are like seeds. Here are 4 re-arranged and re-made songs which documented my life as a city born city brew individual, they also accidentally sketched many years of a city’s history. They turned out to be a cultural conservation campaign, musically. Also, to make it a collection of 5 songs, I included a music background of my career song
這四首歌,是我創作唱作努力20年的一點證據。從2001創作《The Best is Yet to Come》開始,到《活下來》寫了由黃家駒先生到霑叔的人事變遷,然後是《紅河村》的屋邨成長故事,再追溯至《世界中心繼續轉》的主題公園歷史,原來已經20年。至於《The Best》,我覺得這首歌已經不止再屬於我一個人,而是大家的歌,所以這個collection裏第五首歌,是一個《The Best》的伴唱版。合共5首重新製作及演繹的作品,成為我第一個NFT的嘗試。
這些歌都是種子,時間是我的朋友。如果這次NFT拍賣能讓我得到很多資源,憑我20年音樂上的track record,你們都知道我會怎樣好好運用財富:give it back to music,教育,同時一直pay it forward.
Let miracles happen.
#NFT #BINANCE #chetlam #thebestisyettocome
BINANCE
The Best is Yet to Come 2021
https://www.binance.com/en/nft/goods/detail?nftInfoId=455307&isProduct=1
世界中心繼續轉 demo
https://www.binance.com/en/nft/goods/detail?nftInfoId=455308&isProduct=1
紅河村 2021
https://www.binance.com/en/nft/goods/detail?nftInfoId=455311&isProduct=1
活下來 2021
https://www.binance.com/en/nft/goods/detail?nftInfoId=455310&isProduct=1
The Best is Yet to Come 2021 MMO
https://www.binance.com/en/nft/goods/detail?nftInfoId=455312&isProduct=1
what is nft 在 每日幣研 Facebook 的最佳解答
【✨#NFT 是什麼?三個關鍵特性】
過去的一年,無論是知名的歌手、藝術家、NBA 球團甚至是 Elon 他媽都競相發行 NFT ,正當全世界都在為數位藝術品瘋狂的時候,開始出現「NFT 值得投資嗎 」的聲音,但在投資之前,你必須先了解 NFT 。
☑️ NFT 是什麼?
NFT 的全名為 Non Fungible Token 不可替換之代幣(非同質化代幣)。同質化代幣跟非同質化代幣又是什麼,我聽都沒聽過!
舉個例來說:以太幣在你我的加密貨幣錢包中,其實都是一樣的,沒有任何差異,這種代幣就叫同值化代幣(Fungible Token),一般常見位於以太坊上的加密貨幣是屬於 ERC 20 標準。
NFT 代幣則是透過 ERC-721(專用於處理不可替換資產的代幣標準) & ERC-1155 (能夠一次管理不同類型代幣標準)這兩種智能合約標準所創立而成,因此每一樣 NFT,都是獨一無二且無法替代的。
☑️ NFT 能以什麼形式呈現?
NFT 是通過加密的技術把特定物件放到區塊鏈上,這些虛擬品具有獨一無二的特質,符合物品在市場上的稀珍性。因此 NFT 能夠作為數位內容載體的媒介,呈現的形式很多元,除了下述的應用舉例之外,也有非常多的應用層面值得我們去探索。
- 音樂(節拍背景、音樂創作)
- 藝術創作(平面、立體、吉祥物,甚至是迷因、GIF 動圖)
- 活動票券、球員卡
- 遊戲道具
- 內容(Twitter CEO Jack Dorsey「生涯首則Twitter 貼文」之NFT 以$290 萬美元售出)
- 數位身分(許可證、證書)
☑️ NFT 產業鏈是怎麼運作的,由誰組建而成?
大致可以拆分成供給方(創作者、持有版權者)及需求方(收藏家、投機者、粉絲)來細分不同的角色。至於創作者如何操作 NFT ? NFT 對需求方來說有什麼獨特魅力?
收看我們的完整文章,帶您一起探索 NFT❗️
—
📌 NFT 文章傳送門 👉 https://cryptowesearch.com/2021/07/14/what-is-nft/
—
Follow 每日幣研,獲取更多產業快訊及小趣事分享
電子報訂閱連結:https://pse.is/3gkbt2
Instagram: https://pse.is/crypto_wesearch_IG
Facebook: https://pse.is/crypto_wesearch_FB
Youtube : https://pse.is/crypto_wesearch_youtube
what is nft 在 Johntool Youtube 的最佳解答
記得按讚、訂閱我的頻道、開啟小鈴鐺🔔
獲得更多網路賺錢、WordPress架站、工程師的消息!
-----------------------
【WP全方位架站攻略】:https://ibuildwp.com/
【站長之路】:https://www.johntool.com/webmaster/
【我上過的線上課程】:https://www.johntool.com/recommend-courses/
【聯盟行銷賺進百萬】:https://www.johntool.com/what-is-affiliate-marketing/
【YouTube 賺錢方法】:https://www.johntool.com/course-yale-1mbiz-yt
【線上課程賺進百萬】:https://www.johntool.com/course-icc-youtube
-----------------------
【部落格 Johntool-工具王阿璋】:https://www.johntool.com
【Instagram】:https://www.instagram.com/johntooltw/
【粉絲團專頁】:https://www.facebook.com/JohntoolTW/
【TikTok】:https://www.tiktok.com/@johntooltw
【聯絡我】:support@johntool.com
what is nft 在 Johntool Youtube 的最佳貼文
記得按讚、訂閱我的頻道、開啟小鈴鐺🔔
獲得更多網路賺錢、WordPress架站、工程師的消息!
-----------------------
【NBA Top Shot 台灣購買投資教學】:https://www.johntool.com/nba-top-shot-vpn/
【抽卡包必備 VPN】:https://www.johntool.com/nordvpn-youtube
-----------------------
【WP全方位架站攻略】:https://ibuildwp.com/
【站長之路】:https://www.johntool.com/webmaster/
【我上過的線上課程】:https://www.johntool.com/recommend-courses/
【聯盟行銷賺進百萬】:https://www.johntool.com/what-is-affiliate-marketing/
【YouTube 賺錢方法】:https://www.johntool.com/course-yale-1mbiz-yt
【線上課程賺進百萬】:https://www.johntool.com/course-icc-youtube
-----------------------
【部落格 Johntool-工具王阿璋】:https://www.johntool.com
【Instagram】:https://www.instagram.com/johntooltw/
【粉絲團專頁】:https://www.facebook.com/JohntoolTW/
【TikTok】:https://www.tiktok.com/@johntooltw
【聯絡我】:support@johntool.com
what is nft 在 Non Fungible Token - NFT Explained In 5 Minutes - YouTube 的推薦與評價
NFT Explained In 5 Minutes | What Is NFT ? - Non Fungible Token | NFT Crypto Explained | Simplilearn · Chapters. View all · Chapters · Description. ... <看更多>