📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有976部Youtube影片,追蹤數超過110萬的網紅Rachel & Jun's Adventures!,也在其Youtube影片中提到,Click here https://www.vessi.com/racheljun and use my code RACHELJUN to get $25 off of your Vessi shoes! Free shipping to CA, US, AUS, NZ, JP, TW, KR,...
deck you 在 Engadget Facebook 的最讚貼文
You can deck out your character with the game's first high fashion skins too.
deck you 在 Engadget Facebook 的最讚貼文
More controls open up creative possibilities, if you have the patience.
deck you 在 Rachel & Jun's Adventures! Youtube 的最佳解答
Click here https://www.vessi.com/racheljun and use my code RACHELJUN to get $25 off of your Vessi shoes! Free shipping to CA, US, AUS, NZ, JP, TW, KR, SGP
We had some errands to run, but kinda fun errands, and also we wanted to celebrate finalizing our house!! Technically we still have to find a company to do the outside (like the driveway/deck, etc), which we're working on now. But it's HUGE weight off not having to think about the house itself anymore! We'll also keep you guys updated on the house construction itself!
Thanks to Vessi for sponsoring our video!
★Our new merch!!! https://crowdmade.com/collections/junskitchen
The official Crowdmade website is the ONLY place we're selling merch!
★ Patreon! http://patreon.com/junskitchen
►FOLLOW US ε=┌( ・д・)┘
Our Japan channel ⇀ https://www.youtube.com/RachelandJun
Jun's Kitchen ⇀ https://www.youtube.com/JunsKitchen
Twitch ⇀ https://www.twitch.tv/rachelandjun
Twitter | Instagram *@RachelandJun*
►EQUIPMENT (Amazon affiliates links) _〆(・∀ ・ )
Camera ⇀ GH5 (http://amzn.to/2GJWsN2)
►MUSIC ♪♪(o*゜∇゜)o~♪♪
Music from Epidemic Sound: http://share.epidemicsound.com/rDvsz
deck you 在 還不過來Come.188 Youtube 的最佳解答
**Location Information:
https://www.google.com/maps/place/%E8%8A%B1%E8%93%AE%E7%A5%95%E5%A2%83/@23.8938688,121.5883164,17z/data=!3m1 !4b1!4m5!3m4!1s0x34689f96e99ade9d:0x2d8ec0f627656540!8m2!3d23.8938633!4d121.5904974
*Address: No. 9-6 Yanliao Mountain Ridge, Shoufeng Township, Hualien County (the same road as Hualien Farglory Hotel)
*Entrance fee of 100 yuan includes free drinks, coffee, hot tea, snacks + invincible landscape (see the mountains and the sea)
*Facebook fan group: https://www.facebook.com/BeautifulHualien
---------------
Facebook: https://www.facebook.com/come.188
Instagram: https://www.instagram.com/come.188/
*If you need Sub in other languages, please click cc, automatic translation, choose a language, it will be more interesting
-------------------
0:00 Take a photo tour to take you around
1:29 Spiritual photos with palm prints (cemetery nearby =. =)
1:38 Introduction to the dining environment in the landscape glass house, cost
2:02 The beauty spot for taking pictures inside the house (drumming + transparent seat)
2:34 The top observation deck of the landscape glass house
-------------------
Easy cooking series:
https://youtube.com/playlist?list=PLFThU8PKal0YKLOgeuvZ90Ax8FUVTYXjP
Gourmet snack series:
https://youtube.com/playlist?list=PLFThU8PKal0Ycx1WvL4CYS1VbDnxFUiGg
Tourist Attractions Series:
https://youtube.com/playlist?list=PLFThU8PKal0YeZ23kWie807gfT92UHa3a
White noise series:
https://youtube.com/playlist?list=PLFThU8PKal0bLjS_cWzNJLyAymnXxd9fU
---------------------
#Landscape glass house
#Hualian's most beautiful mystery
#Hualien Glass House with the Most Beautiful Landscape
deck you 在 バイリンガルベイビー英会話 Youtube 的最佳貼文
OUR SIX YEAR OLD'S BIRTHDAY IN JAPAN | HIS FIRST POKEMON (GREAT REACTION!!!)
We had a little family birthday party in Japan for my six-year-old son, and he got hist first Pokemon ever!!! We were actually not aware that he loved Pokemon so much, but when one of his friends gave him a Pokemon card deck, his reaction was absolutely adorably!! I can hardly believe that he is six years old already. We love celebrating birthdays as a family, but obviously this year, we couldn't go out anywhere. So we picked a little yakiniku Japanese restaurant near our home and went to dinner VERY early to avoid any crowds. In case you are not familiar with yakiniku, it is actually Korean BBQ. But there are a lot of yakiniku restaurants in Japan. So if you are ever visiting Japan, I HIGHLY recommend you visit a yakiniku Japanese restaurant. Eating out in Japan is so much fun. And eating out at a Japanese yakiniku restaurant is a great experience. Please do try it if you come to Japan.
We are an American and Japanese bicultural family, and we upload videos about our life in Japan. I (Aishah) am American and my husband (Taka) is Japanese. Our children are bicultural (multicultural, if you factor in the fact that I'm half Mexican!). If you're new to our channel and want to follow our life in Japan in English (or you want to learn English with us by watching our videos with subtitles, please subscribe to our channel!
https://www.youtube.com/channel/UCHBnOMB61Xe9eT4oSs3B_NQ?sub_confirmation=1
#Birthday #Japan #Yakiniku
_______________
英語と日本語で6歳の誕生日!!! |国際結婚のナイトルーティン|バイリンガル子育て|オンライン英会話|お家英語|TOEICリスニング|TOEIC勉強|バイリンガルベイビー
「英語と日本語で6歳の誕生日!!! |国際結婚のナイトルーティン|バイリンガル子育て|オンライン英会話|お家英語|TOEICリスニング|TOEIC勉強|バイリンガルベイビー」という動画をアップしました。英語と日本語で子育てしている(バイリンガル)息子のアレックスが6歳になりました!!本来だったらどこかに行きたかったですが、今年は遠くに行かず割と近くにある焼肉屋に行ってきました。私たち(国際結婚)、結婚してもうすぐ11年目を迎えますし、息子も6歳になり、娘ももうすぐ3歳になります。時間は本当にあっという間に経ちますね。動画をみてくださっている皆さんがいつもあたたかくて応援してくださり本当にありがとうございます!
バイリンガルベイビーの動画はほぼ全て英語と日本語の字幕があり、オンライン英会話として使って頂いてもいいですし、オンライン英会話だけではんく、ただ、海外の子育て(アメリカ子育て・英語 子育て)に興味のある方もぜひ観ていただければ嬉しいです。因みに、オンライン英会話でバイリンガルベイビーの動画を使われている方の話しを聞いたところ、英会話のシャドーイングとして動画を使われている方が多く、もしくは、聞き流し英語のように使っている方も多いみたいです。ぜひいろんな方法で使ってみてください!
では、「英語と日本語で6歳の誕生日!!! |国際結婚のナイトルーティン|バイリンガル子育て|オンライン英会話|お家英語|TOEICリスニング|TOEIC勉強|バイリンガルベイビー」をお楽しみください!!
国際結婚の我が家の様々なエピソードにご興味のある方はぜひ、チャンネル登録をお願いします!
https://www.youtube.com/channel/UCHBnOMB61Xe9eT4oSs3B_NQ?sub_confirmation=1
《この動画をオンライン英会話とか英語の勉強にどうやって使う?》
「聞き流し英語」的な感じでこの動画を是非見てください。私達のチャンネルで、実際にネイティブが使っている英語の文法も使っていますし、変にスピードも落としていませんので、このスピードにある程度慣れてたら、海外に行ったらばっちりなはずです!
ですので、是非こちらの動画で英語のリスニングの勉強していただければと思います!
なお、英語のリスニング力アップのためのこの動画のベストな活用方法があります。4ステップ法
①まず、動画を最初見る時に、日本語の字幕を見ながら楽しく動画を最初から最後まで見る。このステップは「英語を勉強する」ステップではなく、単純に「動画の内容」と「ストーリーの展開」を覚えるステップです。つまり、英語・日本語関係なく、この動画で何が起きているかを理解することがステップ1です。ここであまり英語の勉強を気にしなくてもいいです。
②もう一度動画を見るけど、今度は、各フレーズ(気になるフレーズ)を1つ1つ丁寧に聞いて、英語と日本語の字幕(両方とも)一緒に読んで、英語のフレーズ(英語の表現)や言い方を意識する。一時停止したり、巻き戻したりすると理解しにくい英語の表現を理解できるかもしれません。信じがたいですが、私達の動画に本当に「知っておくべき英語」が厳選されています。なのでどのフレーズを学んでも損ではありません。「生きた英会話」と思っていただければと思います。
③もう一度動画を見る。今後は、日本語の字幕を絶対に見ないで動画を見る。理解できない場合は、絶対に英語の字幕だけを見る。なので、このステップは、「海外に留学したばかりの人になった気分」。つまり、日本語に頼らないステップ。
④英語の字幕と日本語の字幕、どれも全く読まないでで最初から最後まで動画をもう一度見る。 絶対に、自分の理解度に驚くはずです。100%は理解できないかもしれないが、聞く前と比較すると本当にビビるぐらい英語力が伸びる。「ええ?これだけで英語力が上がるの?」と思われる方が多いと思うけど、4回も同じ箇所を見ているので、それだけで思っている以上に英語が身についている(同じ歌を4回聞けばなんとなく歌えると一緒)。留学する時に、こういう風にみんな学ぶ。因みに幼児も同じように言語を学ぶ。
この4つのステップを踏んだ後に、もしまだ動画の内容が完璧に理解できないということであれば、リスニング聞き取れなかった箇所だけに戻り、更に2-3回を聞くことがお勧めです。
最後ですが、もし、使っている英語について何か質問があればいつでもコメントで聞いてくださいね。他の視聴者のためにもなるし遠慮なく聞いちゃってください。
Good luck!!!
**Follow us**
Instagram: https://www.instagram.com/bilingualbaby/
Twitter: https://twitter.com/bilingualbaby01