HomeCoinsyearn.finance (YFI)Subgraphs explained: Yearning for data | by Marco Worms | Yearn |...

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022

- Advertisement -


A Subgraph is a service that allows developers and users to query blockchain data using well-known database query languages. In this article, we will explore a Yearn Ethereum Subgraph and learn how to ask it for data (a.k.a. query it). This knowledge is extremely important for web3 developers, but any blockchain user may also leverage information found on public subgraphs for their interests.

Luckily for us, today we have services like The Graph which provides a human-friendly interface for communicating with subgraphs and also the entire. It’s worth noting that The Graph is a complex protocol that maintains an ecosystem of incentives for many roles that co-exist to make the infrastructure keep on going

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022
Things you can do at The Graph

Before diving into a subgraph let’s be clear about why we need one and what it solves: The blockchain is an ever-growing chain of blocks and each block has a small piece of information inside it, when we need to query for something that needs data from many blocks we need to read through them all and aggregate the data, turns out this can get super complex and hard to maintain (to read more on this search for “Event Sourcing”). We already have solutions to deal with querying data called “Databases“, so the subgraph will maintain a Database that we can query for data in a much more human-friendly way to do it.

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022
Overview of what the subgraph solves

Now that we know what a subgraph is, let’s interact with one: Head to the Yearn Vaults V2 Subgraph Mainnet at The Graph and you should be greeted with this screen:

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022
https://api.thegraph.com/subgraphs/name/rareweasel/yearn-vaults-v2-subgraph-mainnet/graphql

On the top left corner click “Explorer“ to open the left-side tab with many entities that we can query for:

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022
Open explorer tab

Now you are free to click on the entities on the left-side bar to add them to your query. When you click on one of them it will expand all the fields you can query for.

Let’s start by clicking on “vaults“ and selecting a few fields to query for. I’ve marked with red arrows everything I clicked. Clicking will automatically fill up the “query“ field for you and you can press the “Play“ button to run the query and see the results on the right. In this example, we listed all Yearn vaults for their addresses and deposit tokens:

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022
Click stuff and see what happens!

The result that came out in a format called JSON. This format is a standard way to structure data in the web and there are tons of ways to consume and transform this data. For a more readable table-like view paste the result into an app like json2table:

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022
Table-like format for the query output at json2table.com

We can filter things and use any of the GraphQL features to make queries the way you want. The clickable interface helps a lot too by providing filters: all fields contain the following filters:

  • First: Limits the total number of results in a query to the first X results
  • OrderBy / OrderDirection: Helps you order the results the way you want
  • Skip: Skip the first X results. For example, if you ask for the First 10 but Skip 10 you would get results from 11 to 20.
  • Where: Opens a list with all properties and you can filter for results that have specific properties you want

Let’s fiddle with “Where“ a bit to query for only ”Endorsed” vaults: Click the “where” filter in vaults:

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022

Select the “classification” property inside “where” and choose the Endorsed field

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022

Now we only query for production vaults in our results:

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022

Here is another example query where we can see all harvests that happen inside vault strategies and how much they profited:

Subgraphs explained: Yearning for data | by Marco Worms | Yearn | Aug, 2022

There are no limits to what you can do with the subgraph, in fact all recent yearn apps use it a lot since it makes for a much better developer experience and quicker solution for most problems that need data from the blockchain. I hope this walkthrough inspired you to dig deeper into what you can query for in subgraphs, and I recommend that you take a deeper look into GraphQL to learn all query tricks.

For programmers, you can use GraphQL in your favorite programming language and build web3 applications along with something like ethers-io (library to help sign transactions in the blockchain).



Source link

- Advertisement -
Mr Bitcointe
Mr Bitcointehttps://www.bitcointe.com/
“Fact You Need To Know About Cryptocurrency - The first Bitcoin purchase was for pizza.” ― Mohsin Jameel
462FansLike
76FollowersFollow
4,567FollowersFollow
5,261FollowersFollow
1,580FollowersFollow
2,230SubscribersSubscribe

Most Popular

bitcoin
Bitcoin (BTC) $ 24,238.49
ethereum
Ethereum (ETH) $ 1,901.91
tether
Tether (USDT) $ 1.00
bitcoin-cash
Bitcoin Cash (BCH) $ 144.63
litecoin
Litecoin (LTC) $ 62.65
eos
EOS (EOS) $ 1.33
okb
OKB (OKB) $ 18.87
tezos
Tezos (XTZ) $ 1.92
leo-token
LEO Token (LEO) $ 4.70
cardano
Cardano (ADA) $ 0.535269
monero
Monero (XMR) $ 159.11
stellar
Stellar (XLM) $ 0.125989
chainlink
Chainlink (LINK) $ 9.10
huobi-token
Huobi (HT) $ 4.45
tron
TRON (TRX) $ 0.070845
usd-coin
USD Coin (USDC) $ 1.00
dash
Dash (DASH) $ 56.31
neo
NEO (NEO) $ 11.95
iota
IOTA (MIOTA) $ 0.350198
nem
NEM (XEM) $ 0.053955
zcash
Zcash (ZEC) $ 79.96
maker
Maker (MKR) $ 1,073.70
paxos-standard
Pax Dollar (USDP) $ 1.00
ethereum-classic
Ethereum Classic (ETC) $ 43.96
vechain
VeChain (VET) $ 0.032972
true-usd
TrueUSD (TUSD) $ 1.00
ftx-token
FTX (FTT) $ 31.45
kucoin-shares
KuCoin (KCS) $ 10.65
waves
Waves (WAVES) $ 6.16