📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有49部Youtube影片,追蹤數超過7萬的網紅肥貓丸 FatCatBall,也在其Youtube影片中提到,OTK:07:53 BGM:https://youtu.be/lKMqwRv3plI ※本牌組勝率很低,請斟酌使用 加入肥貓丸頻道會員,享專屬會員徽章: https://www.youtube.com/channel/UCbr1TwV0Tk7LeQ5Yzi0vRxA/join 加基森拍賣師 Ga...
「random draw」的推薦目錄:
- 關於random draw 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於random draw 在 猴子的動漫部屋 Monkey's Comics Facebook 的最讚貼文
- 關於random draw 在 Facebook 的精選貼文
- 關於random draw 在 肥貓丸 FatCatBall Youtube 的最讚貼文
- 關於random draw 在 肥貓丸 FatCatBall Youtube 的最佳貼文
- 關於random draw 在 123JAPAN! Youtube 的最佳解答
- 關於random draw 在 Random Drawing Generator Challenge - YouTube 的評價
- 關於random draw 在 Random Drawing Generator by AudityDraws in 2021 - Pinterest 的評價
- 關於random draw 在 Recursive random draw [closed] - Mathematics Stack Exchange 的評價
- 關於random draw 在 Random draws with a vector of probabilities - Stack Overflow 的評價
random draw 在 猴子的動漫部屋 Monkey's Comics Facebook 的最讚貼文
#Pikachu 你永遠不可能跟華生這麼親,
懂了嗎? 婊子! (冇啦啦諗起八婆比卡超meme好似Sherlock,我個腦冇得救
Suddenly the bitchy Pikachu vibe reminds me of Sherlock, so here you go.
I don't draw, I am a random meme generator.
#Johnlock #sherlock #Pikachu #福華
random draw 在 Facebook 的精選貼文
你有哪一個日常,是不能略過的呢?
我的話,一定是日常護膚!🧏🏻♂️
今天就把護膚好商品送給你!Giveaway!🎉🎉🎉
商家將送出三份 @ohmydaily.co Finé Cleansing Oil !
它自带五合一功能:深层洁净/卸除彩妆/保湿提亮/恢复肌肤弹性/减少黑头粉刺
參加方法非常簡單 ⬇️
1. like & follow @ohmydaily.co 官方IG和FB
2. like這個post和在下方留言區tag 3位好友
3位幸運兒會透過lucky draw形式選出。
我會在24小時過後公佈結果,記得留守我的IG story啦!
注意事項⚠️
*this campaign is open to public in Malaysia only
*each contestant is entitled to one entry only
*contestant are required to make their profile public for review purposes
*winner will be selected on a random basis
random draw 在 肥貓丸 FatCatBall Youtube 的最讚貼文
OTK:07:53
BGM:https://youtu.be/lKMqwRv3plI
※本牌組勝率很低,請斟酌使用
加入肥貓丸頻道會員,享專屬會員徽章:
https://www.youtube.com/channel/UCbr1TwV0Tk7LeQ5Yzi0vRxA/join
加基森拍賣師
Gadgetzan Auctioneer
每當你施放法術,抽一張牌
Whenever you cast a spell, draw a card.
納茲曼織血者
Nazmani Bloodweaver
在你施放法術後,使你一張隨機手牌的消耗降低(1)
After you cast a spell, reduce the cost of a random card in your hand by (1).
魯莽的火箭手
Reckless Rocketeer
衝鋒
Charge
※本牌組勝率很低,請斟酌使用
附上牌組:
奇蹟火箭
職業:牧師
規則:標準
鳳凰年
2x (1) 怠惰浪潮
2x (1) 恢復
1x (1) 智識之球
2x (1) 聖光懲擊
2x (1) 防腐儀式
1x (2) 幽暗身影
2x (2) 懺悟
1x (2) 暗言術:死
2x (2) 洞悉
1x (3) 光明恩賜
2x (3) 看手相
2x (3) 納茲曼織血者
1x (3) 香蕉商人
2x (4) 亡墓符文
1x (4) 神聖新星
1x (4) 群體驅魔
2x (6) 加基森拍賣師
1x (6) 魯莽的火箭手
1x (7) 靈魂之鏡
1x (8) 杰佩托
AAECAafDAwq9A8kG0wrWCu+SA9qdA8i+A6fLA4/OA7DhAwqXAqQHrKUDmakD2awDk7oDy80D4t4D+98DyuEDAA==
#爐石OTK
random draw 在 肥貓丸 FatCatBall Youtube 的最佳貼文
OTK:05:05、09:18
BGM:https://youtu.be/lKMqwRv3plI
※本牌組很吃手速,請斟酌使用
加入肥貓丸頻道會員,享專屬會員徽章:
https://www.youtube.com/channel/UCbr1TwV0Tk7LeQ5Yzi0vRxA/join
『分裂者』克蘇恩
C'Thun, the Shattered
對戰開始:分裂成數塊 戰吼:造成30點傷害,隨機分給敵方角色
Start of Game: Break into pieces. Battlecry: Deal 30 damage randomly split among all enemies.
加基森拍賣師
Gadgetzan Auctioneer
每當你施放法術,抽一張牌
Whenever you cast a spell, draw a card.
納茲曼織血者
Nazmani Bloodweaver
在你施放法術後,使你一張隨機手牌的消耗降低(1)
After you cast a spell, reduce the cost of a random card in your hand by (1).
※本牌組很吃手速,請斟酌使用
附上牌組:
奇蹟蘇恩
職業:牧師
規則:標準
鳳凰年
2x (0) 沉默
2x (0) 真言術:盾
2x (1) 光輝術
2x (1) 恢復
2x (1) 聖光懲擊
2x (1) 防腐儀式
2x (2) 洞悉
2x (3) 光明恩賜
2x (3) 看手相
2x (3) 納茲曼織血者
2x (4) 亡墓符文
1x (4) 神聖新星
2x (4) 群體驅魔
2x (6) 加基森拍賣師
1x (8) 杰佩托
1x (9) 群體復活
1x (10) 『分裂者』克蘇恩
AAECAafDAwTJBpmbA9qdA7/gAw2XAuUEpAelCdYKrKUD0awD2awDk7oDp8sD4t4D+98DyuEDAA==
#爐石OTK
random draw 在 123JAPAN! Youtube 的最佳解答
Kumusuta ka? it’s Mana?
I’m so glad finally I can tell you about Xmas party and greet and meet event.
Christmas is very important for me and I would like to celebrate Christmas with you all.
Here is information as down below
★Meet and greet
1. 30 minutes MEETING (you can share everything with me)
2. A PHOTO (with my sign and handwriting letter)
*This photo will be sent to you by Email
Event Information
【Date】
12/23 Wed.
【Time】 30 minutes per session
-Session 1 18:30 (GMT+8)
-Session 2 19:00 (GMT+8)
-Session 3 19:30 (GMT+8)
【Registration Fee】
250 peso/ per time
【Participants】
24 people only (random draw)
【Registration deadline】
12/16 Wed. 23:59
If you are interested in the event, please fill out this form
https://forms.gle/C7ZeiPYHzs3tGNU78
★Xmas party (YouTube Live stream, cost free)
【Date】
12/23 Wed.
【Time】
From 17:00 to 18:00 (GMT+8)
【contents】
Making Filipino food and dancing with Xmas song
I really want to meet you now but I am not in the Philippines.
In this meet and greet event, I can see your face and each time only 8 person so I can talk to you so much!!!
And I’m so honored to celebrate Xmas with my important person,
I’m super excited see you on DEC 23rd.
Thank you so much for supporting as always:)
Mahal ko kayo!
Mana
Please Follow Our SNS
MANA:https://www.instagram.com/manahello/
123JAPAN Facebook:https://reurl.cc/3D60mj
Mr.3:https://www.instagram.com/sanyuan_japan/
Collaboration or Business Collaboration Please Contact Us Through Mail
Email:123japan@capsuleinc.cc
Please Check Our Other Channel
三原JAPAN(Mandarin):https://www.youtube.com/channel/UCCBq7s8VOCyek275uvq5lYQ
サンエンTAIWAN(Japanese):https://www.youtube.com/channel/UCQimp8PIBqQSDHXtPg7_g9Q
team sanyuan sub channel:https://www.youtube.com/channel/UC9L2RW00CgoDA9uhjtWRrSA
random draw 在 Random Drawing Generator by AudityDraws in 2021 - Pinterest 的推薦與評價
Feb 27, 2021 - Random Drawing Generator GIF Made by AudityDraws! Use this free idea generator featured in AudityDraws video for new funny ideas or just ... ... <看更多>
random draw 在 Recursive random draw [closed] - Mathematics Stack Exchange 的推薦與評價
Let R(n) be a random draw of integers between 0 and n−1 (inclusive). I repeatedly apply R, starting at 10100. What's the expected number of repeated ... ... <看更多>
random draw 在 Random Drawing Generator Challenge - YouTube 的推薦與評價
... <看更多>