📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有7部Youtube影片,追蹤數超過2萬的網紅Warakit TH gamer,也在其Youtube影片中提到,เด็คที่อยู่ๆก็เก่งขึ้นมาแบบก้าวกระโดดจากการลดค่าร่ายปรับความสามารถการ์ดแค่ใบเดียว โปโก ฮอปเปอร์ ทำให้ติดเมต้าได้ไม่ยาก Dust 8520 ### Pogo rogue # Cl...
「element deck」的推薦目錄:
- 關於element deck 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於element deck 在 美豬媽與布甸仔の日常 Facebook 的最讚貼文
- 關於element deck 在 謙預 Qianyu.sg Facebook 的最讚貼文
- 關於element deck 在 Warakit TH gamer Youtube 的最佳貼文
- 關於element deck 在 肥貓丸 FatCatBall Youtube 的精選貼文
- 關於element deck 在 肥貓丸 FatCatBall Youtube 的最佳貼文
- 關於element deck 在 ARE ELEMENT SKATEBOARDS GOOD? - YouTube 的評價
- 關於element deck 在 Fire Element Skateboard Deck - Pinterest 的評價
element deck 在 美豬媽與布甸仔の日常 Facebook 的最讚貼文
唔知大家去到其他國家鍾唔鍾意睇觀景塔架呢?
睇城市景色嗰啲呢~10X、東X塔之類
我睇過咁多個觀景塔,都冇一個比得上香港嘅景色!
我真係好鍾意香港!
以前好多人睇香港景色都係喺尖沙咀海濱長廊或者太平山頂睇
而家多個選擇啦!就係喺九龍站上蓋嘅Sky100喇~~~
之前疫情唔係咁勁,我帶咗小朋友去Sky100!
Sky100喺Element 2樓入口已經非常靚!係打卡位一號!
仲有香港歷史嘅小動畫~ 一定要同小朋友睇呀!
搭乘全港最快速嘅電梯,只需60秒直達100樓!
之後上到去就豁然開朗,見到非常壯觀嘅香港景色!
雖然我都成日睇香港天際線相片,但真實睇到都真係嘩嘩聲!
一入嚟就睇到香港嘅Signature景色!
今次嘅角度好唔同大家可以360度,從不同角度欣賞到香港嘅景色!
無論係高樓大廈,定係山景、海景都一覽無遺!
我係大約四點半鐘嚟到Sky100
我建議大家喺影相之前行一個圈先
慢慢欣賞咗周圍嘅美景, 之後再慢慢影相
港島、獅子山、四個corner必影!
除咗影相之外,仲有好多好適合小朋友玩嘅活動!
Sky100成日都轉主題!宜家已經變咗聖誕主題喇~~
影下玩下已經5半點! 呢個時候一定要去到西面 Cafe 100!
叫杯香檳,欣賞住香港日落美景!
當然要繼續打卡,影下剪影相!
然後,叫個晚餐,食飽就可以見到香港五光十色嘅夜景喇🌃
黎緊聖誕可以睇埋燈飾,一定更正呀!
.
.
.
@sky100hk #sky100 #cafe100 #hkfoodie #hkfoodies #Girlsssstory #sky100hk #setdinner #維港 #打卡 #finedinner #romantic #香港 #香港景點 #香港景色 #香港打卡 #本地遊 #天際100 #Cafe100 #ICC #hongkongskyline #hklocaltour #hongkongskylineatnight #打卡景點 #打卡熱點 #美豬打卡Tips #hkig #hkiger #親子活動 #親子好去處 #周末好去處 #週末好去處 @Sky100 - Hong Kong's Observation Deck
element deck 在 謙預 Qianyu.sg Facebook 的最讚貼文
【給自己一個機會紅起來】❤️
Give Yourself A Chance To Shine
如果說宇宙的力量可以分為五種,那那五種力量就是金、木、水、火和土了。
世界萬物萬事都能以這五行去分類,而每個人的八字就是這五行的獨特混合體。這混合體遇到外在人事物的五行力量時,就會產生不同的吉凶禍福局面。
我個人經驗是,就算有著同樣八字的雙胞胎,也不會有一模一樣的命運,因為面相有細微之分,姓名的字義、音韻、筆畫和含義也會有不同的靈動力,影響著雙胞胎的命運。
看命,關鍵就在於找出自己需要的五行是什麼。這很考經驗,不是把八字裡的五行加減乘除一番就能斷出。如果這第一步搞錯,全盤的分析就會錯得一塌糊塗了。
在捉準需要的五行後,再來就是引導客人如何在衣食住行方面去補運,進而趨吉避凶。坦白說,如果客人不照著做,其實來看命也沒什麼意思。
知道未來好或不好,然後就聽天由命嗎?那豈不是只是花錢,來聽我說出你人生電視連續劇的劇透而已嗎?
我見了那麼多客人後,自己有個感想。如果人生一切都很安好,但自己就是開心不起來,這樣活著如行屍走肉,太痛苦了。
在批八字時,我都會告訴客人該用什麼顏色來旺自己。不要小看色彩學,每個顏色都有它們的五行。我也多次以色彩學,幫助一些客人應徵成功,在疫情期間依然能奏效。
但在七彩繽紛的顏色當中,只要我一提到紅色,客人們的反應普遍都很大。
什麼?那麼亮啊?
我的老婆說看不慣我穿這色!
那不是天天過年嗎?
我以前的命理師也是這麼說,
但我就是沒有做。
我的答案:你穿了幾十年這樣的顏色,也不見得你好。想命好一點,何不試一下我說的顏色?
紅能分出深淺、濃淡、明暗和不同的色度。沒有人叫你每天穿得像紅包似的。你每天穿黑,也不見得你像包青天那般的黑白分明。
附上A小姐和B先生「走紅」後的個別見證。
寫這電郵見證的B先生,在九月與我做了個後續諮詢。我遠遠看到他時,就已在想,怎麼身上絲毫沒有改變,而且還一副苦瓜臉?
他說,當初我三月見他時,他剛買了一堆衣服。顏色雖不利他,但他不捨得丟,而且老婆也不支持他穿紅色。公司裡的人也都是穿暗色的。
我說,那些新衣你也穿了幾個月了,可以拿去捐給有需要的人,佈施也不算浪費。老婆固然好意,但命是你自己的,你痛苦只有你自己知。
對待自己的命運,我們應該持有科學家的研究精神。你不去做這個實驗,你永遠不會知道我說的到底對不對。做這個實驗不難,不妨叫你的太太給你一個機會去改變。
換衣服顏色而已,又不是叫你換老婆。😂
與其內心糾結直到頭腦打結,我永遠選擇踏出第一步試一下。Never try, never know.
我一生人經歷了那麼多起起落落,老實告訴大家,命好起來時,心豁然開朗時,那種感覺真的很爽。不信,你試一下。
————————————
If the energies in the Universe can be categorised, they would come in the 5 forms of Metal, Wood, Water, Fire and Earth.
All manifestations, be it things or events, in this world falls under these 5 elements, and each and every one of our Bazi is the combination of these 5 elements. The interaction between these elements in our Bazi and those in our circumstances will result in myriad of happenings, good fortunes or otherwise.
My personal experience tells me that even though 2 person may have exactly the same Bazi, but their destinies may turn out different. Reasons being the discrepancy in their facial features, the meanings, tonality, character strokes and essence of their names.
The crux of reading your own destiny lies in uncovering which of the 5 elements is or are mandatory for you. This calls for plenty of practical experience. It is not simply doing simple math of plus, minus, multiply and divide. If this first step is wrong, the whole deck of cards will surely collapse.
After deciphering the correct element, the next step is do guide the client on how to use it to boost their energies in all aspects, from external clothing and living environment to what they eat so as to attract good tidings and ward off misfortunes. Frankly, if the client is uncooperative, the whole of point of Bazi analysis would have been lost.
To know how your future will unfold but behave like a sitting duck? That’s just like buying a movie ticket to watch a preview of your life drama, isnt it?
I had this thought after seeing so many clients: if one’s life is seemingly smooth sailing but one remains unhappy, such zombie-like existence is very painful.
During a typical Bazi analysis session, I would always recommend the client certain colours favourable to him or her to boost the luck. Never underestimate the power of colours. Each colour has its own elemental nature. I have used this successfully several times to help clients succeed at their job interviews, even during this pandemic period.
However as soon as I mentioned the red colour, reactions from my clients tended to be bigger than usual.
Why?
Such a bright colour?
My wife said she’s not used to me in this colour!
That’s like Chinese New Year everyday!
The previous master I consulted said so too, but I did not heed the advice.
My answer: if you have been wearing your same old colours for the past many years and getting not much favours from Lady Luck, why not give my suggestion a try?
There are many tones and shades of Red. Nobody is asking you to dress like a big red ang bao (red packet) everyday. Dressing black everyday doesn’t make you Justice Bao either.
Attached are testimonies from Miss A and Mr B after they went red.
Mr B. came to me in September for a follow-up consultation. When I saw him from afar, I was thinking why there wasn’t any sign of change in him. He even sported a sour face.
He told me that after seeing me in March, he had just bought some clothes in colours that were unfavourable to him. He couldn’t bear to throw them away, and his wife didn’t really support him wearing red either. He said his colleagues were all wearing darker colours.
I told him that those clothes he bought had already served him for several months and he could donate them away. That would not constitute wastage. His wife may have good intentions but your destiny is your own business and only you know your sufferings.
We ought to treat our destiny with the spirit of a scientist. If you don’t experiment, you will never know if my methods work. This experiment is a simple one, ask your wife to support you this once.
It’s only changing the colour of your clothes, not asking to change your wife. 😂
Rather than getting oneself in knots, I will always choose to take the first step. Never try, never know.
My life’s journey has been that of ups and downs. Frankly speaking, when the winds begin blowing in your sails, and lift your spirit, those emotions are exhilarating. Don’t believe me? Try it out yourself.
element deck 在 Warakit TH gamer Youtube 的最佳貼文
เด็คที่อยู่ๆก็เก่งขึ้นมาแบบก้าวกระโดดจากการลดค่าร่ายปรับความสามารถการ์ดแค่ใบเดียว โปโก ฮอปเปอร์ ทำให้ติดเมต้าได้ไม่ยาก
Dust 8520
### Pogo rogue
# Class: Rogue
# Format: Standard
# Year of the Dragon
#
# 2x (0) Backstab
# 2x (0) Preparation
# 2x (0) Shadowstep
# 2x (1) Daring Escape
# 2x (1) Pogo-Hopper
# 2x (1) Togwaggle's Scheme
# 2x (2) Lab Recruiter
# 1x (2) Novice Engineer
# 1x (2) Sap
# 1x (3) Edwin VanCleef
# 2x (3) EVIL Miscreant
# 2x (3) SI:7 Agent
# 2x (4) Spirit of the Shark
# 2x (4) Witchwood Piper
# 1x (5) Barista Lynchen
# 1x (5) Myra's Unstable Element
# 1x (5) Zilliax
# 2x (6) Vanish
#
AAECAaIHBpwCsgLNA+f6AqCAA72ZAwy0AcQB7QLdCIYJpvAC1/oC4PoCtIYDr5EDj5cDx5sDAA==
#
เด็คเด็ดเจ็ดดาว Pogo rogue [post buff] : Hearthstone ไทย
element deck 在 肥貓丸 FatCatBall Youtube 的精選貼文
Combo:02:25、11:09
BGM:https://www.youtube.com/watch?v=vpFbmah63Pc
※本牌組勝率很低,請斟酌使用
更多貓貓請看:https://youtu.be/7t7rP5zeaDg
麥菈的不穩定元素
Myra's Unstable Element
抽出你牌堆中剩下的牌
Draw the rest of your deck.
隱獵團破壞者
Unseen Saboteur
戰吼:對手隨機施放一個手中的法術(隨機選擇目標)
Battlecry: Your opponent casts a random spell from their hand (targets chosen randomly).
博學行者阿洲
Lorewalker Cho
每當有玩家施放法術,對手便獲得一張相同的法術
Whenever a player casts a spell, put a copy into the other player’s hand.
※本牌組勝率很低,請斟酌使用
附上牌組:
隱獵爆抽
職業:盜賊
規則:標準
巨龍年
2x (0) 準備
2x (0) 背刺
1x (2) 剔骨
1x (2) 博學行者阿洲
1x (2) 血法師薩爾諾斯
2x (2) 見習工程師
1x (3) 軍情七處密探
2x (3) 飛舞刀刃
2x (4) 傳送門看守者
2x (4) 學術間諜
2x (4) 逼迫下海
2x (4) 鯊魚之靈
2x (4) 黑巫森林吹笛手
1x (5) 麥菈的不穩定元素
1x (5) 齊里亞斯
2x (6) 傳送門惡魔將領
2x (6) 加基森拍賣師
2x (6) 隱獵團破壞者
AAECAaIHBu0FiAfdCO8I5/oCoIADDLQBnAKbBaQHhgmm8AKo9wK0hgPOjAPSmAOGmgPjmwMA
#爐石Combo
element deck 在 肥貓丸 FatCatBall Youtube 的最佳貼文
OTK:06:05、11:18
BGM:https://www.youtube.com/watch?v=vpFbmah63Pc
BGM:We Share This、School Bus Shuffle、You Are My Rainbow
※本牌組打不贏快攻,請斟酌使用
更多貓貓請看:https://youtu.be/7t7rP5zeaDg
傀儡師多里安
Dollmaster Dorian
每當你抽出手下,為其召喚一個1/1的分身
Whenever you draw a minion, summon a 1/1 copy of it.
托戈瓦哥的陰謀
Togwaggle's Scheme
選擇一個手下將N張它的分身洗入你的牌堆(每回合升級)
Choose a minion. Shuffle |4(copy, copies) of it into your deck. (Upgrades each turn!)
瑪里苟斯
Malygos
法術傷害 +5
Spell Damage +5
麥菈的不穩定元素
Myra's Unstable Element
抽出你牌堆中剩下的牌
Draw the rest of your deck.
※本牌組打不贏快攻,請斟酌使用
附上牌組:
多里安藍龍
職業:盜賊
規則:標準
巨龍年
2x (0) 準備
2x (0) 背刺
2x (1) 托戈瓦哥的陰謀
1x (1) 邪惡攻擊
2x (1) 鋸牙刃
2x (2) 剔骨
2x (2) 血帆劫掠者
2x (3) 劫掠隊伍
2x (3) 南海船長
2x (3) 夢魘聚合體
2x (3) 飛舞刀刃
1x (4) 嘍囉幫竊賊
2x (4) 黃牛票販子
1x (5) 傀儡師多里安
1x (5) 麥菈的不穩定元素
1x (5) 齊里亞斯
1x (6) 加基森拍賣師
1x (8) 鉤牙船長
1x (9) 瑪里苟斯
AAECAaIHCLQDxgWkB8LxAuf6AqCAA9aMA5CXAwu0AZsFqAWIB+cHhgmq/wLeggOzhQPVjAOvkQMA
#爐石OTK
element deck 在 Fire Element Skateboard Deck - Pinterest 的推薦與評價
Skateboard Collection Element Skateboard Decks, Element Skateboards, Fire Element, Collections, Quick. More like this. ... <看更多>
element deck 在 ARE ELEMENT SKATEBOARDS GOOD? - YouTube 的推薦與評價
Are element skateboards just for Nyjah loving children or do they actually make a good deck. Watch to find out! ... <看更多>