Tracking deposits
After everything is ready, you can implement deposit tracking.
For that, iterate through all transactions in each block to search for payment_receiver_inc
operation with an address matching the address in which you are interested:
- TypeScript
- Python
async function trackDeposit(address: string) {
// some logic we want to execute on deposit
function onDeposit(deposit: any) {
console.log(deposit)
}
let latestBlock = (await networkStatus()).current_block_identifier.index
while (true) {
// check if transactions to the deposit address are present in the latest block
const txs = (await waitForBlock(latestBlock)).block.transactions
const deposits = []
for (let tx of txs)
for (let op of tx.operations)
if (op.account.address === address && op.type === 'payment_receiver_inc')
deposits.push({
tx_hash: tx.transaction_identifier.hash,
amount: op.amount.value
})
// process deposits
for (let d of deposits)
onDeposit(d)
latestBlock += 1
}
}
def deposit_flow(deposit_address):
# some logic we want to execute on deposit
def on_deposit(deposit):
print(json.dumps(deposit, indent=2))
latest_block = network_status()["current_block_identifier"]["index"]
while True:
# check if transactions to the deposit address are present in the latest block
txs = wait_for_block(latest_block)['block']['transactions']
deposits = []
for tx in txs:
for op in tx["operations"]:
if op["account"]["address"] == deposit_address \
and op["type"] == "payment_receiver_inc":
deposits.append({
"tx_hash": tx["transaction_identifier"]["hash"],
"amount": op["amount"]["value"]
})
# process deposits
for d in deposits:
on_deposit(d)
latest_block += 1