📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有133部Youtube影片,追蹤數超過2萬的網紅黑小多,也在其Youtube影片中提到,《黑小多のTwitch實況台》←“頻道相關Q&A”也在這邊看唷~ http://www.twitch.tv/blackchan 《黑小多の粉絲團》←大家趕緊去幫我按個讚吧!w http://www.facebook.com/heyxiaotto 《能貓姊整理的全成就資料》←感謝能貓姊! http...
「draw a card」的推薦目錄:
- 關於draw a card 在 Taipei Ethereum Meetup Facebook 的最佳貼文
- 關於draw a card 在 Maruco.小畫室 Facebook 的最佳解答
- 關於draw a card 在 Maruco.小畫室 Facebook 的最佳解答
- 關於draw a card 在 黑小多 Youtube 的最佳解答
- 關於draw a card 在 R. Y. Youtube 的精選貼文
- 關於draw a card 在 R. Y. Youtube 的最佳解答
- 關於draw a card 在 36 Drawing cards ideas | cards, cards handmade, birthday cards 的評價
- 關於draw a card 在 Quick Draw Card Co. - Home | Facebook 的評價
- 關於draw a card 在 Draw Card 抽牌機 的評價
- 關於draw a card 在 Top 10 EDH: Best Card Draw Cards - YouTube 的評價
- 關於draw a card 在 How to draw card from deck without repeating in C? - Stack ... 的評價
- 關於draw a card 在 What happens when each player has to draw a card with no ... 的評價
draw a card 在 Maruco.小畫室 Facebook 的最佳解答
好久沒發文,
最近努力的趕製作品,紙膠帶,塔羅牌都在進行中!
這次想來畫點不一樣的,偏淡色一點的可愛插圖和框框,
貼在手帳上隨意組合排版,直接在上面書寫,日記馬上變可愛~
實在太愛小貓熊了!我的作品裡很常出現牠
-
最近、ちょっと変わったものを描いてみたくなりました。
淡い色のイラストとフレームが素敵です。
手帳に貼ると、その上に書くことができます。
レッサーパンダ大好きです。
-
It's been a long time since I posted an article.
Recently, I've been working hard to make my work, washi tape and tarot cards are in progress!
This time I want to draw something different.
The lovely illustrations and frames are a little lighter in color.
Paste them on the handbook and arrange them as you like.
You can write directly on it. Diary immediately become cute ~
I really love the red pandas! It often appears in my work.
-
🛒Shop⬇️
➤官網Official website: https://maruco.tw/pages/shopping-procedure
(まずアカウントを登録します)海外はPAYPAL/クレジットカード決済が可能です。
Please register first. Paypal or credit card payment is supported.
➤Pinkoi JP
https://jp.pinkoi.com/store/maruco03
➤蝦皮
https://shopee.tw/maruco03
-
Twitter/Instagram:@maruco_art
FB/微博:Maruco小畫室
.
.
#Maruco小畫室 #maruco_art #水彩 #插畫 #動物 #手帳 #貼紙 #シール #紙膠帶 #マスキングテープ #文具好き #文具好朋友 #stationery #washitape #maskingtape #イラスト #illustration #watercolor #instaartist #レッサーパンダ #小貓熊 #redpanda #ほぼ日 #hobonichi
draw a card 在 Maruco.小畫室 Facebook 的最佳解答
新品出貨完畢,目前咖啡小貓印章已售完,
新品除了印章也已到自己印台中、板橋寄賣,
香港Funcolor待公告🌟
來看療癒的ipad繪圖縮時紀錄🎨
最近很難得的又拿起ipad畫圖,
可愛的小房間,一直很想畫這個主題,
與 @funcolor_pj 的合作❤
畫完覺得好可愛好期待成品啊☺❤
-
All new orders have been shipped.
The coffee kitten stamp is currently sold out.
See the ipad drawing time lapse record 🎨
Recently it is very rare to pick up the ipad again to draw a picture.
I've been wanting to draw this theme for a long time.
Collaboration with @funcolor_pj ❤
I'm looking forward to the finished product after painting it ☺❤
-
🛒Shop⬇️
➤官網Official website: https://maruco.tw/pages/shopping-procedure
(まずアカウントを登録します)海外はPAYPAL/クレジットカード決済が可能です。
Please register first. Paypal or credit card payment is supported.
➤Pinkoi JP
https://jp.pinkoi.com/store/maruco03
➤蝦皮
https://shopee.tw/maruco03
-
Twitter/Instagram:@maruco_art
FB/微博:Maruco小畫室
.
.
#Maruco小畫室 #maruco_art #水彩 #插畫 #動物 #手帳 #貼紙 #シール #紙膠帶 #マスキングテープ #文具好き #文具好朋友 #stationery #washitape #maskingtape #イラスト #illustration #watercolor #instaartist #房間 # 兔子 #rabbit #roomillustration #部屋 #ウサギ #ipadart #ipaddrawing
draw a card 在 黑小多 Youtube 的最佳解答
《黑小多のTwitch實況台》←“頻道相關Q&A”也在這邊看唷~
http://www.twitch.tv/blackchan
《黑小多の粉絲團》←大家趕緊去幫我按個讚吧!w
http://www.facebook.com/heyxiaotto
《能貓姊整理的全成就資料》←感謝能貓姊!
https://docs.google.com/spreadsheets/d/1j7JMNRIhwuwFZFctuVALVUztkkWgeQjFT_vTaHvW9Ts/edit?usp=sharing
----------------------------------------------------
0:00 需要通關的關卡
0:41 彈鋼琴
-----57個所需成就-----
1:46 1.Jewelry Store 珠寶店 - Diamonds are Forever
1:52 2.任何銀行劫案 - A Good Haul
1:57 3.Shadow Raid 影之襲擊 - I Will Pass Through Walls
2:02 4.GO Bank 全武銀行 - All Eggs in One Basket
2:07 5.Diamond Store 鑽石店 - Hostage Situation
2:25 6.任何運鈔車劫案(不包含火車) - But Wait - There’s More
2:36 7.Train Heist 運鈔車:火車劫案 - We’re Gonna Need a Bigger Boat
2:44 8.Mallcrasher 商場破壞者 - Self Checkout
2:48 9.Four Stores 四間店 - Platinum Card 或 Yeah, He’s a Gold Digger
2:53 10.White Xmas 白色聖誕劫- What’s in the Box?
2:56 11.Ukrainian Job 烏克蘭人的委託 - Let’s do th…
3:01 12.Meltdown 爐心熔解 - They Don’t Pay Us Enough
3:08 13.Aftershock 震後餘生 - Bring It Back Safe
3:17 14.Nightclub 夜總會 - Let Them Boogie
3:30 15.Stealing Xmas 聖誕大盜 - The Grinch
3:39 16.Watchdogs 看門狗 - Out of bounds
4:06 17.Firestarter 縱火者 - Lord of War
4:13 18.Rats 鼠輩 - Full Measure
4:19 19.Big Oil 石油大亨 - Doctor Fantastic
4:28 20.Framing Frame 偷天換日 - I Wasn’t Even There!
4:38 21.Election Day 選舉日 - I’m A Swinger
4:48 22.Big Bank 大銀行 - Don’t bring the Heat
4:59 23.Hotline Miami 熱線邁阿密 - Walk Faster
5:16 24.Hoxton Breakout 老哈逃獄記 - Watch The Power Switch!
5:22 25.Hoxton Revenge 老哈復仇記 - Silent But Deadly
5:30 26.The Diamond 驚世奇鑽 - Cat Burglar
5:37 27.Golden Grin Casino 金牙賭場劫案 - High Roller
5:52 28.Bomb: Dockyard 炸彈劫案:碼頭 - I’ve got the Power
6:06 29.Bomb: Forest 炸彈劫案:森林 - Pump It Up
6:20 30.Scarface Mansion 疤面公館 - Settling a Scar
6:28 31.The Alesso Heist 音樂會劫案 - Sound of Silence
6:39 32.Counterfeit 偽鈔風雲 - Dr. Evil
6:48 33.First World Bank 世界第一銀行 - OVERDRILL
6:54 34.Murky Station 黑水火車站 - The Pacifist
7:06 35.Boiling Point 沸點 - Remember, No Russian
7:22 36.Goat Simulator 山羊模擬器 - Hazzard County
7:49 37.Santa’s Workshop 聖誕工坊 - Santa Slays Slackers
8:11 38.Car Shop 車店 - Gone in 240 seconds
8:19 39.The Biker Heist 暴走族劫案 - Full Throttle
8:37 40.Panic Room 戰慄空間 - Quick Draw
8:48 41.Brooklyn 10-10 荒唐警局 - A Rendezvous With Destiny
9:04 42.The Yacht Heist 遊艇劫案 - Pacifish
9:11 43.Undercover 骯髒交易 - Not Even Once
9:29 44.Slaughterhouse 屠宰場 - Making a Statement
9:47 45.Beneath the Mountain 山脊之下 - Clean House
10:12 46.Birth of Sky 誕於天際 - 1...2...3… JUMP!
10:25 47.Heat Street 熱街 - It’s Nice To Be Nice
10:39 48.Green Bridge 格林大橋 - Attacked Helicopter
10:49 49.Alaskan Deal 阿拉斯加交易- The Fuel Must Flow
10:59 50.Diamond Heist 經典鑽石劫案 - Blood Diamond
11:13 51.Reservoir Dogs 落水狗劫案 - Waste Not, Want Not
11:27 52.Brooklyn Bank 布魯克林銀行- All the Gold in Brooklyn
11:36 53.Breakin’ Feds 闖越FBI - Stalker
12:08 54.Henry’s Rock 亨利峽谷 - Hack This!
12:22 55.Shacklethorne Auction 拍賣會劫案 - Press [F] to pay Respect
12:47 56.Hell’s Island 地獄島 - Beacon of.. nope
12:55 57.No Mercy 慈愛醫院 - Keeping the Cool
13:15 結語
------------------------------------------------------------------------------------------------
如果你喜歡我的Youtube影片,記得在影片左下方的地方按一下「喜歡」的按鈕,
以及「訂閱」我的Youtube,訂閱我的頻道就能與小多簽訂暗黑契約,
亦或是在下面留下你的評論,你的評論也可以讓小多的暗黑力量繼續壯大唷!(ˋ wˊ)/
#黑小多
#劫薪日2
#隱藏結局
draw a card 在 R. Y. Youtube 的精選貼文
Summoning Sophia, Goddess of Rebirth using Double Summon, Vylon Cube, Goblin Zombie, and Shien's Spy in the hand.
I used Ancient Pixie Dragon's effect to draw Sophia, so it's technically a 5-card combo.
Duel Replay:
https://duellinks.konami.net/att/079e19cd92ced9c76da06d7eceb5ca9948a9e9d942
Music: Ys Origin - Genesis Beyond The Beginning
------------------------------------------------
繁體中文版:https://youtu.be/6Y6fG5z6Rg4
日本語版:https://youtu.be/bJt2Yju_9n0
draw a card 在 R. Y. Youtube 的最佳解答
A 4-Card Combo using Gateway to Chaos, Junk Converter, 1 Level 2 Dragon Tuner, and Overdone Burial.
Duel Replay:
https://duellinks.konami.net/att/00c6eeb007bed27f48587f45bd04dbc64e08871547
------------------------------------------------
繁體中文版:https://youtu.be/5v7LUOr1p5g
日本語版:https://youtu.be/SsBM8yvIW18
draw a card 在 Quick Draw Card Co. - Home | Facebook 的推薦與評價
Quick Draw Card Co. 1336 likes · 68 talking about this. Quickdraw Card Co is the 1st UK military specific, fully illustrated deck of playing cards... ... <看更多>
draw a card 在 Draw Card 抽牌機 的推薦與評價
Draw Card. 圖卡名單; 獎項名單; 抽獎結果. 編輯圖卡. ×. 新增圖卡. 獎項名單. ×. 可用數量:0. 新增獎項. 開獎結果. ×. #, 卡片, 名稱, 獎項. 下載 下載兌獎列表 ... ... <看更多>
draw a card 在 36 Drawing cards ideas | cards, cards handmade, birthday cards 的推薦與評價
Jul 9, 2018 - Explore Emma Smith's board "Drawing cards", followed by 227 people on Pinterest. See more ideas about cards, cards handmade, birthday cards. ... <看更多>