Skip to content

Commit

Permalink
Add handling for unexpected errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
nessshon authored Apr 23, 2024
1 parent cd21c50 commit b36fc92
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions app/scheduler/tasks/update_token_holders.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import asyncio

from aiogram import Bot
from pytonapi import AsyncTonapi
from pytonapi.utils import nano_to_amount
from sqlalchemy.ext.asyncio import async_sessionmaker

from app.bot.utils.messages import send_message
from app.config import Config
from app.db.models import TokenDB


async def update_token_holders() -> None:
loop = asyncio.get_event_loop()
bot: Bot = loop.__getattribute__("bot")
config: Config = loop.__getattribute__("config")
tonapi: AsyncTonapi = loop.__getattribute__("tonapi")
sessionmaker: async_sessionmaker = loop.__getattribute__("sessionmaker")

tokens = await TokenDB.all(sessionmaker)
for token in tokens:
holders = {}
if token.type == TokenDB.Type.NFTCollection:
request = await tonapi.nft.get_all_items_by_collection_address(account_id=token.address)
for nft in request.nft_items:
if nft.owner.address.to_raw() in holders:
holders[nft.owner.address.to_raw()] += 1
else:
holders[nft.owner.address.to_raw()] = 1
else:
request = await tonapi.jettons.get_all_holders(account_id=token.address)
for address in request.addresses:
holders[address.owner.address.to_raw()] = nano_to_amount(int(address.balance))
try:
if token.type == TokenDB.Type.NFTCollection:
request = await tonapi.nft.get_all_items_by_collection_address(account_id=token.address)
for nft in request.nft_items:
if nft.owner.address.to_raw() in holders:
holders[nft.owner.address.to_raw()] += 1
else:
holders[nft.owner.address.to_raw()] = 1
else:
request = await tonapi.jettons.get_all_holders(account_id=token.address)
for address in request.addresses:
holders[address.owner.address.to_raw()] = nano_to_amount(int(address.balance))

except Exception as e:
await send_message(bot, config.bot.DEV_ID, text=f"{e.__class__.__name__}: {e}")
continue

await TokenDB.update(sessionmaker, primary_key=token.id, holders=holders)

0 comments on commit b36fc92

Please sign in to comment.