🏌️‍♀️⛽️Ok EVM gas golfers, got one that is going to test your every gas instinct.

NONE of these functions are optimal... but which one is the most gas efficient and why?

Pick a choice and also leave a comment on why you think so!

❌❌ NO LOOKING ANYTHING UP! ❌❌

a, b, or c?
The answer is C

Each does something a little different gas wise:
- External vs Public functions
- +=
- The location of the function in the file

But the biggest gas factor here is going to be BOTH reading and writing to storage.
The Chainlink core github repo runs with the standard or adding the prefix "s_" to any storage variable, so engineers know it's expensive to use.

In function C, we load the s_playerFunds into memory and read from memory as we loop through the array.
Whereas in a and b, we read right from storage, which is one of the most expensive operations out there!

We also initialize a totalFunds value in memory, instead of constantly writing to s_totalFunds.

a got that part right, but b just wrote to storage for every call.
Long story short:

Reading and writing to storage is expensive! If you have a large array in storage you want to do a lot of reads and writes on, pop it into memory first and go from there!

Here is what that looks like in our function C
playerFunds and totalFunds both get "wiped" after the function finishes, but s_totalFunds and s_playerFunds stays. You can access s_totalFunds & s_playerFunds even after the function finishes, but not playerFunds & totalFunds

And that's the difference between storage vs memory!
Shoot me a like, retweet, and follow if you learned something!

EDIT: It should be noted, that reading an array in from storage calls SLOAD on every element. But we still get the gas efficiency in C > B because we are calling playerFunds.length instead of s_playerFunds.length

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Patrick Collins

Patrick Collins Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @PatrickAlphaC

Mar 14
🛠🔥💪 Released a draft of a #foundry starter kit for smart contract devs!

If anyone wants to take it for a test ride to give some feedback 😊

What's so nice about foundry you may ask? 👇🧵

github.com/smartcontractk…
🏌️‍♂️ 1. It's built by gas-golfers, many of whom working with @paradigm including wizards like @gakonst and @transmissions11.

🧶 2. Built-in fuzz tests feel REALLY nice.

🚄 3. It's fast. Very fast.

🧵👇
🤥 4. "Cheat Codes" are an intuitive way to modify state of contracts you don't own, modify blockchain values, and more "magic" so you can test for any occasion.

💪 5. The logo is clearly my twin brother.
Read 5 tweets
Mar 5
Trivia: Proxies are often used for upgradeable smart contracts. They use delegatecall to call functions of a contract that has the logic they want, but store data in themselves.

Continuing the theme from my last trivia, why is this a bad proxy contract?
So there were two issues here:

1. setImplementation is public, meaning anyone can update this proxy!

2. The proxy uses a storage slot to hold the implementation address. If the implementation has storage, you’ll get hilariously rekt by storage collisions.
📀A proxy contract stores data in the proxy contract based on the implementation.

This means, if your implementation stores data, it might overwrite where we saved the implementation address!
Read 5 tweets
Mar 2
😁 Web3 devs rejoice, you can always look around open source and learn from the best.

Get started with your smart contract development journey with these outstanding coding repos to get you started - both full-stack and backend.

All you need to do, is git clone, and go 🧵👇
Backend:

The @HardhatHQ javascript solidity starter kit
github.com/smartcontractk…

The @BrownieEth python solidity vyper starter kit
github.com/smartcontractk…

The @dapptools raw solidity bash starter kit (foundry in progress)
github.com/smartcontractk…
@HardhatHQ @BrownieEth @dapptools Backend Continued

The @solana @ProjectSerum anchor rust starter kit
github.com/smartcontractk…

The @trufflesuite javascript solidity starter kit
github.com/smartcontractk…

The @wighawag hardhat-deply template:
github.com/wighawag/templ…
Read 6 tweets
Nov 8, 2021
1/ 📚 Want to learn the EXACT steps you need to take to become a #blockchain engineer?

🤝Got you covered! 🤝

It's just 4 steps:
1. Understand Why 🧠
2. Take a Course 🧑🏽‍🏫
3. Join a Hackathon, Community, and Build 👏
4. Do it! ✅

A thread and article🧵

betterprogramming.pub/how-to-become-…
2/ Understand Why

You might think this is an easy step to skip, but if you don't have an unbreakable, bulletproof reason to become a blockchain engineer when it gets hard, you might back off. 💪

So create your list of unwaivering, unfaltering justification to begin your journey
3/
You can borrow some reasons from my past article, or use some of these:

🌎 Create an impact on the world that will last forever
💨 Create a faster, digitally immersive, world
💸 Relandscape the future of finance
(continue)

medium.com/coinmonks/6-re…
Read 19 tweets
Feb 18, 2021
1/ #Chainlink, a thread

#Blockchain allows us to do amazing things, the easiest value add to understand is smart contracts.

In my CS class back in 2013 my professor taught us about #Bitcoin and I literally went “that sounds stupid”.
2/ Why does it have value? I also never thought gold made any sense either. Now my point of view has changed on Bitcoin and I think it has its place as a store of value.

However, I thought blockchain didn’t do much aside from a digital gold.
3/ I didn’t understand why people were so hyped.

Fast forward to a couple years working at a hedge fund. I get to see aaalllllll the shit of the current financial world. I got to do something called “trade matching” as part of my jobs.
Read 20 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(