📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有106部Youtube影片,追蹤數超過7,810的網紅mimi33333333,也在其Youtube影片中提到,https://www.instagram.com/movie_mimi_eye/ 焼肉を食べに行ってきました。 尾張旭のはな牛。 はなぎゅうと読むらしい。 久しぶりの焼肉です。 やはり脂の少ない赤身の部分がおいしいよね。 そしてセセリ。 鶏肉なんかもいただきました。 だけどチヂミなんかも食べ...
the tail end 在 Benny Price Fitness Facebook 的最讚貼文
This video was filmed on my very first day in the gym with @tompurvisptrts at the tail end of May. He’d never met me before, and we were on the back of an impromptu 1-hr whiteboard lecture 📝 on what most of the world calls “warming up” and at RTS we generally refer to as “exercise preparation”.
-
A lot of our later conversations were much more technical 🤓 but I thought this was a really accessible way to introduce Tom to many of you who won’t be aware of the man who is behind so much of the accurate information you’ve learnt over the years.
-
Tom’s a big proponent of his own RTS motto of “Think science, speak client” and meeting the person you’re talking to “where they’re at” 🗣 You’ll note how casual his terminology was here because he wasn’t sure yet what I was comfortable with. A month later it would probably sound like French to most of the general population 😂
-
Anyway, here he’s putting in quite simple layman’s terms what is happening during a warm-up set ⚡️ He’s just getting his muscles “used to” contracting, because 5 minutes ago they were doing nothing 😴
-
Think of those sets in terms of their purpose. They’re to prepare your joints for the range they’re going to have to explore, your muscles to contract and the force they and their connective tissue are about to produce and translate respectively, and your neural system to make all of this happen with maximal effort 💯 Yes, the process will encourage increased blood flow so don’t worry about that🫀
-
Lastly, they represent your opportunity to focus on what feels good today and what doesn’t 🤔 and what cues you’re going to focus on delivering in your set this session (based on either an intention bias or something in particular you’re working on or experimenting with) without the distraction of managing and controlling heavy load 🥵
-
If you start thinking about your “non-working” sets in this manner, you might just find you stop wasting too much energy “getting warm” 🤒🤦🏻♂️ and instead spend it improving the readiness of your mind 🧠 muscles 💪🏽 and joints 🦾 to execute the sets ahead of you.
-
Bonne courage, mes amis préparés. Go forth and contract.
the tail end 在 Facebook 的最讚貼文
Catching the tail end of summer.
#shotoniphone #proraw
IG: www.instagram.com/tcycheng
the tail end 在 mimi33333333 Youtube 的精選貼文
https://www.instagram.com/movie_mimi_eye/
焼肉を食べに行ってきました。
尾張旭のはな牛。
はなぎゅうと読むらしい。
久しぶりの焼肉です。
やはり脂の少ない赤身の部分がおいしいよね。
そしてセセリ。
鶏肉なんかもいただきました。
だけどチヂミなんかも食べたり。
最後はユッケジャンテールクッパを食べます。
まだご飯もので締めることはできる胃腸なのでした。。
I went to eat Yakiniku.
It's called Hanagyu in Owari-Asahi.
I heard it's called Hanagyu.
This is yakiniku after a long time.
After all the lean part with less fat is delicious.
And seseri.
I also got chicken.
But I also ate Buchimgae.
At the end I eat yukhoe jang tail gukbap.
It was my gastrointestinal tract where I could still eat rice last.
ビデオカメラは SONY HDR-PJ790V を使って撮影しました。
60pでの撮影です。
編集はEDIUS X Proです。
A video camera is SONY HDR-PJ790V.
チャンネル登録
Subscribe
http://www.youtube.com/subscription_center?add_user=mimi33333333
みみの目
http://miminome.blogspot.jp/
#みみの目ムービー
#はな牛
#みみさん
#焼肉
#グルメレポート
#GourmetReport
the tail end 在 The Singapore Mermaid Youtube 的最讚貼文
JOLLY SAILOR BOLD (SYRENA'S VERSION)
It's been a long time coming.
Original segments written by: Syrena, Singapore's First Mermaid
Produced by: Jean-Marc Boulier
Videography: Joshfather Photography
Tails: with thanks to Finfolk Productions. The Syrena's Song tail of the month is available on finfolk.com until the end of May!
---
Lyrics:
Take me down to the sea
Find a you, lose a me
In the deep, in the blue
So alluring.
And your arms swallowed me whole
So I battle for my soul
But your eyes - locked on mine
So assuring
But my heart sings so loudly
Traitorous with love
Yes, my heart sings so loudly
Traitorous with love
For my heart is pierced by Cupid,
I disdain all glittering gold
There is nothing can console me
But my Jolly Sailor Bold
From Tower Hill to Blackwall
I'll wander, weep, and moan
All for my Jolly Sailor
Till he comes sailing home
Ooo...
Come all ye pretty fair maids
Wherever ye may be
Who loves a Jolly Sailor
Who sails the open sea
Take me down
Take me down
Take me down
Aaaah...
Take me down
Take me down
Take me down
Aaaah...
My heart is pierced by Cupid
I disdain all glittering gold
There is nothing can console me
But my Jolly Sailor Bold.
the tail end 在 LightFuryLF Youtube 的最佳解答
Boom combo บ้านๆ Dragon Knight (Skill dragon tail) + Oracle (skill fortune end) + Mirana (Skill arrow) ดราฟ 3 ตัวถึก 1 พระ ทำให้ฝั่งตรงข้ามอ่อนแรงด้วย DMG ที่ไม่เพียงพอ TNC combo teamfight ใหญ่ Faceless Void+Lich+Mars+Snapfire แถมได้ Viper มาแก้ทางศัตรูแบบไม่โดน ban ขัดขวาง
Carry ที่โดนแก้ทางหนัก หรือจะเป็น Kpii พลัง Ping
ทัวร์ ?DOTA Summit Online 13: Southeast Asia พบกันระหว่าง TNC Predator⚔️BOOM Esports (patch 7.27d)
เริ่มดราฟ - 01:02
เริ่มเกม - 10:57
► รายชื่อผู้เล่น
TNC Predator
Carry - Gabbi // Mid - Armel // Offlane - kpii // Sup 4 - Tims // Sup 5 - Whitemon
BOOM Esports
Carry - Dreamocel // Mid - Mikoto // Offlane - Fbz // Sup 4 - Hyde- // Sup 5 - Khezcute
►สมัครเป็นสมาชิกของช่องนี้เพื่อเข้าถึงสิทธิพิเศษต่างๆ
https://www.youtube.com/channel/UCyZuvgP1t2vvZHgb8gsl3zw/join
#Dota2 #พากย์ไทย #LightFuryLF
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
► ติดตามผมได้ที่
LF แฟนเพจ ➡️ https://www.facebook.com/lightfuryth/
Discord ของช่อง ➡️ https://discord.gg/AGyAFAE
► ติดตามการสตรีมของ LightFury ใน Twitch ➡️ https://www.twitch.tv/lightfurylf
Twitter ของ LF ➡️ https://twitter.com/LightFuryLF
กลุ่ม Facebook หมู่บ้านหลังเขางาคุเระของ LF ➡️ https://www.facebook.com/groups/311645886304343/
► สนับสนุน LF
โอนธนาคาร [KBank] ➡️บัญชีธนาคารกสิกรไทย นายนรุตม์ ไผทสิทธิกุล เลขที่บัญชี : 3912748064