bytecoin icon indicating copy to clipboard operation
bytecoin copied to clipboard

How to detect deposits?

Open thiemdv opened this issue 7 years ago • 1 comments

Hello,

Is there a way to detect deposits realtime? Like subscribing pendingTransactions in Ethereum or executing walletnotify command in Bitcoin.

Thanks.

thiemdv avatar Nov 13 '18 17:11 thiemdv

import requests
import time

# Bytecoin daemon JSON-RPC URL
RPC_URL = "http://127.0.0.1:8081/json_rpc"

# Wallet address to monitor
WALLET_ADDRESS = "your_wallet_address_here"

# JSON-RPC headers
headers = {
    "Content-Type": "application/json"
}

def get_last_block_height():
    payload = {
        "jsonrpc": "2.0",
        "id": "1",
        "method": "getlastblockheader"
    }
    response = requests.post(RPC_URL, json=payload, headers=headers)
    if response.status_code == 200:
        return response.json()["result"]["block_header"]["height"]
    else:
        raise Exception("Failed to get last block height")

def get_block(height):
    payload = {
        "jsonrpc": "2.0",
        "id": "1",
        "method": "f_block_json",
        "params": {
            "height": height
        }
    }
    response = requests.post(RPC_URL, json=payload, headers=headers)
    if response.status_code == 200:
        return response.json()["result"]["block"]
    else:
        raise Exception("Failed to get block")

def check_transactions(block):
    for tx in block["transactions"]:
        for out in tx["outputs"]:
            if out["address"] == WALLET_ADDRESS:
                print(f"Deposit detected: {out['amount']} BCN in transaction {tx['hash']}")

def monitor_deposits():
    last_height = get_last_block_height()
    while True:
        current_height = get_last_block_height()
        if current_height > last_height:
            for height in range(last_height + 1, current_height + 1):
                block = get_block(height)
                check_transactions(block)
            last_height = current_height
        time.sleep(10)  # Check every 10 seconds

if __name__ == "__main__":
    monitor_deposits()

ljluestc avatar Feb 18 '25 16:02 ljluestc