TON - getTransaction and Pagination
When working with the TON blockchain, retrieving transaction history for a specific address can be done using the getTransactions
method. This method supports pagination, allowing you to fetch transactions in batches. However, it has some particularities to make it work.
getTransaction method
Example request:
address
: The address for which you want to fetch the transaction history.limit
: The maximum number of transactions to return in a single request (e.g., 50).lt
: The logical time of the transaction. This parameter is used for pagination to fetch transactions that occurred before a specific point in time.archival
: Set to true to ensure that archival nodes are used, which have the complete history of transactions.
curl --location 'https://ton-mainnet.gateway.tatum.io/getTransactions?address=UQA6FPupjsjSsTIWrP_j8l0kbVCfl-bAYWhfkBDdyFdWPf1x&limit=50<=1&archival=true' \
--header 'x-api-key: [YOUR_API_KEY]'
Pagination Explained
The parameterlt
(logical time) is required for pagination. It represents the timestamp of a transaction. When paginating, you start with the highest possible lt value (since lt
is a 64-bit unsigned integer, its maximum value is 18446744073709551615).
After fetching the first set of transactions, you extract the smallest lt from the response and subtract one to get the next batch of transactions. This process continues until no more transactions are returned.
Implementing Pagination in Python
Below is a Python script example that demonstrates how to fetch all transactions for a given address, handling pagination automatically:
import requests
import json
API_KEY = 'Use Your OWN API Key'
BASE_URL = 'https://ton-mainnet.gateway.tatum.io/getTransactions'
ADDRESS = 'UQA6FPupjsjSsTIWrP_j8l0kbVCfl-bAYWhfkBDdyFdWPf1x'
INITIAL_LT = '18446744073709551615'
OUTPUT_FILE = 'ton_transactions.json'
def fetch_transactions(lt):
params = {
'address': ADDRESS,
'limit': 50,
'lt': lt,
'archival': 'true'
}
headers = {
'x-api-key': API_KEY
}
response = requests.get(BASE_URL, headers=headers, params=params)
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} - {response.text}")
return None
def save_transactions(transactions):
with open(OUTPUT_FILE, 'a') as f:
for transaction in transactions['result']:
f.write(json.dumps(transaction) + '\n')
def paginate_transactions(lt):
while True:
print(f"Fetching transactions with lt={lt}...")
transactions = fetch_transactions(lt)
if not transactions or len(transactions['result']) == 0:
print("No more transactions available.")
break
save_transactions(transactions)
lt = transactions['result'][-1]['transaction_id']['lt']
lt = str(int(lt) - 1)
with open(OUTPUT_FILE, 'w') as f:
f.write('')
paginate_transactions(INITIAL_LT)
print(f"Transactions have been saved to {OUTPUT_FILE}.")
Tatum staff does not review or check third-party code.
Updated 4 months ago