📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有233部Youtube影片,追蹤數超過52萬的網紅巴哈姆特電玩瘋,也在其Youtube影片中提到,00:00 本週手機遊戲新聞 01:05《地城謎蹤》 iOS 下載: https://apps.apple.com/tw/app/id1571643809 01:56《Fatal Funnel》 iOS 下載: https://apps.apple.com/tw/app/fatal-funnel/...
「index card」的推薦目錄:
- 關於index card 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於index card 在 小斯 Facebook 的最讚貼文
- 關於index card 在 小斯 Facebook 的精選貼文
- 關於index card 在 巴哈姆特電玩瘋 Youtube 的最佳貼文
- 關於index card 在 サンダー カード&ホビーチャンネル Youtube 的最佳貼文
- 關於index card 在 サンダー カード&ホビーチャンネル Youtube 的最佳解答
- 關於index card 在 My Index Card Productivity Strategy - YouTube 的評價
- 關於index card 在 590 Fun with Index Cards ideas - Pinterest 的評價
index card 在 小斯 Facebook 的最讚貼文
【經小斯申請送HK$800超市禮券】美國運通白金信用卡!9月30日或之前經小斯成功申請有額外高達HK$800超市禮券 + 迎新高達HK$2,960,合共HK$3,760!每月去Shell入油仲可以賺高達HK$260簽賬回贈!
👉申請網址:flyformiles.hk/platinum-credit-card
(記得要用呢條link喺9月30日或之前成功申請先有小斯額外HK$800超市禮券)
✅優惠詳情及*現金券領取方法:flyformiles.hk/11606
咁抵既優惠當然要繼續啦!只要喺9月30日或之前經小斯申請美國運通白金信用卡,一經批核就有HK$800超市現金券,迎新高達HK$2,960,合共HK$3,760!!然後每月去Shell入油仲可以賺高達HK$260簽賬回贈呀!
計埋迎新即係:
1️⃣經小斯成功申請有HK$800超市現金券
2️⃣4張戲飛 (價值HK$460)
3️⃣首3個月簽HK$5,000再有HK$500 city’super現金券
4️⃣頭HK$12萬簽賬HK$1=5美國運通積分,計返係換到$2,000禮券
➡️加埋以上所有野,即係總共有HK$3,760啊!
2021年仲有全新優惠添!
- Shell油站入油每個合資格賬戶每月高達HK$260簽賬回贈
- 本地指定商戶包括超市、油站、便利店簽賬HK$1=高達6分
- Mira Dining指定餐廳及聘珍樓等粵菜食府外賣自取低至7折!
真係個個月用黎入油的話,淨係個入油都差唔多夠回本了!而且宜家仲有全年city’super、LOG-ON 及 cookedDeli 97折優惠添呀!
*留意返今次HK$800現金券優惠係需要於9月1日至9月30日期間經小斯申請:flyformiles.hk/platinum-credit-card,並於9月30日或之前成功批核,然後喺批核後7日內填返小斯份google form 登記先拎到架:flyformiles.hk/11606
index card 在 小斯 Facebook 的精選貼文
【經小斯申請送HK$800超市禮券】美國運通白金信用卡!8月31日或之前經小斯成功申請有額外高達HK$800超市禮券 + 迎新高達HK$2,960,合共HK$3,760!每月去Shell入油仲可以賺高達HK$260簽賬回贈!
👉申請網址:flyformiles.hk/platinum-credit-card
(記得要用呢條link喺8月31日或之前成功申請先有小斯額外HK$800超市禮券)
✅優惠詳情及*現金券領取方法:flyformiles.hk/11606
咁抵既優惠當然要繼續啦!只要喺8月31日或之前經小斯申請美國運通白金信用卡,一經批核就有HK$800超市現金券,迎新高達HK$2,960,合共HK$3,760!!然後每月去Shell入油仲可以賺高達HK$260簽賬回贈呀!
計埋迎新即係:
1️⃣經小斯成功申請有HK$800超市現金券
2️⃣4張戲飛 (價值HK$460)
3️⃣首3個月簽HK$5,000再有HK$500 city’super現金券
4️⃣頭HK$12萬簽賬HK$1=5美國運通積分,計返係換到$2,000禮券
➡️加埋以上所有野,即係總共有HK$3,760啊!
2021年仲有全新優惠添!
- Shell油站入油每個合資格賬戶每月高達HK$260簽賬回贈
- 本地指定商戶包括超市、油站、便利店簽賬HK$1=高達6分
- Mira Dining指定餐廳及聘珍樓等粵菜食府外賣自取低至7折!
真係個個月用黎入油的話,淨係個入油都差唔多夠回本了!而且宜家仲有全年city’super、LOG-ON 及 cookedDeli 97折優惠添呀!
*留意返今次HK$800現金券優惠係需要於8月1日至8月31日期間經小斯申請:flyformiles.hk/platinum-credit-card,並於9月30日或之前成功批核,然後喺批核後7日內填返小斯份google form 登記先拎到架:flyformiles.hk/11606
index card 在 巴哈姆特電玩瘋 Youtube 的最佳貼文
00:00 本週手機遊戲新聞
01:05《地城謎蹤》
iOS 下載:
https://apps.apple.com/tw/app/id1571643809
01:56《Fatal Funnel》
iOS 下載:
https://apps.apple.com/tw/app/fatal-funnel/id1519955820
Android 下載:
https://play.google.com/store/apps/details?id=com.minidragon.fatalfunnel
02:44《Detonation Racing》
iOS 下載:
https://apps.apple.com/tw/app/detonation-racing/id1477484840
03:30《熱血羽球》
Android下載:
https://play.google.com/store/apps/details?id=com.duoyitw.m3a5&hl=zh-TW
04:15《銀河騎士傳 掌位之絆》
※ 目前僅於日本地區推出
iOS 下載:
https://apps.apple.com/jp/app/id1568410821
04:58《PUI PUI 天竺鼠車車 嚼嚼停車場》
※ 目前僅於日本地區推出。
iOS 下載:
https://apps.apple.com/jp/app/id1578450328
Android 下載:
https://play.google.com/store/apps/details?id=jp.gl3inc.mogumoguparking
05:51《Archangel : Rise of Immortal》
※ 目前僅於韓國地區推出
iOS 下載:
https://apps.apple.com/kr/app/id1563879638
Android 下載:
https://play.google.com/store/apps/details?id=com.glitch.g2hus.global
06:36《暗區突圍》
※封閉測試中
官方網站:
https://aqtw.qq.com/
07:25《真・三國無雙 霸》
※ 目前僅於中國地區推出
iOS 下載:
https://apps.apple.com/cn/app/id1526804267
Android (官網下載 APK):
https://zs.qq.com/act/6036/a20210625xzz/indexpc.html
08:04《光明與黑暗~光與闇的英雄~》
※ 預計 2022 年推出
08:50《天堂W》
官方網站:
https://lineagew.plaync.com/index
10:24《因格瑪の預言》
iOS 下載:
https://apps.apple.com/tw/app/id1578817634
Android 下載:
https://play.google.com/store/apps/details?id=com.enigmite.tw
11:35 這些人氣動畫也要推出手機遊戲
20:19 下週即將上架的手機遊戲
《HeroVersus》
iOS:https://apps.apple.com/tw/app/id1440082595
《因格瑪の預言: 幻想與消除》
iOS:https://apps.apple.com/tw/app/id1578817634
Android:https://play.google.com/store/apps/details?id=com.enigmite.tw
《Jumbo Jet Flight Simulator》
iOS:https://apps.apple.com/tw/app/id1578668005
Android:https://play.google.com/store/apps/details?id=com.teapotgames.jumbojetflightsimulator
《魔界戰記DISGAEA RPG》
iOS:https://apps.apple.com/tw/app/id1563332677
Android:https://play.google.com/store/apps/details?id=com.boltrend.disgaea.tc
《布達佩斯謀殺案!》(Murders on Budapest!)
iOS:https://apps.apple.com/tw/app/id1573679275
Android:https://play.google.com/store/apps/details?id=com.buffstudio.murder.budapest.free.visual.novel.adventure.story.games
《MARVEL未來革命》
iOS:https://apps.apple.com/tw/app/id1453366542
Android:https://play.google.com/store/apps/details?id=com.netmarble.marvelfr
《瀕臨絕種團劇場》
iOS:https://apps.apple.com/tw/app/id1556790157
Android:https://play.google.com/store/apps/details?id=com.storia.rescutetheater
《傾聽畫語》
iOS:https://apps.apple.com/tw/app/id1552126919
Android:https://play.google.com/store/apps/details?id=com.aktsk.silvrlin.btf
《Numpurr Card Wars》
iOS:https://apps.apple.com/tw/app/id1535634828
Android:https://play.google.com/store/apps/details?id=com.nukenin.solitainya
《遇見妳的貓》
iOS:https://apps.apple.com/tw/app/id1573719995
Android:https://play.google.com/store/apps/details?id=com.droidhang.tdm.gp
《傳奇4》(MIR4)
iOS:https://apps.apple.com/tw/app/MIR4/id1566500955
Android:https://play.google.com/store/apps/details?id=com.wemade.mir4global
《Puzzle Town》
iOS:https://apps.apple.com/tw/app/id1572958182
Android:https://play.google.com/store/apps/details?id=com.dmg.puzzletown
《Lotus Reverie: First Nexus》
iOS:https://apps.apple.com/tw/app/id1573885288
《Baldo》
iOS:https://apps.apple.com/tw/app/id1513822917
-------------------------------------------------
《靈魂潮汐》
※中國地區
iOS:https://apps.apple.com/cn/app/id1542963691
《少年歌行互動版》
※中國地區
iOS:https://apps.apple.com/cn/app/id1572787818
《時間守護者》(タイムディフェンダーズ)
※日本地區
iOS:https://apps.apple.com/jp/app/id1533632935
Android:https://play.google.com/store/apps/details?id=com.vespainc.timedefenders
《仙境傳說:失落的回憶》(The Lost Memories)
※泰國地區
iOS:https://apps.apple.com/th/app/id1526410633
Android:https://play.google.com/store/apps/details?id=com.ggt.tlm.aos
《Frontier Zero》(フロンティア‧ゼロ~境界を越える想い~)
※日本地區
iOS:https://apps.apple.com/jp/app/id1561931319
Android:https://play.google.com/store/apps/details?id=com.eyougame.frontierzero
《劍靈2》(블레이드&소울2)
※韓國地區
Android:https://play.google.com/store/apps/details?id=com.ncsoft.bns2
《諾弗蘭物語》(鍊金少女M)
※中國地區
iOS:https://apps.apple.com/cn/app/id1538742698
《金鏟鏟之戰》
※中國地區
iOS:https://apps.apple.com/cn/app/id1478101301
《惡魔秘境》
※中國地區
iOS:https://apps.apple.com/cn/app/id1573348505
![post-title](https://i.ytimg.com/vi/OtA0Au9P8Gk/hqdefault.jpg?sqp=-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBWFcj64fIvW_z7RFZx9tC31LyVlg)
index card 在 サンダー カード&ホビーチャンネル Youtube 的最佳貼文
コラボ再生リスト→https://www.youtube.com/watch?v=JaQRIHDtzhQ&list=PLlW8XvWUnCwbqTMj8OO7X0xVcY8ZGeDf7&index=1
アドバンテージ警察介入か? 1対5交換の男・ゆゆうた!
使用させてもらったゆゆうたさんの演奏
https://youtu.be/2kTLDfhzdnY
https://youtu.be/EEdWVekhAAs
鈴木ゆゆうた→https://www.youtube.com/channel/UCNMG8dXjgqxS94dHljP9duQ
Twitter→https://twitter.com/dai2noyuyuta
Instagram→https://www.instagram.com/instanoyuyuta/?hl=ja
LINE公式アカウントができました!!友達登録よろしくお願いします!!
→https://lin.ee/FiT01ut
チームサンダーオリジナルタオルの販売はコチラ!
販売ページ→ https://muuu.com/users/60d3c4eb6b71d0c6
紹介記事→ https://www.uuum.jp/posts/6066?preview=a2b12cfe73d823996d06ac8509892d77
※紹介記事内にあるURLは現在利用できませんので、上記販売ページのリンクをご利用ください。
【個人チャンネル】
ゲームや雑談おにしす https://www.youtube.com/channel/UC0EHK9IW7lHOcZsGeJjoB-Q?view_as=subscriber
ミソのデュエルのミソ https://www.youtube.com/channel/UC4FX68sHwzNb-dQgwewtq-Q
【Twitter】
サンダー https://twitter.com/thunder_L_ysk
オックン https://twitter.com/onisys
ミソ https://twitter.com/miso_bloooood
【Instagram】
オックン https://www.instagram.com/onisys_duel/
ミソ https://www.instagram.com/miso_duel/
![post-title](https://i.ytimg.com/vi/fwUFqCBH7VI/hqdefault.jpg)
index card 在 サンダー カード&ホビーチャンネル Youtube 的最佳解答
コラボ再生リスト→https://www.youtube.com/watch?v=JaQRIHDtzhQ&list=PLlW8XvWUnCwbqTMj8OO7X0xVcY8ZGeDf7&index=1
持ってる男・ゆゆうた!【ブラック・マジシャン】VS【青眼の白龍】の永遠のライバル対決!
使用させてもらったゆゆうたさんの演奏
https://youtu.be/LngmS_8ZoiY
https://youtu.be/EEdWVekhAAs
鈴木ゆゆうた→https://www.youtube.com/channel/UCNMG8dXjgqxS94dHljP9duQ
Twitter→https://twitter.com/dai2noyuyuta
Instagram→https://www.instagram.com/instanoyuyuta/?hl=ja
LINE公式アカウントができました!!友達登録よろしくお願いします!!
→https://lin.ee/FiT01ut
チームサンダーオリジナルタオルの販売はコチラ!
販売ページ→ https://muuu.com/users/60d3c4eb6b71d0c6
紹介記事→ https://www.uuum.jp/posts/6066?preview=a2b12cfe73d823996d06ac8509892d77
※紹介記事内にあるURLは現在利用できませんので、上記販売ページのリンクをご利用ください。
【個人チャンネル】
ゲームや雑談おにしす https://www.youtube.com/channel/UC0EHK9IW7lHOcZsGeJjoB-Q?view_as=subscriber
ミソのデュエルのミソ https://www.youtube.com/channel/UC4FX68sHwzNb-dQgwewtq-Q
【Twitter】
サンダー https://twitter.com/thunder_L_ysk
オックン https://twitter.com/onisys
ミソ https://twitter.com/miso_bloooood
【Instagram】
オックン https://www.instagram.com/onisys_duel/
ミソ https://www.instagram.com/miso_duel/
![post-title](https://i.ytimg.com/vi/J7KDfiIzkus/hqdefault.jpg)
index card 在 590 Fun with Index Cards ideas - Pinterest 的推薦與評價
Looking for a storage solution for your index cards? ---> www.artbysunfire.com. See more ideas about index cards, cards, index card binder. ... <看更多>
index card 在 My Index Card Productivity Strategy - YouTube 的推薦與評價
... <看更多>