📜 [專欄新文章] 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影片,追蹤數超過1,210的網紅Minsley,也在其Youtube影片中提到,这个星期介绍大家 来自韩国Skin 10 Expert and Professional的产品 这产品属于比较平价 我们学生族肯本不用烦恼啊~ 让我最惊讶的是那个美白精华 那橙色一粒粒的 真的很WOW! Skin 10 Expert and Professional的产品 虽然看起来没有说很高级 但...
「in priority order」的推薦目錄:
in priority order 在 立法委員林奕華 Facebook 的最佳解答
我國因莫德納疫苗無法穩定到貨,除原本優先接種疫苗名單第一至第三類人員和孕婦,仍維持間隔28天施打第二劑外,其他都宣佈推遲為間隔10至12週。雖然已是同島不同命,但現在等第二劑的民眾更是焦慮,因為到底能不能在12週內順利完成接種得到足夠的保護力,竟然也得不到政府的保證?在此提醒中央疫情指揮中心,到目前為止找得到有關莫德納疫苗施打的官方指引中,第二劑都是建議4週或6週;就算有疫苗限制,WHO也寫明間隔最長也就是12週!
有關莫德納接種第二劑應該間隔多久,美國CDC和WHO網站寫得很清楚。美國CDC認為間隔為4週,最長為6週,超過6週才接種第二劑的有效性訊息有限。WHO也是建議間隔4週,可延長至6週;若疫苗供應嚴重限制的國家,為提高第一劑的覆蓋率,可考慮將第二劑疫苗延遲至最長12週;建議遵守完整接種時間表,而且兩次接種不建議混打。
附上美國CDC和WHO相關連結和肉容:
➡️美國CDC
♦️英文版:
Moderna COVID-19 vaccine
Get your second shot 4 weeks (or 28 days) after your first.
You should get your second shot as close to the recommended 4-week interval as possible. However, your second dose may be given up to 6 weeks (42 days) after the first dose, if necessary. You should not get the second dose early. There is currently limited information on the effectiveness of receiving your second shot earlier than recommended or later than 6 weeks after the first shot.
♦️中文版:
莫德納COVID-19疫苗
請在接種第一劑後的4週(或28天)接種第二劑。
您應盡可能在建議的4週間隔內接種第二劑。但是,如有必要,第二劑可以在接種第一劑後6週(42天)接種。不應過早接種第二劑。目前,關於在建議的時間之前或第一劑接種6週之後接種第二劑的有效性訊息有限。
♦️網站連結:
https://www.cdc.gov/coronavirus/2019-ncov/vaccines/second-shot.html
➡️WHO
♦️英文版:
SAGE recommends the use of the Moderna mRNA-1273 vaccine at a schedule of two doses (100 µg, 0.5 ml each) 28 days apart. If necessary, the interval between the doses may be extended to 42 days.
Studies have shown a high public health impact where the interval has been longer than that recommended by the EUL. Accordingly, countries facing a high incidence of COVID-19 combined with severe vaccine supply constraints could consider delaying the second dose up to 12 weeks in order to achieve a higher first dose coverage in high priority populations.
Compliance with the full schedule is recommended and the same product should be used for both doses.
♦️中文版:
戰略諮詢專家組推薦的莫德納mRNA-1273疫苗使用方案為,分兩次接種(每次100微克,0.5毫升),間隔28天。如有必要,接種間隔可延長至42天。
研究表明,如果間隔時間比《緊急使用清單》建議的時間長,會對公共衛生產生嚴重影響。因此,面臨COVID-19發生和疫苗供應嚴重限制的國家,可考慮將第二劑疫苗延遲至最多12週,以便在高優先人群中實現更高的第一劑覆蓋率。
建議遵守完整接種時間表,兩次接種應使用相同的產品。
♦️網站連結:
https://www.who.int/news-room/feature-stories/detail/the-moderna-covid-19-mrna-1273-vaccine-what-you-need-to-know?gclid=Cj0KCQjw6ZOIBhDdARIsAMf8YyHAy7nQ2sGbTqgw2vsOWnOTUCKFMFsrQX97xWqq9gHrEy_YAnoBMVsaAprKEALw_wcB
in priority order 在 Facebook 的最佳貼文
Daily advice for HK immigrants to the West:
Regarding parenting, you’ll want to exercise judgment regarding putting your kids in Chinese school on weekends. If they enjoy Chinese school and if the school teaches well, by all means let them enjoy the ride. You also need to balance their needs to socialize in sports while getting educated in Chinese school. A lot of sports events happen on weekends too. When my son was wrestling from junior high to high school (i.e. throughout secondary school), I was his taxi driver all over the state for all competitions not only on most weekends but often on weekdays. Even during off-season, we would look for tournaments to go to in order to get him to the state-ranked level of excellence. None of that includes endless hours in wrestling gyms to train with top coaches. Let me be frank. I’m in favor of being multilingual, but not all Chinese schools are run well. If bad teachers teach them, they’ll learn to hate Chinese school and maybe even their own culture. You’ll have to ask yourself whether that’s worth it. For me, sports (or other after school activities) and grades would take priority to help my kids assimilate. As long as you speak your mother tongue to your kids, that may be good enough. They’re smart. They’ll retain more than you can imagine. One day, they’ll remember all the stuff you taught them at home and explore their culture and language further on their own. Let your kids own their own culture, not YOUR culture.
in priority order 在 Minsley Youtube 的最讚貼文
这个星期介绍大家
来自韩国Skin 10 Expert and Professional的产品
这产品属于比较平价
我们学生族肯本不用烦恼啊~
让我最惊讶的是那个美白精华
那橙色一粒粒的
真的很WOW!
Skin 10 Expert and Professional的产品
虽然看起来没有说很高级
但却藏着神秘又低调的气场
让你用了真的会WOW!
想不到SUNWAY VELOCITY MALL里
藏着那么多好康
平时都没发现呢
你们也带着集美们一起去逛逛吧~
This week I will introduce you
A product brand from Korea – SKIN 10 EXPERT AND PROFESSIONAL
This product is cheaper than others
But it’s final result on my face
Is really WOW!
So don’t miss it!
Sunway Velocity Mall is a big mall
that have variety of product brand inside
It has many goodies there
So if you is a student like
Never ever miss this promotion!
另外SUNWAY VELOCITY MALL一直都很照顾学生福利
所以已推出VELOSTUDENT CLUB
凡是申请的学生
在每一次消费
都能得到折扣噢~
不要错过啦(特别是长期留在KL的你们)
In addition , Sunway Velocity Mall always take good care of stodents
Every student has applied for the VELOSTUDENT CARD
There will be a discount for every purchase
Students should not miss this good health ~
(Especially for those who have been at KL)
https://www.sunwayvelocitymall.com/velostudent-club/
還有還有!
为了满足顾客的特殊需求,
雙威偉樂城购物中心最近推出了“优先顾客”计划
让顾客无需排队就能优先进入商店
享受更好的购物体验
了解更多信息
可以去到他們的网站:
And there's more!
In order to meet the special needs of customers,
Sunway Velocity Mall recently launched a"Priority Customer" initiative to cater to Special Needs customers for a more seamless shopping experience in having priority access to shops without having to queue.
Do visit their website to find out more:
https://www.sunwayvelocitymall.com/priority-customer/
Sunway Velocity Mall真的應有盡有!
我們在那兒見~
Sunway Velocity Mall has just about everything you could wish for
See you there~
我是談健身也談生活的Minsley曾誼雯~~
更多我的消息點擊下面關注我吧!
Facebook :
Minsley
https://www.facebook.com/minsleywenwen/?modal=admin_todo_tour
Instagram :
minsley_430
https://www.instagram.com/minsley_430/?hl=en
in priority order 在 Minsley Youtube 的最佳解答
這是我第一次Vlog
我在影片里是全素颜的
因为美容师叫我不要化妆去
所以我就全素颜跟你们分享啦~
在好多雙眼睛的狀況下完成了這個影片~(其實我很尷尬)
好開心這一次可以參與
SUNWAY VELOCITY MALL
VELOSTUDENT UNBOXING CHALLENGE
雙威偉樂城購物廣場有好康給你們啦~
這一次我很榮幸可以到
SUNWAY VELOCITY MALL SOTHYS PARIS
享受五星級的面部護理
整個過程都超舒服的!!
這次SOTHYS PARIS有超大的優惠
價值RM1020的面部護理
現在只需要RM299就能享受五星級面部護理啦~(真的很很很值得!)
而且還會有免費禮物給你們
這個優惠從即日起到今年年尾都在進行
趕快去SUNWAY VELOCITY MALL SOTHYS PARIS
犒勞一下自己的肌膚吧!
This is my first vlog
I'm all without makeup in the movie
Because the beautician asked me not to make up
So I'm going to share the whole without makeup with you
Finished this video in a lot of eyes ~ (Actually I am embarrassed)
I'm so happy that I can participate
SUNWAY VELOCITY MALL
VELOSTUDENT UNBOXING CHALLENGE
Sunway Velocity Mall has good deal for you ~
It's my honor to be here this time
SUNWAY VELOCITY MALL SOTHYS PARIS
Enjoy a five-star facial treatment
The whole process is super comfortable!!
This time SOTHYS PARIS has a huge discount
The original price of Synergy Facial Treatment is RM1020
Now you can get a five-star facial with just RM299 (it's really worth it!).
And there will be free gifts for you
The offer is available from now until the end of the year
Hurry to SUNWAY VELOCITY MALL SOTHYS PARIS
Treat your skin!
另外SUNWAY VELOCITY MALL 一直很照顧學生
凡是學生申請了VELOSTUDENT CARD 過後
每一次的消費都會有折扣哦~
學生族不要錯過這個好康啦~
In addition, SUNWAY VELOCITY MALL always takes good care of students
Every student has applied for the VELOSTUDENT CARD
There will be a discount for every purchase
Students should not miss this good health ~
https://www.sunwayvelocitymall.com/velostudent-club/
還有還有!
为了满足顾客的特殊需求,
雙威偉樂城购物中心最近推出了“优先顾客”计划
让顾客无需排队就能优先进入商店
享受更好的购物体验
了解更多信息
可以去到他們的网站:
And there's more!
In order to meet the special needs of customers,
Sunway Velocity Mall recently launched a"Priority Customer" initiative to cater to Special Needs customers for a more seamless shopping experience in having priority access to shops without having to queue.
Do visit their website to find out more:
https://www.sunwayvelocitymall.com/priority-customer/
希望過後可以和你們在 SUNWAY VELOCITY MALL
一起購物吧!
I hope I can join you in SUNWAY VELOCITY MALL later
Let's go shopping!
我是談健身也談生活的Minsley曾誼雯~~
更多我的消息點擊下面關注我吧!
Facebook :
Minsley
https://www.facebook.com/minsleywenwen/?modal=admin_todo_tour
Instagram :
minsley_430
https://www.instagram.com/minsley_430/?hl=en
#svm #sunwayvelocitymall #velostudentunboxing
#sothyssunwayvelocity #sothysskincare #sothysbeauty #sothysfacial #giftvouchers
in priority order 在 CarDebuts Youtube 的精選貼文
การผลิตรถยนต์/ซูเปอร์คาร์ Lamborghini ในโรงงานที่ประเทศอิตาลี หลังจากที่มีการแพร่ระบาดของโคโรน่าไวรัส
อิตาลีเป็นประเทศที่ได้รับผลกระทบ จากโรคระบาดโควิด19 อันดับต้นๆของโลก แต่เรื่องของเศรษฐฏิจ ก็ยังต้องเดินหน้าต่อไป เพื่อไม่ให้เกิดผลกระทบในวงกว้างตามมา ในเรื่องปากท้องของประชาชน และความมั่นคงในด้านเศรษฐกิจของประเทศ Automobili Lamborghini จึงตัดสินใจเดินสายการผลิตรถยนต์ต่อ เมื่อวันที่ 4 พฤษภาคมที่้ผ่านมา
โดยบริษัทมีมาตรการในด้านความปลอดภัย ในเรื่องสุขภาพของพนักงาน ซึ่งภาพที่เราจะได้เห็นต่อไปนี้ นอกจากจะแสดงให้เห็นถึงการผลิต ซูเปอร์คาร์อันดับต้นๆของโลกแล้ว Lamborghini ยังเผยให้เห็นถึงมาตรการต่างๆ ในการป้องกันการแพร่ระบาดของเชื้อไวรัสโคโรน่า ในโรงงานอีกด้วย ซึ่งแน่นอนว่า มันคือสิ่งที่เราไม่เคยเห็นมาก่อนในอดีต Lamborghini จะมีกระบวนการผลิตแบบใหม่ เป็นอย่างไร เราลองไปชมกัน
Sant’Agata Bolognese, 30 April 2020 – Automobili Lamborghini is set to resume its production activities from 4 May, in accordance with the Italian Prime Minister’s decree of 26 April. The Sant-Agata Bolognese company is currently completing all preparatory measures to ensure its people return to work in conditions of maximum safety.
While company activities were temporarily suspended, Lamborghini repurposed some of its departments to produce medical protection masks and visors for St. Orsola Hospital in Bologna, and established a partnership with SIARE Engineering International Group for co-engineering and manufacturing breathing simulators.
The preparation for the 4 May reopening has been the subject of ongoing discussion and definition within the Joint Committee of the Company and Trade Unions on the COVID-19 emergency, set up in the wake of the temporary suspension of company activities on March 13. Over the last weeks, the parties have worked together on a precise safety protocol governing the procedures for returning to work and the equipment to be used in order to ensure the maximum safety of Lamborghini’s workforce, starting with a comprehensive educational campaign.
Stefano Domenicali, Chairman and CEO of Automobili Lamborghini, commented: ”We are ready to restart with great energy, but also with strict protocols for safeguarding what is most precious to us: the safety of our people. This priority is why we were the first Italian automotive company to close and continues to be our guiding principle for a well-reasoned and safe recovery, because we still have not won the battle against COVID-19. We will constantly monitor the contagion’s evolution and be ready to adjust our protocols in accordance with the guidelines provided by the Italian government and the Emilia-Romagna Region, which we would like to thank for their support during this sensitive phase.”
“We continue to nurture the dreams of our fans and customers, and on 7 May, through a virtual launch, we will present a new car in order to complete our model range. We closed the first quarter of 2020 despite the situation with very positive results. From these results we want to secure our swift return to a leadership position, delivering our vehicles once again as soon as our dealers around the world reopen for business.”