📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有1部Youtube影片,追蹤數超過25萬的網紅SKRpresents 陶山音樂,也在其Youtube影片中提到,李杰明 W.M.L Playlist: https://www.youtube.com/playlist?list=PL_BJh1Mu7PPeS2THyw0DMZOYBOhYS2EAx 李杰明 W.M.L IG: https://www.instagram.com/wmlhiphop/ SKRpre...
one call away心得 在 兔子。Rabbit + Design = Life Facebook 的最佳貼文
決賽名單出爐囉🎉喜歡的歡迎幫我按個讚也可以去觀賞其他入圍者的作品唷👋
--
兔子寫了一篇不專業的心得文👇
http://iamrabbit37.blogspot.com/…/07/smile-everyday-video.h…
歡迎大家一起來創造屬於自己的影片☺
🌵夏日點點名/ 決 賽 者/🌺Rabbit Lee
作品理念:
配樂我特別挑選了Mikky Ekko - Smile
-------------------------------------
面對這個世界
不需要太過憂心
因為一切擔憂都是不必要的
會到來的事,終會發生
所以,微笑吧
-------------------------------------
歌詞非常符合這次作品的理念而且歌也好好聽,希望大家都能
:微笑(big smile)面對每一個今天:
/
Mikky Ekko - Smile
Smile, the worst is yet to come
微笑吧,這還不是最糟的
We'll be lucky if we ever see the sun
若我們夠幸運 將能夠抵達那片光明
Got nowhere to go, we could be here for a while
還沒有任何目標,我們還能在這停留一會
But the future is forgiven, so smile
無須擔憂未知的未來,所以微笑吧
We're trying so hard to get it all right
我們如此努力的試圖讓一切都安好
But only feel lonely at the end of the night
但最終我們卻只在夜晚感到孤獨
And I wanna be somewhere away from this place
我想去一個能遠離此處的地方
Yeah, somewhere just a little closer to grace
一個更加靠近美好的地方
I'll smile, the worst is yet to come
我會笑著,因為這還不是最黑暗的時刻
We'll be lucky if we ever see the sun
若我們夠幸運 將能夠抵達那片光明
Got nowhere to go, we could be here for a while
還沒有任何目標,我們還能在這停留一會
But the future is forgiven, so smile
無須擔憂未知的未來,所以微笑吧
Smile
笑吧
Well, call me loser, call me thief
你可以叫我輸家,或是小偷
Tell me I'm special when you spit at me
只要在你傷害我時告訴我 我是特別的
'Cause I don't wanna be lonely, I wanna be loved
因為我不想獨自一人,我想要被愛
And I want you to hold me like I'm the only one
我渴望你擁抱我 就像我是你的唯一
I'll smile, the worst is yet to come
我會笑著,因為這還不是最糟的
We'll be lucky if we ever see the sun
若我們夠幸運 將抵達那片光明
Got nowhere to go, we could be here for a while
還沒有任何目標,我們還能在這停留一會
But the future is forgiven, so smile
無須擔憂未知的未來,所以微笑吧
Smile
笑吧
And time will eventually knock on my door
我終將意識到時間的流逝
And tell me I'm not needed around anymore
向我傾訴你心中的話 當我不在你左右時
But he'll hold me so close at the end of the day
他在一天的結束之時緊緊抱著我
When I'm quiet I can nearly hear him say
當我安靜得幾乎可以聽見他輕聲說
Smile, the worst is yet to come
微笑吧,最糟的事還沒到來
We'll be lucky if we ever see the sun
若我們夠幸運 將能夠抵達那片光明
Got nowhere to turn, and we've got nothing but time
沒有任何轉圜的餘地,我們所剩下的只有時間
But the future is forever, the future is forever
但未來啊 是會一直在前方的,未知的事物會一直存在著
so smile, so smile, so smile
所以微笑吧,微笑吧,微笑吧
Smile, the worst is yet to come
微笑吧,最壞的事情還沒到來呢
We'll be lucky if we ever see the sun
若我們夠幸運 將能夠抵達那片光明
請觀賞我的作品影片👇👇👇
如果妳們喜歡請給我一個讚❤️
https://vimeo.com/278279338
更多細節照請點我👇
https://drive.google.com/open…
one call away心得 在 玳瑚師父 Master Dai Hu Facebook 的最佳解答
《命運的驚奇》
The Unexpected Surprises of Destiny
(English version below)
拖著倦累的身驅,走近設備完善的書房,欲將還沒完成的文章給完成,手機鈴聲響起,心中早已知曉,來電者是誰,所為何事也。為何知之?因知 曉十二地支屬性也。來電者為馬肖人,馬肖人最為長氣,同樣一件事情,她他可以說上千萬遍,祇為了達到其他目的也。哎喲喲!好可怕哦!
既然接聽了,吾就打起十二分精神,好好「迎戰」。其實吾也感謝她的來電,因為在一個多鐘頭的通話中,再印證吾 的玄學心得,實而無誤。對方聽出耳油,笑聲四起,欣喜讚嘆一個人的八字,竟然有這麼多的「看頭」,並一「馬」當先地與吾預約批大人的八 字,小朋友的名字,以及居家風水之堪察事宜,以期「馬」到成功。
當吾運用「四柱算命術」,於她他人及自己時,都一樣地讚嘆,命運的驚奇居然含藏於我們的生辰四柱裡,才八個天干地支,就道盡了我們一生的 榮枯,起、居、飲、食、行、住、坐、臥,如該在什麼時間起床,該住什麼類型的房屋、該喝什麼飲品,該食什麼食物及水果,該走哪一條路,該 駕哪種車,該睡什麼素質的床,等等等。為人批八字,若心存解決眾生的煩惱,實為菩提事業,實為修行。
有些人選擇不批八字,因不想活在擔心害怕中,我們就尊重她他。吾,玳瑚師父,則選擇早知,最起碼吾可以少走冤 枉路,眼前的障礙物,吾可先移開,或跳越過,眼前的坑洞及陷阱,吾也可以避過,更何況,吾也可以虔心誠意地向那位師父乞討,化解障礙的妙 方,這樣又何來活在擔心害怕中呢?請勿庸人自擾。
-------------------
As I dragged my weary body towards my well-furbished study room, planning to finish a half-written article, my mobile phone rang. I already knew in my heart the identity of the caller and her purpose of calling. How did I know? Because I am well-versed in the inherent characteristics of the 12 Chinese Zodiac signs. The caller is of the Horse zodiac sign. People of that zodiac sign are very long-winded, and can repeat the same thing a hundred million times just to achieve their objectives! How scary is that!
Since I answered the call, I raised my spirits to get ready for "battle". Truth is, I was grateful for her call because in the over-an-hour conversation, I once again proved the authenticity of my Chinese Metaphysics. The caller listened attentively and, in the midst of laughter, was full of praises that a person's birth chart could yield so much information. She was as quick as a horse in securing an appointment from me to read the birth charts of her family, the names of her children, as well as a Feng Shui audit of her residence. She hopes for success to follow, as sure-footed as a horse!
When I read birth charts of myself and others, using the Four Pillar Destiny technique, I am just as awe-struck and full of praises that the thrills and surprises of destiny are actually buried deep within our birth chart. A birth chart consists only of 8 characters from the heavenly stems and earthly branches, yet they hold the deepest secrets to the rise and ebb of our fortunes and all aspects of our lifestyles.
For example: what time should we get out of bed? What kind of house should we stay in? What beverage should I consume? What food and fruits should I eat? Which route should I take? What car should I drive? What kind of bed should I rest in? etc, etc.
If one decipher the birth charts with the heart of wanting to solve the sufferings of sentient beings, it is a noble career of Bodhicitta and utmost compassion, and an extension of your spiritual cultivation.
Some people prefer not to have their birth charts analysed so that they will not live in fear and anxiety. We should respect their wishes. On the other hand, Master Dai Hu chooses to gain an early access to a deep insight into my own destiny, so that I can be spared the sufferings of treading on the wrong path in life. With this insight, the obstacles in front of me can be moved away, or easily overcome. I can also leap over the deep manhole or traps in front of me. Furthermore, I can sincerely seek advice from the Master, on how to eradicate these obstacles. With wondrous solutions available to me, why would I live in anxiety and fear of the unknown? Only people of mediocrity worry about such things.
www.masterdaihu.com/the-unexpected-surprises-of-destiny/
******************
《圓智溫宵聚慶佳節 - 以茶會友(九)》
讓玳瑚師父教導妳你,如何無懼無惑過個好猴年!
Yuan Xiao Festival : Tea Session with Master Dai Hu - #9
Let Master Dai Hu teach you how to live a fulfillling 2016, free from fear and anxiety, no matter what uncertainties may come your way this Fire Monkey year.
www.masterdaihu.com/celebrating-the-lantern-festival-in-wisdom-and-warmth/
one call away心得 在 SKRpresents 陶山音樂 Youtube 的最讚貼文
李杰明 W.M.L Playlist: https://www.youtube.com/playlist?list=PL_BJh1Mu7PPeS2THyw0DMZOYBOhYS2EAx
李杰明 W.M.L IG: https://www.instagram.com/wmlhiphop/
SKRpresents IG: https://www.instagram.com/skrpresents/
Listen: https://www.soundscape.net/a/9986
_______________________________________________________
詞:李杰明 W.M.L
曲:李杰明 W.M.L & 陶山 Skot Suyama
編制:陶山 Skot Suyama
導演:Michael Li
I Need You
I need you here
Always near my dear
最愛的人容易變最恨的
The person I love easily becomes the person I hate
最愛的人無理取鬧怎麼變最笨的
How come I always become unreasonable in front of my loved one?
氣憤的 疑問著
Feeling angry and questioning myself
我剩了 最後的耐性 脾氣太硬
I'm too stubborn with very low patience
怎麼冷眼瞪著 卻說自己在整頓呢?悔恨著!
I know I'm fighting her but then I lied, said I'm trying, regret
我想保護 我最愛的人
I want to protect her from myself
可我卻扭曲她的好處 變最爛的人
But I'm always twisting her kindness and making her the enemy
犯罪的人 也曾經想誠心的澄清
Sinner all at once, I want to confess and be good
結果是藉口在騙我自己
But it's all excuses just so I can lie to myself
別面對自己的不安全感控制慾
I don't have to face my insecurity that turns into control
I need you here
Always near my dear
我出生 在這個世界
My mom left right after my birth
我用著哭聲 脆弱的向我生母致謝
I use my fragile crying and say thank you for giving birth to me
她是第一也是唯一必須用依賴的心態
She is the first and the only person I was supposed to rely on
我親愛的心脈 也是一切兵敗的侵害!
But even blood can't make her stay
她走了 順便摧毀我第一次信任
She left, destroying my first trust
成了心中不信任的定論 沒印證的氣憤
I learn to never trust again, but then the anger grows in me
心中的矛盾?逃避?饒了我吧!
So many voices in my head always
所有的聲音都別再議論!別記恨
Please give me a break, don't hate her
快點撕開心房嘗試再信任
Try to trust again
我信任我爸
I trust my Dad
我不想長大 總是找話題想找他講話
I don't want to grow up always finding excuses to be with him
是一種牽掛
Relying on him
但可惜瞞不過隱藏的變化
Because in life, everything changes
我走到九歲 我守候的一切是否像酒後的一夜 都不見了?
When I was 9, our 2 person team broke up again
你親那人是你新的心上人
He fell in love with a girl
稱呼母親的對象請在我心中先換人
And she become the person I have to call Mom
我只是嫉妒她要與我分享你的愛
I'm jealous that she gets to share your love with me
我知道你還是愛我
I know you still love me
我只是難過我用的詞是“還是”
I'm just sad that the word I use is ''still''
I need you here
Always near my dear
恨她?愛她?誠實的感受在哪?
Hate? Love? What are my real feelings?
剩下 掙扎 心中的路也已分岔
Left me struggle, my heart has different voices
別信任 一個病人 我心中有太多疑問
Can't trust myself, I'm sick, I have so many doubt
這信任 已經冰冷 我是我唯一的親人
Trust turned cold, I am the only person I can love
我只要信任誰 那個誰就離開
Whenever I trust someone, that person leaves
我只要記恨誰 那個誰就存在
When ever I hate someone, that person stays
我無法控制 我的控制慾
I can't control my feeling of controlling my lover
很諷刺的 我重視的是 擔心內心安全
But ironically I end up pushing her away to secure myself
我卻貪心得到愛
I'm greedy for love
我無法專心割掉心脈
Cant cut the desire for Mom
貪心所以期待
Still greedy, still hoping
一再被愛影響心態
Never in the right mind
關心都變奇怪
Even my caring is twisted
我其實一直信著愛能讓以前的傷不再是遺憾
I actually still believe that one day love can heal my wound
但或許愛是一種絕症 而我早已罹患!
But maybe love is cancer and I already have it