Alvaro Luken
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Intro to Ethereum Transactions <details><summary><b>Previous Section Recap</b></summary><br/> In the previous section, we learned about the basic structure and syntax of [JSON-RPC](https://www.jsonrpc.org/) requests and how they are used to invoke methods on a remote server and receive a response. We mainly looked at read-only queries including methods: - **`eth_getBlockByNumber`**: returns information about a block by number - **`eth_getBalance`**: returns the balance of the provider Ethereum address - **`eth_blockNumber`**: returns the number of the most recent block > Remember that JSON-RPC has nothing to do with blockchains or crypto. It is just another typical API standard, like REST. These are **read-only** methods, meaning, we are only requesting data from the Ethereum blockchain. In this section, we look at *signed* JSON-RPC requests, so that we may also do write-queries to the Ethereum computer via **transactions**. </details> ### Intro We learned that Ethereum nodes contain a JSON-RPC interface which we can use to send JSON-RPC requests. We looked mainly at how to do read-only requests... basically just requests that ask the Ethereum computer for data. We are only reading from the ledger at this point. What about writing? The vehicle to "write", or change the state of the Ethereum computer, is the **transaction**. Let's dive in... ### Ethereum = A Transaction-Based State Machine First of all, let's get it straight: **the Ethereum computer lives and breathes transactions**. They are the only vehicles that can actually change any state in the computer, as show in the diagram below. ![tb-state-machine](https://res.cloudinary.com/divzjiip8/image/upload/v1670379192/alchemyu/Screen_Shot_2022-12-06_at_6.13.07_PM.png) ### What is a Transaction? (Ethereum) An Ethereum transaction refers to an action initiated by an EOA (externally-owned account), in other words an account managed by a human, not a contract. For example, if Bob sends Alice `1 ETH`, Bob's account must be debited and Alice's must be deducted. **This state-changing action takes place within a transaction.** ### Block & Transactions ![blocksandtxs](https://res.cloudinary.com/divzjiip8/image/upload/v1670379535/alchemyu/Screen_Shot_2022-12-06_at_6.18.51_PM.png) > Notice how the World state begins at `σt` and when a block full of transactions is applied to it, the world state then becomes `σt+1` - this is just a way to quickly diagram the Etherum world state changing in this diagram and further below. **Transactions** are collected into blocks. A **block** is a package of data (in the form of transactions.) ### Chain of States ![chain-of-states](https://res.cloudinary.com/divzjiip8/image/upload/v1670379611/alchemyu/Screen_Shot_2022-12-06_at_6.20.06_PM.png) If you focus on how the global singleton world state of Ethereum changes after each block, Ethereum can be seen as a chain of states. ### Chain of Blocks... A Blockchain! ![chain-of-blocks](https://res.cloudinary.com/divzjiip8/image/upload/v1670379875/alchemyu/Screen_Shot_2022-12-06_at_6.24.31_PM.png) Blocks, packed with transactions, are the ultimate state-changers to the Ethereum world state. Focusing purely on the blocks, Ethereum can then also be seen as a chain of blocks... or a... BLOCKCHAIN! 🤯 ### Stack of Transactions ![stack](https://res.cloudinary.com/divzjiip8/image/upload/v1670380019/alchemyu/Screen_Shot_2022-12-06_at_6.26.56_PM.png) If you ignore the block-based view and focus purely on a ledger view (just focusing on the numbers), Ethereum can then be seen as just a collected stack of transactions. Each transaction subsequently changes the state, and so these state-changers are simply stacked! ### Refresher on the Ethereum World State Ethereum can be viewed as a chain of states. There is only ever one single world state and that world state is changed by blocks packed full of data in the form of transactions. ![eth-world-state](https://res.cloudinary.com/divzjiip8/image/upload/v1670441261/alchemyu/Screen_Shot_2022-12-07_at_11.27.24_AM.png) As seen above, the **Ethereum world state** is simply a mapping between Ethereum addresses and their account state. > We encourage you to review past material if needed! Specifically, the [Week 2 lesson on Ethereum's use of the account model to keep track of account states](https://university.alchemy.com/course/ethereum/md/utxo-vs-account-model) and the [Week 2 lesson on Ethereum's use of Patricia Merkle Tries to keep track of the world state](https://university.alchemy.com/course/ethereum/md/patricia-merkle-tries). #### Several Views of Ethereum World State The Ethereum world state can be seen from several perspectives, all just different conceptual vehicles - choose the one that best fits your conceptual understanding! ![world-state-views](https://res.cloudinary.com/divzjiip8/image/upload/v1670441831/alchemyu/Screen_Shot_2022-12-07_at_11.37.01_AM.png) As seen above, the Ethereum world state can be seen as a mapping, table or an object. It is the ultimate source of all Ethereum state including balances and smart contract code + state. Accounts are simply ledger entries that are indexed via a public address into the world state. Query the world state by providing it an Ethereum address, and the world state will return that address's account state (balance, nonce, smart contract code & state if applicable). > Try it on the [Alchemy Composer](https://composer.alchemy.com/) now! Try the `eth_getBalance` method and get Vitalik's (`0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045`) ETH balance! This query is sent directly to the Ethereum world state! ### Wait, So An Account Can Be A Smart Contract? Yes. **There are two types of accounts in Ethereum**: 1. **EOA**: This is an account directly controlled by a private key - An EOA cannot contain EVM code 2. **Contract account**: This is an account that does NOT have a private key - As seen in the diagram, this account contains two extra properties on its state: - **storage hash**: contains the root hash of a Merkle patricia trie that holds any state relevant to this smart contract account (ie. variable values, owners, etc) - **code hash**: bytecode representation of skeleton code ![sc](https://res.cloudinary.com/divzjiip8/image/upload/v1670442521/alchemyu/Screen_Shot_2022-12-07_at_11.48.30_AM.png) > Feeling like you need a refresher on types of accounts on Ethreum? [Review this past lesson on Ethereum accounts](https://university.alchemy.com/course/ethereum/md/ethereum-accounts). ### How Are The Account Public Addresses Determined? If the account is an EOA, the Ethereum public address is derived from the private key. If the account is a smart contract, that smart contract public address is derived from the deployer address and the deployer nonce value. Better seen here: ![types-of-accounts](https://res.cloudinary.com/divzjiip8/image/upload/v1670443239/alchemyu/Screen_Shot_2022-12-07_at_12.00.25_PM.png) The output, regardless of whether the account is an EOA or a smart contract, is always 160 bits representing the Ethereum public address. You'll typically hear Ethereum public addresses desribed as 20 bytes long with a `0x` appended in front. You may also hear Ethereum addresses are 40-characters long, or 42 with the `0x` appended in front. > 1 byte = 2 hexadecimal characters = 8 bits! ### Ok, Back to Transactions Why did we tangent into exploring the Ethereum world state and types of accounts on Ethereum? Well, because transactions directly affect the world state and it's important to know what types of accounts are behind those transactions! Let's jump back into focusing specifically on **transactions**... ![tb-state-machine-2](https://res.cloudinary.com/divzjiip8/image/upload/v1670444821/alchemyu/Screen_Shot_2022-12-07_at_12.26.49_PM.png) A **transaction** is a single cryptographically-signed instruction. It is a signal of intent from an owner of a private key that they want to change the Ethereum state in one way or another. Reading data from Ethereum does not require an account! Anyone can ping the Ethereum computer and read data instantly... did we mention [Alchemy Composer](https://composer.alchemy.com/) is a thing? 👀 But writing data requires you own a private key and some ETH (to pay for gas!)... all write operations cost gas and so you need ETH to pay for that gas. And all write operations must be signed by a private key! #### Trivia: Can smart contract accounts inititate a transaction? ![tx+world-state](https://res.cloudinary.com/divzjiip8/image/upload/v1670445234/alchemyu/Screen_Shot_2022-12-07_at_12.33.42_PM.png) Notice in the diagram above, only an EOA can send a transaction to Ethereum. EOAs are typically human-controlled accounts; humans are in the real physical world. The bridge to the Ethereum metaverse is transcended via submitting transactions. As an external entity to the Ethereum computer, an EOA signals an intent to change state in the metaverse with a valid transaction. ### Two Types of Transactions in Ethereum In Ethereum, there are two practical types of transactions: ![two-types-of-tx](https://res.cloudinary.com/divzjiip8/image/upload/v1670445489/alchemyu/Screen_Shot_2022-12-07_at_12.37.57_PM.png) 1. **Contract creation**: a special type of transaction that deploys a brand new smart contract This transaction essentially *creates* a brand new entry in the Ethereum world state ![first-type-of-tx](https://res.cloudinary.com/divzjiip8/image/upload/v1670445748/alchemyu/Screen_Shot_2022-12-07_at_12.42.15_PM.png) 2. **Message call**: a transaction inititated by an EOA that interacts with either another EOA or a smart contract This transaction does NOT create a new entry in the world state, it just *updates* an exisiting entry in the Ethereum world state. ![second-type-of-tx](https://res.cloudinary.com/divzjiip8/image/upload/v1670446309/alchemyu/Screen_Shot_2022-12-07_at_12.51.37_PM.png) ### Ethereum Transaction Architecture ![eth-tx-architecture](https://res.cloudinary.com/divzjiip8/image/upload/v1670450744/alchemyu/Untitled_4.png) The above diagram shows all the properties that are packaged up within an Ethereum transaction. > The diagram shows what some refer to as a Type 2. Type 2 transactions are any transactions that are not Type 1, or legacy. Legacy transactions are transactions that do not include the EIP-1559 upgrades. You can distinguish what type a transaction by looking at the `type` property of the transactions on explorers like [Etherscan](https://etherscan.io/). Let's define all of the transaction fields present above: - **`nonce`**: index, gets incremented every time transaction gets mined - **`recipient`**: the receiving address (if an externally-owned account, the transaction will transfer value. If a contract account, the transaction will execute the contract code) - **`value`**: amount of ETH to transfer from sender to recipient (in WEI, a denomination of ETH) - **`yParity, r, s`** (aka: digital signature): signature components - **`init or data`**: typically referred to as “calldata”, `0` if just a typical ETH transfer - **`gasLimit`**: maximum amount of gas units that can be consumed - **`type`** (aka: minerTip): type `0` for legacy (pre-EIP-1559) or type `2` for EIP-1559-compatible txs - **`maxPriorityFeePerGas`** (aka: minerTip): the maximum amount of gas to be included as a tip to the validator - **`maxFeePerGas`**: the maximum amount of gas willing to be paid for the transaction (inclusive of `baseFeePerGas` and `maxPriorityFeePerGas`) - **`chainId`**: in order to protect against replay attacks on other EVM chains, each transaction must now include a specific id per chain. Mainnet is `0`. Göerli is `5`. You can check other chain ids here: https://chainlist.org/ The main difference between a read-only JSON-RPC query and a write JSON-RPC request is the fact that only the write request requires a digital signature. So, you must send a *signed JSON-RPC Request*, in other words, a **transaction**. > If you want to read data from Ethereum, a standard JSON-RPC request will do (ie. `eth_getBalance`). If you want to write data to Ethereum, a signed JSON-RPC request is needed, otherwise referred to as, a transaction. [Here is further review on the Ethereum transaction object](https://docs.alchemy.com/docs/understanding-the-transaction-object-on-ethereum). ### Blockchain = Globally Shared Transaction Database ![global-tx-db](https://res.cloudinary.com/divzjiip8/image/upload/v1670454686/alchemyu/Screen_Shot_2022-12-07_at_3.10.06_PM.png) Transactions are so important, that one can refer to a blockchain as a globally shared, transactional database. They are the heart and soul of what drives the state changes behind Ethereum. ### P2P Network Not only are blockchains globally-shared databases, they are globally-shared **decentralized** databases: ![decentralized-db](https://res.cloudinary.com/divzjiip8/image/upload/v1670454597/alchemyu/Screen_Shot_2022-12-07_at_3.09.46_PM.png) Everyone keeps a copy of the latest Ethereum world state. When a new block packed full of state-changing transactions gets mined, the block is independently verified by every node in the peer-to-peer network. If the block is truthful (no double spending transactions, all digital signatures check out, etc) then the node adds that block to their own local version of the blockchain as the latest representation of the Ethereum world state. This happens every time a block is mined: worldState`n` transitions to worldState`n+1` and so on. ![p2p](https://res.cloudinary.com/divzjiip8/image/upload/v1670455029/alchemyu/Screen_Shot_2022-12-07_at_3.16.49_PM.png) The Ethereum network is made up of nodes decentralized all over the world. Each of these nodes performs verification on any new block of transactions being added to the Ethereum blockchain. We've learned that in order to interact with one of these nodes constituting the Ethereum P2P network, one must send a JSON-RPC request (in order to read data, which anyone can do!) or send a *signed* JSON-RPC request (in order to write data, which means you are changing some state in the Ethereum computer). > Remember, a "*signed* JSON-RPC request" is just fancy a fancy term for transaction. ![interact-with-p2p](https://res.cloudinary.com/divzjiip8/image/upload/v1670455249/alchemyu/Screen_Shot_2022-12-07_at_3.20.37_PM.png) As the diagram above shows, as an EOA, you have three routes to interact with the Ethereum computer via a connection to an Ethereum node: 1. **Contract creation**: As an EOA, you deploy a new smart contract to the Ethereum computer *via a special transaction* (signed JSON-RPC request) 2. **Message call**: As an EOA, you either send some ETH to another EOA or interact with a smart contract in some way *via a transaction* (signed JSON-RPC request) 3. **Inspection**: Any user can make read queries to any Ethereum nodes, no account needed. Try out the `eth_getBalance` method in the [Alchemy Composer](https://composer.alchemy.com/) if you don't believe us! (non-signed JSON-RPC request) ### Transaction Object Example 1. Alice sends Bob `1 ETH` ```json { to: "0x2c8645BFE28BEEb6E19843eE9573b7539DD5B530", // Bob gasLimit: "21000", maxFeePerGas: "30", // 28 (base) + 2 (priorityFee) maxPriorityFeePerGas: "2", // minerTip nonce: "0", value: "100000000000000000", // 1 ether worth of wei data: '0x', // no data, we are not interacting with a contract type: 2, // this is not a legacy tx chainId: 4, // this is AU, we deal only in test networks! (Göerli) } ``` 2. Alice calls a function on a smart contract ```json { to: "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8", // smart contract address gasLimit: "36000", maxFeePerGas: "30", // 28 (base) + 2 (priorityFee) maxPriorityFeePerGas: "2", // minerTip nonce: "1", // this is Alice's second transaction, so the nonce has increased! value: "100000000000000000", // 1 ether worth of wei data: '0x7362377b0000000000000000000000000000000000000000000000000000000000000000', // this calldata tells the EVM what function to execute on the contract, contains parameter values here as well type: 2, // this is not a legacy tx chainId: 4, // this is AU, we deal only in test networks! (Göerli) } ``` Wait a second, how the heck did the `data` for the contract interaction get calculated? Let's cover this... ### How To Manually Construct Calldata Once we send a transaction that points to a smart contract, how does the contract know what specific function you intend to call? Well, all those specifics end up going in the `data` field of each transaction. Here is the algorithm to manually construct calldata: 1. Say Alice wants to call the `withdrawEther()` function of a faucet smart contract... 2. Alice must take the [**keccak256**](https://emn178.github.io/online-tools/keccak_256.html) hash of that function signature: ![hash-of-fn](https://res.cloudinary.com/divzjiip8/image/upload/v1670456828/alchemyu/Screen_Shot_2022-12-07_at_3.46.13_PM.png) The resulting output is: `7362377b8e2cc272f16ab5d5441f976bd53fd78ccd01e3c67a1f6b2efdae09e0` 3. Take the first 4 bytes (8 characters) of the hash output, which is just: `7362377b` 4. This function takes no arguments, so no need to append any parameter data - If the function took arguments, you would need to hash the entire function signature with that parameter type, for example: `helloWorld(uint256)` 5. Final calldata construction, padded out to 32 bytes: `0x7362377b0000000000000000000000000000000000000000000000000000000000000000` ### Conclusion Phew... what a learning blast! The TLDR is: transactions rule everything in Ethereum. They are they main changers of state, so we should know them down to the architecture level... which after today, we do! We are still at the low-level at this point. We saw how to make read requests in the previous activity quite easily. We won't make you perform a signed request as an activity... why? Because it's quite a long script to write! Trust us, we've gotten low-level enough. We have just one more module left this week: Intro to Ethers.js. Tools like Ethers.js and the Alchemy SDK abstract all the low-level dealings we see here away from us, allowing us to focus on pure development. You'll see. We learned the low-level so that we become competent web3 developers, but we'll learn all the high-level libraries meant to make us web3 developer superstars... starting with [Ethers.js](https://docs.ethers.io/v5/) and the [Alchemy SDK](https://www.alchemy.com/sdk).

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully