Michael Macaulay
    • 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
    --- title: Building a Subgraph on Arbitrum One --- This guide will explain how to create, deploy, and publish your Arbitrum subgraph to The Graph's decentralized network. Arbitrum is an EVM-compatible L2. You can test your dapp by deploying to [Arbitrum testnet](https://docs.arbitrum.io/for-devs/concepts/public-chains#what-arbitrum-testnet-chains-are-available). You can find Arbitrum documentation [here](https://docs.arbitrum.io/) and leverage the block explorer [here](https://arbiscan.io/). Popular Arbitrum dapps using subgraphs on The Graph include: -[Premia](https://thegraph.com/explorer/subgraphs/8SxuHUYYBLHs1UkgFFYNaS7MgrEiAMbDyt5YzwZsSa6R?view=Overview&chain=arbitrum-one) -[Camelot](https://thegraph.com/explorer/subgraphs/8zagLSufxk5cVhzkzai3tyABwJh53zxn9tmUYJcJxijG?view=Overview&chain=arbitrum-one) -[Dolomite](https://thegraph.com/explorer/subgraphs/DF4dkCD1NNxuqF3tovPpQG1VgkxsaGjr5TXJTtsKZH7D?view=Overview&chain=arbitrum-one) After completion, you will be able to query data from your subgraph using GraphQL with 100,000 free queries per month. This guide is written assuming that you have: - A smart contract address on Arbitrum - A basic understanding of GraphQL ## 1. Create an account Subgraph Studio Go to the [Subgraph Studio](https://thegraph.com/studio/), connect your wallet, and create an account. Creating an account will require you to verify your email address. ## 2. Create a subgraph on Subgraph Studio Go to the [Subgraph Studio](https://thegraph.com/studio/) and connect your wallet. Once connected, you can begin by clicking “create a subgraph.” Select Arbitrum and click continue. ## 3. Install the Graph CLI The Graph CLI is written in JavaScript and you will need to have either `npm` or `yarn` installed to use it. On your local machine, run one of the following commands: Using [npm](https://www.npmjs.com/): ```sh npm install -g @graphprotocol/graph-cli ``` Using [yarn](https://yarnpkg.com/): ```sh yarn global add @graphprotocol/graph-cli ``` ## 4. Initialize your Subgraph > You can find commands for your specific subgraph on the subgraph page in [Subgraph Studio](https://thegraph.com/studio/). The command to initialize your subgraph will like the following: ```sh graph init --studio example-subgraph ``` When you initialize your subgraph, the CLI tool will ask you for the following information: - Protocol: choose the protocol your subgraph will be indexing data from. Since Arbitrum is an EVM-compatible network, you will need to choose Ethereum here. - Subgraph slug: create a name for your subgraph. Your subgraph slug is an identifier for your subgraph. - Directory to create the subgraph in: choose a local directory. - Ethereum network: specify Arbitrum One. - Contract address: Locate the smart contract address you’d like to query data from. - ABI: If the ABI is not autopopulated, you will need to input it manually as a JSON file. - Start Block: it is suggested that you input the start block to save time while your subgraph indexes blockchain data. You can locate the start block by finding the block where your contract was deployed. - Contract Name: input the name of your contract. - Index contract events as entities: it is suggested that you set this to true as it will automatically add mappings to your subgraph for every emitted event. - Add another contract(optional): you can add another contract. If you do not have another contract, you can skip this step by pressing enter. Initialize your subgraph from an existing contract by running the following command: ```sh graph init --studio <SUBGRAPH_SLUG> ``` See the following screenshot for an example for what to expect when initializing your subgraph: [Add screenshot here] ## 5. Write your Subgraph The previous commands create a scaffold subgraph that you can use as a starting point for building your subgraph. When making changes to the subgraph, you will mainly work with three files: - Manifest (`subgraph.yaml`) - The manifest defines what data sources your subgraph(s) will index. - Schema (`schema.graphql`) - The GraphQL schema defines what data you wish to retrieve from the subgraph. - AssemblyScript Mappings (`mapping.ts`) - The mapping file translates data from your data sources to the entities defined in the schema. For more information on how to write your subgraph, see [Creating a Subgraph](/developing/creating-a-subgraph). ## 6. Deploy to the Subgraph Studio Once your subgraph is written, run the following commands: ```sh $ graph codegen $ graph build ``` - Authenticate and deploy your subgraph. The deploy key can be found on the Subgraph page in Subgraph Studio. ```sh $ graph auth --studio <DEPLOY_KEY> $ graph deploy --studio <SUBGRAPH_SLUG> ``` You will be asked for a version label. It's strongly recommended to use [semver](https://semver.org/) for versioning like `0.0.1`. That said, you are free to choose any string as version such as:`v1`, `version1`, `asdf`. ## 7. Test your subgraph You can test your subgraph by making a sample query in the playground section. The logs will tell you if there are any errors with your subgraph. The logs of an operational subgraph will look like this: ![Subgraph logs](/img/subgraph-logs-image.png) If your subgraph is failing, you can query the subgraph health by using the GraphiQL Playground. Note that you can leverage the query below and input your deployment ID for your subgraph. In this case, `Qm...` is the deployment ID (which can be located on the Subgraph page under **Details**). The query below will tell you when a subgraph fails, so you can debug accordingly: ```graphql { indexingStatuses(subgraphs: ["Qm..."]) { node synced health fatalError { message block { number hash } handler } nonFatalErrors { message block { number hash } handler } chains { network chainHeadBlock { number } earliestBlock { number } latestBlock { number } lastHealthyBlock { number } } entityCount } } ``` ## 8. Publish Your Subgraph to The Graph’s Decentralized Network Once your subgraph has been deployed to the Subgraph Studio, you have tested it out, and are ready to put it into production, you can then publish it to The Graph Network. In the Subgraph Studio, click on your subgraph. On the subgraph’s page, you will be able to click the publish button on the top right. Select the network you would like to publish your subgraph to. It is recommended to publish subgraphs to Arbitrum One to take advantage of the [faster transaction speeds and lower gas costs](/arbitrum/arbitrum-faq). Before you can query your subgraph, Indexers need to begin serving queries on it. In order to streamline this process, you can curate your own subgraph using GRT. At the time of writing, it is recommended that you curate your own subgraph with 10,000 GRT to ensure that it is indexed and available for querying as soon as possible. To save on gas costs, you can curate your subgraph in the same transaction that you published it by selecting this button when you publish your subgraph to The Graph’s decentralized network: ![Subgraph publish](/img/publish-and-signal-tx.png) ## 9. Query your Subgraph Now, you can query your subgraph by sending GraphQL queries to your subgraph’s Query URL, which you can find by clicking on the query button. You can query from your dapp if you don't have your API key via the free, rate-limited temporary query URL that can be used for development and staging. For more information about querying data from your subgraph, read more [here](/querying/querying-the-graph/).

    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