📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有61部Youtube影片,追蹤數超過329的網紅Beixin & Robin,也在其Youtube影片中提到,With travel temporarily halted, we decided to explore the hidden gems within Singapore. Join us as we explore Thomson Nature Park, the 50-hectare buff...
「array at」的推薦目錄:
- 關於array at 在 Taipei Ethereum Meetup Facebook 的最讚貼文
- 關於array at 在 Facebook 的最佳解答
- 關於array at 在 Facebook 的最佳貼文
- 關於array at 在 Beixin & Robin Youtube 的最佳貼文
- 關於array at 在 Zermatt Neo Youtube 的最讚貼文
- 關於array at 在 Zermatt Neo Youtube 的最佳解答
- 關於array at 在 JavaScript Array at 的評價
- 關於array at 在 Use JavaScript's new array.at() - YouTube 的評價
- 關於array at 在 Array.at() method missing from typescript array type 的評價
- 關於array at 在 zloirock/core-js: Standard Library - GitHub 的評價
- 關於array at 在 Array Methods (ECMAScript 2015) Sample 的評價
- 關於array at 在 Accessing array index variable from bash shell script loop? 的評價
array at 在 Facebook 的最佳解答
Have you try ARONA stick noodles that recently launched by QSR Brands? ARONA, the air-dried stick noodles are made from high grade imported wheat and 100% natural & vegetarian ingredients - with a soft, springy and chewy texture similar to the quality of a Japanese Ramen, these noodles are easy to prepare and perfectly complement to a variety of cooking styles – from stir fried conditions to a vast array of savoury soups.
ARONA noodles are available in 3 different flavours - Original, Oat, and Pumpkin. Priced at RM3.80 per packet. Swipe left to view the chewy texture of the noodles. For more information checkout Arona Noodles
#aronanoodles #qsrt #healthynoodles #healthyeating
array at 在 Facebook 的最佳貼文
It’s coffegram time! Amacam boleh jadi pelakon dalam K(at)-drama tak? Ni muka kena tinggal dengan awek ni 🤣
Anyways just wanna share Coffee ni makin suka gaduh dengan kucing jiran, tapi masalah setiap kali fight mesti kalah, malukan tuan dia ishhh 😆
Also a reminder to all pet owners, don’t forget to bring your pets for regular check ups okay? Take Your Pet to The Vet campaign runs from 1 June until 30 November 2021. In conjunction with the campaign, Royal Canin Malaysia has prepared and array of activities and prizes for all ages in this year’s campaign. Sounds fun bukan? Go win something for your pets!
#Pet2Vet #RoyalCaninMalaysia @royalcaninmalaysia
array at 在 Beixin & Robin Youtube 的最佳貼文
With travel temporarily halted, we decided to explore the hidden gems within Singapore. Join us as we explore Thomson Nature Park, the 50-hectare buffer park unique for its rich cultural heritage as the site of a former Hainan Village. Thomson Nature Park is also home to a rich array of biodiversity and is notably a key conservation site for the critically endangered Raffles’ Banded Langur.
There are five trails within this park: Ruins and Figs Trail (1.50km), Streams and Ferns Trail (1.40km), Rambutan Trail (0.40km), Langur Trail (0.35km), and Macaque Trail (0.15km). Although we weren't fortunate enough to spot the elusive Raffles' Banded Langur on our multiple trips here, we did manage to catch wild boars and plenty of cheeky macaques.
May our videos be a form of escapism and source of zen for you, and encourage you to appreciate the little, simple things in life. Even in these turbulent times, there is still much beauty in this world if we keep our hearts and minds open.
Cheers!
---------
x Connect with Us
Instagram - https://instagram.com/beixinandrobin
Beixin's instagram - https://instagram.com/beixin
Robin's instagram - https://instagram.com/robinlamhj
Facebook - http://www.facebook.com/beixinandrobin
x Gear List:
Main Camera - Canon EOS R
Main Lens - Canon RF 28-70mm f2
Tele Lens - Canon RF 70-200mm f2.8
Mic - Rode Videomic Pro NTG
Drone - DJI Mavic 2 Pro
x Music
Artlist - https://artlist.io/Robin-58878
(Get 2 months free)
x Software
Adobe Premiere Pro
Contact us at hello@beixinandrobin.com
array at 在 Zermatt Neo Youtube 的最讚貼文
For this episode, we headed back to No Signboard Seafood, this time at the Esplanade, to demolish a $1000 Seafood Mee Goreng Challenge! As a belated birthday treat, Sean has again offered to pay for my meal at No Signboard Seafood, this time only allowing Mee Goreng. Naturally, we loaded up the Mee Goreng with the most delectable toppings that No Signboard Seafood has to offer to push the cost to over $1000, making it possibly the most expensive plate of Mee Goreng ever!
No Signboard Seafood specialises in Chinese cuisine, with a focus on seafood, especially the world-famous Chilli Crab. This particular outlet at Esplanade is unique in that it has some Western cuisine available like Boston Lobster Scrambled Eggs with Caviar and pasta dishes. We had to go with Mee Goreng, which is a Malay-style fried noodle dish with a flexible bunch of proteins and vegetables added, tossed in a sweet and spicy sauce.
For my challenge, I ordered 10 servings of Mee Goreng, 3 Whole Boston Lobsters, a bunch of Scallops, 2 Wagyu Steaks and a large ball of Caviar. The staff masterfully plated this feast onto my giant plate, forming a beautiful array. The mee goreng was sweeter and less spicy than most mee gorengs you would find in Singapore, loaded with tofu and shrimp. It was delicious enough to eat on its own and had a deep wok hei aroma to it. The lobsters were plump and juicy, with the tail meat coming off the shell with almost no resistance. They were expectedly sweet and tender, being freshly cooked from the live lobster tanks on-site.
The scallops were stir-fried in XO sauce, pairing the naturally sweet scallops with the strong umami flavour of the sauce, while the steaks were cooked to medium-rare perfection and rich with a deep “beefy” flavour. Finally, the caviar was a surprising addition to mee goreng, usually eaten on its own or on toast with its rich salty flavour. However, it went exceedingly well with the mee goreng as each sweet and savoury bite of the mee goreng was met with pops of salty explosions, complementing one another rather spectacularly.
This meal was so good that I could hardly call it a challenge and I thankfully enjoyed my birthday dinner. No Signboard Seafood also offers a host of other Chinese-Singaporean cuisine, including crabs cooked in a multitude of ways and many fish options. Do check out No Signboard Seafood for their excellent take on Mee Goreng and their classic dishes!
Visit No Signboard Seafood at:
8 Raffles Avenue
#01-14 16 Esplanade
Singapore 039802
Connect with us!
Facebook - https://www.facebook.com/zermattneofls
Instagram - http://instagram.com/zermattneo
http://instagram.com/teegongborpi
Use code ZERMATT for 58% off ALL Myprotein products.
For those that are interested in doing ZenyumClear™️ Aligners:
https://bit.ly/zermattneo-yt
Use code ZERMATT100 for special discount!
Hair Sponsor - Toliv Salon
5 Purvis Street, #01-03, Singapore
https://www.facebook.com/tolivboutique
array at 在 Zermatt Neo Youtube 的最佳解答
For this episode, we went down to Machan’s Kitchen at Little India to devour a $100 Indian Curry Platter! Machan’s Kitchen serves traditional South Indian cuisine - ranging from curries to authentic Indian desserts - allowing you to taste the whole of Southern India conveniently in the cosily decorated restaurant.
We lavishly plated over 20 dishes onto giant banana leaves, forming a tantalising albeit messy array on the table. The food generally had the base flavours of South Indian cuisine, spicy, heavily flavoured with Indian spices and rich and hearty. The dishes that really caught my attention would be the Chicken 65, the Mutton Curry and the paratha.
The Chicken 65 was tender and spicy but with a pleasant acidic zing from the lemon juice added that cut through the heat and elevated the strong spice blend used on the chicken. The mutton curry was similarly spiced and spicy, but with an element of a deep meaty flavour from the long cooking process to cook the mutton. The mutton was melt-in-your-mouth tender. The paratha was fluffy, with each bite packed with the rich buttery flavour from the ghee used in the dough. It went exceptionally well with all the side dishes given.
Unfortunately, I was unable to request for less chilli in the dishes, so each dish was actually overwhelming spicy for my relatively weak chilli palate. For those who enjoy spicy food, eating here would be a treat, but it became rather painful for me towards to end. This would be a great place to share food with friends or family, especially if you are looking for authentic South Indian cuisine with a focus on Indian curries. Do give Machan’s Kitchen a try for a fantastic Indian Curry Feast!
Visit Machan’s Kitchen at:
74 Race Course Rd
Singapore 218574
Connect with us!
Facebook - https://www.facebook.com/zermattneofls
Instagram - http://instagram.com/zermattneo
http://instagram.com/teegongborpi
Use code ZERMATT for 58% off ALL Myprotein products.
For those that are interested in doing ZenyumClear™️ Aligners:
https://bit.ly/zermattneo-yt
Use code ZERMATT100 for special discount!
Hair Sponsor - Toliv Salon
5 Purvis Street, #01-03, Singapore
https://www.facebook.com/tolivboutique
array at 在 Use JavaScript's new array.at() - YouTube 的推薦與評價
Use JavaScript's new array. at (). Wes Bos. Wes Bos. 172K subscribers. Subscribe. <__slot-el>. Subscribed. 1.3K. I like this. I dislike this. ... <看更多>
array at 在 JavaScript Array at 的推薦與評價
The at() method with a negative index will return an element from the end of the array. Was this tutorial helpful ? ... <看更多>