PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How to scan the latest TRX block for transfer events to check if an address has a new transaction

 July 28, 2022     blockchain, python, smartcontracts, tron     No comments   

Issue

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)

Solution

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.



Answered By - whitebat 199
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing