Hello, I’m trying to get a token’s a real-time buy/sell data using Python3. It should print that if someone buys or sells a crypto token of a given contract address.
Also I’m using this code for this.
import moralis
from moralis import MoralisWeb3
# Initialize Moralis with your API key
moralis_api_key = 'your_moralis_api_key'
moralis_web3 = MoralisWeb3(provider_url=f"https://speedy-nodes-nyc.moralis.io/{moralis_api_key}/bsc/mainnet")
# Specify the token contract address to listen for transactions on
token_contract_address = "0x123456789abcdef"
# Define a callback function to execute when a transaction is detected
def handle_transaction(tx):
if tx.get("to") == token_contract_address:
# Check if it's a buy or sell transaction
if tx.get("input").startswith("0xf305d719"): # Buy transaction
print(f"Buy transaction detected! Tx hash: {tx['hash']}")
print(f"Buyer: {tx['from']}")
print(f"Amount: {int(tx['value'], 16)}")
elif tx.get("input").startswith("0x7a9e5f68"): # Sell transaction
print(f"Sell transaction detected! Tx hash: {tx['hash']}")
print(f"Seller: {tx['from']}")
print(f"Amount: {int(tx['input'][34:], 16)}")
# Subscribe to new transactions on the blockchain
moralis_web3.eth.subscribe('pending_transactions', handle_transaction)
but it shows ImportError: cannot import name ‘MoralisWeb3’ from 'moralis’