📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
「length of array c」的推薦目錄:
- 關於length of array c 在 Taipei Ethereum Meetup Facebook 的最佳解答
- 關於length of array c 在 香港人心在日本 Facebook 的最佳貼文
- 關於length of array c 在 Mr Jiro Chan Facebook 的最讚貼文
- 關於length of array c 在 How do I determine the size of my array in C? - Stack Overflow 的評價
- 關於length of array c 在 How to determine or get array length (size) in C/C++ - YouTube 的評價
- 關於length of array c 在 How can I declare an array of variable size (Globally) 的評價
length of array c 在 香港人心在日本 Facebook 的最佳貼文
以前去旅行淨係識食玩瞓,偶然想改變下模式?不如嚟個深度遊啦!無論幾熱門嘅旅遊目的地,只要願意深入探索,都一樣可以擁有全新體驗~依家我哋為您推介幾個深度遊嘅好去處啦!
大阪|梅田中崎町
過百年歷史建築下有各種個性小店,文青嚟到呢度實影相影到唔捨得走!
首爾|京畿道香草島樂園景區
韓國最大規模嘅室內香草植物園,亦係唔少熱爆韓劇同綜藝節目嘅拍攝地,喺度散下步、睇下風景夠晒寫意~
清萊|辛哈農場
唔止有茶園、士多啤梨園、葡萄園、蔬菜溫室,仲係泰國著名啤酒嘅生產地,慢慢行可以玩足一日呀!
唔知您下次又想去邊呢?依家「買去程、送回程」*優惠第一輪正式開賣,航點包括大阪、首爾、清萊、曼谷** 及台中等地,即刻展開難忘嘅旅程啦:https://goo.gl/bFweAz
訂購日期:2019 年 1 月 14 日(上午 9 時)至 1 月 20 日(晚上 11 時 59 分)
旅遊日期:2019 年 1 月 21 日至 12 月 16 日
*優惠數量有限,須受有關細則及條款約束。票價不包括相關稅項及附加費。
**航班時間表及機位銷售須按監管部門批核而定。
“Eat. Sleep. Play.” It might have been your travel motto for long, but don’t you want to try something different this year? Why not give in-depth travel a try? I guarantee you’ll be blown away by the new insights and see the most cliché vacation spots in a new light! Let us now unfold a few destinations that really deserve another chance to visit!
Osaka| Nakazakicho
Stroll along the busy streets where an array of stylish boutiques run the length! Don’t forget to take your camera and take the perfect pictures there with the historic buildings!
Seoul| Pocheon Herb Island
It’s the largest botanical garden in Korea! One of the most popular filming locations, where lots of hot Korean dramas and variety shows were filmed! Have a walk there and enjoy the wonderful scenery there!
Chiang Rai| Singha Park
Tea plantations, strawberry and grape fields, glasshouses for growing vegetables…the list goes on and on! Out of all these, the most unmissable is the Thai beer brewery there! Devote one whole day to explore this gem!
Wondering where to go next? Now, set your sight on the very first round of “Return for FREE” *promotion! Destinations include Osaka, Seoul, Chiang Rai, Bangkok**, Taichung and many more for you to explore! Kick-start your journey now only by clicking: https://goo.gl/V3kyR1
Booking Period: 14 January (9 AM) – 20 January (11:59 PM)
Travel Period: 21 January 2019 – 16 December 2019
*Limited availability, terms and conditions apply. Fares exclude tax and surcharges.
**The flight schedule is subject to regulatory approval.
length of array c 在 Mr Jiro Chan Facebook 的最讚貼文
เป็น R32 ที่สวยมากๆคันนึงเลย แต่งไม่เยอะดี...โดนใจ
**Extended Cut Video**
Available now from Driver Motorsports is this heavily modified Midnight Purple 1991 R32 Nissan Skyline GT-R! This GTR is for sale in the USA, it is 100% legal, customs cleared with a valid Virginia title and all importation documents. This Skyline is set up very nicely and offered at $38,900.
This Midnight Purple GTR is fully loaded with an array of premium aftermarket performance parts! This beast is equipped with a Haltech standalone ECU, R34 N1 turbos, Amuse Titanium exhaust, 13 Point Cusco roll cage, Endless front brakes, Twin Disc clutch and much, much, more.
The mod list speaks for itself on this one. This GTR has the performance equipment to be driven on the track, but its not slacking in the looks department either. The Endless Racing hood and front lip will be sure to break necks on the street, car shows, or on the track! The interior is nicely done with white faced Greddy gauges, MOMO steering wheel, and Bride Zeta driver's seat. From the Work wheels to the eye catching Midnight Purple paint, and from the rumble of the exhaust to the magazine worthy engine bay, this GTR is a true head turner. If you are looking for a drop dead gorgeous R32 GTR Skyline that has great performance and looks then this one is for you.
Mods:
R34 N1 Turbos
HKS Exhaust Manifolds
HKS Turbo Elbows
Rocket Dancer Equal Length Downpipes
Amuse Titanium Exhaust
AM Catch Can
HKS Oil Filter Relocation Kit
AM Oil Cooler
AM Power Steering Cooler
HKS IC Piping
AM FMIC
Custom Suction Kit
Tabata Aluminum Radiator
Samco Radiator Hoses
HKS Twin Power Ignition System
HKS Air Filters
Greddy Fuel Rail
Nismo 555cc Fuel Injectors
Sard Surge Tank
Twin Disc Clutch
Nismo LSD
Haltech Platinum Pro Stand Alone ECU
Blue Dress Up Bolts Engine and Bay Kit
Battery Relocated to Trunk
AM Tension Rod Brace
AM Tension Rods
Am Front Upper Control Arm
LMR Sway Bars (Front and Rear)
Zeal Coilovers with Swift Springs
Nismo Rear Lower Control Arms
Nismo Traction Arms
AM Rear Upper Control Arms
Hicas Delete Bar
ABS Delete
Aluminum Subframe Collars
Endless BBK with 2-Piece Rotors in Front
Cusco 3-Point Rear Strut Tower Bar *Rare*
Front Strut Tower Bar
Work Emotion T7R 18x10.5 Wheels
Federal RS-R 285/30/18 Tires
Endless Racing Front Lip
Tinted Front Turn Signals
Endless Racing Vented Carbon Fiber Hood
Widebody Vented Front Fenders
Custom Widened Rear Fenders
N1 Side Skirts
Bride Zeta 2 Driver’s Seat
MOMO Steering wheel
Nismo Shift Knob
13 Point Cusco Roll Cage
HKS EVC 6 Boost Controller
Mine’s 320kph Gauge Cluster
Greddy Boost Gauge
Greddy Water Temp Gauge
Greddy Oil Pressure Gauge
Greddy Oil Temp Gauge
Greddy Fuel Pressure Gauge
Greddy Ext Gauge
Ready for pick up in Rustburg VA 24588 or we can arrange shipment to your door. This car is ready for the track, car shows or your daily driver. If you are looking to buy a quality JDM car in great condition come see us because we only import the best!
Please private message, text or email us about financing options.
Call or text Chris: 434-942-5424
e-mail: drivermotorsports@gmail.com
Please follow us on Instagram
@drivermotorsports
www.instagram.com/drivermotorsports
www.DriverMotorsports.com
Driver Motorsports is a Virginia independent auto dealer.
#jdm #drivermotorsports #driver #rhd #japan #import #japaneseclassic #japaneselegend #skyline #skylinegtr #r32 #rb26dett #rb26 #greddy #hks #gtr #twinturbo #boost #awd #workwheels #premiumimports
length of array c 在 How can I declare an array of variable size (Globally) 的推薦與評價
According to the documentation, Arrays must be defined as int myArray[10]; where 10 can be substituted for a known length (another integer), or filled with an ... ... <看更多>
length of array c 在 How do I determine the size of my array in C? - Stack Overflow 的推薦與評價
... <看更多>
相關內容