📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
ethereum classic 在 Facebook 的精選貼文
#天下專欄 #Ethereum倫敦的一小步 #人類經濟史上的一大步
台灣時間 8/5 這天,全球最多 Dapp 應用在其上運行的公鏈 Ethereum,完成了名為「倫敦」的作業系統升級,是它往所謂 2.0 架構演化過程中,關鍵的一步。
在區塊鏈業界,這樣的軟體升級稱為「硬分岔」,因為一條公鏈是由千千萬萬互不隸屬的節點 (也就是俗稱的礦工),所共同支持而運行。當新版軟體與舊版無法完全相容,在約定好升級的那一刻 (通常是某個區塊編號) 開始,有升級新版軟體的節點,只能跟其他新版軟體節點協作,而選擇不升級新版軟體、繼續使用舊版軟體的節點,則只能與其他使用舊版軟體的節點協作。
換言之,如果有相當比例的節點不升級,那麼從該區塊開始,這條公鏈就一分為二了,如同科幻電影常見的平行時空一樣,會出現兩組錢包擁有一樣的歷史,但不一樣的未來。
如果選擇不升級的節點夠多,那麼他們所組成的公鏈便還有一定價值,這時相關上下游生態系,包括晶片設計商、Dapp 開發者、錢包軟體、交易所等,會各自選擇單邊或雙邊支持,那麼公鏈生態系也會開始分岔。在以太坊短短 6 年歷史中,就曾經分出過 Ethereum Classic,這條目前市值排行第 18 的平行時空公鏈。
由於這次倫敦升級的幾個重大更新,會大大影響節點的長期權益,因此業界對它既期待又擔憂,所幸結果非常成功,絕大多數以太坊節點都依約換用新版軟體,沒有留下顯著的平行公鏈。
這次倫敦升級有三大更新類別:
第一是交易效率優化,把手續費從自由競價改為固定基礎費率加小費,讓有交易需求用戶,可以更容易預期費率,同時把區塊大小加倍,以應付突然的交易暴衝。
第二是改變貨幣與節點經濟,把大約三分之一的手續費回收並銷毀,讓節點開始減少對手續費的依賴,同時讓以太幣的供給年增量從約 3.3% 降到約 2%,減少通貨膨脹,並增加未來調整供給的彈性。
最後,是把工作證明 (Proof of Work) 的演算法定時炸彈,設定在今年底,換言之,將在該時間點後全面轉用持有量證明 (Proof of Stake),正式揮別年耗能 600 億度、約全球產電量 0.25% 的猜數字遊戲,以更綠色、文明的方式,繼續支持 DeFi、NFT 等 Dapp 生態的發展。
ethereum classic 在 Facebook 的精選貼文
JPMorgan now offers clients access to six crypto funds but only if they ask
JPMorgan กลัวตกรถ! ดอดขายกองทุน crypto ให้กลุ่ม bank private !!
By BRIAN QUARMBY
JPMorgan Chase ได้เปิดการเข้าถึงกองทุน crypto 6 กองอย่างเงียบๆ ในช่วงสามสัปดาห์ที่ผ่านมา เนื่องจากดูเหมือนว่า จะนำเสนอ crypto ให้กับลูกค้าที่หลากหลาย
การเคลื่อนไหวครั้งล่าสุด ลูกค้า bank’s private จะสามารถเข้าถึงกองทุน Bitcoin ใหม่ที่ สร้างขึ้นโดยบริษัท New York Digital Investment Group (NYDIG)
ซึ่งเป็นเจ้าของ Stone Ridge Asset Management และ “Stone Ridge Bitcoin Strategy Fund” เสนอ Bitcoin ผ่าน futures markets
โดยกองทุน NYDIG นั้น จะเพิ่มกองทุน crypto 5 กองทุน ที่ธนาคารเปิดให้เข้าถึง เมื่อเดือนที่แล้วGrayscale Investments ' Grayscale Bitcoin Trust , Bitcoin Cash Trust, Ethereum Trust Ethereum Classic Trust รวมถึง Osprey Bitcoin Trust
ในขณะที่สถาบันการเงินแบบดั้งเดิม ได้เปลี่ยนผ่านครั้งใหญ่ที่นำเสนอ crypto ผ่านกองทุนที่แตกต่างกัน 6 กองทุน มีรายงานว่า ใช้วิธีการอย่างระมัดระวังในการให้บริการสินทรัพย์ดิจิทัลใหม่
ตามแหล่งข่าว Business Insider ที่ปรึกษาของ JPMorgan ไม่ได้รับอนุญาตให้ส่งเสริมกองทุน crypto อย่างเปิดเผย และสามารถทำธุรกรรมได้เฉพาะตามคำขอของลูกค้าเท่านั้น
กองทุน Grayscale และ Osprey Funds เปิดให้ผู้ใช้ทุกคนในแพลตฟอร์ม การจัดการความมั่งคั่งต่างๆ รวมถึงแอปซื้อขาย Chase ที่สามารถใช้งานเองได้ในขณะที่กองทุน NYDIG เปิดให้เฉพาะลูกค้าธนาคารเอกชนเท่านั้น
investment banking ยักษ์ใหญ่ มีประวัติที่ซับซ้อนเกี่ยวกับ cryptocurrency หลังจากที่ CEO Jamie Dimon อธิบายว่า Bitcoin เป็นการฉ้อโกง ในปี 2017
โดยนักวิเคราะห์ที่ Goldman Sachs ดูเหมือนจะทำงานผ่านปัญหาเดียวกัน แม้ว่าบริษัทจะทำงานอย่างตั้งใจ เพื่อนำเสนอภาคส่วนนี้ก็ตาม
ในเดือนมิถุนายน Jeff Currie หัวหน้าฝ่ายวิจัยสินค้าโภคภัณฑ์ระดับโลกที่ Goldman Sachs อธิบายว่า Bitcoin เป็นสินทรัพย์ที่ “เสี่ยง” คล้ายกับทองแดง ในเดือนเดียวกัน นักวิเคราะห์จากธนาคารได้เผยแพร่รายงาน crypto ซึ่งสรุปว่า Bitcoin ไม่ใช่ “ที่เก็บมูลค่าระยะยาวหรือสินทรัพย์ประเภทที่ลงทุนได้”
Goldman Sachs ให้บริการ crypto ผ่าน derivatives trading deskและ แพลตฟอร์มการซื้อขาย Bitcoin Futures ที่เปิดตัวเมื่อเดือนที่แล้ว บริษัท ยังได้ยื่นขอETF ที่ใช้ DeFi ในปลายเดือนกรกฎาคม
สำหรับนักลงทุนที่ สนใจ ข้อมูลการลงทุนเชิงลึก
จากบทวิเคราะห์ระดับโลก รวมหลักแสนต่อปี
สามารถ สมัครเข้าดูได้ที่ห้องเรียนวงในครับ
สนใจ คอมเม้นใต้บทความได้เลย
--------------------------------
แอดปลา
ethereum classic 在 Ethereum Classic Aims To Takeover Ethereum Hashrate Post ... 的價格和怎麼買
Sourceshttp://ethereumclassic.org/blog/2022-03-18-etc-welcomes-ethash-miners/http://ethereumclassic.org/mining/hardwareAffiliate ... ... <看更多>