Skip to content

Commit

Permalink
fix notification symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-samoylenko committed Jan 16, 2025
1 parent 2c7bf36 commit f3fd686
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 44 deletions.
125 changes: 83 additions & 42 deletions app/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,115 +11,156 @@
from ..utils import estimateenergy
from ..logging import logger
from ..wallet import Wallet
from ..block_scanner import BlockScanner
from ..block_scanner import BlockScanner, parse_tx
from ..connection_manager import ConnectionManager
from . import api
from ..wallet_encryption import wallet_encryption


@api.post("/generate-address")
def generate_new_address():

client = Tron()
addresses = client.generate_address()

db = get_db()
db.execute(
"INSERT INTO keys (symbol, public, private, type) VALUES (?, ?, ?, 'onetime')",
(g.symbol, addresses['base58check_address'], wallet_encryption.encrypt(addresses['private_key'])),
(
g.symbol,
addresses["base58check_address"],
wallet_encryption.encrypt(addresses["private_key"]),
),
)
db.commit()

BlockScanner.add_watched_account(addresses['base58check_address'])
BlockScanner.add_watched_account(addresses["base58check_address"])

return {
"status": "success",
"base58check_address": addresses["base58check_address"],
}

return {'status': 'success', 'base58check_address': addresses['base58check_address']}

@api.post('/balance')
@api.post("/balance")
def get_balance():
start = time.time()

w = Wallet(g.symbol)
balance = w.balance
return {'status': 'success', 'balance': balance, 'query_time': time.time() - start,}
return {
"status": "success",
"balance": balance,
"query_time": time.time() - start,
}

@api.post('/status')

@api.post("/status")
def get_status():
bs = BlockScanner()
last_seen_block_num = bs.get_last_seen_block_num()
block = ConnectionManager.client().get_block(last_seen_block_num)
return {'status': 'success', 'last_block_timestamp': block['block_header']['raw_data']['timestamp'] // 1000}
block = ConnectionManager.client().get_block(last_seen_block_num)
return {
"status": "success",
"last_block_timestamp": block["block_header"]["raw_data"]["timestamp"] // 1000,
}


@api.post('/transaction/<txid>')
@api.post("/transaction/<txid>")
def get_transaction(txid):
tron_client = ConnectionManager.client()
tx = tron_client.get_transaction(txid)
info = BlockScanner.get_tx_info(tx)
info = parse_tx(tx)
try:
latest_block_number = tron_client.get_latest_block_number()
tx_block_number = tron_client.get_transaction_info(txid)['blockNumber']
tx_block_number = tron_client.get_transaction_info(txid)["blockNumber"]
confirmations = latest_block_number - tx_block_number or 1
except tronpy.exceptions.TransactionNotFound:
logger.warning(f"Can't get confirmations for {txid}")
confirmations = 1
return {'address': info.to_addr, 'amount': info.amount, 'confirmations': confirmations, 'category': 'receive'}
return {
"address": info.dst_addr,
"amount": info.amount,
"confirmations": confirmations,
"category": "receive",
}

@api.post('/dump')

@api.post("/dump")
def dump():
rows = query_db('select * from keys where symbol = ? or type = "fee_deposit"', (g.symbol, ))
rows = query_db(
'select * from keys where symbol = ? or type = "fee_deposit"', (g.symbol,)
)
keys = []
for row in rows:
keys.append({
'public': row['public'],
'private': wallet_encryption.decrypt(row['private']),
'type': row['type'],
'symbol': row['symbol'],
})
return {'accounts': keys}

@api.get('/addresses')
keys.append(
{
"public": row["public"],
"private": wallet_encryption.decrypt(row["private"]),
"type": row["type"],
"symbol": row["symbol"],
}
)
return {"accounts": keys}


@api.get("/addresses")
def list_addresses():
rows = query_db('select public from keys where symbol = ? or type = "fee_deposit"', (g.symbol, ))
return {'accounts': [row['public'] for row in rows]}
rows = query_db(
'select public from keys where symbol = ? or type = "fee_deposit"', (g.symbol,)
)
return {"accounts": [row["public"] for row in rows]}

@api.post('/fee-deposit-account')

@api.post("/fee-deposit-account")
def get_fee_deposit_account():
client = ConnectionManager.client()
key = query_db('select * from keys where type = "fee_deposit"', one=True)
try:
balance = client.get_account_balance(key['public'])
balance = client.get_account_balance(key["public"])
except tronpy.exceptions.AddressNotFound:
balance = Decimal(0)
return {'account': key['public'], 'balance': balance}
return {"account": key["public"], "balance": balance}


@api.post('/estimate-energy/<src>/<dst>/<decimal:amount>')
@api.post("/estimate-energy/<src>/<dst>/<decimal:amount>")
def estimate_energy(src, dst, amount):
res = estimateenergy(src, dst, amount, g.symbol)
logger.warning(f"estimateenergy result: {res}")
return res


#
# Multiserver
#

@api.get('/multiserver/status')

@api.get("/multiserver/status")
def get_multiserver_status():
statuses = ConnectionManager.manager().get_servers_status()
return {'statuses': statuses}
return {"statuses": statuses}


@api.post('/multiserver/change/<int:server_id>')
@api.post("/multiserver/change/<int:server_id>")
def multiserver_change_server(server_id):
if server_id in range(len(ConnectionManager.manager().servers)):
ConnectionManager.manager().set_current_server_id(server_id)
return {'status': 'success', 'msg': f"Changing server to {server_id}"}
return {"status": "success", "msg": f"Changing server to {server_id}"}
else:
return {'status': 'error',
'msg': f"Can't change server to {server_id}: no such server in {ConnectionManager.manager().servers}"}
return {
"status": "error",
"msg": f"Can't change server to {server_id}: no such server in {ConnectionManager.manager().servers}",
}


@api.post('/multiserver/switch-to-best')
@api.post("/multiserver/switch-to-best")
def multiserver_switch_to_best():
if ConnectionManager.manager().refresh_best_server():
return {'status': 'success',
'msg': f"Changing server to {ConnectionManager.manager().get_current_server_id()}"}
return {
"status": "success",
"msg": f"Changing server to {ConnectionManager.manager().get_current_server_id()}",
}
else:
return {'status': 'success',
'msg': f"Server {ConnectionManager.manager().get_current_server_id()} is already the best server"}
return {
"status": "success",
"msg": f"Server {ConnectionManager.manager().get_current_server_id()} is already the best server",
}
4 changes: 2 additions & 2 deletions app/block_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def scan(self, block_num: int) -> bool:
)
continue
logger.info(f"Sending notification for TX {tron_tx=}")
self.notify_shkeeper(tron_tx.symbol, tron_tx.txid)
self.notify_shkeeper(tron_tx.symbol.value, tron_tx.txid)
if (
self.main_account not in (tron_tx.src_addr, tron_tx.dst_addr)
and tron_tx.dst_addr in valid_addresses
Expand Down Expand Up @@ -259,7 +259,7 @@ def scan(self, block_num: int) -> bool:
if tron_tx.dst_addr in valid_addresses:
if tron_tx.status == "SUCCESS":
logger.info(f"Sending notification for {tron_tx}")
self.notify_shkeeper(tron_tx.symbol, tron_tx.txid)
self.notify_shkeeper(tron_tx.symbol.value, tron_tx.txid)
# Send funds to main account
if tron_tx.is_trc20:
transfer_trc20_from.delay(
Expand Down

0 comments on commit f3fd686

Please sign in to comment.