# Dev Rant: On Querying the Blockchain

By [Danki](https://paragraph.com/@danki) · 2023-04-28

---

Warning: pettiness ensues. Serious people really shouldn’t read beyond the asterisks.

\*\*\*\*\*\*

There seems to be a pecking order of coolness in programming languages.

For instance, Python is at the top of the totem

Rust and Ruby is smug along with Python, trashing Javascript

Javascript, Java, Swift, and C++ are humble but are actually the ones powering most of web2

And everybody trashes PHP.

Why am I saying this? Because after 8 hours of trying to debug my code, I em now insecure about my skillz and would like a socially acceptable way to trash on other devs. Turns out PHP is da way (kidding, i <3 PHP)

Going back… As for technology, the hierarchy of coolness as of today seems to be dis:

AI and Data Engineering > Blockchain > All other tags > Web

Now, I am on blockchain and I wanna trash Web2 for their centralized databases because… hierarchy says we’re cooler. But danki cannot do dat.

And do you know why? Because even making simple queries in the blockchain is effin near impossible.

Don’t believe me? Take this basic ERC-20 token contract for instance:

    contract Token is IERC20 {
        mapping(address => uint256) private _balances;
    
        mapping(address => mapping(address => uint256)) private _allowances;
    
        uint256 private _totalSupply;
    
        string private _name;
        string private _symbol;
            .
            .
            .
    }

And say I would like to query this to list all the addresses and the balance of each wallet who got more than 500 token in allowance and have more than 2000 in their balance.

If we use SQL, we’d do it like this:

    SELECT address, balance FROM Token WHERE balance > 500 AND total_received_allowance > 2000

No sweat.

But how do you even query something like this from the blockchain? Well you have two options:

1.  Reconfigure your mappings just to cater to this one particular query (and destroy the whole data architecture of your smart contract)
    
2.  Sacrifice centralization. Accept defeat and use a database.
    

None of these two is satisfying. The first destroys the usability of your code. The second negates the purpose of using a blockchain aside from being unreasonably expensive (it costs thousands of dollars to build a blockchain indexing server).

Seems like the end of truly decentralized but usable apps. Bye fren.

Just kidding… we have a solution.
---------------------------------

Funnily enough, I came across this problem while developing my own dApp and I got curious if somebody has thought of at least a reasonable workaround, because I am definitely not the only one in dis.

That’s how I came across with what was the best and least sexy pitch in crypto: The Graph Protocol.

What does The Graph Protocol do?
--------------------------------

[In their own words](https://thegraph.com/en/), The Graph is an indexing protocol for querying networks like Ethereum and IPFS.

I know dis is quite confusing if you haven’t encountered a problem like the one I talked about in the example, but let’s chew on dis little by little.

Indexing- something like what Google do with the humongous amount of information on the internet. They organize so that people can easily find what they need.

In the same effect, The Graph does the same thing with all the information in the blockchain. But the blockchain is a little more complicated because instead of webpages, the data is written inside transactions. You can’t simply sort and aggregate info in the transactions the way a DB sorts through a web application, or how Google sorts through a sea of webpages.

So how do they even do it?
--------------------------

The Graph uses a bunch of nodes with different roles that work together to curate and serve the blockchain information that you need. There are 4 different roles in the network:

1.  **Developer** - Builds the subgraph. A subgraph is practically an enumeration of the contract/s where you want to get your data from.
    
2.  **Curator** - Takes in the subgraphs that developers have built and manually picks the good quality ones for the indexers to index.
    
3.  **Indexer** - Processes the user queries.
    
4.  **Delegator** - Owns GRT tokens and stakes them to an indexer so that they can earn a part of the query fees.
    

The first time I learned about this fairly complex network, I was shookt by the novelty. Coz it was indeed different from querying web2. But also, it opens up a whole new world where the people themselves can take care of their own data and queries without the need for megacorporations or centralized infrastructures to aggregate it for them.

Ok, I’ll let y’all digest it for now.

The next post will be for the blockchain devs. Danki will give a little tutorial on how you can create your own subgraph for your dApp.

Ciao!

* * *

Anyway I’m giving out a free-to-mint nft for my first few subscribers. Catch it while u can mah frens:

[subscriber-edition://0x7674b264817b17BBCf6D983ee39391D4C851ED62?tokenId=0](subscriber-edition://0x7674b264817b17BBCf6D983ee39391D4C851ED62?tokenId=0)

---

*Originally published on [Danki](https://paragraph.com/@danki/dev-rant-on-querying-the-blockchain)*
