🚀 Journey of 1 USDC: Discovering the Intricacies of Ethereum Transactions 🪙
What Really Happens When You Send 1 USDC? 👀 PLUS opportunities in web3 this week
Gm Gm Folks!
This is Bits n Bytes, the zippy newsletter that packs a punch, without leaving you breathless! 😊
TLDR;
What goes behind sending crypto across wallets? 💳
How transactions are built, propagated, and executed in Ethereum.💸
The real truth behind what happens when you send 1 stablecoin.🪙
That’s a tad too much to cover in one go, isn’t it?🥴We got ya, we'll break it down into a 5 part series covering what it takes for a transaction to conclude on Ethereum!
Blockchain technology is often associated with the hype of cryptocurrencies and the risks that come with them. However, beyond the fuzz and the scams lies a world of real value, driven by math, cryptography, and computer science. With its open-source nature, every movement in the blockchain can be seen and tracked, making it a transparent and accessible platform for anyone, anywhere, anytime.😍
Now, have you ever wondered what really happens when you hit the send button to transfer 1 USDC or USDT to a wallet address?
Well, wallets in the likes of Metamask, Rainbow Wallet, etc provide you with enough buttons and text fields to send funds across wallets. The moment you hit send, after some time the wallet says the transaction's been confirmed. All of a sudden, your friend’s wallet is now 1 USDC richer.
Wonder no more! As your crypto bro, we’re here to give you a walkthrough of what happens behind the scenes!😎😎
First, let's start with some background information. USDC and USDT are both stablecoins, meaning their value is pegged to the US dollar. This means that 1 USDC or USDT would in most cases be worth ~ $1 USD (unless it’s depegged due to market conditions).
Now, imagine that you want to send 1 USDC to your friend's wallet address (We know you aren’t getting that money back from your friend 😹).
👉 The first thing that happens is that you initiate the transaction by providing your friend's wallet address and the amount of USDC you want to send. This information is then broadcasted to the blockchain network.📢
The blockchain network is a decentralized network of computers, also known as nodes, that work together to process transactions. ⛓️
👉 These nodes validate the transaction by verifying that you have enough USDC and gas fee in your wallet to complete the transaction and that the wallet address you provided is valid. 🧐
👉 Once the transaction is validated, it is added to a pool of unconfirmed transactions, also known as the mempool. This pool is where all the pending transactions are waiting to be processed by miners. When a miner successfully solves an equation, they add the transaction to a block and then add that block to the blockchain. 🚏
👉 In order for a transaction to be confirmed, it must be included in a block that is added to the blockchain. This process can take a few minutes to several hours, depending on how busy the network is and how high the transaction fee you paid was. 😵Transaction fees are paid to miners as a reward for processing transactions and can affect how quickly your transaction is processed.
👉 Once your transaction is confirmed and added to the blockchain, the USDC is transferred from your wallet to your friend Sam’s wallet. This transfer is recorded on the blockchain, which means it is publicly visible and cannot be altered. This ensures the security and transparency of the transaction.🫡
Let’s take a step back and go back to initiating a transaction - instruction to execute an action. In this case that'd be sending 1 USDC to your friend. And a wallet (e.g., Metamask) helps build such transactions in a relatively beginner-friendly way.🫶
Wallets are software applications that simplify the process of sending transactions to the Ethereum network. These transactions allow users to execute actions, such as sending 1 or many coins to a wallet address. A wallet, (say Metamask), assists users in constructing these transactions in a user-friendly manner, particularly for beginners.
The transaction built by a wallet can be represented as an object with specific fields and their corresponding values. Our transaction object would initially appear as follows👇👇
{
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
// [...]
}
Here to states the target address. In this case, 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 is the address of the USDC smart contract.
Wait, what👀?
Weren't we supposed to be sending 1 USDC to your friend Sam? Shouldn't it be Sam's wallet address? 😓
📌 Well, no. To send USDC, one must essentially craft a transaction that executes a piece of code stored in the blockchain (the fancy name for Ethereum's database) that will update the recorded balances of USDC.
📌 Both the logic and related storage to execute such updates are held in an immutable and public computer program stored in Ethereum's database - The USDC smart contract.
Hence, you want to build a transaction that tells the contract 🤔
Hey buddy, update your internal balances, taking 1 USDC out of my balance, and adding 1 USDC to Sam's balance.🤝
In Ethereum jargon, “Hey buddy" translates to setting USDC's address in the to
field of the transaction.
However, the to field is not enough. From the information you provide in your favorite wallet's UI, the wallet fills up several other fields to build a well-formatted transaction.🤔🤔
{
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"amount": 0,
"chainId": 1,
"nonce": 0,
// [...]
}
📌 It fills the amount
field with a 0. So you're sending 1 USDC to Sam, and you neither use Sam's address nor put an 1
in the amount
field.
👉The amount field is actually included in a transaction to specify how much ETH (the native currency of Ethereum) you're sending along your transaction. Since you don't want to send ETH right now, then the wallet would correctly set that field to 0.
👉 As of the chainId
, it is a field that specifies the chain where the transaction is to be executed. For Ethereum Mainnet, that is 1.
What about the nonce field?
▶️ That's a number that should be increased every time you send a transaction to the network. It acts as a defense mechanism to avoid replaying issues.
▶️ Wallets usually set it for you. To do so, they query the network asking what's the latest nonce your account used and then set the current transaction's nonce accordingly.
In the example above it's set to 0, though in reality, it will depend on the number of transactions your account has executed.💯
Bruh, did you say “query the network"? 🤨
👉 Well, the wallet executes a read-only call to an Ethereum node and the node answers with the requested data. There are multiple ways to read data from an Ethereum node, depending on the node's location, and what kind of APIs it exposes.
👉 Let's imagine the wallet has direct network access to an Ethereum node. More commonly, wallets interact with third-party providers (like Infura, Alchemy, QuickNode, and many others).
👉 Requests to interact with the node follow a special protocol to execute remote calls. Such a protocol is called JSON-RPC.
Now that your transaction payload is ready for the network, we got some business to deal with 👀👀
In the world of blockchain, gas fees are the magical pixie dust that makes your transactions come to life! ✨
But be warned, my friend, deciding how much of this precious gas to use can be a bit of wizardry.🪄
You see, every transaction needs to pay a fee to the network of nodes that work tirelessly to validate and execute it. 💸
The cost of this fee is paid in ETH and depends on how much gas your transaction consumes, how much you're willing to pay per gas unit, and how much the network is willing to accept as the minimum fee.🤑
Now, here's the kicker🔻🔻
the more you're willing to pay, the faster your transaction will be processed.
It's like a race to the finish line, and the higher your gas fee, the more likely you are to win! So, if you're in a hurry to send some ETH to your BFF or buy that fancy NFT, you might want to pay a higher gas fee. But if you're not in a rush, you can wait a bit and pay a lower fee. 🏃♀️🪙
Different wallets use different strategies to determine the right gas fee for your transaction. Some may consult the network to find out the current minimum fee, while others might estimate the gas cost and add a buffer to ensure your transaction is processed quickly. It's all a bit of a mystery, but that's part of the fun!🫣
Cool, right? 🫰
Now that you're getting familiar with the idea that setting the gas fee prices is not straightforward, and it is a fundamental step for building a successful transaction, let's go back to the actual transaction now. There are three gas-related fields that need to be set:
{
"maxPriorityFeePerGas": ..,
"maxFeePerGas": ..,
"gasLimit": ..,
}
Whenever a wallet UI lets you choose between some version of slow, regular, or fast transactions, it's actually trying to decide what values are the most appropriate for those exact parameters.
What about gaslimit though?🎰
📌 When it comes to Ethereum, you don't want to go in guns blazing without a plan. That's why wallets have a handy tool to help you estimate how much gas you'll need for your transaction. Think of it like a crystal ball but for gas usage.🔮
📌 This mechanism lets you simulate your transaction before you actually hit that "send" button. That way, you can get a close estimate of how much gas your transaction will need and set a reasonable gas limit.
Plus, you'll even get an estimate of how much the transaction will cost in good ol' USD.
Why not just set a crazy high gas limit and call it a day?
➡️ Well, my friend, that's a surefire way to leave your wallet defenseless against sneaky smart contracts.
➡️ You see, these contracts can have all sorts of tricky logic, and it's up to you to foot the bill for their execution. 💸
➡️ By setting a sensible gas limit from the get-go, you're protecting yourself from those pesky scenarios that could drain your precious ETH funds in gas fees.
To get this estimate, wallets use a node's endpoint called eth_estimateGas. Before you send that 1 USDC to Sam, your wallet can simulate the transaction and figure out the perfect gas limit for your transfer. It's like having your own personal gas guru to guide you through the Ethereum wilderness 💯
Now our transaction payload looks something like this 👇
{
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"amount": 0,
"chainId": 1,
"nonce": 0,
"data": "0xa9059cbb00000687tg678909876545678909876567890098765457000de0b6b3a7640000",
"maxPriorityFeePerGas": 4830000000,
"maxFeePerGas": 987080000000,
"gasLimit": 21000
}
NOTE: maxPriorityFeePerGas
and maxFeePerGas
will ultimately depend on the network conditions at the moment of sending the transaction. There are other things that are part of the txn payload namely the access list and transaction type 😵💫
Alright folks, we have a transaction to be sent on Ethereum. But before we can send those coins flying through the digital ether, we need to make sure the network knows it's really you sending them, and not some sneaky hacker.👀
Enter the magical world of digital signatures!
📌 When you hit that SEND button, your wallet springs into action, using your account's private key (which it has access to) and a fancy cryptographic algorithm involving curvy shapes called ECDSA.
📌 This creates a digital signature that's unique to your account and can't be replicated by anyone else.
📌 What's actually being signed is the keccak256 hash of the concatenation between the transaction's type and the RLP-encoded content of the transaction. But don't worry, you don't need to understand all the ins and outs of this process to send those coins safely on their way.
📌 It's like a treasure map that only the right people can read. This treasure map, or signature, is made up of three parts: v, r, and s.
📌 These symbols help the network verify that the transaction is legit and not some sneaky imposter trying to steal your booty. If you're interested in learning more about the magic behind these symbols, the Internet can be your treasure trove of knowledge.
📌 If you want to see what signing a transaction looks like in action, you can check out the ethereumjs/tx package and use the ethers package to make things even easier. It's like casting a spell to send some USDC coins, arrrr!
FYI: signing is not encrypting.
Your transactions are always in plaintext. Once they go public, anyone can make sense of their content👇
{
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"amount": 0,
"chainId": 1,
"nonce": 0,
"data": "0xa9059cbb00000687tg678909876545678909876567890098765457000de0b6b3a7640000",
"maxPriorityFeePerGas": 4830000000,
"maxFeePerGas": 987080000000,
"gasLimit": 21000,
"accessList": [],
"type": 2,
"v": 1,
"r": "57d733933b12238a2aeb0069b67c6bc58ca8eb6827547274b3bcf4efdad620a",
"s": "e49937ec81db89ce70ebec5e51b839c0949234d8aad8f8b55a877bd78cc293"
}
📌 Alright, so now we need to turn our fancy signature into a language the Ethereum network can understand.👨💻
📌 This involves encoding it into a series of bytes using a method called RLP. Think of it like translating from human language to computer language. The first byte indicates the type of transaction.
Now that you have created and signed your transaction, it's time to send it to an Ethereum node. This is where the magic happens!
Ethereum nodes have a special JSON-RPC endpoint called eth_sendRawTransaction, which they use to receive transaction requests. Once the transaction is successfully submitted, you'll receive a 32-byte-long sequence of hex characters - this is your transaction's hash, and it's how you can uniquely identify it on the Ethereum network.
What happens next, when an Ethereum node receives the serialized signed transaction?
📌 The receiving node will validate the transaction, propagate it to other nodes in the network, and eventually add it to a block on the blockchain.
📌 But that's not all there is to it! Stay tuned for next week's edition where we dive deeper into the transaction lifecycle on the Ethereum network.
Until then, keep reading and upgrading your crypto know-how in a flash with Bits n Bytes.
🟣 Opportunities in Web3 this week 🟣
👉 Polygon DevX Hackathon : 🔗 Register here
👉 Encode X Mantle Web3athon (4 weeks long) : 🔗 Register here
👉 Consensus Web3 Hackathon : 🔗 Register here
👉 Chainlink Spring Hackathon: 🔗 Register here
We're stoked that you've been keeping up with our latest edition of Web3 Bits N Bytes. We hope you had a blast reading it and learned some new bits of info!
As part of our community of passionate Web3 believers, we want to keep the party going and see this community grow! So, spread the word, my friend! Share this 💌newsletter with your buds and colleagues who are also into decentralized tech. And don't forget to follow us on Twitter for some fun interaction.🤗
If you haven't already, be sure to subscribe to our newsletter so you never miss out on any of the action. We promise to keep bringing you the latest news, insights, and perspectives on the amazing world of Web3 and the decentralized future.
Thank you for being awesome and supporting us. We can't wait to see you in the next edition of Web3 Bits N Bytes! Let's keep the party going!
Adios 👋
Exactly something I was looking forward to get better understanding of the flow. Good job !!
Waiting for the next post on transactions lifecycle.