📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有264部Youtube影片,追蹤數超過3萬的網紅Brian2Taiwan,也在其Youtube影片中提到,因為出很嚴重的車禍,美國9/17時間走了,我會美國一個月左右,10月中回台灣 隔離旅館都定好了,也照時打第二劑疫苗了。 謝謝大家的支持 想幫助我朋友的家人的人可以到這個gofundme捐錢給Caitlin的家人. https://gofund.me/552e6821 After passing...
「i want to get away」的推薦目錄:
- 關於i want to get away 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於i want to get away 在 Deeecupps V Facebook 的最佳解答
- 關於i want to get away 在 Asther Yinn Facebook 的最讚貼文
- 關於i want to get away 在 Brian2Taiwan Youtube 的精選貼文
- 關於i want to get away 在 starringsarahchang Youtube 的最讚貼文
- 關於i want to get away 在 betty titi Youtube 的最佳貼文
- 關於i want to get away 在 Lenny Kravitz - I want to get away - YouTube 的評價
- 關於i want to get away 在 Fly Away~Lenny Kravitz~Lyrics - YouTube 的評價
- 關於i want to get away 在 "Fly Away" - Lenny Kravitz - YouTube 的評價
- 關於i want to get away 在 I want to get away.. I want to fly... - Off The Grid Rentals 的評價
i want to get away 在 Deeecupps V Facebook 的最佳解答
No one tells you that life changes immediately after you’re married, then it changed her again after you gave birth. There is no manual. There is no manual about having to care for a family, your child, your career, your dream, and everything just seems to be on a no-hand-break mode, you just gotta go and get it done. Sometimes I feel like I’m driving at 180/h with windows down- there is no time to even blink!
It’s a little scary to admit out loud that I am really tired to do it all at once. Is it bad to admit that I love work because I get to spend a few hours away from being a mother to me - who thrive under pressure, who love work and achieving milestones. Who chased dreams for myself. Or is it bad if I want to take a few hours longer to sleep in my bed without having to wake up and greet the entire household?
Negativities are louder in our own mind so it’s important to find small happiness while you’re ticking off your to-do-list by days. They get softer and weaker with contentment. Count your gratitude, manifest your good energy and accept your flaws.
Photo dumping my gratitudes of last week, guess who tops the list?
1. @emmalishar
2. New mugs and glasses
3. @emmalishar
4. Discovered @supergoop Glow Screen Body
5. My fur kids
6. @bhbhealth FMD honey 🍯
7. New lens
8. Dumpling making with mum
9. @shophanya October drop
10. @emmalishar
i want to get away 在 Asther Yinn Facebook 的最讚貼文
Mac and Cheese with Baked Beans 💛
Ayam Brand Baked Beans has always been my favorite when it comes to home cook food. It saves my time and effort in the kitchen!
Most importantly, they are packed with fiber and plant-based protein with NO preservatives, NO added MSG, NO GMOs and NO trans fat! Safe and Halal ✨
#AyamBrand products are available at major supermarkets and hypermarkets, get them now!
Stand a chance to win away Ayam Brand products worth RM50 🎁
Just comment below “I want to have Ayam Brand Baked Beans with….” and tag 2 friends.
Two random winners will be contacted by Ayam Brand Malaysia
*Contest ends on 30 Sept 2021
*Malaysian Citizen only
*T&C Apply
Check them out at:
https://www.ayambrand.com.my/
#ayambrand #ayambrandmy
#giveaway #giveawaymalaysia #giveawaymy
i want to get away 在 Brian2Taiwan Youtube 的精選貼文
因為出很嚴重的車禍,美國9/17時間走了,我會美國一個月左右,10月中回台灣
隔離旅館都定好了,也照時打第二劑疫苗了。
謝謝大家的支持
想幫助我朋友的家人的人可以到這個gofundme捐錢給Caitlin的家人.
https://gofund.me/552e6821
After passing from complications from a severe car accident, my friend passed away on the morning of the 17th.
I’m back in the USA and will go back to Taiwan in the middle of October. Ive already gotten my second dose of the vaccine (first was AZ, second was Moderna), and I’ve got my quarantine hotel for when I get back to Taiwan already booked.
If you want to help Caitlin’s family, you can donate to the gofundme above.
#caitlin
#台灣
#usa
i want to get away 在 starringsarahchang Youtube 的最讚貼文
Our stay at Ying Lun postpartum center
我們在英倫產後護理之家的日子
For my second child, I choose to stay at Ying Lun Postpartum Care. I was blown away by the amazing service and amenities there! For my second child, I thought that I would be pretty set in terms of knowing how to take care of the newborn baby, but I was pleasantly surprised that I really learned a lot during my stay at Ying Lun. While the services are mainly for mother and the newborn, when my eldest arrived to the postpartum center to visit with a scar on her forehead the nurses immediately came to help check and redress her wound. It was so sweet!
我的第二胎,我選擇留在 英倫產後護理之家 。我被他們的服務和設施驚艷到!我認為我會在如何照顧新生兒方面做得很好,因爲這是我的第二胎,但是我很高興我在這裡學到了很多東西。雖然主要為媽媽們和新生兒提供服務,但是當我的大女兒來這探望我時,額頭上有小傷口,護士立即來幫助我們檢查傷口,真是太窩心了!
My favorites things about Ying Lun were:
關於英倫,我最喜歡的東西是:
1. The food! I didn't get sick of the meals there, and they were so delicious I pretty much finished all of them. As a breastfeeding mom, I finished the breakfast, lunch, dinner, and three snacks they provided.
1.食物!那裡的餐點令我百吃不厭,而且非常美味,每一道我都吃得精光。作為一個正在餵母乳的媽媽,我每天都吃完了早餐,午餐,晚餐和三種小點心。
2. The nurses! The nurses at Ying Lun were extremely knowledgeable, accommodating and generally concerned for your well-being. I could tell they really went out of their way to take care of the babies and even talk to them show them love in the nursery.
2.英倫的護士們非常有智慧,也樂於助人,並且關心所有人的健康。他們真的竭盡所能照顧嬰兒,愛護他們。
3. The amenities! I loved the luxurious decor, the comfy slippers they provided, the playroom for my eldest daughter, the photo studio @A Plus 專業攝影 , the spa package, the gym and even the coffee in the lobby. It was delicious!
3.設施非常的豪華!裝飾也很漂亮,我超喜歡他們提供的舒適拖鞋,大女兒常常流連忘返的遊戲室,A Plus 專業攝影 ,Spa,健身房,甚至是大廳提供的咖啡都很美味!
I guess it's hard to list my favorite things considering I liked everything about it! Check out our video below!
我喜歡的東西實在太多了,很只列出其中幾項!可以觀看我們的影片看看到底有多棒!
Thanks so much Yinglun for your kindness and warmth. It was exactly my family and I needed! ❤ ❤
謝謝英倫在這幾周裡的幫助和溫暖,正是我和家人所需的要!
If you want to hear more about our experience you can also tune in my latest Kungfu Momma Show podcast about it!
如果大家想了解更多有關我們的經驗,還可以收聽我們最新的功夫媽媽秀Podcast
Anchor: https://cutt.ly/UbMJPge
i want to get away 在 betty titi Youtube 的最佳貼文
#Talkingtothemoon #BrunoMars #cover
Follow!!
My instagram: @titibettyy
Talking to the moon-Bruno mars
Lyrics:
I know you're somewhere out there
Somewhere far away
I want you back, I want you back
My neighbors think I'm crazy
But they don't understand
You're all I had, you're all I had
At night, when the stars light up my room
I sit by myself
Talking to the moon
Trying to get to you
In hopes you're on the other side talking to me, too
Or am I a fool who sits alone talking to the moon?
Oh-oh
I'm feeling like I'm famous, the talk of the town
They say I've gone mad
Yeah, I've gone mad
But they don't know what I know
'Cause when the sun goes down, someone's talking back
Yeah, they're talking back, oh
At night, when the stars light up my room
I sit by myself
Talking to the moon
Trying to get to you
In hopes you're on the other side talking to me, too
Or am I a fool who sits alone talking to the moon?
Ah-ah, ah-ah, ah-ah
Do you ever hear me calling
(Ah-ah, ah-ah, ah-ah) oh-oh-oh, oh-oh-oh
'Cause every night, I'm talking to the moon
Still trying to get to you
In hopes you're on the other side talking to me, too
Or am I a fool who sits alone talking to the moon?
Oh-oh
I know you're somewhere out there
Somewhere far away
i want to get away 在 Lenny Kravitz - I want to get away - YouTube 的推薦與評價
... <看更多>