📜 [專欄新文章] 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影片,追蹤數超過21萬的網紅Xiaofei小飛,也在其Youtube影片中提到,我的哥哥Jonathan在美國北卡羅萊納州擔任消防員,我跟他聊了從事戶外運動和旅行的風險,還有台灣和美國關於登山、溯溪和野外搜救的法規差異。歡迎加入我們的討論,在留言區告訴我們你/妳的想法。因為大家的公開論述,這些議題才有機會變得更好。 內容索引:Table of contents: 0:23 蚯...
mitigate the risk 在 IELTS Fighter - Chiến binh IELTS Facebook 的最讚貼文
- Chữa đề Writing thật tiếp theo thôi cả nhà ơi - Đề số 10 nhé: Some people think it is more important to spend public money on promoting a healthy lifestyle in order to prevent illness than to spend it on the treatment for people who are already ill. To what extent do you agree or disagree?
Bài mẫu:
The state spending on public health has become a widely perpetual concern. Some individuals argue that these already scarce resources should be reserved for the prevention of lifestyle-related illness. In my opinion, the government should focus more on the prevention of illnesses rather than medical treatment.
On the one hand, certain acute diseases, non-preventable regardless of governmental efforts to promote a healthy lifestyle, still need a state budget allocation for medical care. One of the primary duties of the government is to provide publicly funded healthcare to the whole population. Therefore, covering therapeutic and medical costs for those already developing symptoms of acute conditions would be a significant part of that duty, helping mitigate the financial burden associated with those maladies. In other words, a dearth of investment in treatment would be devastating patients’ individual life and wreaking havoc on overall social welfare.
On the other hand, promoting a healthy lifestyle as a prevention strategy is meant to avoid the entire economic burden of chronic diseases, affecting a significant proportion of the population. Those conditions, occurring across different life course stages, share common preventable risk factors relating to unhealthy behaviors, including poor nutrition, inadequate physical activity, and chronic heavy drinking and smoking. If left unchecked, trends in chronic diseases risk factors combined with a growing and aging population will increase the numbers of people living with chronic conditions, later causing the heavy burden of illness in patients, their families, and the community. Therefore, given a scarcity of state budgets for various public services, the government should directly provide information, including health education campaigns, or regulating information, such as limits on advertising and guidelines on food labelings. As a result, positive changes in individual lifestyle would follow, helping them withstand the ravages of time, and saving the state budget for other economically beneficial needs, such as technological investment, education, and infrastructure, rather than spending on treatment.
In conclusion, while allocating its healthcare budget in treatment, the government should promote a healthy lifestyle to avoid preventable chronic diseases due to its economic rationality.
Words: 341
----
Tháng 9 này, cùng IELTS Fighter Find your Fire - thắp đam mê, kệ Covid, học tập nâng cao kiến thức mỗi ngày nha. Các bạn cùng chờ đón cuộc thi Đại sứ IELTS Junior nè, Livestream học tập mỗi tuần và workshop online cho sinh viên siêu thú vị nữa nha. Theo dõi fanpage IELTS Fighter - Chiến binh IELTS để cập nhật thông tin nóng thường xuyên nhé.
mitigate the risk 在 Dr 文科生 Facebook 的最讚貼文
《偽科學打手繼續玩弄科學》
我見某專頁話自己引用左三篇論文去證明佢嘅論點
而我唔知道到底嗰位自稱科學家嘅人有冇認真去睇清楚嗰三篇嘅論文,其中一篇丹麥嘅論文
1. https://doi.org/10.7326/M20-6817?fbclid=IwAR3Sd69faOzsR4tdGJsR67W94qBU7oqrEVb0GG2ElyshBt0sBckRp4PzzKU
眼超超醫生已經詳細地分析他們的實驗設計並不能夠套用在香港的處境上
1. Objective: To assess whether recommending surgical mask use outside the home reduces wearers' risk for SARS-CoV-2 infection in a setting where masks were uncommon and not among recommended public health measures.
2. Limitation: Inconclusive results, missing data, variable adherence, patient-reported findings on home tests, no blinding, and no assessment of whether masks could decrease disease transmission from mask wearers to others.
當呢一篇丹麥論文嘅作者,已經開宗明義咁講咗呢個研究到底有幾多限制嘅時候,同時亦都將研究嘅objective講得好清楚,係調查係一啲佩戴口罩並不常見或者佩戴口罩並非控制疫情的公共衛生措施的地方,少部份人佩戴口罩到底有沒有保護的效果。
當香港已經有口罩令,而絕大部份的市民亦主動經常佩戴口罩保護自己保護他人的時候,這份研究的methodology跟findings根本不能套用在香港。
另外兩篇論文
2. https://academic.oup.com/jid/article/201/4/491/861190
3. https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0029744
其實研究結果係suggest face mask may reduce respiratory illnesses especially in shared or crowded living setting
作者同時指出使用多重的公共衛生保護措施,如戴口罩和勸洗手,有可能可以減少respiratory illness的傳播以及mitigate influenza pandemic
而我唔知到底呢個自稱係生物科學家嘅作者到底點樣分析文獻同論文,得出戴口罩完全無助減少傳染,實為指鹿為馬。
至於該生物學家一次又一次佢話根本沒有研究支持佩戴口罩防疫,我想她大概不懂使用Google或PubMed吧,隨便搜索一下就已經能夠找到好幾份研究支持並建議佩戴口罩作防疫之用。
4. To mask or not to mask: Modeling the potential for face mask use by the general public to curtail the COVID-19 pandemic
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7186508/?fbclid=IwAR20JbSJe1u-GWXYSIHKu9cgsph-3ljKH__buPZz3wWujVtMIWVC1gyEhEQ
5. Mask or no mask for COVID-19: A public health and market study
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7428176/?fbclid=IwAR2q1GD3dfFVh9Y3P0wSOOvu1kG0pMADZGoua2KZo7eU_6bQxIQXvz0YPwQ
6. Efficacy of masks and face coverings in controlling outward aerosol particle emission from expiratory activities
https://www.nature.com/articles/s41598-020-72798-7
7. A rapid systematic review of the efficacy of face masks and respirators against coronaviruses and other respiratory transmissible viruses for the community, healthcare workers and sick patients
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7191274/
8. Reducing transmission of SARS-CoV-2
https://science.sciencemag.org/content/368/6498/1422?utm_campaign=SciMag&utm_source=JHubbard&utm_medium=Facebook
當你追擊得偽科學多,你就會發現好多這種玩弄文字藝術嘅人其實係真心無料,不過佢地有嘅係時間,會不斷talk shit轉話題,再自我感覺良好去話自己係高質argument,玩泥漿摔角玩到你心累唔再同佢地爭論時,佢地就會覺得贏咗。
*留意下佢地永遠錯咗柒咗之後都會扮無嘢
例如Hands only CPR竟然要先擔心aspiration pneumonia、無心跳但又識瘋狂自主呼吸、開口埋口保持氣道暢通但連airway maneuver都唔識、呢兩日加多個胡亂解讀paper
我地要小心提防呢種經常玩弄文字,搬弄似是而非的概念嘅人,頭腦一唔清就會被誤導。
Hopefully this would be the last post about this matter, this is really going nowhere
mitigate the risk 在 Xiaofei小飛 Youtube 的最讚貼文
我的哥哥Jonathan在美國北卡羅萊納州擔任消防員,我跟他聊了從事戶外運動和旅行的風險,還有台灣和美國關於登山、溯溪和野外搜救的法規差異。歡迎加入我們的討論,在留言區告訴我們你/妳的想法。因為大家的公開論述,這些議題才有機會變得更好。
內容索引:Table of contents:
0:23 蚯蚓坑因為危安因素關閉
The Wormhole is Closed for safety reasons
2:36 美國消防員對於關閉危險景點的看法
US Firefighters opinion on closing dangerous places
3:40 戶外的人為安全疏失有哪些?
What is safety negligence outdoors?
5:22 哪些情況下呼叫救難隊應該自費處理?哪些則非?
When should rescues be billed or free?
5:36 降低戶外活動風險的方法
Ways to mitigate risk outdoors
7:30 什麼是「狀況認知」?
What is Situational Awareness?
8:30 安全告示必須改善
Improving safety signage
9:30 從事戶外活動必須知道自己的極限在哪
Know your limits outdoors
11:30 美國消防局在什麼情況下會要求民眾自費?
When the US fire department bills people
12:50 耍蠢和人為疏失的不同
Stupid vs Negligent
13:44 消防隊的救難預算
Fire Department Rescue Budget
14:40 美國和台灣對於風險評估和判斷的差異
Differences between the US and Taiwan risk acceptance
15:50 神秘海灘因為危安因素關閉
Closed: Mystery Beach
16:30 離岸流是什麼?遇到時該如何自救?
Rip Currents and how to save yourself
17:55 如何讓民眾預知景點的可能風險?
Making people aware of known hazards
19:00 有沒有配備救生員的差異是什麼?
Lifeguards vs No lifeguards
20:10 台灣vs美國:意外發生後,到底是誰的錯?
Taiwan vs USA: Blame after accidents
23:30 台灣的保險業對戶外旅遊業的不友善
Taiwan insurance market hurts outdoor tourism in Taiwan
26:00 國家級風景區和台灣消防隊需要承擔的不平待遇
Unfair liability for Taiwanese Fire Departments and National Parks
28:58 我對台灣戶外法規的期許
My hopes for the future of Taiwan outdoor law
29:19 玉門關瀑布群因為危安因素關閉
Closed: Jade Gate Waterfall
31:19 在美國大峽谷,自拍竟然可以真的害死人
Selfies are killing people in the Grand Canyon, USA
33:20 台灣山區入境申請管制的問題有那些?
Problems with Taiwan mountain registration system
34:20 什麼情況下應該封閉一個自然環境?
When should nature be closed?
36:30「禁止游泳」告示知多少?
No swimming signs
41:10 錯誤資訊可能造成的危險
The dangerous of misinformation
42:00 雨中瀑布
Waterfall in the rain
多訊息跟詳細地圖:https://tw.followxiaofei.com
Facebook: https://fb.com/followxiaofei
Instagram: https://instagram.com/followxiaofei
支持我: https://patreon.com/xiaofei
#美國哥哥 #小飛 #消防員