Abyss Crypto

How To Get A List of Ethereum Tokens

There are a lot of tokens out there! Far more than are listed on most of the sites you're used to visiting. We don't want a curated list of the tokens some 3rd party has approved. We want to know what tokens exist.


Decentralized Option #1: Uniswap v2 The Graph Sub Graph

For a more in depth understanding on The Graph, we have a page about it: Uniswap The Graph Basics

There is a Playground: https://thegraph.com/hosted-service/subgraph/uniswap/uniswap-v2

The Token Object

Fortunately for us, there's a complete list of tokens in Uniswap's defi ecosystem on it's The Graph sub graph!

The Query In TheGraph's GraphQL API

    {
        tokens {
            id symbol name
        }
    }

Example Data Returned:

                {
                    "data": {
                      "tokens": [
                        {
                          "id": "0x00000000000045166c45af0fc6e4cf31d9e14b9a",
                          "symbol": "BID",
                          "name": "TopBidder"
                        },
                        {
                          "id": "0x0000000000004946c0e9f43f4dee607b0ef1fa1c",
                          "symbol": "CHI",
                          "name": "Chi Gastoken by 1inch"
                        },
                        {
                            // etc   
                        }

How to process with PHP:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
        ['query' => "{ tokens { id symbol name } }"]));
    
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    $data = json_decode($result);
    foreach($data->data->tokens as $token) {
        echo "Have token " . $token->name . " symbol: " . $token->symbol;
    }

But Wait - There's a Problem! There Are Too Many Tokens For One Query

To get more than just a page of tokens, you'll need to be saving these somewhere, and make use of GraphQL's skip and first aguments. A simple example is below where we skip 200 results, then grab the first 50. You could then make a query with skip 250, again grabbing 50.
    {
        tokens(skip:200, first:50) {
            id symbol name
        }
    }

But Wait - There's ANOTHER Problem! There Are Too Many Tokens For One Sorting

The Graph limits the skip value to 5000, and we all know that there are more than 5000 crypto tokens. We have two additional tools we can use to grab more tokens. orderBy, and orderDirection

orderDirection can be asc or desc

Relevant orderBy options are id , symbol , name , totalSupply , tradeVolume , txCount , totalLiquidity

Using all of these combinations of orderBy with both the asc and desc options is our best currently known way of trying to find EVERY token on Ethereum using Uniswap's The Graph
    {
        tokens(orderBy: id, orderDirection: desc, skip:200, first:50) {
            id symbol name
        }
    }



If you have information about a better way to access this data, or a way to access this data in a programming language we don't have listed, please let us know!