-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcreate_unspent_outputs.py
More file actions
48 lines (40 loc) · 1.74 KB
/
Copy pathcreate_unspent_outputs.py
File metadata and controls
48 lines (40 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import asyncio
import os
from dotenv import dotenv_values
from asyncpg import UndefinedTableError
from denaro import Database
config = dotenv_values(".env")
async def run():
db: Database = await Database.create(
user=config['DENARO_DATABASE_USER'] if config['DENARO_DATABASE_USER'] else "denaro" ,
password=config['DENARO_DATABASE_PASSWORD'] if config['DENARO_DATABASE_PASSWORD'] else "",
database=config['DENARO_DATABASE_NAME'] if config['DENARO_DATABASE_NAME'] else "denaro",
host=config['DENARO_DATABASE_HOST'] if config['DENARO_DATABASE_HOST'] else None,
ignore=True
)
async with db.pool.acquire() as connection:
try:
res = await connection.fetchrow('SELECT * FROM unspent_outputs WHERE true LIMIT 1')
if res is not None:
print('Unspent outputs table already exist')
exit()
except UndefinedTableError:
print('Creating table unspent_outputs and type tx_output')
await connection.execute("""
CREATE TYPE tx_output AS (
tx_hash CHAR(64),
index SMALLINT
);
CREATE TABLE IF NOT EXISTS unspent_outputs (
tx_hash CHAR(64) REFERENCES transactions(tx_hash) ON DELETE CASCADE,
index SMALLINT NOT NULL
);"""
)
print('Created')
print('Retrieving outputs... This will take a few minutes')
unspent_outputs = await db.get_unspent_outputs_from_all_transactions()
print(f'Found {len(unspent_outputs)} outputs. Adding them...')
await db.add_unspent_outputs(unspent_outputs)
print('Done.')
loop = asyncio.get_event_loop()
loop.run_until_complete(run())