📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有63部Youtube影片,追蹤數超過73萬的網紅FPSThailand,也在其Youtube影片中提到,Title: ตามฝัน : OST. PUBG Thailand Open Summer 2021 Artist: Bless me please Lyrics : Chanignun Thipairote Music : Chanignun Thipairote Arrange : Chani...
「i need a goal」的推薦目錄:
i need a goal 在 Benny Price Fitness Facebook 的最讚貼文
This morning, as I started putting on my shoes to go for a run, my grandfather exclaimed "You've got to teach online in 25minutes!" 😱
"Yeah, I'm just going for a whizz round the block." 😎
"What on earth is the point in that??" 🤨 he responded.
-
I would be less fit if I never went for a run unless I had time to warm up, run 5k and stretch. I would be weaker if I only lifted weights when I had 1.5hrs for a full balls-to-the-wall session 🏋🏻♂️
-
Sure, some people can routinely make time for high intensity, high volume exercise.
Some people love making that the centrepiece of their day.
Some are both and content with fitness being a focal point of their life 👍🏽 but, contrary to what the instagram fitness world will lead you to believe, that's really fucking rare.
-
Way too may of us seem to think the goal is to "get fit". I can almost guarantee it shouldn't be for most. Not by whatever arbitrary definition of it you're using. You just need to get fit-ter 📈
-
A friend of mine the other day said "I can't go to the gym without a PT because if I go on my own I just get in a 60% workout and it's a waste of my time." 🤦🏻♂️
HOW have we been convinced that exercise at 60% is a waste of time? I’ll bet most “waste” more time daily scrolling 📱
-
Side note: If you think a PT's “job” is to run your arse into the ground at 100%, I can almost guarantee you've got a mediocre one 💁🏻♂️
-
Equally, it doesn't have to take a lot of your day ⏱ I consulted a couple the other day who have been "meaning to get fit" for a while. They already had a few machines, but seemed to think 15mins on them 3 times a week was pointless. No. What's pointless is having machines you never use.
-
🙏🏽 Please, just start by doing SOMETHING. Something you enjoy, that doesn't feel unpleasantly or unsustainably intense / difficult, and then do it for as long as is convenient. Once it's a habit, start trying to give it a bit more effort, and making a bit more time for it. I know we say to have goals in fitness and business and life but, for some, maybe it's the goal that has been getting in the way? Maybe it's the goal that's so daunting that you don't do ANYTHING to get yourself closer to it? 🧐
i need a goal 在 Facebook 的最佳貼文
Hey guys!! Hope all is well and you have been keeping yourself and your family safe and healthy!!
Staying at home can be stressful, not just for us but for our children too. The sad thing is, we always forget that our kids get restless and stressed as well. That's why we need some movement to keep us sane! Zeydan and I have been doing some random jumping as it helps us feel playful and healthy. It feels really good to sweat out worries in a fun and enjoyable way! You should give it a try!
Come join in the Jump For Happiness Contest by Sime Darby Property in conjunction with the launch of its newest homes at Serenia Aiora. The goal is to collectively reach 250K jumps in total. Join me to be part of the movement and stand a chance to win amazing prizes! Swipe left for more details or head on to @simedarbyproperty to check out the link in bio.
#SereniaCity #SereniaAiora #JumpForHappiness #SimeDarbyProperty
i need a goal 在 FPSThailand Youtube 的最讚貼文
Title: ตามฝัน : OST. PUBG Thailand Open Summer 2021
Artist: Bless me please
Lyrics : Chanignun Thipairote
Music : Chanignun Thipairote
Arrange : Chanignun Thipairote
Vocal : Chanignun Thipairote
Chorus : Chanignun Thipairote
Drum : Attakrit Chattongpoon
Guitar : Pathomphol Khiewpleng
Keyboard : Wittawath Tangjitbumrung
Bass : Phiraphat Makrat
Mix &Mastered : Chawalit Sodla
Spotify : https://spoti.fi/33GAeZ7
iTunes : https://apple.co/2RycAem
[เนื้อเพลง]--------------------------------------------------
หากมีความฝัน ที่ขอให้เป็นจริง อยากจะขอให้ฉันนั้นพร้อมชิง
แย่งชิงความฝัน เมื่อหันรอบๆตัว คู่แข่งน่ากลัว และฉันต้องตั้งใจ
เพราะอะไร จึงต้องไป เพราะมันคือ สิ่งที่ฉันต้องไขว่คว้ามาให้ได้
อย่ายอมแพ้ แค่เพียงลุกขึ้นใหม่ ไม่หวั่นไหว เพราะใจฉันมันแกร่ง...พอ
จงทำตามความฝัน ที่หวังและตั้งใจ ต่อไป ชีวิตเหมือนสนามแข่งขัน ต้องรอด ออกไป
ฉันเข้าใจ ว่ามันไกล แม้ยากเย็น เพื่อได้เห็นปลายทางที่ยิ่งใหญ่
ก้าวมาเถอะผองพี่น้อง ไม่ต้องกลัว อย่าแต่มัว หลบหลัง คิดชิงชัง
ก้าวเดินไป ให้มั่นใจ พร้อมด้วยแรง จะแข่งขันเพื่อวันที่ยิ่งใหญ่
จงทำตามความฝัน ที่หวังและตั้งใจ ต่อไป
ชีวิตเหมือนสนามแข่งขัน ต้องรอด ออกไป
ฉันเข้าใจ ว่ามันไกล แม้ยากเย็น เพื่อได้เห็นปลายทางที่ยิ่งใหญ่
I have a dream that want to come true
I need to prepare for the contention
To achieve the dream,I look around for preparation.
The contenders are formidable so I need to extremely intend for the contention.
Why do I need to do this? Because this is the goal to achieve for me
Don’t give up, just get up and try again.
Nothing can make me dreaded, my heart is strong enough.
Just go and grab that dream
Life is a competition that I need to accomplish
I know that my dream is tough but I'm willing to try.
Come together everybody, do not frightened
Take a confident walk to the competition of our greatest future
Continue working on your dream from now on
Life is a competition that I need to accomplish
I know that my dream is tough but I'm willing to try.
--------------------------------------------------
กดติดตาม Bless me please บนสตรีมมิ่งต่างๆได้ที่
Spotify: https://spoti.fi/3aHWvJM
JOOK: https://bit.ly/2A9ddCm
TikTok: https://vt.tiktok.com/SS3L3J/
FB: https://bit.ly/2TfBX2w
YT: https://bit.ly/2LCzzif
Fungjai: https://bit.ly/2ABIPkg
ติดต่องาน FPSThailand ได้ที่
EMAIL : voo@fpsthailand.com, kirosz@fpsthailand.com
► รายการสดที่ Twitch เท่านั้น กดได้ที่ลิ้ง https://www.twitch.tv/fpsthailand
► เพจ FPSThailand
https://www.facebook.com/fpsthailand
FPSThailand.com , เกาะติดสถานการณ์และวงการเกม FPS และ E-Sports ของเมืองไทยและ Life style ของเหล่าเกมเมอร์มากมาย -- Watch live at https://www.twitch.tv/fpsthailand
i need a goal 在 九粒Jolie 你的英文Bestie Youtube 的精選貼文
#new #love #song
Comment below if you’ve ever met your twin flame 👯🔥
遇過粒欸雙生火焰的趕快留言跟我分享😻
If you’ve never heard of that before, here’s the definition.
A twin flame is an intense soul connection, sometimes called a "mirror soul.”
不知道雙生火焰的粒,九粒來解釋一下:
雙生火焰(又稱為靈魂雙胞胎)是一種很強烈的靈魂連結,有時候也會稱為”靈魂的鏡子”
趕快嘎哇分享粒欸故事吧!Comment below and share your story with me!!
❣️記得按下右邊的收藏小內褲 🩲才可以隨時回來複習吶👏🏼
ps: 肩膀昨晚熊熊花炎所以今天被可愛的中醫阿姨包得嘎肉粽一樣🤣
My shoulder had a sudden inflammation. That’s why this is my fashion choice for the day.
💫今日英文
1️⃣ stay up 熬夜
2️⃣ wonder 想知道
3️⃣ lose track of time 忘了時間
4️⃣ overlap 重疊 / 有共同之處
5️⃣ type 菜 型
6️⃣ coincidence 巧合
7️⃣ connection 連結
8️⃣ shock (使)震驚
9️⃣ fall 暈船 愛上
🔟 birth control 避孕藥
📝 Lyrics
We stayed up all night
Wondering how this happened
Seems like we lost track of time
Cause too much is overlapping
wasn’t love at first sight not my type
But our souls be matching
Nothing is a coincidence
I know I met you for a reason
It was talk, lol, soul connection
Shocked oh no way this is happening
thought I couldn’t fall no more
But you pulled me back up from that hole
You became my new goal
I guess I need to get on birth control
🎶 Amazing Beats By : “Midnight Love”
@thaibeats_official
S/O to my talented boy @weemantalkshit for recording this demo for me within 8 minutes 😂
#饒舌 #饒舌創作 #英文學習
⚠️: 喜歡嘎九粒偶英文的看過來!九粒的生活口說入門課在這邊!
連結點起來,英文一起牛b起來!👉🏻 https://bit.ly/3q4ZW39
關於此頻道的評價:
⭐️ ⭐️⭐️⭐️⭐️
JR Lee “講著一口道地流利英文的搞怪少女,看她的影片會一邊笑死.. 一邊還是笑死”
知名導演Charlie Tsai: “身體放鬆,嘴巴張開,情緒噴張,跟著九粒High起來!”
阿卡貝拉:“開心好玩開腦洞嘎辣妹學英文”
神剪接師Yayun Hsu: “她就是顆健達出奇蛋- 你有什麼願望,她都給你滿足”
還不趕快訂起來😏
👉🏻 https://www.youtube.com/joliechi
i need a goal 在 FPSThailand Youtube 的最佳貼文
Title: ตามฝัน : OST. PUBG Thailand Open Summer 2021
Artist: Bless me please
Lyrics : Chanignun Thipairote
Music : Chanignun Thipairote
Arrange : Chanignun Thipairote
Vocal : Chanignun Thipairote
Chorus : Chanignun Thipairote
Drum : Attakrit Chattongpoon
Guitar : Pathomphol Khiewpleng
Keyboard : Wittawath Tangjitbumrung
Bass : Phiraphat Makrat
Mix &Mastered : Chawalit Sodla
Spotify : https://spoti.fi/33GAeZ7
iTunes : https://apple.co/2RycAem
[เนื้อเพลง]--------------------------------------------------
หากมีความฝัน ที่ขอให้เป็นจริง อยากจะขอให้ฉันนั้นพร้อมชิง
แย่งชิงความฝัน เมื่อหันรอบๆตัว คู่แข่งน่ากลัว และฉันต้องตั้งใจ
เพราะอะไร จึงต้องไป เพราะมันคือ สิ่งที่ฉันต้องไขว่คว้ามาให้ได้
อย่ายอมแพ้ แค่เพียงลุกขึ้นใหม่ ไม่หวั่นไหว เพราะใจฉันมันแกร่ง...พอ
จงทำตามความฝัน ที่หวังและตั้งใจ ต่อไป ชีวิตเหมือนสนามแข่งขัน ต้องรอด ออกไป
ฉันเข้าใจ ว่ามันไกล แม้ยากเย็น เพื่อได้เห็นปลายทางที่ยิ่งใหญ่
ก้าวมาเถอะผองพี่น้อง ไม่ต้องกลัว อย่าแต่มัว หลบหลัง คิดชิงชัง
ก้าวเดินไป ให้มั่นใจ พร้อมด้วยแรง จะแข่งขันเพื่อวันที่ยิ่งใหญ่
จงทำตามความฝัน ที่หวังและตั้งใจ ต่อไป
ชีวิตเหมือนสนามแข่งขัน ต้องรอด ออกไป
ฉันเข้าใจ ว่ามันไกล แม้ยากเย็น เพื่อได้เห็นปลายทางที่ยิ่งใหญ่
I have a dream that want to come true
I need to prepare for the contention
To achieve the dream,I look around for preparation.
The contenders are formidable so I need to extremely intend for the contention.
Why do I need to do this? Because this is the goal to achieve for me
Don’t give up, just get up and try again.
Nothing can make me dreaded, my heart is strong enough.
Just go and grab that dream
Life is a competition that I need to accomplish
I know that my dream is tough but I'm willing to try.
Come together everybody, do not frightened
Take a confident walk to the competition of our greatest future
Continue working on your dream from now on
Life is a competition that I need to accomplish
I know that my dream is tough but I'm willing to try.
--------------------------------------------------
กดติดตาม Bless me please บนสตรีมมิ่งต่างๆได้ที่
Spotify: https://spoti.fi/3aHWvJM
JOOK: https://bit.ly/2A9ddCm
TikTok: https://vt.tiktok.com/SS3L3J/
FB: https://bit.ly/2TfBX2w
YT: https://bit.ly/2LCzzif
Fungjai: https://bit.ly/2ABIPkg
ติดต่องาน FPSThailand ได้ที่
EMAIL : voo@fpsthailand.com, kirosz@fpsthailand.com
► รายการสดที่ Twitch เท่านั้น กดได้ที่ลิ้ง https://www.twitch.tv/fpsthailand
► เพจ FPSThailand
https://www.facebook.com/fpsthailand
FPSThailand.com , เกาะติดสถานการณ์และวงการเกม FPS และ E-Sports ของเมืองไทยและ Life style ของเหล่าเกมเมอร์มากมาย -- Watch live at https://www.twitch.tv/fpsthailand