---
# System prepended metadata

title: Thunder Finance Stats Code

---

# Thunder FInance Statistic Code


## For transaction counts

```python=
import asyncio

from pytoncenter import get_client
from pytoncenter.utils import get_opcode
from pytoncenter.v3.models import *
from tonpy import CellSlice
from pytoncenter.extension.message import JettonMessage

import json

"""
EQAYh6Rg1DG2wns5mOZOiOHaWokKeXGItNZk-H976bnv3Nqg
EQAJWjDt-16ycgAhDG3ieHOmut-1KrfLpDqLpx8CDygf_TdE
"""


# targets = ["EQAYh6Rg1DG2wns5mOZOiOHaWokKeXGItNZk-H976bnv3Nqg", "EQAJWjDt-16ycgAhDG3ieHOmut-1KrfLpDqLpx8CDygf_TdE"]
async def fetch(client, target: str):
    accounts = set()  # unique accounts which are involved in the transactions (from address)
    withdraw_cnt = 0  # number of withdraw transactions
    deposit_cnt = 0  # number of deposit transactions
    claim_cnt = 0  # number of claim transactions
    withdraw_and_claim_cnt = 0  # number of withdraw and claim transactions
    total_tx = 0  # total number of transactions

    offset = 0
    while True:
        print("fetching transactions from offset", offset)
        transactions, _ = await client.get_transactions(GetTransactionsRequest(account=target, sort="asc", limit=128, offset=offset))
        if not transactions:
            break
        offset += len(transactions)
        for tx in transactions:
            total_tx += 1
            if tx.in_msg.message_content is None:
                continue
            if tx.in_msg.message_content.decoded is not None:
                # comment
                continue
            cs = CellSlice(tx.in_msg.message_content.body)
            opcode = get_opcode(cs.preload_uint(32))
            if tx.in_msg.source is not None:
                accounts.add(tx.in_msg.source)

            if opcode == JettonMessage.TransferNotification.OPCODE:
                deposit_cnt += 1

            if opcode == "0x4212017c":  # withdraw and harvest
                withdraw_and_claim_cnt += 1

            if opcode == "0x097bb407":  # withdraw
                withdraw_cnt += 1

            if opcode == "0x8839dc49":  # claim
                claim_cnt += 1
    return accounts, deposit_cnt, withdraw_cnt, claim_cnt, withdraw_and_claim_cnt, total_tx


def write_json(accounts_cnt, deposit_cnt, withdraw_cnt, claim_cnt, withdraw_and_claim_cnt, total_tx):
    # store the statistics to json file
    with open(f"statistics.json", "w") as f:
        f.write(
            json.dumps(
                {
                    "accounts": accounts_cnt,
                    "total_tx": total_tx,
                    "deposit_cnt": deposit_cnt,
                    "withdraw_cnt": withdraw_cnt,
                    "claim_cnt": claim_cnt,
                    "withdraw_and_claim_cnt": withdraw_and_claim_cnt,
                },
                indent=4,
            )
        )


async def main():
    client = get_client(version="v3", network="mainnet")
    targets = ["EQAYh6Rg1DG2wns5mOZOiOHaWokKeXGItNZk-H976bnv3Nqg", "EQAJWjDt-16ycgAhDG3ieHOmut-1KrfLpDqLpx8CDygf_TdE"]
    accounts = set()
    total_deposit_cnt = 0
    total_withdraw_cnt = 0
    total_claim_cnt = 0
    total_withdraw_and_claim_cnt = 0
    total_txs = 0
    for target in targets:
        accounts, deposit, withdraw, claim, withdraw_and_claim, ttx = await fetch(client, target)
        accounts.update(accounts)
        total_deposit_cnt += deposit
        total_withdraw_cnt += withdraw
        total_claim_cnt += claim
        total_withdraw_and_claim_cnt += withdraw_and_claim
        total_txs += ttx
    write_json(len(accounts), total_deposit_cnt, total_withdraw_cnt, total_claim_cnt, total_withdraw_and_claim_cnt, total_txs)


if __name__ == "__main__":
    asyncio.run(main())

```