📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有142部Youtube影片,追蹤數超過60萬的網紅Tasty Japan,也在其Youtube影片中提到,すし×すもうが織りなす新感覚アニメ「どすこいすしずもう」のキャラクター作りにTasty Japanが挑戦! 子どもたちに大人気のキャラクター、サーモンざくらとあまえびおうが・・・美味しいココットオムライスに!かわいいオムライスのお手軽レシピ♪みなさんも、ぜひお家で作ってみてください〜♪ 「どすこい...
fun to draw 在 Emcee Minnie Facebook 的最讚貼文
今晚来一场绝对令你难忘的中秋派对❤️❤️
JIO你来中秋派对 | Mid-Autumn Festival: Moonlight of thee Crystal Castle
19.09.2021 8pm-10pm
中秋没有你在身边,天上的月亮依然皎洁,你我依旧相伴,因为。。。玻璃市州联委会举办了线上中秋派对啦!!!就在《玻璃城下の月光》与你相聚✨
🥮 此线上中秋派对将举办一系列的线上活动,如:线上互动游戏,歌手演唱表演🎼,还有月亮🌝的故事!
接下来,此次将会选出颜值爆表hold住场的派对王和女王😏🥳
除此之外也准备令人振奋的活动、有奖游戏和幸运抽奖!
将会有总值 RM 500 的 Touch 'n Go eWallet 等你来赢取!
千万不要错过此次的活动!一起感受到节日的快乐!
9/19,8pm - 10pm 约定你!!
It’s the same moonlight, but a different Mid-Autumn celebration!
Looking forward to online Mid-Autumn activities? We got your back!
🥮 19/9 8pm - 10pm, YBAM Perlis SLC presents the Mid- Autumn Online Party 'Moonlight of the Crystal Castle' for you to enjoy with your friends and family.
🏮Join us in these fun activities!🏮
Lucky draw / Prize giving quiz / Singing performance / Interactive online games / Story-telling / The beauty contest
收到邀请函的你,还不赶紧点击并填写以下报名表格!
Click the link below to register:
Google form: https://forms.gle/e5bPeUJYptJPvmMs6
若有疑问,请联络 For further inquiries, feel free to contact them:
廖韦微 Leow Wei Wei 0199595007
张可欣 Teoh Keh Shin 0174793284
✅订阅马佛青Telegram Follow YBAM on Telegram: https://t.me/YBAMalaysia
#emceeminnie #femaleemcee #中秋节派对
fun to draw 在 Mama's Infinity Love Facebook 的精選貼文
Joreen totally enjoyed her art class at @heartstudiosg
In this September Holiday Programme, Joreen learnt to draw Japanese Fast Food on a canvas.
Mama is so impressed with Joreen's artwork!
Special shout out for the passionate teachers at @heartstudiosg appreciate all the guidance on our Joreen! Joreen said the class is very fun!
#5y2m #joreentanjiyoon #artclass #septtwo1lo
fun to draw 在 Tasty Japan Youtube 的最佳貼文
すし×すもうが織りなす新感覚アニメ「どすこいすしずもう」のキャラクター作りにTasty Japanが挑戦!
子どもたちに大人気のキャラクター、サーモンざくらとあまえびおうが・・・美味しいココットオムライスに!かわいいオムライスのお手軽レシピ♪みなさんも、ぜひお家で作ってみてください〜♪
「どすこいすしずもう」はテレビ神奈川、チバテレ、テレ玉ほかにて好評放送中!
TV放送・配信情報はこちらから! https://sushisumo.jp/onair/
---
ココットオムライスでぶつかり稽古【どすこいすしずもう】コラボ第3弾
8.5cmココット型 2個分
材料:
玉ねぎ(みじん切り)1/8個
ベーコン 1枚
バター 10g
ごはん 200g
トマトケチャップ 大さじ3
塩コショウ 少々
卵 1個
サラダ油 小さじ1
塩 少々
赤パプリカ 1/4個
トマトケチャップ (仕上げ用)適量
作り方:
1. フライパンにバターを入れて弱火にかけ、バターが溶けたら玉ねぎを入れて炒める。
2. 玉ねぎが透き通ってきたら、1cm幅にカットしたベーコン、トマトケチャップ、塩コショウを加えて炒める。
3. ごはんを加えて炒め、全体がむらなく混ざったら火を止めて、ココットに詰める。
4. ボウルに卵を割り入れ、塩を加えてよく溶きほぐす。
5. フライパンにサラダ油をひいて(4) を流し入れ、焼き色がつかないように両面を焼く。まな板に移し、ココットをおいて丸くくり抜いたら(3) にのせる。
絞り袋にケチャップを入れてサーモンざくらとあまえびおうの顔を描く。
6. 赤パプリカをサーモンざくらの鼻と、あまえびおうのえびのしっぽの形にカットして、それぞれ飾ったら、完成!
---
Omurice en Cocotte
Servings: 2 (8.5cm cocotte)
INGREDIENTS
1/8 onion (chopped)
1 slice bacon
10g butter
200g steamed rice
3 tablespoons ketchup
Some salt and pepper
1 egg
1 teaspoon vegetable oil
Some salt
1/4 red bell pepper
Some ketchup (for decorations)
PREPARATION
1. Melt butter in a pan over low heat. Add chopped onion and stir.
2. Add bacon pieces (cut into 1cm pieces), ketchup, salt, and pepper.
3. Add rice and combine well. Transfer the rice into the cocottes.
4. Beat the egg in a bowl. Add some salt.
5. Heat a lightly oiled pan. Pour the beaten egg into the pan. Flip the egg crape and make sure to cook both sides. Transfer the egg crepe to a cutting board. Using the cocotte, cut the egg crepe into circles. Place them on the rice (3).
6. Using a piping bag, draw fun faces with ketchup! You can also use some red bell pepper pieces as face parts.
7. Enjoy!
---
#TastyJapan #レシピ #どすこいすしずもう
MUSIC
Licensed via Audio Network
![post-title](https://i.ytimg.com/vi/nK_eN0Nxjl8/hqdefault.jpg)
fun to draw 在 Venny Law Youtube 的最佳貼文
Things to note: start your painting with darker colour first :)
I bought the above paints by numbers here ??
https://shp.ee/jbazdg4
Music: Tomppabeats-Far Away
IG: vennylaw
![post-title](https://i.ytimg.com/vi/A1yumhzNUns/hqdefault.jpg)
fun to draw 在 FON KIRURU Youtube 的最佳貼文
ขอขอบคุณเหล่านักพากย์มากๆเลยจ้า!!?
((Thanks to the voice actor!!?))
คุณ BasicBomb
เพจสำหรับติดตาม : https://www.facebook.com/69BASICBOMB69/
คุณ JEFFY RUN OVER
เพจสำหรับติดตาม : https://www.facebook.com/adminJEFFY
คุณ Camuri
เพจสำหรับติดตาม : https://www.facebook.com/CaMuRibird
และคุณ Deadsoul Mos(นักพากย์เสียงประจำบัคแจ็คในไอเดน)??
-----------------------
สวัสดีเพื่อนๆทุกท่านจากทั่วโลกนะคะ อนิเมชั่นนี้เคยคุยเล่นๆกับน้อง Camuri ว่าอยากจะลองจับตัวละครของเราเองมาแต่งคอสเพลย์ดูบ้าง ตอนแรกจะวาดอย่างเดียวแต่ไปๆมาๆงั้นก็ทำเป็นอนิเมชั่นเลยละกัน จริงๆคิดจะให้ Pon แต่งเป็นมุซันในเรื่องดาบอสูตรนะคะ แต่ไปตัดสินใจขอแต่งเป็นดาร์คช็อกโก้แทน(จากเกมส์ cookie run kingdom) ส่วนคนอื่นๆเพื่อนๆหน้าจะพอดูออกแน่นอนค่ะว่าใครแต่งเป็นใครบ้าง ถ้าหากเพื่อนๆคิดอยากให้ Pon Rost Voning หรือต้าวมังกรkiruru ให้แต่งเป็นใครเดี๋ยวจะวาดมาลงเพจให้นะคะ 555555+ ???
และขอขอบคุณเพื่อนๆทุกท่านที่ติดตามผลงานของเรานะคะ ไว้เจอกันโปรเจคหน้านะคะ ???
---------------------------
Hello friends from all over the world. In this work I talked to my friend Camuri. I talked to him about how to cosplay for characters. But I feel that drawing is still not fun.
So I imagined making animations just for fun and to bring the characters to life with cosplay. And each of them will be dressed differently. I think the friends will see who they make up and from what games they are.
At first I wanted Pon to dress up as a muzan from Kimetsu No Yaiba, but I decided to make him a dark cocoa cookie in the cookie run kingdom game. If my friends want to see my characters Pon Rost Voning and kiruru in Cosplay. I will try to draw them on the page of course! LOLOLOL!???
And thank you to my friends for always following my work. See you next time in my project. ??
(If the translation is wrong, really sorry!)
-----------------------
Music used : Where I am From , Trancer
-----------------------
(สามารถติดตามผลงานภาพวาดหรืออื่นๆได้ที่นี่นะคะ)
You can follow up on other channels here.
► http://kiruru2592.deviantart.com/
► https://twitter.com/KiruruFon
► https://www.facebook.com/VoningAndKiruru/?view_public_for=1819356651678212
If something goes wrong, I'm sorry and Thank You!!
![post-title](https://i.ytimg.com/vi/o_VKmYvRiO8/hqdefault.jpg)