nullcount / rain

Evaporate stagnant liquidity from source nodes and condensate to sinks. Make it rain.
MIT License
2 stars 5 forks source link

blocks not clocks #32

Closed nullcount closed 11 months ago

nullcount commented 1 year ago

Most strategies involve waiting for blocks. The blocks are the bottleneck. Offer an option to run strategies as a listener type. It should listen for new blocks to execute the strategy.

To use the bitcoind gRPC API in Python, you can use the grpcio and grpcio-tools packages to create a gRPC client that can communicate with the bitcoind server. You can then use the getblockchaininfo method to retrieve information about the current state of the blockchain, including the current block height.

To watch for new blocks, you can use the watchblocks method, which will return a stream of notifications whenever a new block is added to the blockchain. You can then use a Python asyncio event loop to receive these notifications and perform any actions you want, such as sending a notification or updating a database.

Here is an example of how you might use the bitcoind gRPC API in Python to watch for new blocks and receive notifications:

import asyncio
from grpc import Channel
from grpc.experimental import aio

from bitcoin.rpc.bitcoind import BitcoindServiceStub, WatchBlocksRequest
from bitcoin.rpc.bip0044 import Account
from bitcoin.wallet import Wallet

# Create the gRPC channel
channel = aio.insecure_channel('localhost:8332')

# Create the gRPC client
client = BitcoindServiceStub(channel)

# Create the asyncio event loop
loop = asyncio.get_event_loop()

# Get the current block height
response = loop.run_until_complete(client.getblockchaininfo())
current_height = response.blocks

# Set up the request to watch for new blocks
request = WatchBlocksRequest(from_block=current_height+1, to_block=-1, account=Account.EXTERNAL)

# Start watching for new blocks
async for block_notification in client.watchblocks(request):
    # Print the notification when a new block is added
    print(block_notification)

    # Perform any other actions you want here
    # ...

You can then run this script using Python 3.7 or newer, which includes support for the asyncio module.

I hope this helps! Let me know if you have any other questions.