📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有9部Youtube影片,追蹤數超過5,140的網紅寶博士,也在其Youtube影片中提到,阿亂王新仁 - 臺灣首個登上 ArtBlocks 拍賣 Good Vibrations 台灣時間 8/22(日)凌晨 0 點 開賣(也就是週六深夜) https://artblocks.io/project/140 起價: 1.559 ETH (荷蘭拍賣) 價格曲線:每十分鐘降價一次,依序為 1.5...
「generative art」的推薦目錄:
- 關於generative art 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於generative art 在 Sharmaine Kwan - Artist Facebook 的最佳貼文
- 關於generative art 在 GIGAZINE Facebook 的最佳貼文
- 關於generative art 在 寶博士 Youtube 的最讚貼文
- 關於generative art 在 老闆,來點寇汀吧。Boss, CODING please. Youtube 的最佳解答
- 關於generative art 在 老闆,來點寇汀吧。Boss, CODING please. Youtube 的最佳貼文
- 關於generative art 在 77 Inspirations from generative art ideas 的評價
- 關於generative art 在 generative-art · GitHub Topics 的評價
generative art 在 Sharmaine Kwan - Artist Facebook 的最佳貼文
Alice R50 testing in progress~ 🔊 Come visit and feed her words! As an interactive installation she will generate and recite original poems from your input~
'Poems from the eternal hoop'
In her heydays, Alice R50 eased people’s loneliness. Today, she is stripped down to a court jester’s head, saying poems from user input as if they were religious chants. She does so by means of The Hoop Sayer, an algorithm written by Johan F. Hoorn and implemented in Ruby by Giovanni Lion.
The generative loop that creates ‘variations on a theme’ is like a banquet of liberal arts in which poets work together on a solitary sequence. The rule of poetry the algorithm uses is the rhetorical and writing technique of Anadiplosis, repeating the last word of the preceding line. In Chinese, this stylistic device is known as 頂真 (dingzhen). As an algorithm that dynamically interacts with the visitors, the robot and the people work together to create never-before-heard verses, which then form new and original poems within the constraints of the designed rules. The process itself is a beautiful open and collaborative art in itself.
generative art 在 GIGAZINE Facebook 的最佳貼文
幻想的で美麗な模様がカーソルを動かすだけの単純操作で描ける「Silk」を使ってみた(2015)
https://gigazine.net/news/20150525-silk-interactive-generative-art/
generative art 在 寶博士 Youtube 的最讚貼文
阿亂王新仁 - 臺灣首個登上 ArtBlocks 拍賣
Good Vibrations
台灣時間 8/22(日)凌晨 0 點 開賣(也就是週六深夜)
https://artblocks.io/project/140
起價: 1.559 ETH (荷蘭拍賣)
價格曲線:每十分鐘降價一次,依序為 1.559Ξ, 1.259Ξ, 0.959Ξ, 0.659Ξ, 0.359Ξ, 0.159Ξ
限量 1024 張,售完即止
科普一下,ArtBlocks 是現在最熱門也最有價值的 NFT 系列藏品,由 ArtBlocks 創辦人 Jeff Davis 為首的多位知名藝術家作審查小組,嚴選全球各地藝術家上架演算生成藝術作品(Generative Art),上線半年目前是整個 NFT 市場排名前五大,總成交量 2 億 3 千萬美金,約新台幣 60 億元,總計 9 萬顆以太幣;單一作品最高成交價為 Chromie Squiggle 編號 #3784 以 244 萬美金(750 ETH)售出,原始擁有者是個名為「0x79」的藏家,其初始購入價格僅 25 美元!
ArtBlocks 作品最好玩的地方就是它像個由演算法藝術家打造出來的,一個又一個的隨機藝術盲盒,你可以用相對低的價格,購買一個作品,透過公正公平公開的演算法,在天時地利人和下,創造出萬中選一的絕美作品(連藝術家本人都無法知道該作品可能長什麼樣!)這次阿亂的作品若最後在 0.159 ETH 完售,那麼相當於你有可能用新台幣 1 萬 5 千元的價格,買到台灣一級數位藝術家的首批以太坊 NFT 作品,也是台灣首位藝術家登上 ArtBlocks 的作品,有其歷史意義,甚至有可能在未來以更高的價位販售給廣大的 ArtBlocks 作品全球藏家。如果你還在觀望 NFT 的,這是個好的機會,一邊可以學習如何購買,一邊可以支持台灣藝術家。
即時查詢 ArtBlocks 合約交易狀況 https://etherscan.io/address/0x47e312d99c09ce61a866c83cbbbbed5a4b9d33e7
ArtBlocks Curated 底價即時查詢 https://opensea.io/collection/art-blocks?search[sortAscending]=true&search[sortBy]=PRICE&search[toggles][0]=BUY_NOW
generative art 在 老闆,來點寇汀吧。Boss, CODING please. Youtube 的最佳解答
課程的 TA 直播,以及聊聊 Shader 跟 WebGL
Hahow 互動藝術程式創作入門 課程連結:https://bit.ly/2UJXu7F
00:00 開頭聊聊
05:46 課程同學作品分享
18:49 Shader 分享
38:15 3D軟體中的渲染
56:45 其他問題聊聊
#shader
#WevGL
generative art 在 老闆,來點寇汀吧。Boss, CODING please. Youtube 的最佳貼文
課程的 TA 直播,聊聊生成式藝術跟其他問題。
Hahow 互動藝術程式創作入門 線上課程連結 👉 https://bit.ly/boss-creative-coding
Creative Coding 台灣站 👉 https://bit.ly/3iV5sT8
0:00 開頭聊聊
12:00 做科技藝術的公司
30:25 生成式藝術的概念
48:52 課程學生作品觀摩
#creativecoding
#生成式藝術
#創意寫碼
generative art 在 generative-art · GitHub Topics 的推薦與評價
Creative Coding: Generative Art, Data visualization, Interaction Design, Resources. ... The Generative AI Landscape - A Collection of Awesome Generative AI ... ... <看更多>
generative art 在 77 Inspirations from generative art ideas 的推薦與評價
... Lupi's board "inspirations from generative art", followed by 5348 people on Pinterest. See more ideas about generative art, generative, generative design. ... <看更多>