forked from nessshon/token-access-control-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
21 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |