📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有1990部Youtube影片,追蹤數超過9,410的網紅Miley青山舞莉,也在其Youtube影片中提到,SHOP NOW FROM LINK BELOW! NUGU X MADISON KAI https://abit.ly/miley_madisonkai_youtube ► A B O U T Hello! I'm Miley and I'm from Tokyo. I was raised...
by random 在 Facebook 的最讚貼文
Random pic by @honglianghl_ 🐻❄️ 🗯🔇
跟新家书桌的新床架合影下
要来说鬼故事了
那天在弟弟家看完灵媒回家后
iMac自动开机又关机 (距离1m以外)
一直到现在都不敢一个人睡觉
开着佛歌睡觉到电脑sleep
半夜5am 电脑还会自动开继续播未结束的佛歌
同一时间
手机开着的佛歌 也关不到 要关机才可以关掉音乐
实 在 太 诡 异
Help🔕🔇🥺
by random 在 Facebook 的最佳貼文
以下有4種語言 中英法韓 如果你認識適合的外國朋友歡迎tag他們!🇹🇼🇬🇧🇫🇷🇰🇷
⬇️English, Français, 한국어 version below⬇️
PS: 這一張是4年前 留長頭髮 剛來台灣 還沒開始拍YT 第一次用Gopro的我 哈哈
🇹🇼
我這幾年住台灣當YouTuber去過了台灣許多地方,看了許多的風景,認識了許多的台灣人。來這邊4年,我從事youtube的工作,而這很容易讓我覺得在台灣該看的我都看完了。
但我每一次滑IG或抖音我都會看到一些影片讓我覺得:“這個是台灣嗎?”
尤其去年,全世界被封城的時候,只有我們住台灣的新住民可以跟朋友炫耀說我們可以出去玩繼續正常生活。這個點也在網路上變成一個被討論的話題,在各國的外國人,看到這些台灣的風景都覺得很驚訝,也開始好奇台灣是怎麼樣的地方。
也因為這次疫情,世界開始看到台灣。這次外交部舉辦【第7屆全民潮台灣短片徵件競賽】 Trending Taiwan潮台灣 ,讓每一個人都有機會成為台灣美好品牌形象片的導演。不管你是外國人或是台灣人,不管你是用手機或是很貴的相機拍,都有機會來幫忙台灣宣傳!
總共有20個獎項,總獎金93萬,趕快來分享你眼中的台灣吧!!
⬇️趕快來報名⬇️
https://bit.ly/2X1hkf4
🇬🇧
Being a Youtuber in Taiwan, I've had the opportunity to travel around and see a fair share of what the island has got to offer. Living 4 years in the same country usually means there's nothing that can really surprise you anymore.
Yet every once in a while, when I lay down scrolling down my socials, I end up looking at insane pictures or videos with a #taiwan in the description. And every time I'm like "No way this picture has been taken here".
Taiwan has been under the spotlight more than it has ever been last year for its good handling of the virus. You could see articles about Taiwan in every country, and scenes of the unaffected daily life of Taiwanese people were going viral on social media. These videos and pictures spreading on Instagram or Facebook were mostly shot by random people wanting to share their life during the pandemic.
No matter if you're a professional film maker or someone who only owns a smartphone, you can send your videos to the Trending Taiwan Short Film Competition Trending Taiwan潮台灣 and maybe see your project become material for international promotional movies.
Try to get a share of the total prize pool of NTD$930,000 and become an ambassador for Taiwan!
⬇️Submit your project right now⬇️
https://bit.ly/2X1hkf4
🇫🇷
En tant que YouTuber à Taïwan, j'ai eu la chance de voyager dans tout le pays et voir plus ou moins tout ce qu'il y avait à voir. Après 4 ans passé au même endroit, c'est facile de se dire qu'on a fait le tour.
Mais de temps en temps, en me baladant sur mon feed Instagram, je tombe sur des vidéos ou des photos de paysages improbables avec un hashtag #taiwan dans la description. Et à chaque fois ça me fait me dire: "Comment ça peut être à Taïwan ça?".
Depuis l'an dernier et la pandémie mondial, Taïwan a bénéficié d'un gros coup de pub à l'international pour sa gestion de la crise sanitaire et l'on a pu lire ou entendre des reportages sur le pays dans les quatre coins du monde. Des images de la vie quotidienne restée presque normale à Taïwan faisaient le buzz sur les réseaux sociaux et ont permis à beaucoup de poser pour la première fois les yeux sur cette magnifique île qu'est Taïwan.
Ces images qui ont fait le tour du monde étaient filmées par des gens lambdas voulant partagés leur vie ici et c'est exactement ceux à qui s'adresse le concours du "Trending Taiwan Short Film Competition" Trending Taiwan潮台灣. Que vous soyez un cinéaste aguerri ou juste muni d'un smartphone avec appareil photo, vous pouvez partager vos court métrages et devenir l'un des ambassadeurs de Taïwan à l'international!
Les gagnants du concours pourront recevoir un total de NTD $930,000 et voir leur vidéos apparaitre dans les campagnes promotionnelle du pays!
Venez participer et partager vos vidéos:
https://bit.ly/2X1hkf4
🇰🇷
대만에서 3년동안 유튜브 영상을 찍으면서 갈곳을 이미 다 가봤고 볼 것을 이미 다 본것 같은 느낌 들기 시작했어요. 4년이나 같은곳에 살면 길 가다가 놀라울만한 일이 거의 없어요.
그래도 가끔씩 인스타 볼때 천국 같은 풍경 사진 나오는데 사진 밑에 해쉬태그 #taiwan 이라고 써 있더라고요
이럴때마다 이런 사진 제가 잘 아는 대만에서 찍을 수 있다는거 믿기지 않아요.
작년부터 전세게가 락다운중이었을때 SNS에서 대만과 관련 뉴스 퍼지기 시작했어요. 대부분의 사람들이 나가지 못했을때 대만에서 거의 평소대로 지내고 있었어요. 그 화면들 인스타나 패북에서 보일 수 있었고 대만에게 어마어마한 광고가 되었고 그영상이나 사진들이 대부분 일반인이 찍은것이었어요.
올해도 전세게한테 대만을 보여주기 위해서 대만 외교부 행하는 潮台灣 Trending Taiwan潮台灣단편 영화제 열리고 대만에 관한 이야기 찍고 싶어하는 누구나 참가할 수 있어요!
핸드폰을 써서 찍은 영상으로도 참가할 수 있으니 부끄러움없이 작품을 보내주세요~~
상 20개, 총 93만 대만 달러의 장려금이 있으니 한번 참가해보세요~
https://bit.ly/2X1hkf4
#第7屆全民潮台灣
by random 在 Miley青山舞莉 Youtube 的最讚貼文
SHOP NOW FROM LINK BELOW!
NUGU X MADISON KAI
https://abit.ly/miley_madisonkai_youtube
► A B O U T
Hello! I'm Miley and I'm from Tokyo. I was raised in Singapore, Indonesia, and Japan so I think some international kids could relate to me. My videos are focused on my everyday life as a Waseda student but I also love sharing my everyday fashion, vlogs, and other random things. I hope you enjoy💗
► S O C I A L S
Instagram http://instagram.com/milez728
Tik Tok https://vt.tiktok.com/hs2D23/
Twitter https://twitter.com/miley_728
► C O N T A C T
Business Inquiries info@qualiam.com
http://www.qualiam.com/contact.html
Japan 生まれ Indonesia, Singapore, and Japan 育ちの青山舞莉です!早稲田大学の政治経済学部に通っています。スポーツ、フード、おしゃれ、メイク、お出かけ、なんでも経験したい好奇心旺盛なミーの大好きなことをアップしていきます!
ー年齢は?Age?
21歳 im twenty one :)
ー誕生日はいつ?Birthday?
2000年7月28日 July 28th 2000
millennium baby (did i spell that right?)
ー学年は?what year are you?
今は早稲田の大学4年生です!2021の秋から4年生になりました!im a senior in college!! waseda university yay
ーどこの人?where are you from?
im full japanese!! 日本人!
ー身長は?Height?
156cm! i think thats like 5'1
ーカメラは何使ってる? what camera do you use?
Canon
ー編集ソフトウエアどれ使ってる?Editing software?
Adobe Spark for thumbnails & Final Cut Pro for my videos :)
青山舞莉のInstagram
https://www.instagram.com/milez728/
青山舞莉のTwitter
https://twitter.com/miley_728
青山舞莉のTikTok
https://t.tiktok.com/i18n/share/user/...
Contact
http://www.qualiam.com/contact.html
CREDIT
··················································
Intro by SIMPLY INTROS!
https://youtu.be/u4q69J2PwXM
··················································

by random 在 Miley青山舞莉 Youtube 的最佳解答
SHOP NOW FROM LINK BELOW!
NUGU X MADISON KAI
https://abit.ly/miley_madisonkai_youtube
► A B O U T
Hello! I'm Miley and I'm from Tokyo. I was raised in Singapore, Indonesia, and Japan so I think some international kids could relate to me. My videos are focused on my everyday life as a Waseda student but I also love sharing my everyday fashion, vlogs, and other random things. I hope you enjoy💗
► S O C I A L S
Instagram http://instagram.com/milez728
Tik Tok https://vt.tiktok.com/hs2D23/
Twitter https://twitter.com/miley_728
► C O N T A C T
Business Inquiries info@qualiam.com
http://www.qualiam.com/contact.html
Japan 生まれ Indonesia, Singapore, and Japan 育ちの青山舞莉です!早稲田大学の政治経済学部に通っています。スポーツ、フード、おしゃれ、メイク、お出かけ、なんでも経験したい好奇心旺盛なミーの大好きなことをアップしていきます!
ー年齢は?Age?
19歳 im nineteen :)
ー誕生日はいつ?Birthday?
2000年7月28日 July 28th 2000
millennium baby (did i spell that right?)
ー学年は?what year are you?
今は早稲田の大学2年生です!2020の秋から3年生になります!im a sophomore in college!! waseda university yay
ーどこの人?where are you from?
im full japanese!! 日本人!
ー身長は?Height?
156cm! i think thats like 5'1
ーカメラは何使ってる? what camera do you use?
Canon
ー編集ソフトウエアどれ使ってる?Editing software?
Adobe Spark for thumbnails & Final Cut Pro for my videos :)
青山舞莉のInstagram
https://www.instagram.com/milez728/
青山舞莉のTwitter
https://twitter.com/miley_728
青山舞莉のTikTok
https://t.tiktok.com/i18n/share/user/...
Contact
http://www.qualiam.com/contact.html
CREDIT
··················································
Intro by SIMPLY INTROS!
https://youtu.be/u4q69J2PwXM
··················································

by random 在 Miley青山舞莉 Youtube 的最佳貼文
SHOP NOW FROM LINK BELOW!
NUGU X MADISON KAI
https://abit.ly/miley_madisonkai_youtube
► A B O U T
Hello! I'm Miley and I'm from Tokyo. I was raised in Singapore, Indonesia, and Japan so I think some international kids could relate to me. My videos are focused on my everyday life as a Waseda student but I also love sharing my everyday fashion, vlogs, and other random things. I hope you enjoy💗
► S O C I A L S
Instagram http://instagram.com/milez728
Tik Tok https://vt.tiktok.com/hs2D23/
Twitter https://twitter.com/miley_728
MADISON KAI https://abit.ly/miley_madisonkai_youtube
► C O N T A C T
Business Inquiries info@qualiam.com
http://www.qualiam.com/contact.html
Japan 生まれ Indonesia, Singapore, and Japan 育ちの青山舞莉です!早稲田大学の政治経済学部に通っています。スポーツ、フード、おしゃれ、メイク、お出かけ、なんでも経験したい好奇心旺盛なミーの大好きなことをアップしていきます!
ー年齢は?Age?
19歳 im nineteen :)
ー誕生日はいつ?Birthday?
2000年7月28日 July 28th 2000
millennium baby (did i spell that right?)
ー学年は?what year are you?
今は早稲田の大学2年生です!2020の秋から3年生になります!im a sophomore in college!! waseda university yay
ーどこの人?where are you from?
im full japanese!! 日本人!
ー身長は?Height?
156cm! i think thats like 5'1
ーカメラは何使ってる? what camera do you use?
Canon
ー編集ソフトウエアどれ使ってる?Editing software?
Adobe Spark for thumbnails & Final Cut Pro for my videos :)
青山舞莉のInstagram
https://www.instagram.com/milez728/
青山舞莉のTwitter
https://twitter.com/miley_728
青山舞莉のTikTok
https://t.tiktok.com/i18n/share/user/...
Contact
http://www.qualiam.com/contact.html
CREDIT
··················································
Intro by SIMPLY INTROS!
https://youtu.be/u4q69J2PwXM
··················································
