📜 [專欄新文章] 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.
👏 歡迎轉載分享鼓掌
同時也有39部Youtube影片,追蹤數超過9萬的網紅Shiney,也在其Youtube影片中提到,A Total War Saga: TROY ซื้อเกมได้ที่ https://store.epicgames.com/legendauser/a-total-war-saga-troy A Total War Saga: TROY is the first in the award-...
「enough is enough campaign」的推薦目錄:
- 關於enough is enough campaign 在 Taipei Ethereum Meetup Facebook 的最佳貼文
- 關於enough is enough campaign 在 Facebook 的最佳貼文
- 關於enough is enough campaign 在 Bao Tranchi Facebook 的最佳解答
- 關於enough is enough campaign 在 Shiney Youtube 的最讚貼文
- 關於enough is enough campaign 在 Shiney Youtube 的最讚貼文
- 關於enough is enough campaign 在 Shiney Youtube 的最讚貼文
- 關於enough is enough campaign 在 Enough is Enough UK - Home - Facebook 的評價
- 關於enough is enough campaign 在 Enough is Enough: It's time to fight the cost of living crisis 的評價
enough is enough campaign 在 Facebook 的最佳貼文
看到這個標題是不是覺得好像又要惹事了?
爸爸給我看了一份報紙,報導關於醫院裡的“秘密”。我看了覺得很誇張,心想醫生有權力決定給誰生給誰死嗎?我懷疑是假新聞,於是我一口氣打給了十幾家政府醫院求證。接下來,就發生了影片裡面一連串的故事...
這個影片很長,但很痛心,很無助,也很感人,感謝最後神秘人站出來...
To all Malaysian, as you have seen in the video, even though we have donated medical equipments which worth more than 1 Million Ringgit to those hospitals. But we must honestly say that some of these medical equipments are considered as consumables and are only enough for 2 to 4 weeks. That is not enough at all....😔 We hope that everyone can continue to help the hospitals and all patients in Malaysia to get them through this difficult period... Please share this video to your datuk friends or tag them out. God bless Malaysia.
醫生求救! Youtube 高清版影片: https://bit.ly/3juSFqR
#KerajaanGagal #Lawan #INeedHelp #KitaJagaKita
Support our brand new NW logo black T-shirt! and also support our "I Need Help" theme T-shirt for the #WhiteFlag campaign
欲購買黃明志“黑白旗主題潮Tee” 和 "NW Logo 潮Tee請到
https://namewee4896.com/shop/new-arrivals/oversize-logo-tee-unisex
-
欲網購黃明志最新實體專輯《亞洲通才》及歷年專輯和周邊商品請到。Purchase Namewee Latest 《Asian Polymath》 , Others Music Albums & Merchandises Please log in to https://namewee4896.com/
Namewee 黃明志 Telegram Channel :
https://t.me/nameweeofficial
Namewee 黃明志 Official Facebook Fan Page:
https://www.facebook.com/namewee/
Namewee YouTube Channel Link:
http://www.youtube.com/namewee/
Namewee Twitter Channel:
@NameweeO
#Namewee #黃明志
enough is enough campaign 在 Bao Tranchi Facebook 的最佳解答
Another day, another knock off. 🤦🏻♀️🤬It’s hard enough as it is to be a Fashion designer. To come up with original ideas that are the heart & soul and identity of your brand. It’s even more heartbreaking to work so hard to manifest these ideas, only to wake up and see
an Investment firm with MILLIONS more dollars than you steal your original idea, base their entire campaign around it, and have it manufactured overseas for pennies to pump it out by the tens of thousands. Done dirt cheap done real fast.
In every other industry, ideas are protected by trademarks. Musicians, Authors, songwriters, painters, movies, hell even the papparazi photographers hiding behind
Trashcans get more protections than designers over their “art”. But fashion is just an open field for stealing ideas with no protections for the person being stolen from.
The thief is protected by the legal system which favors the one with the most 💵MONEY and the most lawyers.
It’s an extremely frustrating, expensive, legal uphill battle. Just so you can see . . . First picture is their obvious knock off. Our original Feline Fatale bodysuit was made back in 2015. To time stamp it, next photo is of Mariah Carey wearing our original Feline Fatale bodysuit for the Aug/Sept 2016 COMPLEX magazine cover. And the third image is of Christie Brinkley wearing it for her 2017 Sports Illustrated spread. #stolen #knockoff #madecheaply #cheap #inferior #fashionthieves #getyourownideas #supportsmallbusinesses #supportindependentdesigners #investinquality
enough is enough campaign 在 Shiney Youtube 的最讚貼文
A Total War Saga: TROY
ซื้อเกมได้ที่ https://store.epicgames.com/legendauser/a-total-war-saga-troy
A Total War Saga: TROY is the first in the award-winning strategy series to focus on the Bronze-Age Mediterranean and the legendary twenty-year conflict between the kingdoms of Troy and Mycenaean Greece, known as the Trojan War.
Total War Saga: Troy is a strategy computer game developed by the Creative Assembly Sofia and published by Sega. It is part of the Total War Saga sub-series and will be released on August 13, 2020 on the Epic Games Store, then on Steam in Summer 2021. As part of the 12 month exclusivity promotional deal with Epic Games, Total War Saga: Troy will be free of charge for the first 24h of it's release.
The game is set during the Trojan War in the Bronze Age, taking place the furthest back in time the series has ever covered.
The Greek gods featured in Troy can influence the game in similar ways to their Homeric counterparts, with their influence spanning entire factions, rather than directly intervening on the battlefield. Gaining favour with the different gods requires players to invest time or sacrifice resources, but also provides various benefits as the game progresses.
Although there is little documentation of Bronze Age naval warfare, naval combat has been included in the form of island battles, inspired by that featured in Total War: Warhammer II – Curse of the Vampire Coast. The Greek god Poseidon will play a major role in these battles.
Hero versus hero combat has been implemented in Troy, wherein two fighting heroes will be given room by nearby units, creating a circular battle area and preventing other units from intervening. Some heroes have an active battle ability called Divine Challenge, which taunts opposing characters into one-on-one combat. Eight heroes are included in the game, such as the Greek champion- Achilles, and the Prince of Troy- Hector.
A Trojan horse mechanic plays a major role within the campaign, appearing in the campaign at specific moments, and providing significant bonuses to players should it be acted upon quickly enough.
Troy introduces a new multiple resource economy that reflects the advancing pre-monetary barter economy of the Bronze Age setting. The five different resources are food, wood, stone, bronze, and gold, which can be found within different regions to varying degrees of scarcity. Food and wood are used to recruit early game units and construct simple buildings, whereas more formidable structures will require stone, and higher tier units will require bronze. Gold is vital for trade due to its universal rarity.
The diplomacy system in Troy builds on that featured in Total War: Three Kingdoms, allowing you to appease allies, your enemies in their place, allowing the player to change the entire course of the Trojan war. Other new diplomatic features which are unique to Troy will also be introduced.
enough is enough campaign 在 Shiney Youtube 的最讚貼文
A Total War Saga: TROY
ซื้อเกมได้ที่ https://store.epicgames.com/legendauser/a-total-war-saga-troy
A Total War Saga: TROY is the first in the award-winning strategy series to focus on the Bronze-Age Mediterranean and the legendary twenty-year conflict between the kingdoms of Troy and Mycenaean Greece, known as the Trojan War.
Total War Saga: Troy is a strategy computer game developed by the Creative Assembly Sofia and published by Sega. It is part of the Total War Saga sub-series and will be released on August 13, 2020 on the Epic Games Store, then on Steam in Summer 2021. As part of the 12 month exclusivity promotional deal with Epic Games, Total War Saga: Troy will be free of charge for the first 24h of it's release.
The game is set during the Trojan War in the Bronze Age, taking place the furthest back in time the series has ever covered.
The Greek gods featured in Troy can influence the game in similar ways to their Homeric counterparts, with their influence spanning entire factions, rather than directly intervening on the battlefield. Gaining favour with the different gods requires players to invest time or sacrifice resources, but also provides various benefits as the game progresses.
Although there is little documentation of Bronze Age naval warfare, naval combat has been included in the form of island battles, inspired by that featured in Total War: Warhammer II – Curse of the Vampire Coast. The Greek god Poseidon will play a major role in these battles.
Hero versus hero combat has been implemented in Troy, wherein two fighting heroes will be given room by nearby units, creating a circular battle area and preventing other units from intervening. Some heroes have an active battle ability called Divine Challenge, which taunts opposing characters into one-on-one combat. Eight heroes are included in the game, such as the Greek champion- Achilles, and the Prince of Troy- Hector.
A Trojan horse mechanic plays a major role within the campaign, appearing in the campaign at specific moments, and providing significant bonuses to players should it be acted upon quickly enough.
Troy introduces a new multiple resource economy that reflects the advancing pre-monetary barter economy of the Bronze Age setting. The five different resources are food, wood, stone, bronze, and gold, which can be found within different regions to varying degrees of scarcity. Food and wood are used to recruit early game units and construct simple buildings, whereas more formidable structures will require stone, and higher tier units will require bronze. Gold is vital for trade due to its universal rarity.
The diplomacy system in Troy builds on that featured in Total War: Three Kingdoms, allowing you to appease allies, your enemies in their place, allowing the player to change the entire course of the Trojan war. Other new diplomatic features which are unique to Troy will also be introduced.
enough is enough campaign 在 Shiney Youtube 的最讚貼文
A Total War Saga: TROY
ซื้อเกมได้ที่ https://store.epicgames.com/legendauser/a-total-war-saga-troy
A Total War Saga: TROY is the first in the award-winning strategy series to focus on the Bronze-Age Mediterranean and the legendary twenty-year conflict between the kingdoms of Troy and Mycenaean Greece, known as the Trojan War.
Total War Saga: Troy is a strategy computer game developed by the Creative Assembly Sofia and published by Sega. It is part of the Total War Saga sub-series and will be released on August 13, 2020 on the Epic Games Store, then on Steam in Summer 2021. As part of the 12 month exclusivity promotional deal with Epic Games, Total War Saga: Troy will be free of charge for the first 24h of it's release.
The game is set during the Trojan War in the Bronze Age, taking place the furthest back in time the series has ever covered.
The Greek gods featured in Troy can influence the game in similar ways to their Homeric counterparts, with their influence spanning entire factions, rather than directly intervening on the battlefield. Gaining favour with the different gods requires players to invest time or sacrifice resources, but also provides various benefits as the game progresses.
Although there is little documentation of Bronze Age naval warfare, naval combat has been included in the form of island battles, inspired by that featured in Total War: Warhammer II – Curse of the Vampire Coast. The Greek god Poseidon will play a major role in these battles.
Hero versus hero combat has been implemented in Troy, wherein two fighting heroes will be given room by nearby units, creating a circular battle area and preventing other units from intervening. Some heroes have an active battle ability called Divine Challenge, which taunts opposing characters into one-on-one combat. Eight heroes are included in the game, such as the Greek champion- Achilles, and the Prince of Troy- Hector.
A Trojan horse mechanic plays a major role within the campaign, appearing in the campaign at specific moments, and providing significant bonuses to players should it be acted upon quickly enough.
Troy introduces a new multiple resource economy that reflects the advancing pre-monetary barter economy of the Bronze Age setting. The five different resources are food, wood, stone, bronze, and gold, which can be found within different regions to varying degrees of scarcity. Food and wood are used to recruit early game units and construct simple buildings, whereas more formidable structures will require stone, and higher tier units will require bronze. Gold is vital for trade due to its universal rarity.
The diplomacy system in Troy builds on that featured in Total War: Three Kingdoms, allowing you to appease allies, your enemies in their place, allowing the player to change the entire course of the Trojan war. Other new diplomatic features which are unique to Troy will also be introduced.
enough is enough campaign 在 Enough is Enough: It's time to fight the cost of living crisis 的推薦與評價
Join the campaign to fight the cost-of-living crisis at https://wesayenough.co.uk/Follow us on Twitter ... ... <看更多>
enough is enough campaign 在 Enough is Enough UK - Home - Facebook 的推薦與評價
Enough is Enough UK. 59197 likes · 6439 talking about this. The campaign to fight the cost-of-living crisis. ... <看更多>