Skip to content

Commit 6187de1

Browse files
committed
Add address export script
1 parent e6b70c5 commit 6187de1

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

scripts/get_address_balances.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import asyncio
2+
import typing
3+
4+
from sqlalchemy.orm import selectinload
5+
from sqlalchemy import func, select
6+
7+
from app.database import sessionmanager
8+
from app.models import AddressBalance
9+
from app.settings import get_settings
10+
11+
settings: typing.Any = get_settings()
12+
13+
14+
async def main():
15+
sessionmanager.init(settings.database.endpoint)
16+
17+
file = open("balances.csv", "w")
18+
print("Address,Balances,Currency", file=file)
19+
20+
async with sessionmanager.session() as session:
21+
total = await session.scalar(select(func.count(AddressBalance.id))) or 0
22+
balances = await session.stream_scalars(
23+
select(AddressBalance).options(selectinload(AddressBalance.address))
24+
)
25+
26+
current = 0
27+
async for balance in balances:
28+
print(
29+
f"{balance.address.address},{float(balance.balance)},{balance.currency}",
30+
file=file,
31+
)
32+
33+
if current % 100 == 0:
34+
print(
35+
f"Progress: {current}/{total} ({(current / total) * 100 :.2f}%))",
36+
end="\r",
37+
)
38+
current += 1
39+
40+
print("\nComplete. Total rows:", total)
41+
42+
file.close()
43+
44+
45+
if __name__ == "__main__":
46+
asyncio.run(main())

0 commit comments

Comments
 (0)