📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有10000部Youtube影片,追蹤數超過2,910的網紅コバにゃんチャンネル,也在其Youtube影片中提到,...
「as per our conversation」的推薦目錄:
- 關於as per our conversation 在 Taipei Ethereum Meetup Facebook 的精選貼文
- 關於as per our conversation 在 Alexander Wang 王梓沅英文 Facebook 的精選貼文
- 關於as per our conversation 在 Alexander Wang 王梓沅英文 Facebook 的最佳貼文
- 關於as per our conversation 在 コバにゃんチャンネル Youtube 的最佳解答
- 關於as per our conversation 在 大象中醫 Youtube 的最讚貼文
- 關於as per our conversation 在 大象中醫 Youtube 的最佳解答
- 關於as per our conversation 在 Re: [請益] Pre talk還是Per talk - 看板Eng-Class - 批踢踢實業坊 的評價
- 關於as per our conversation 在 Do you send "As Per Our Conversation" emails? - SSW Rules 的評價
- 關於as per our conversation 在 Is 'As per our discussion' and 'As per your instructions' wrong? 的評價
as per our conversation 在 Alexander Wang 王梓沅英文 Facebook 的精選貼文
#Zoom公開課
【一堂 KKBox、Google、Netflix、勤業眾信、HP、McDonald’s 菁英聚在一起的商業寫作課】
一封專業的 business email,要兼顧很多面向。而不只是抄本抄東抄西,一下 as per our conversation、一下 at your earliest convenience 的煩死人。
另外,email 的長度,最好要讓對方在搭捷運時是願意讀的。"Just get to the point. Emails are meant to be short and sweet.” 但最難的是要如何又簡短、清楚、又合宜。
在上一梯的 360 度商業英文寫作當中,作業中我讓他們練習 revise 世界知名電腦大廠的台灣員工的英文 email、也讓他們模擬用英文幫我婉拒出書的邀請。
一封好的 business email (或任何商業寫作) 要至少注意
・商用文法 (which / that 大亂用的、it 指代不清的)、
・用字遣詞是否符合商業和該產業的文化與習慣、
・簡潔性 (英美母語人士現在喜歡用簡短、口語的方式寫 email)、
・語域(依照主題、關係調整語言正式度) 等等。
我在 6/27 (日),將在 Zoom 上帶來一場商英寫作公開課。不要錯過喔!
(適合對象:多益 650 分以上)
一秒報名:
https://www.accupass.com/event/2102170818411345515603
時間:6/27 (日) 2:30 - 3:30 pm
地點:zoom link 將在公開課當天從 email 寄出(未收到者,要記得檢查垃圾信箱喔)
as per our conversation 在 Alexander Wang 王梓沅英文 Facebook 的最佳貼文
【Email 裡常出現 Following up on...用法,連 Google、台積電的資深業務都用錯了20年?】
在email 的聯繫 (correspondence) 上,很多人習慣在會議後、對話後、或是 conference call 後,寫一封 follow-up email。
但有很多要寫商務信的人,沒有想太多地複製 "Following up on the email below,..." "As per our conversation," (有點老套假掰) 複製「部分語塊」下,在過去常常發現即便是在國際大型公司工作甚久的主管,也會在 email 裡犯下這樣的文法錯誤。
• 某 Fortune 500 公司知名公司業務:
"Following up on our discussion over XYZ, the latest pricing information has been attached in this email for your review."
這樣的句子問題就發生在,following up on 在前頭明顯是個因為「前後主詞共用」導致省略主詞的句型,不料到去做 follow up on 的變成了 the latest pricing information了。
➠ 一個簡單的邏輯題:要去 follow up on 的是人還是最新的價目表呢?
基於這樣簡單的思考,我們應該要將句子改成:
"Following up on our discussion over XYZ, I have attached the latest pricing information."
• 相似的錯誤也可以從這句話看到:
To slash costs, layoffs become inevitable.
要去大幅刪減成本的 (to slash costs) 到底是公司還是裁員呢?當然是「公司」才有辦法執行,所以應該是:
To slash costs, American Airlines is preparing for some management-level layoffs.
這樣的句子才對喔!
上面所介紹的觀念,是在英文文法子句簡化中的重要概念。例句中的 pricing information、follow up on + a conversation / discussion、slash costs、management-level layoffs 叫做搭配詞。
與其思考自己寫作不夠好、或是口說不夠好,我常常認為,大多的台灣學生,都應該先從英文文法、搭配詞這兩個核心基礎打起,才真正符合科學「刻意練習」理論的第一個step: set a specific goal.
不然嚷嚷要學寫作,結果一短短的 email 裡有28處文法問題、18處搭配詞問題,也不是辦法。
(進步寫作、進步口說這樣的目標有時太模糊了喔,完全不知怎麼下手,是吧?)
2/24 (一) 我在「新竹」創勝文教分部有一場「改變一生英文文法、搭配詞公開課」。
我將用90分鐘,基於40年第二語言習得研究結果,從兩個英語核心「文法」和「搭配詞」與大家分享要怎麼樣學習,才能讓我們脫離「年年學英文,年年從頭學」、「字字是英文,句句非英文」的兩大現象。
英文口說想講好?英文寫作想寫好?你可能都要從文法、搭配詞開始打底。
✔︎ 公開課一秒報名:
https://www.accupass.com/event/2002161558341961388823
Venue: 新竹市光復路2段295號20樓之2 (帝國經貿大樓,馬偕醫院對面)
Time: 2/24 (一) 7:30 pm - 9:00 pm (7:00 pm 開放入場)
✔︎ 新竹以外欲觀賞公開課的同學,請直接在下方留言,清楚說明要線上試聽改變一生文法、搭配詞公開課,謝謝!(我的文法搭配詞公開課,適合多益500分以上的同學。
as per our conversation 在 コバにゃんチャンネル Youtube 的最佳解答
as per our conversation 在 大象中醫 Youtube 的最讚貼文
as per our conversation 在 大象中醫 Youtube 的最佳解答
as per our conversation 在 Do you send "As Per Our Conversation" emails? - SSW Rules 的推薦與評價
Learn why sending ' as per our conversation ' emails is a good idea in business.You should follow up important conversations (usually by phone ... ... <看更多>
as per our conversation 在 Is 'As per our discussion' and 'As per your instructions' wrong? 的推薦與評價
Either is acceptable usage in American English. As per is a bit redundant, though. It is essentially: As according to our conversation. ... <看更多>
as per our conversation 在 Re: [請益] Pre talk還是Per talk - 看板Eng-Class - 批踢踢實業坊 的推薦與評價
pretalk (n.)
在某種場合或儀式前的對話,類似精神喊話
as previous talk (如前所述)同義詞:
as per our conversation,
as mentioned before,
as discussed/agree,
like the time we spoke,
following our discussion/conversation, etc.
※ 引述《valentino78 (valentino)》之銘言:
: 請問如果要表達"如之前所談"於email的開頭
: 是pre talk還是per talk
: 情境大概是
: 雙方先透過面談或電話討論出共識
: 再將結論以email發出
: 最近有看到公司同仁發的email開頭是用pre talk
: 我還以為只是對方字打太快打錯
: 因為我以為應該是用per來表示:according to (根據)
: 但又想了一下,如果是要表達"如談"
: 使用previous是不是就等於他寫的pre talk
: 請問是否有pre talk這種用法?
: 或是pre talk其實是有其他意思?
: 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.124.110.250
※ 文章網址: https://www.ptt.cc/bbs/Eng-Class/M.1554274961.A.1CF.html
... <看更多>