Shunji Zhan
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • 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 No publishing access yet

        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.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # ETH Compatible Gas Params ## Problem We have a couple issues with our current gas system: ### 1) extra effort developers have to take extra effort to get familiar with our gas system. Although we have detailed doc explaining it, people still get confused and asked many questions. We also need to spend extra time supporting them. This is not a great dev UX. My personal thought is that even 90% compatibility still impacts a lot for UX and development efficiency. ![](https://i.imgur.com/FMPlNJY.png) ### 2) inaccurate tx cost estimation Total tx cost estimation is not accurate, it's off by 90%. Now we basically have two types of params: - `storageLimit=640` for normal tx, which correspond to **0.2** ACA tx cost estimation. - `storageLimit=64000` for contract deployment and some heavy contract calls, which correspond to **20** ACA tx cost estimation. However, normal contract call and contract deployment actually cost ~0.01 / ~1 ACA, which is huge off to the estimated value. We got asked **many many** times: "I have 10 ACA in my account, but why contract deployment still failed?" ### 3) validUntil expires In the current encoding, `validUntil` is a absolute block number. Many devs have encountered the problem that they have a gas that worked before, but failed after (especially with truffle where they hard coded gas params in the config). A better encoding should dynamically calculate validUntil. ## Solution The main underlying issue for the current implementation is that we need to hardcode a `storageLimit` manually, it's either a default 640, or a much bigger 64000. It requires dev to have some pre-knowledge of the tx, to choose what's the best storageLimit to use. However, in a smooth flow, **devs don't have to know anything about the tx resources beforehand**. It's handled **automatically** by eth toolings when sending the tx. We should be able to solve this problem by using the `estimateGas` to auto calculate tx-dependent values (`storageLimit`, `gasLimit`), and encode them into `gasLimit`. We can also encode `validUntil` into gasPrice, since `validUntil` doesn't need any context knowledge of the tx. ### Features In order to design a gas system to solves the above issues, we need to ensure these 5 features: - accurate tx cost estimation. `GasPrice * gasLimit` should be close to the actual cost. - ETH tools compatible. Most existing ETH apps/codes should be able to switch to EVM+ without gas modification, and devs with ETH development experience should get up to speed with EVM+ with minimum effort. - eth `gasPrice/gasLimit` should be able to decode to substrate `gasLimit/gasPrice/validUntil`, and vice versa. - no validUntil issue mentioned above. - eth GasPrice/gasLimit should be some intuitive number, for better UX. ### Idea and Assumptions Since `estimateGas(tx) => gasLimit`, we should encode both substrate `gasLimit` and `gasPrice` to eth `gasLimit`. We can also encode `validUntil` to `gasPrice`. The main idea is that **when decoding eth gasPrice/gasLimit to substrate params, the values don't need to be absolute accurate, it only needs to be close, and a little bigger than the estimated gas/storage for the tx to succeed.** We will assume to following range for these variables: - `vallidUntil`: [0, 99999999] - `storageLimit`: [0, 2^20] = [0, 1048576] - `gasLimit`: [30000 * 1, 30000 * 999] = [30000, 2997000] - eth `gasLimit`: [21000, 9999999999] - `estimatedTxCost`: [0.01 ACA, 99.9 ACA] = [1{16}0, 1{19}0] - `gasPrice`: const 100 Gwei = [1{9}0] ### Decode Process #### when there is no tip (default case) Let's say eth gasLimit is encoded as `aaaabbbcc` and gasPrice is encoded as `100yyyyyyyyy`, which can be decoded to substrate gas params as following: - `validUntil = yyyyyyyyy` - `storageLimit = 2^min(21, cc)` - `gasLimit = 30000 * bbb` - `tip = extraCost / unitCostPerTip = (gasPrice - 1xxyyyyyyyyy) * gasLimit` #### when there is tip `gasLimit` won't be affected by tip, since tip is encoded into gasPrice `ab0yyyyyyyyy`. Let's say `ab0 = 120`, and `(120 / 100) - 1 = 0.2 = 20%` - `tip = 20% of the original cost` and in the eth toolings (truffle, metamask, etc..) - `estimatedTxCost = aaaabbbcc * gasPrice ≈ aaaa{5}0 * 1{11}0 = aaaa{16}0`, which has a precision of a{16}0 = 0.01 ACA, and range of [0.01, 99] ACA ### Encode Process first we need to calculate the actual estimated `txCost`, as well as substrate gas params `usedStorage`, `usedGas`. ```ts const { usedStorage, usedGas } = substrateEstimateResource(tx) const txCost = calculateTxCost(tx); // in eth 18 decimals ``` then encode these params to eth `gasLimit` ``` /* -------------------------------- */ /* ----- gasPrice: xxyyyyyyyy ----- */ /* -------------------------------- */ tip = 3%; validUntil = 1234567 // dynamic gasPrice = 100 Gwei * (1 + tip) + validUntil = 103000000000 + 1234567 = 103001234567 // xxyyyyyyyy /* -------------------------------- */ /* ----- gasLimit: aaaaaabbcc ----- */ /* -------------------------------- */ bbbccMask = 100000; ccMask = 100; gasLimitChunk = 30000; bbb = Math.ceil(usedGas / gasLimitChunk); cc = Math.ceil(log2(usedStorage + 1)); aaaa = Math.ceil((txCost / gasPrice) / bbbccMask) aaaa00000 = aaaa * bbbccMask bbb00 = bb * ccMask gasLimit = aaaa00000 + bbb00 + cc; // aaaaabbbcc ``` ### Examples | operation | storageUsed | gasUsed | gasPrice | gasLimit | gasPrice * gasLimit | actual cost | |---------------------|-------------|---------|-------------------|----------|--------------------|-------------------| | transfer ACA | 0 | 21000 | ~100 Gwei | 100100 | 0.01 | 0.002 | | small contract call | 0 | 44763 | ~100 Gwei | 100200 | 0.01 | 0.003 | | contract call | 64 | 260830 | ~100 Gwei | 400707 | 0.04 | 0.0329 | | heavy contract call | 320 | 200358 | ~100 Gwei | 1100709 | 0.11 | 0.1 | | super contract call | 11727 | 747877 | ~100 Gwei | 35402514 | 3.5402 | 3.53 | | deploy contract | 11000 | 351991 | ~100 Gwei | 33401214 | 3.34 | 3.33 | links for the above tx: - https://blockscout.acala.network/tx/0x32e6a4bdbeaea2ea09370dc06d42ea346903e4b3a816434d03ba629d0e8e7d49 - https://blockscout.acala.network/tx/0xd727baaa33a5334d4e3a6cfe3dec6e089acfc9db1517d119458168f314a95d64 - https://blockscout.acala.network/tx/0xed4543dcff7e36b25510895eb6509569d30671e073f10e55384e01feeaa99d0d - https://blockscout.acala.network/tx/0x9f9fd19dc05577f13ad436eee0e8f75df37c6741a8a06f162f09e648784e15bb - https://blockscout.acala.network/tx/0xe7b71fccc7dc65927fe481327f72a3040aa7b51e1c8f62580dca25c5c40a094f - https://blockscout.acala.network/tx/0x828b8421ccda2571144a93e426f1ed1f7fb5daf31e85714dc1f921ac9fa877aa ## Conclusion The above proposal should solve all [problems](#Problem) for the current gas system, and satisfy all [features](#Features) required for the new one. - `GasPrice * gasLimit` is close to the actual cost. - ETH tools compatible. For any tx, eth tools will usually call `gasPrice()` then `estimateGas(tx)` internally, and return valid eth gas params. - eth `gasPrice/gasLimit` is able to decode to substrate `gasLimit/gasPrice/validUntil`, and vice versa. Note that after decoding eth `gasLimit` to substrate paras, the `storageLimit` and `gasLimit` will be slightly larger than the original, which is fine and can make sure tx pass. - validUntil is dynamically calculated everytime `gasPrice()` is called - eth GasPrice/gasLimit is intuitive, gasPrice = 100 Gwei, and gasLimit is similar to normal eth tx.

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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