How to find the richest wallets in the TON blockchain - get data from the dton.io indexer and Python
Tags: python, cryptocurrency, blockchain, data
Published: 2023-09-28 10:26:17

News about rich people and investments of large funds are always on the front pages. People are interested in what people with large capital buy and invest in.

Blockchain allows you to satisfy curiosity on a new level, because all information is publicly available in full. In this article I will tell you how to get whale wallets of the TON blockchain using a request to the dton.io indexer.

We’ll also talk about how you can simply enrich your data. An example of what we get at the end can be seen here:

https://tonlearn.tools/#/ton-toncoin-whales

## What is dton.io?

Dton.io is a blockchain indexer, which means it collects information from each new block into its database. You can make GraphQL queries into this database and thus collect historical information without parsing the entire blockchain.

The two most important sources of dton.io data are the transaction table and a view of the latest state of wallets/accounts on the network. Make queries in this view and table, you can collect almost any information.

In this tutorial, we will need the states of wallets; we will take the balance

## Install dependencies

As we know, GraphQL requests are sent in the body of an HTTP POST request, so let’s install requests and other necessary dependencies:


```python
# install requriments
!pip install requests

import requests
import string 
```

Let's add the dton.io endpoint:

```python
endpoint = 'https://dton.io/graphql/'
```


## Let's get the bills

Let's use the account status view:


```python
 query = '''
  {
    account_states(
      workchain: 0
      page_size: 100
    ){
      balance: account_storage_balance_grams
      address
    }
  }
  '''
```
Let's get some addresses and their balances, the question arises: how to get the top 100? To do this, the indexer has an order by field in which you can write a field by which to order the answer.

## Order the selection

Since we are interested in balance, let's add sorting by the account_storage_balance_grams field.

```python
#how many sales were there in the last 24 hours
def getTopHundredTonOwners(endpoint):
  query = '''
  {
    account_states(
      order_by: "account_storage_balance_grams"
      order_desc: true
      workchain: 0
      page_size: 100
    ){
      balance: account_storage_balance_grams
      address
    }
  }
  '''

  response = requests.post(endpoint, json={'query': query})

  data = response.json()['data']['account_states']
  return data
```

These are the top 100 wallets, but what kind of wallets are they, maybe they are exchanges or some kind of technical wallets?


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imc467gkmeewpbswlgh7.png)

## How to enrich data

Of course, most of the top 100 are just wallets, but there are also exchanges and special ones. wallets and it would be cool to compare the address and its name, if available. But where to get the necessary base? Let's start from the problem of who needs such a dataset most of all - of course, wallets, because it will also give them UX - it is convenient for users to see who is sending them funds, for example + plus this is some protection against spam - if the token is not in the whitelist, then perhaps it is a scam or advertising sending of a token (they throw a token and in the message there is a link somewhere).

Therefore, let’s pay attention to the open source lists from the tonkeeper wallet:

https://github.com/tonkeeper/ton-assets

Of course, these are not all possible combinations of wallets and their owners, and one owner may have many wallets, but it’s better than nothing.

## We enrich and the explorer trick

It would also be convenient if we had not just an address, but a link to the blockchain explorer with this address. To do this, we need to put the address in another form.

Addresses of smart contracts running in TON are expressed in two main formats:
Raw hex addresses: raw full representation of smart contract addresses
User-friendly addresses: extended addresses for user convenience
More information about addresses in [documentation](https://docs.ton.org/learn/overviews/addresses). To convert an address from one type to another, we will use a script, which we will not analyze in this tutorial, but at the very end of the tutorial there will be all links to the code.

Let's put it all together:

```python
url = "https://raw.githubusercontent.com/tonkeeper/ton-assets/main/accounts.json"
resp = requests.get(url=url)
assets_list = resp.json()

for row in top_hundred:
  for asset in assets_list:
    if('0:'+row['address'].lower() == asset['address']):
      row['name'] = asset['name']
      print(asset['name'])
    else:
      pass
      #row['name'] = None

  row['tonscan'] = "https://tonscan.org/address/" + account_forms('0:'+row['address'])['bounceable']['b64url']
  row['normalized_balance'] = round(int(row['balance']) /1000000000)
```

We get:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ov1r0yjmesrwtkyon6y6.png)

## What can be improved

In the next tutorial we will add what percentage of TONcoin whales own from the total supply of TONcoin and cumulative.

## Conclusion

I like the TON blockchain for its technical elegance; at least it’s not another copy of Ethereum, which is being overclocked with the help of a lot of capital without looking back, and in general why the user needs it. If you want to learn more about the TON blockchain, I have open source lessons that will teach you how to create full-fledged applications on TON.

https://github.com/romanovichim/TonFunClessons_Eng

I post new tutorials and data analytics here: https://t.me/ton_learn

You can see the ranking of collections from this tutorial in a convenient form here: https://tonlearn.tools/#/ton-toncoin-whales

Code from this tutorial: https://colab.research.google.com/drive/16AiDKEwHi_GX0IY3cATjHAZHkO0dHu6o?usp=sharing