So, my app is fully in docker compose, like this:
volumes:
tron_data:
networks:
bitcart-net:
driver: bridge
services:
app:
build: .
command: python main.py
volumes:
- ./:/app
networks:
- bitcart-net
bitcart-trx:
image: bitcart/bitcart-trx:stable
environment:
- TRX_NETWORK=testnet
- TRX_SERVER=https://nile.trongrid.io
- TRX_DEBUG=true
networks:
- bitcart-net
volumes:
- tron_data:/data
And here's the code:
from bitcart.coins.trx import TRX
trx = TRX(xpub="my private key", rpc_url="http://bitcart-trx:5009")
@trx.on("new_transaction")
def handle_new_transaction(event, tx, amount):
print(event, tx, amount)
if __name__ == "__main__":
trx.start_websocket()
This code above doesn't print anything at all, even when I transfer funds to the wallet, so I tried the following:
class CustomTRX(TRX):
async def process_updates(self, updates, *args, **kwargs):
print(updates)
return await super().process_updates(updates, *args, **kwargs)
Now it prints the updates, as it should:
[{'event': 'new_block', 'height': 41733568}]
[{'event': 'new_block', 'height': 41733569}]
[{'event': 'new_block', 'height': 41733570}]
[{'event': 'new_block', 'height': 41733571}]
When I transfer funds to my wallet, it doesn't get the transaction at all, no matter how much I wait.
But when I change the last line of main.py
to trx.poll_updates()
it works ;(
[]
[{'event': 'new_block', 'height': 41733860}, {'event': 'new_block', 'height': 41733861}]
[{'event': 'new_transaction', 'tx': 'a72b4dd259c8da44349ee2de7d382477d4d85c1b1fe9513ecef7b06f0b4123a9', 'from_address': 'TXEVfNEesAvUEyNkxu5tBxhdMUH5B3hCPv', 'to': 'TCkTgU9QGp3YdKChhX4VKHxHjxzK47AvfA', 'amount': '15.000000', 'contract': None}]
new_transaction a72b4dd259c8da44349ee2de7d382477d4d85c1b1fe9513ecef7b06f0b4123a9 15.000000
[]
[]
What's really, really weird to me is that it DID work, but then it just stopped...
Update:
I wrote big a... explanation of the problem, but in the end of it I found the problem 🥲
At first, I tried to separate bitcart-trx
from the compose like this:
docker run -p 5009:5009 -v ./.data/:/data -e TRX_NETWORK=testnet -e TRX_SERVER=https://nile.trongrid.io -e TRX_DEBUG=true --restart unless-stopped bitcart/bitcart-trx:stable
rpc_url
in this case was http://host.docker.internal:5009
, and it still didn't work
But then I ran my code separate from compose too, just with python main.py
, rpc_url
now was http://localhost:5009
.
And it for some reason it works... 🤯
So the problem must be in the docker network or something? I don't understand why exactly "new_transaction" events are not sent...