|
1 | 1 | from typing import Literal |
2 | 2 |
|
3 | | -from app.utils import get_energy_delegator |
| 3 | + |
| 4 | +from app.utils import get_energy_delegator, get_key |
| 5 | +from app.schemas import KeyType |
4 | 6 |
|
5 | 7 | from . import staking_bp |
6 | 8 | from ..db import query_db2 |
7 | 9 | from ..connection_manager import ConnectionManager |
8 | 10 | from ..wallet_encryption import wallet_encryption |
9 | 11 | from ..logging import logger |
| 12 | +from ..config import config |
10 | 13 |
|
11 | 14 | from tronpy import Tron |
12 | 15 | from tronpy.keys import PrivateKey |
| 16 | +from tronpy.exceptions import AddressNotFound |
| 17 | + |
| 18 | + |
| 19 | +@staking_bp.get("/info") |
| 20 | +def get_staking_info(): |
| 21 | + """ |
| 22 | + Returns staking-related configuration options and account status information. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + dict: Contains: |
| 26 | + - config: Staking-related configuration options |
| 27 | + - fee_deposit_account: Fee deposit account address and status (on-chain/off-chain) |
| 28 | + - energy_delegator_account: Energy delegator account address and status (on-chain/off-chain) |
| 29 | + """ |
| 30 | + try: |
| 31 | + tron_client: Tron = ConnectionManager.client() |
| 32 | + |
| 33 | + # Get fee deposit account |
| 34 | + _, fee_deposit_address = get_key(KeyType.fee_deposit) |
| 35 | + fee_deposit_status = "unknown" |
| 36 | + fee_deposit_info = None |
| 37 | + |
| 38 | + if fee_deposit_address: |
| 39 | + try: |
| 40 | + fee_deposit_info = tron_client.get_account(fee_deposit_address) |
| 41 | + fee_deposit_status = True |
| 42 | + except AddressNotFound: |
| 43 | + fee_deposit_status = False |
| 44 | + |
| 45 | + # Get energy delegator account (might be different from fee_deposit) |
| 46 | + energy_delegator_priv, energy_delegator_address = get_energy_delegator() |
| 47 | + energy_delegator_status = None |
| 48 | + energy_delegator_info = None |
| 49 | + |
| 50 | + if energy_delegator_address: |
| 51 | + try: |
| 52 | + energy_delegator_info = tron_client.get_account( |
| 53 | + energy_delegator_address |
| 54 | + ) |
| 55 | + energy_delegator_status = True |
| 56 | + except AddressNotFound: |
| 57 | + energy_delegator_status = False |
| 58 | + |
| 59 | + # Collect staking-related configuration |
| 60 | + staking_config = { |
| 61 | + "energy_delegation_mode": config.ENERGY_DELEGATION_MODE, |
| 62 | + "energy_delegation_mode_allow_burn_trx_for_bandwidth": config.ENERGY_DELEGATION_MODE_ALLOW_BURN_TRX_FOR_BANDWITH, |
| 63 | + "energy_delegation_mode_allow_burn_trx_on_payout": config.ENERGY_DELEGATION_MODE_ALLOW_BURN_TRX_ON_PAYOUT, |
| 64 | + "energy_delegation_mode_allow_additional_energy_delegation": config.ENERGY_DELEGATION_MODE_ALLOW_ADDITIONAL_ENERGY_DELEGATION, |
| 65 | + "energy_delegation_mode_energy_delegation_factor": float( |
| 66 | + config.ENERGY_DELEGATION_MODE_ENERGY_DELEGATION_FACTOR |
| 67 | + ), |
| 68 | + "energy_delegation_mode_separate_balance_and_energy_accounts": config.ENERGY_DELEGATION_MODE_SEPARATE_BALANCE_AND_ENERGY_ACCOUNTS, |
| 69 | + "energy_delegation_mode_energy_account_pub_key": config.ENERGY_DELEGATION_MODE_ENERGY_ACCOUNT_PUB_KEY, |
| 70 | + "sr_voting": config.SR_VOTING, |
| 71 | + # tmp fix for TypeError: Object of type SrVote is not JSON serializable |
| 72 | + "sr_votes": str(config.SR_VOTES), |
| 73 | + "sr_voting_allow_burn_trx": config.SR_VOTING_ALLOW_BURN_TRX, |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + "status": "success", |
| 78 | + "config": staking_config, |
| 79 | + "fee_deposit_account": { |
| 80 | + "address": fee_deposit_address, |
| 81 | + "is_active": fee_deposit_status, |
| 82 | + "info": fee_deposit_info, |
| 83 | + }, |
| 84 | + "energy_delegator_account": { |
| 85 | + "address": energy_delegator_address, |
| 86 | + "is_active": energy_delegator_status, |
| 87 | + "info": energy_delegator_info, |
| 88 | + "is_externally_managed": energy_delegator_priv is None, |
| 89 | + "is_same_as_fee_deposit": energy_delegator_address |
| 90 | + == fee_deposit_address, |
| 91 | + }, |
| 92 | + } |
| 93 | + except Exception as e: |
| 94 | + logger.exception("Error getting staking info") |
| 95 | + return { |
| 96 | + "status": "error", |
| 97 | + "msg": str(e), |
| 98 | + } |
13 | 99 |
|
14 | 100 |
|
15 | 101 | @staking_bp.get("/", defaults={"address": None}) |
16 | 102 | @staking_bp.get("/<address>") |
17 | 103 | def get_resources(address): |
18 | | - if not address: |
19 | | - _, address = get_energy_delegator() |
20 | | - tron_client: Tron = ConnectionManager.client() |
21 | | - account_info = tron_client.get_account(address) |
22 | | - |
23 | | - index = tron_client.get_delegated_resource_account_index_v2(address) |
24 | | - account_resource = tron_client.get_account_resource(address) |
25 | | - delegated_resources = [] |
26 | | - if "toAccounts" in index: |
27 | | - for to_addr in index["toAccounts"]: |
28 | | - deleg_res = tron_client.get_delegated_resource_v2(address, to_addr) |
29 | | - if "delegatedResource" in deleg_res: |
30 | | - for i in deleg_res["delegatedResource"]: |
31 | | - delegated_resources.append(i) |
32 | | - |
33 | | - return { |
34 | | - "account_info": account_info, |
35 | | - "delegated_resources": delegated_resources, |
36 | | - "account_resource": account_resource, |
37 | | - } |
| 104 | + try: |
| 105 | + if not address: |
| 106 | + _, address = get_energy_delegator() |
| 107 | + tron_client: Tron = ConnectionManager.client() |
| 108 | + account_info = tron_client.get_account(address) |
| 109 | + |
| 110 | + index = tron_client.get_delegated_resource_account_index_v2(address) |
| 111 | + account_resource = tron_client.get_account_resource(address) |
| 112 | + delegated_resources = [] |
| 113 | + if "toAccounts" in index: |
| 114 | + for to_addr in index["toAccounts"]: |
| 115 | + deleg_res = tron_client.get_delegated_resource_v2(address, to_addr) |
| 116 | + if "delegatedResource" in deleg_res: |
| 117 | + for i in deleg_res["delegatedResource"]: |
| 118 | + delegated_resources.append(i) |
| 119 | + return { |
| 120 | + "account_info": account_info, |
| 121 | + "delegated_resources": delegated_resources, |
| 122 | + "account_resource": account_resource, |
| 123 | + } |
| 124 | + except AddressNotFound: |
| 125 | + return { |
| 126 | + "status": "error", |
| 127 | + "msg": "account not found on-chain", |
| 128 | + "details": {"address": address}, |
| 129 | + } |
| 130 | + except Exception as e: |
| 131 | + return { |
| 132 | + "status": "error", |
| 133 | + "msg": str(e), |
| 134 | + } |
38 | 135 |
|
39 | 136 |
|
40 | 137 | @staking_bp.post("/freeze/<int:amount>/<string:res_type>") |
|
0 commit comments