📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有875部Youtube影片,追蹤數超過4萬的網紅受害者協會 - 爆笑精彩時刻,也在其Youtube影片中提到,有任何爐石的精彩片段 歡迎投稿到受害者協會的臉書 https://pse.is/3grneb 對於影片內容有任何疑慮 請聯繫受害者協會FB將會以最快速度為您處理 --------------------------------------------------------------------...
top deck 在 Facebook 的最讚貼文
《療癒系海獺,kayak in Elkhorn Slough》
沒想到海獺可以這麼悠哉,以仰式的姿態徜徉在海上,我們划著獨木舟看著它們,彷彿時間靜止不動,放空放空,是開啟長假的最佳模式。當然除了海獺,還有海豹和海鳥,連一條一條小孩愛撈起來的海帶,都不會讓你失望的。青春組一艘,我們另一艘,一起競賽,也弄得全身很濕,後來還讓在前頭的我拉著兩艘船划前進而不自知。
獨木舟有兩款,一種是closed deck ,另一種是sit on top。sit on top就是全開式,上層沒有包覆,比較容易曬也比較容易弄溼,但比較不易搖晃,適合新手上路,closed deck 就是全包式,除了救生衣外,下半身還會加個下擺,整個身體都在船體裡,比較抗寒抗曬,根據每個人狀況各取所需。
船公司在說明的時候,有特別提醒建議划到seal bend,差不多1miles的位置,不要想說體力很好繼續划到2miles遠,因為回程很費力,可能手臂酸痛,悔不當初。
健身伴跟我特別提醒Russia twist 划船健身動作記得用上,讓背酸痛比手臂酸痛好,老師應該會很感動我們這些學生連放假還這麼學以致用吧!
租獨木舟公司
https://kayakconnection.com/elkhorn-slough/tours/
注意事項:
1.記得不要穿牛仔褲,最好穿棉質衣物。
2.記得帶換洗衣物,真心不騙,一定會濕。
3.冷熱未知,防曬跟防寒都要。
4.戴上一瓶水,划累解渴需要。
5.預約早上時間比較好,出去的時候,氣力足夠,可以逆著潮流方向前進,但回來的時候就會是輕鬆的讓潮流帶你的船回家。下午則是相反方向,就會比較累。
6.塞車是日常,早出門,是良方。
top deck 在 Facebook 的最佳貼文
The secret to multitasking,
is extreme focus and organisation. 😉
There are many reasons to own the Zenbook Pro Duo (UX582)OLED Display + Duo screen
and one of the best thing about it
is that it has TWO SCREENS. ✌🏻
The revolutionary tilting ScreenPad 2.0,
built into the top half of the lower deck,
allows for easy multitasking 💻
and the degree° of the tilt
provides a more comfortable viewing experience. 🕶
Not forgetting the OLED display of this laptop.
It’s attention catching as it displays everything in vivid detail. 🤓
Power of 10 - New Possibilities With A Thin & Light Laptop, powered by Intel® Core™ i9 Processors
Learn more: https://www.asus.com/my/
#ASUS #OLED #Intel10thGen
#ASUSCreatorAward2021
@asusmalaysia
top deck 在 受害者協會 - 爆笑精彩時刻 Youtube 的最讚貼文
有任何爐石的精彩片段
歡迎投稿到受害者協會的臉書 https://pse.is/3grneb
對於影片內容有任何疑慮
請聯繫受害者協會FB將會以最快速度為您處理
--------------------------------------------------------------------
#爐石戰記#比賽精華#精彩時刻
--------------------------------------------------------------------
受害者協會_爆笑精彩時刻:分享爐石戰記的各式精彩時刻
https://goo.gl/cmBybW
根據“Creative Commons Attribution”許可 (https://creativecommons.org/licenses/by/4.0/) 使用Kevin MacLeod創作的歌曲“Greko Sketch”
來源:http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100389
藝術家:http://incompetech.com/
top deck 在 受害者協會 - 爆笑精彩時刻 Youtube 的最佳解答
有任何爐石的精彩片段
歡迎投稿到受害者協會的臉書 https://pse.is/3grneb
對於影片內容有任何疑慮
請聯繫受害者協會FB將會以最快速度為您處理
--------------------------------------------------------------------
#爐石戰記#比賽精華#精彩時刻
--------------------------------------------------------------------
受害者協會_爆笑精彩時刻:分享爐石戰記的各式精彩時刻
https://goo.gl/cmBybW
根據“Creative Commons Attribution”許可 (https://creativecommons.org/licenses/by/4.0/) 使用Kevin MacLeod創作的歌曲“Greko Sketch”
來源:http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100389
藝術家:http://incompetech.com/
top deck 在 受害者協會 - 爆笑精彩時刻 Youtube 的最佳貼文
有任何爐石的精彩片段
歡迎投稿到受害者協會的臉書 https://pse.is/3grneb
對於影片內容有任何疑慮
請聯繫受害者協會FB將會以最快速度為您處理
--------------------------------------------------------------------
#爐石戰記#比賽精華#精彩時刻
--------------------------------------------------------------------
受害者協會_爆笑精彩時刻:分享爐石戰記的各式精彩時刻
https://goo.gl/cmBybW
根據“Creative Commons Attribution”許可 (https://creativecommons.org/licenses/by/4.0/) 使用Kevin MacLeod創作的歌曲“Greko Sketch”
來源:http://incompetech.com/music/royalty-free/index.html?isrc=USUAN1100389
藝術家:http://incompetech.com/
top deck 在 Top Deck Games - Home | Facebook 的推薦與評價
Top Deck Games, Westmont, New Jersey. 4364 likes · 455 talking about this · 1332 were here. The premiere destination for all of your gaming needs. ... <看更多>