Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to detect deposits? #96

Open
thiemdv opened this issue Nov 13, 2018 · 1 comment
Open

How to detect deposits? #96

thiemdv opened this issue Nov 13, 2018 · 1 comment

Comments

@thiemdv
Copy link

thiemdv commented Nov 13, 2018

Hello,

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

Thanks.

@ljluestc
Copy link

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@thiemdv @ljluestc and others