|
| 1 | +import os |
| 2 | +import json |
| 3 | +import time |
| 4 | +import logging |
| 5 | +from web3 import Web3 |
| 6 | +from dotenv import load_dotenv |
| 7 | + |
| 8 | +# Load environment variables from .env file |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +# Configure logging |
| 12 | +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 13 | + |
| 14 | +# Load environment variables |
| 15 | +ETHEREUM_RPC_URL = os.getenv('ETHEREUM_RPC_URL') |
| 16 | +BSC_RPC_URL = os.getenv('BSC_RPC_URL') |
| 17 | +ETHEREUM_CONTRACT_ADDRESS = os.getenv('ETHEREUM_CONTRACT_ADDRESS') |
| 18 | +BSC_CONTRACT_ADDRESS = os.getenv('BSC_CONTRACT_ADDRESS') |
| 19 | +BSC_PRIVATE_KEY = os.getenv('BSC_PRIVATE_KEY') |
| 20 | + |
| 21 | +# Connect to Ethereum and BSC |
| 22 | +eth_web3 = Web3(Web3.HTTPProvider(ETHEREUM_RPC_URL)) |
| 23 | +bsc_web3 = Web3(Web3.HTTPProvider(BSC_RPC_URL)) |
| 24 | + |
| 25 | +# Load the contract ABI |
| 26 | +with open('EthereumBridgeDAO.json', 'r') as f: |
| 27 | + eth_contract_data = json.load(f) |
| 28 | + |
| 29 | +with open('BSCBridgeDAO.json', 'r') as f: |
| 30 | + bsc_contract_data = json.load(f) |
| 31 | + |
| 32 | +# Create contract instances |
| 33 | +eth_contract = eth_web3.eth.contract(address=ETHEREUM_CONTRACT_ADDRESS, abi=eth_contract_data['abi']) |
| 34 | +bsc_contract = bsc_web3.eth.contract(address=BSC_CONTRACT_ADDRESS, abi=bsc_contract_data['abi']) |
| 35 | + |
| 36 | +# Function to unlock tokens on BSC |
| 37 | +def unlock_tokens(user, amount): |
| 38 | + try: |
| 39 | + # Prepare the transaction |
| 40 | + tx = bsc_contract.functions.unlockTokens(user, amount).buildTransaction({ |
| 41 | + 'from': bsc_web3.eth.accounts[0], # Replace with your BSC account |
| 42 | + 'gas': 2000000, |
| 43 | + 'gasPrice': bsc_web3.toWei('5', 'gwei'), |
| 44 | + 'nonce': bsc_web3.eth.getTransactionCount(bsc_web3.eth.accounts[0]), |
| 45 | + }) |
| 46 | + |
| 47 | + # Sign and send the transaction |
| 48 | + signed_tx = bsc_web3.eth.account.signTransaction(tx, private_key=BSC_PRIVATE_KEY) |
| 49 | + tx_hash = bsc_web3.eth.sendRawTransaction(signed_tx.rawTransaction) |
| 50 | + logging.info(f'Tokens unlocked for {user}. Transaction hash: {tx_hash.hex()}') |
| 51 | + except Exception as e: |
| 52 | + logging.error(f'Error unlocking tokens for {user}: {e}') |
| 53 | + |
| 54 | +# Event listener for TokensLocked event |
| 55 | +def handle_tokens_locked(event): |
| 56 | + user = event['args']['user'] |
| 57 | + amount = event['args']['amount'] |
| 58 | + logging.info(f'Tokens locked: {amount} by {user}. Unlocking on BSC...') |
| 59 | + unlock_tokens(user, amount) |
| 60 | + |
| 61 | +# Main loop to listen for events |
| 62 | +def main(): |
| 63 | + logging.info('Listening for TokensLocked events...') |
| 64 | + while True: |
| 65 | + try: |
| 66 | + # Create a filter for the TokensLocked event |
| 67 | + event_filter = eth_contract.events.TokensLocked.createFilter(fromBlock='latest') |
| 68 | + for event in event_filter.get_new_entries(): |
| 69 | + handle_tokens_locked(event) |
| 70 | + time.sleep(10) # Polling interval |
| 71 | + except Exception as e: |
| 72 | + logging.error(f'Error in event listening loop: {e}') |
| 73 | + time.sleep(5) # Wait before retrying |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + main() |
0 commit comments