The Graph Builders Newsletter - 18

🏗️ Subscribe to The Graph Builders Newsletter 🏗️

gm web3 builders,

Welcome to The Graph Builders Newsletter #18 for June 2024! 🏗️

New CLI Features

Publish Subgraphs from the CLI

Ever built a subgraph and wanted to simply publish straight from the terminal instead of navigating to Subgraph Studio?

Now we can!

graph publish is now live with graph-cli 0.73.0!

Publish using Arbitrum One GRT with:

graph publish –protocol-network:arbitrum-one –subgraph-id:<Qm…>

Publish using Arbitrum Sepolia testnet GRT with:

graph publish –protocol-network:arbitrum-sepolia –subgraph-id:<Qm…>

graph publish can not only publish new subgraphs but new versions as well!

Go ahead and upgrade and check out graph publish –help for more instructions.

New Subgraph Features

If you haven’t had a chance to try out these new features, give them a try!

Speed up Indexing of eth_calls With Declarative eth_calls

Minimize the runtime overhead of unavoidable eth_calls by declaring them in the manifest. Declared calls are performed in parallel before handlers run, reducing total time spent compared to sequential execution.

Currently, eth_calls can only be declared for event handlers. Here’s an example of how to declare a call in the manifest:

event: TransferWithPool(address indexed, address indexed, uint256, bytes32 indexed)
handler: handleTransferWithPool
calls:
ERC20.poolInfo: ERC20[event.address].getPoolInfo(event.params.to)

The declaration syntax is Contract[address].function(params). Use event.address and event.params.<name> as values for address and params. The handler accesses the result by binding to the contract and making the call, with results cached in memory for efficiency.

Note: Declared eth_calls are supported in subgraphs with specVersion >= 1.2.0.

Check if an Address is a Contract or EOA with hasCode()

With the new hasCode() function from the ethereum module, available from apiVersion ≥ 0.0.9, we can easily check is an address is a smart contract or an EOA in our mappings.ts.

This opens up opportunities for filtering of addresses from within the subgraph, leading to increased accuracy and capabilities from within our subgraphs.

import { ethereum, Address } from '@graphprotocol/graph-ts'
let contractAddr = Address.fromString('0x2E645469f354BB4F5c8a05B3b30A929361cf77eC')
let isContract = ethereum.hasCode(contractAddr).inner // returns true
let eoa = Address.fromString('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045')
let isEOA = ethereum.hasCode(eoa).inner // returns false

This function returns a boolean, allowing you to implement specific logic based on address type, enhancing the security and functionality of your dApp.

Precise Filtering with Indexed Argument Filters / Topic Filters

Topic filters, also known as indexed argument filters, are a powerful feature in subgraphs that allow for precise filtering of blockchain events based on their indexed arguments. This enables subgraphs to efficiently focus on relevant data, which is particularly useful for tracking specific addresses and their interactions with smart contracts. Available from specVersion >= 1.2.0.

Imagine the possibilities: you can also track direct transfers between two addresses or monitor all transactions in either direction between two addresses.

When a smart contract emits an event, indexed arguments can be used as filters. For example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Token {
event Transfer(address indexed from, address indexed to, uint256 value);
function transfer(address to, uint256 value) public {
emit Transfer(msg.sender, to, value);
}
}

In your subgraph manifest, configure topic filters like this:

eventHandlers:
- event: Transfer(indexed address,indexed address,uint256)
handler: handleTransfer
topic1: ['0xSpecificAddress']
- event: Transfer(indexed address,indexed address,uint256)
handler: handleTransfer
topic2: ['0xSpecificAddress']

This setup filters Transfer events to monitor transactions involving the specified address as either the sender or receiver.

New Explorer Features

The Graph Explorer has a host of new features to check out. There’s a brand new UI, filtering of subgraphs by category, and a new quickstart section to get builders started querying subgraphs fast!

Indexing Rewards Unlocked

Indexing Rewards for Base, Binance, Scroll & Linea have been enabled. Look out for increased levels of activity and decentralization on these chains’ subgraphs 👀

Learning Corner

Are You Following Subgraph Best Practices?

Here are four Subgraph Best Practices that will improve your subgraphs performance.

Here’s a quick example of one of these best practices:

Best Practice 1: Faster Query Speed with Subgraph Pruning

Used in the manifest, indexerHints can easily prune your subgraph’s database, increasing efficiency and query speed.

specVersion: 1.0.0
schema:
file: ./schema.graphql
indexerHints:
prune: auto
dataSources:
- kind: ethereum/contract
name: Contract
network: mainnet

Now the subgraph will automatically prune stale data, leading to improved query speed.

Dive into the other Subgraph Best Practices and improve your subgraph’s performance!

Subgraph Best Practice 1: Pruning with indexerHints

Subgraph Best Practice 2: Use @derivedFrom to Manage Arrays

Subgraph Best Practice 3: Use Immutable Entities and Bytes as IDs

Subgraph Best Practice 4: Avoid eth_calls

The Graph Builders Office Hours - Thursdays 5pm UTC

Jump into The Graph Builders Office Hours every Thursday at 5pm UTC in The Graph’s Discord hosted by Edge & Node Developer Relations Marcus Rein.

Each session includes live workshops and Q&A discussions with developers who are building in The Graph Ecosystem.

Learn to build better with recordings of past episodes of The Graph Builders Office Hours workshops!

Core Devs are Hiring

Ever want to help build The Graph?

Check out these job openings and apply to join a core dev team that’s building The Graph!

Thanks for reading and happy hacking! 💌

Marcus Rein

Developer Relations

Edge & Node, working on The Graph

About The Graph

The Graph is the source of data and information for the decentralized internet. As the original decentralized data marketplace that introduced and standardized subgraphs, The Graph has become web3’s method of indexing and accessing blockchain data. Since its launch in 2018, tens of thousands of developers have built subgraphs for dapps across 70+ blockchains - including  Ethereum, Solana, Arbitrum, Optimism, Base, Polygon, Celo, Fantom, Gnosis, and Avalanche.

As demand for data in web3 continues to grow, The Graph enters a New Era with a more expansive vision including new data services and query languages, ensuring the decentralized protocol can serve any use case - now and into the future.

Discover more about how The Graph is shaping the future of decentralized physical infrastructure networks (DePIN) and stay connected with the community. Follow The Graph on X, LinkedIn, Instagram, Facebook, Reddit, Farcaster and Medium. Join the community on The Graph’s Telegram, join technical discussions on The Graph’s Discord.

The Graph Foundation oversees The Graph Network. The Graph Foundation is overseen by the Technical Council. Edge & Node, StreamingFast, Semiotic Labs, Messari, GraphOps, Pinax and Geo are eight of the many organizations within The Graph ecosystem.


Category
Developer Corner
Published
July 5, 2024

Marcus Rein

View all blog posts