I want to scan the latest block https://trx.tokenview.com/en/blocklist
of the tron network if there is a new transaction on a specific address.
I managed to get the latest block and the balance of the address but I cannot figure out how to scan the latest block and look for any new transaction of the address. Any idea will be very helpful.
from tronpy import Tron
client = Tron()
#-- Get the latest block
latestBlock = client.get_latest_block_number()
print (latestBlock)
#-- Get the balance
accountBalance = client.get_account_balance('TTzPiwbBedv7E8p4FkyPyeqq4RVoqRL3TW')
print (accountBalance)
#-- if the address has new transaction in the latest block at the time of the scan:
#-- display all the data (receiver, sender and amount, etc)
So one thing i tried and worked for me is the following: i hit two endpoints: trc20url = f"https://api.trongrid.io/v1/accounts/{address}/transactions/trc20?&contract_address={contract_add}&min_timestamp={getminTime}&max_timestamp={maxTime}" trxurl = f"https://api.trongrid.io/v1/accounts/{address}/transactions"
for the trc20 endpoint check this: https://stackoverflow.com/a/72162936/17474386
now for the trx or trc10 also: what i did i called the get request as follows:
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
trxresponse = requests.get(trxurl, headers = headers)
trxdicResponse = trxresponse.json()
i then looped over the result:
for tx in trxdicResponse['data']:
if (tx["raw_data"]["contract"][0]['type']) ==
'TransferContract':
# this means this is a trx tx , (transferAssetContract for trc10, and triggersmartcontract for trc20)
from there you can build a list of the txs and do whatever you want, also if you want this process to be automated, you can do the following:
trxblock = client.get_block()
lastBlockNum = trxblock['block_header']['raw_data']['number']
maxTime = trxblock['block_header']['raw_data']['timestamp']
getuserBlock = get from DB
getfirstBlock = getuserBlock.trxLastBlock
getminTime = client.get_block(getfirstBlock)['block_header']['raw_data']['timestamp']
so what you do is you get the latest block and get its timestamp, then get the last block you looped over from your DB and get its timestamp and then you just feed them to the request parameters , then update the last scanned block in your DB then repeat.