412 lines
17 KiB
Python
412 lines
17 KiB
Python
|
import requests
|
||
|
|
||
|
from custom_decorators import singleton
|
||
|
from utils.tronscan import convert_to_tronscan_timestamp
|
||
|
|
||
|
trc20token_info = {
|
||
|
"usdt": {"tokenId": "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
|
||
|
"tokenAbbr": "USDT",
|
||
|
"tokenName": "Tether USD",
|
||
|
"tokenLogo": "https://static.tronscan.org/production/logo/usdtlogo.png",
|
||
|
"issuerAddr": "THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC",
|
||
|
"vip": True}
|
||
|
}
|
||
|
|
||
|
@singleton
|
||
|
class Tronscan:
|
||
|
def __init__(self, api_key):
|
||
|
self.api_key = api_key
|
||
|
|
||
|
def accountv2(self, address):
|
||
|
"""
|
||
|
Get account detail information
|
||
|
:param address: Account address
|
||
|
:return: Returns the detail information of an account.
|
||
|
"""
|
||
|
response = requests.get(f"https://apilist.tronscanapi.com/api/accountv2?address={address}",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key})
|
||
|
return response.json()
|
||
|
|
||
|
def transactions(self, start=0, limit=10, start_timestamp=None, end_timestamp=None,
|
||
|
from_address=None, to_address=None, tokens=None, block=None,
|
||
|
type_=None, method=None):
|
||
|
"""
|
||
|
Get a list of transactions.
|
||
|
:param start: Start number. Default 0
|
||
|
:param limit: Number of items per page. Default 10
|
||
|
:param start_timestamp: Start time
|
||
|
:param end_timestamp: End time
|
||
|
:param from_address: Sender's address.
|
||
|
:param to_address: Recipient's address.
|
||
|
:param tokens: Tokens involved
|
||
|
:param block: Block
|
||
|
:param type_: Transaction type
|
||
|
:param method: Method called in a smart contract signature. Only one value can be specified each time.
|
||
|
:return: Getx a list of transactions.
|
||
|
"""
|
||
|
params = {
|
||
|
"sort": "-timestamp",
|
||
|
"count": "true",
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
}
|
||
|
|
||
|
if start_timestamp is not None:
|
||
|
params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp)
|
||
|
if end_timestamp is not None:
|
||
|
params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp)
|
||
|
if from_address is not None:
|
||
|
params["fromAddress"] = from_address
|
||
|
if to_address is not None:
|
||
|
params["toAddress"] = to_address
|
||
|
if tokens is not None:
|
||
|
params["tokens"] = tokens
|
||
|
if block is not None:
|
||
|
params["block"] = block
|
||
|
if type_ is not None:
|
||
|
params["type"] = type_
|
||
|
if method is not None:
|
||
|
params["method"] = method
|
||
|
|
||
|
response = requests.get(
|
||
|
"https://apilist.tronscanapi.com/api/transaction",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params
|
||
|
)
|
||
|
return response.json()
|
||
|
|
||
|
def transaction_info(self, hash_):
|
||
|
"""
|
||
|
Get transaction detail information by transaction hash.
|
||
|
:param hash_: Transaction hash
|
||
|
:return: Get transaction information.
|
||
|
"""
|
||
|
params = {
|
||
|
"hash": hash_,
|
||
|
}
|
||
|
response = requests.get(
|
||
|
"https://apilist.tronscanapi.com/api/transaction-info",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params
|
||
|
)
|
||
|
return response.json()
|
||
|
|
||
|
def token_trc20_transfers(self, start=None, limit=None, contract_address=None,
|
||
|
start_timestamp=None, end_timestamp=None, confirm=True,
|
||
|
related_address=None, from_address=None, to_address=None):
|
||
|
"""
|
||
|
Get the transfer list of TRC20 and TRC721 tokens.
|
||
|
:param start: Start number. Default 0
|
||
|
:param limit: Number of items per page. Default 10
|
||
|
:param contract_address: Contract address
|
||
|
:param start_timestamp: Start time
|
||
|
:param end_timestamp: End time
|
||
|
:param confirm: Whether to return confirmed transfers only. Default: True
|
||
|
:param related_address: Account address
|
||
|
:param from_address: Sender's address
|
||
|
:param to_address: Recipient's address
|
||
|
:return: Get the transfer list of TRC20 and TRC721 tokens.
|
||
|
"""
|
||
|
params = {
|
||
|
"filterTokenValue": 1
|
||
|
}
|
||
|
if start is not None:
|
||
|
params["start"] = start
|
||
|
if limit is not None:
|
||
|
params["limit"] = limit
|
||
|
if contract_address is not None:
|
||
|
params["contract_address"] = contract_address
|
||
|
if start_timestamp is not None:
|
||
|
params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp)
|
||
|
if end_timestamp is not None:
|
||
|
params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp)
|
||
|
if confirm is not None:
|
||
|
params["confirm"] = str(confirm).lower()
|
||
|
if related_address is not None:
|
||
|
params["relatedAddress"] = related_address
|
||
|
if from_address is not None:
|
||
|
params["fromAddress"] = from_address
|
||
|
if to_address is not None:
|
||
|
params["toAddress"] = to_address
|
||
|
|
||
|
response = requests.get(
|
||
|
"https://apilist.tronscanapi.com/api/token_trc20/transfers",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params
|
||
|
)
|
||
|
return response.json()
|
||
|
|
||
|
def transfer(self, sort="-timestamp", start=0, limit=10, count="true",
|
||
|
address=None, from_address=None, to_address=None, tokens=None, block=None):
|
||
|
"""
|
||
|
Get account's transfer list.
|
||
|
:param sort: Sort type
|
||
|
:param start: Start index, default is 0
|
||
|
:param limit: Number of transfers per page
|
||
|
:param count: Whether to return total transfer number.
|
||
|
:param address: Address, like contract address
|
||
|
:param from_address: Sender's address
|
||
|
:param to_address: Recipient's address
|
||
|
:param tokens: Specific tokens
|
||
|
:param block: Block number
|
||
|
:return: Get account's transfer list.
|
||
|
"""
|
||
|
params = {
|
||
|
"sort": sort,
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
"count": count,
|
||
|
"filterTokenValue": 1
|
||
|
}
|
||
|
|
||
|
if address is not None:
|
||
|
params["address"] = address
|
||
|
if from_address is not None:
|
||
|
params["fromAddress"] = from_address
|
||
|
if to_address is not None:
|
||
|
params["toAddress"] = to_address
|
||
|
if tokens is not None:
|
||
|
params["tokens"] = tokens
|
||
|
if block is not None:
|
||
|
params["block"] = block
|
||
|
|
||
|
response = requests.get(
|
||
|
"https://apilist.tronscanapi.com/api/transfer",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params
|
||
|
)
|
||
|
return response.json()
|
||
|
|
||
|
def internal_transactions(self, start=0, limit=10, address=None, contract=None, block=None):
|
||
|
"""
|
||
|
Get internal transaction list for a specific address or block.
|
||
|
:param start: Start index, default is 0
|
||
|
:param limit: Number of transfers per page
|
||
|
:param address: Specific address. At least one of address, block, or contract must be specified
|
||
|
:param contract: Sender's address
|
||
|
:param block: Block number
|
||
|
:return: Get the internal transaction list.
|
||
|
"""
|
||
|
params = {
|
||
|
"start": start,
|
||
|
"limit": limit
|
||
|
}
|
||
|
|
||
|
if address is not None:
|
||
|
params["address"] = address
|
||
|
if contract is not None:
|
||
|
params["contract"] = contract
|
||
|
if block is not None:
|
||
|
params["block"] = block
|
||
|
|
||
|
response = requests.get(
|
||
|
"https://apilist.tronscanapi.com/api/internal-transaction",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params
|
||
|
)
|
||
|
return response.json()
|
||
|
|
||
|
def token_trc20_transfers_with_status(self, start=0, limit=10, trc20Id=None, address=None,
|
||
|
direction=0, db_version=0, reverse="false"):
|
||
|
"""
|
||
|
Get account's transaction data.
|
||
|
:param start: Start index, default is 0
|
||
|
:param limit: Number of transfers per page
|
||
|
:param trc20Id: TRC20 token address
|
||
|
:param address: Account address
|
||
|
:param direction: 0 for all, 1 for transfer-out, 2 for transfer-in
|
||
|
:param db_version: Whether to include approval transfers. 1 for include, 0 for exclude
|
||
|
:param reverse: Sort by creation time. Valid values: "true" or "false"
|
||
|
:return: Get account's transaction data.
|
||
|
"""
|
||
|
params = {
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
"direction": direction,
|
||
|
"db_version": db_version,
|
||
|
"reverse": reverse
|
||
|
}
|
||
|
|
||
|
if trc20Id is not None:
|
||
|
params["trc20Id"] = trc20Id
|
||
|
if address is not None:
|
||
|
params["address"] = address
|
||
|
|
||
|
response = requests.get(
|
||
|
"https://apilist.tronscanapi.com/api/token_trc20/transfers-with-status",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params
|
||
|
)
|
||
|
return response.json()
|
||
|
|
||
|
def search(self, term, type="token", start=0, limit=10):
|
||
|
"""
|
||
|
Search token/contract/account information
|
||
|
Note : The maximum value for limit is 50.
|
||
|
:param term: Search term
|
||
|
:param type: Search type, including "token", "address", "contract", "transaction" and "block"
|
||
|
:param start: Start number. Default: 0
|
||
|
:param limit: Number of items per page. Default: 10
|
||
|
:return: Returns account authorization change records.
|
||
|
"""
|
||
|
params = {
|
||
|
"term": term,
|
||
|
"type": type,
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
}
|
||
|
response = requests.get("https://apilist.tronscanapi.com/api/search/v2",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params)
|
||
|
return response.json()
|
||
|
|
||
|
def approve_change(self, contract_address, from_address, to_address, start=0, limit=20, show=3):
|
||
|
"""
|
||
|
Returns account authorization change records.
|
||
|
:param contract_address: Contract address
|
||
|
:param from_address: Originator address
|
||
|
:param to_address: Recipient address
|
||
|
:param start: Start number. Default: 0
|
||
|
:param limit: Number of items per page. Default: 20
|
||
|
:param show: Token type. 1: TRC20 2: TRC721 3: ALL(default) 4: TRC1155
|
||
|
:return:
|
||
|
"""
|
||
|
params = {
|
||
|
"contract_address": contract_address,
|
||
|
"from_address": from_address,
|
||
|
"to_address": to_address,
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
"show": show,
|
||
|
"type": "approve",
|
||
|
}
|
||
|
response = requests.get("https://apilist.tronscanapi.com/api/account/approve/change",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params)
|
||
|
return response.json()
|
||
|
|
||
|
def transfer_trx(self, address, start_timestamp=None, end_timestamp=None, start=0, limit=20, direction=1,
|
||
|
db_version=0,
|
||
|
reverse=True, fee=False):
|
||
|
"""
|
||
|
Get the list of trx transfers related to a specific address
|
||
|
Note : The value sum of start and limit must be less than or equal to 10000.
|
||
|
:param address: Query address
|
||
|
:param start_timestamp: Start timestamp
|
||
|
:param end_timestamp: End timestamp
|
||
|
:param start: Start number. Default: 0
|
||
|
:param limit: Number of items per page. Default: 20
|
||
|
:param direction: Default: 1. 1 represents inbound transfers, 2 represents outbound transfers, and 0 represents both.
|
||
|
:param db_version: Default: 0, which indicates to filter transfers with invalid “to” or “from” addresses out.
|
||
|
:param reverse: Sort the data in a descending order. Default: true
|
||
|
:param fee: Whether to return data of TRX burning for resource consumption. Default: false
|
||
|
:return: Returns the list of TRX transfers for a specific address.
|
||
|
"""
|
||
|
params = {
|
||
|
"address": address,
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
"direction": direction,
|
||
|
"db_version": db_version,
|
||
|
"reverse": reverse,
|
||
|
"fee": fee,
|
||
|
}
|
||
|
if start_timestamp is not None:
|
||
|
params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp)
|
||
|
if end_timestamp is not None:
|
||
|
params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp)
|
||
|
response = requests.get("https://apilist.tronscanapi.com/api/transfer/trx",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params)
|
||
|
return response.json()
|
||
|
|
||
|
def transfer_token10(self, address, trc10Id, start_timestamp=None, end_timestamp=None, start=0, limit=20,
|
||
|
direction=1, db_version=0, reverse=True):
|
||
|
"""
|
||
|
Get the transfer list of a specific TRC10 token for a certain address
|
||
|
Note : The value sum of start and limit must be less than or equal to 10000.
|
||
|
:param address: Query address
|
||
|
:param trc10Id: TRC10 token ID
|
||
|
:param start_timestamp: Start timestamp
|
||
|
:param end_timestamp: End timestamp
|
||
|
:param start: Start number. Default: 0
|
||
|
:param limit: Number of items per page. Default: 20
|
||
|
:param direction: Default: 1. 1 represents inbound transfers, 2 represents outbound transfers, and 0 represents both.
|
||
|
:param db_version: Default: 0, which indicates to filter transfers with invalid “to” or “from” addresses out.
|
||
|
:param reverse: Sort the data in a descending order. Default: true
|
||
|
:return: Returns the transfer list of a TRC10 token for a specific account.
|
||
|
"""
|
||
|
params = {
|
||
|
"address": address,
|
||
|
"trc10Id": trc10Id,
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
"direction": direction,
|
||
|
"db_version": db_version,
|
||
|
"reverse": reverse,
|
||
|
}
|
||
|
if start_timestamp is not None:
|
||
|
params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp)
|
||
|
if end_timestamp is not None:
|
||
|
params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp)
|
||
|
response = requests.get("https://apilist.tronscanapi.com/api/transfer/token10",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params)
|
||
|
return response.json()
|
||
|
|
||
|
def transfer_trc20(self, address, trc20Id, start_timestamp=None, end_timestamp=None, start=0, limit=20,
|
||
|
direction=1, db_version=0, reverse=True):
|
||
|
"""
|
||
|
Get the transfer list of a specific TRC20 token for a certain address
|
||
|
Note : The value sum of start and limit must be less than or equal to 10000.
|
||
|
:param address: Query address
|
||
|
:param trc20Id: TRC20 token ID
|
||
|
:param start_timestamp: Start timestamp
|
||
|
:param end_timestamp: End timestamp
|
||
|
:param start: Start number. Default: 0
|
||
|
:param limit: Number of items per page. Default: 20
|
||
|
:param direction: Default: 1. 1 represents inbound transfers, 2 represents outbound transfers, and 0 represents both.
|
||
|
:param db_version: Default: 0, which indicates to filter transfers with invalid “to” or “from” addresses out.
|
||
|
:param reverse: Sort the data in a descending order. Default: true
|
||
|
:return: Returns the transfer list of a TRC20 token for a specific account.
|
||
|
"""
|
||
|
params = {
|
||
|
"address": address,
|
||
|
"trc20Id": trc20Id,
|
||
|
"start": start,
|
||
|
"limit": limit,
|
||
|
"direction": direction,
|
||
|
"db_version": db_version,
|
||
|
"reverse": reverse,
|
||
|
}
|
||
|
if start_timestamp is not None:
|
||
|
params["start_timestamp"] = convert_to_tronscan_timestamp(start_timestamp)
|
||
|
if end_timestamp is not None:
|
||
|
params["end_timestamp"] = convert_to_tronscan_timestamp(end_timestamp)
|
||
|
response = requests.get("https://apilist.tronscanapi.com/api/transfer/trc20",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params)
|
||
|
return response.json()
|
||
|
|
||
|
def account_wallet(self, address, asset_type=0):
|
||
|
"""
|
||
|
Get the information of tokens held and followed in the account's web wallet
|
||
|
:param address: Query address
|
||
|
:param asset_type: Asset types: 0 - All (default); 1 - Assets (TRX, TRC10, TRC20); 2 - Collectibles (TRC721 and TRC1155)
|
||
|
:return: Returns a list of tokens held and followed by an account.
|
||
|
"""
|
||
|
params = {
|
||
|
"address": address,
|
||
|
"asset_type": asset_type,
|
||
|
}
|
||
|
response = requests.get("https://apilist.tronscanapi.com/api/account/wallet",
|
||
|
headers={'TRON-PRO-API-KEY': self.api_key},
|
||
|
params=params)
|
||
|
return response.json()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
address = "TB592A5QwHvvcJoCmvALmzT3S9Pux91Gub"
|
||
|
tronscan = Tronscan(api_key='cc87d361-7cd6-4f69-a57b-f0a77a213355')
|
||
|
print(tronscan.transfer_trc20(address, trc20token_info["usdt"]["tokenId"]))
|